16 #define sprintf sprintf_s
17 #define snprintf _snprintf
18 #pragma warning( push)
19 #pragma warning( disable : 4996 )
54 for (
int i=0; i<arrsz; i++) {
61 throw pg_excpt(
"pgstream",
"Unknown variable type");
99 throw pg_excpt(
"pgstream",
"Unknown oid type");
106 init(query.c_str(), prepare_mode, 0);
112 init(query, prepare_mode, 0);
133 int len = strlen(query);
145 in_quotespl = !in_quotespl;
150 if (in_quotespl && *q==
'\'') {
162 const char* start_var=q;
163 while (isalnum(*q)) q++;
167 const char* start_type=++q;
168 while (isalnum(*q)) q++;
170 vtype.assign(start_type, q-start_type);
174 throw pg_excpt(
"pg_stream",
"syntax error in bind parameter");
177 sql_bind_param p(std::string(start_var,q-start_var), (start_var-1)-query);
178 if (!vtype.empty()) {
198 PGresult* res = PQexec(
m_db.
conn(), q.c_str());
199 if (res) PQclear(res);
203 PGresult* res = PQexec(
m_db.
conn(), q.c_str());
204 if (res) PQclear(res);
222 std::vector<sql_bind_param>::iterator it =
m_vars.begin();
223 for (; it !=
m_vars.end(); ++it) {
232 args += it->type_name();
235 args = std::string(
"(") + args +
")";
236 std::string q = std::string(
"PREPARE ") +
238 PGresult* r = PQexec(
m_db.
conn(), q.c_str());
239 if (!r || PQresultStatus(r) != PGRES_COMMAND_OK) {
251 std::string q = std::string(
"DECLARE ") +
253 PGresult* r = PQexec(
m_db.
conn(), q.c_str());
254 if (!r || PQresultStatus(r) != PGRES_COMMAND_OK) {
270 unsigned int i, s=
m_vars.size();
271 for (i=0; i<s; i++) {
293 throw pg_excpt(
"pg_stream",
"not enough memory");
295 m_chunk_size = m_chunk_size*2;
320 char bind_num_buf[4];
324 throw pg_excpt(
"pg_stream",
"too many bind variables (max:999)");
325 sprintf(bind_num_buf,
"$%u", argpos+1);
327 size = strlen(bind_num_buf);
334 int placeholder_len=p.
name().size()+1;
344 for (
unsigned int i=argpos+1; i<
m_vars.size(); i++) {
345 m_vars[i].offset(size-placeholder_len);
371 size_t len = strlen(p);
376 char local_buf[1024+2+1];
378 if (len < (
sizeof(local_buf)-1)/2)
381 buf=(
char*)malloc(2+2*len+1);
382 int escaped_size=PQescapeString(buf+1, p, len);
384 buf[escaped_size+1]=
'\'';
385 buf[escaped_size+2]=
'\0';
413 sprintf(buf,
"%d", i);
418 sprintf(buf,
"(%d)", i);
431 sprintf(buf,
"%u", i);
444 sprintf(buf,
"%ld", l);
449 sprintf(buf,
"(%ld)", l);
462 sprintf(buf,
"%lu", l);
475 sprintf(buf,
"%lld", l);
480 sprintf(buf,
"(%lld)", l);
493 sprintf(buf,
"%llu", l);
505 sprintf(buf,
"%hd", s);
517 sprintf(buf,
"%hu", s);
529 snprintf(buf,
sizeof(buf),
"%g", d);
552 m_data_ptr(NULL), m_data_len(0), m_allocated(false)
557 m_data_ptr(NULL), m_data_len(0), m_allocated(false)
583 unsigned char* buf = PQescapeBytea((
const unsigned char*)b.
m_data_ptr,
590 unsigned char* buf1 = (
unsigned char*)malloc(escaped_size+2);
592 throw pg_excpt(
"pg_stream",
"not enough memory");
595 memcpy(buf1+1, buf, escaped_size);
596 buf1[escaped_size]=
'\'';
597 buf1[escaped_size+1]=
'\0';
629 throw pg_excpt(
"pg_stream",
"Mismatch between bound variables and query");
638 std::cout <<
"params\n";
639 for (
int i=0; i<(int)
m_vars.size(); i++) {
641 std::cout << v.
name() <<
" => pos=" << v.
pos() <<
" value=" << v.
value() <<
"\n";
652 if (PQresultStatus(
m_res)!=PGRES_COMMAND_OK) {
653 const char *errp = PQresultErrorField(
m_res, PG_DIAG_SQLSTATE);
681 const char fmt[] =
"%u variable(s) not bound\n";
682 char errbuf[4+
sizeof(fmt)];
696 unsigned int sz=
m_vars.size();
697 Oid* param_types = (Oid*)alloca(sz*
sizeof(Oid));
698 const char** param_values = (
const char**)alloca(sz*
sizeof(
const char*));
699 int* param_lengths = (
int*)alloca(sz*
sizeof(
int));
700 int* param_formats = (
int*)alloca(sz*
sizeof(
int));
701 std::vector<sql_bind_param>::iterator it =
m_vars.begin();
703 for (;it !=
m_vars.end(); ++it, ++i) {
704 param_types[i] = it->pg_type();
706 param_formats[i] = 1;
708 param_values[i] = (
const char*)it->data_ptr();
709 param_lengths[i] = it->data_size();
712 param_values[i] = NULL;
713 param_lengths[i] = 0;
717 param_formats[i] = 0;
719 param_lengths[i] = it->value().length();
720 param_values[i] = it->value().c_str();
723 param_values[i] = NULL;
724 param_lengths[i] = 0;
730 param_values, param_lengths, param_formats,
735 sz, param_values, param_lengths,
748 if (PQresultStatus(
m_pg_res)!=PGRES_TUPLES_OK &&
749 PQresultStatus(
m_pg_res)!=PGRES_COMMAND_OK) {
750 const char *errp = PQresultErrorField(
m_pg_res, PG_DIAG_SQLSTATE);
756 const char* t=PQcmdTuples(
m_pg_res);
773 std::string fetch = std::string(
"FETCH ") + nb +
" FROM " +
m_cursor_name;
834 throw pg_excpt(
"pgstream",
"Type mismatch: bytea type expected");
972 if (!strcmp(param,
"prepare_statements"))
974 else if (!strcmp(param,
"bind_variables"))
976 else if (!strcmp(param,
"nested_transactions"))
979 std::string err=std::string(
"Unrecognized option passed to pg_cnx::set_option: ")+param;
980 throw pg_excpt(
"pg_cnx", err.c_str());
987 if (!strcmp(param,
"prepare_statements"))
989 else if (!strcmp(param,
"bind_variables"))
991 else if (!strcmp(param,
"nested_transactions"))
994 std::string err=std::string(
"Unrecognized option passed to pg_cnx::get_option: ")+param;
995 throw pg_excpt(
"pg_cnx", err.c_str());
1013 m_conn = PQconnectdb(cnx_string);
1015 throw pg_excpt(
"pg_cnx",
"Unable to allocate a connection");
1017 if (PQstatus(
m_conn) == CONNECTION_BAD) {
1024 if (PQtransactionStatus(
m_cnx.
conn()) != PQTRANS_INTRANS) {
1077 std::string errstr=
"";
1080 errstr = PQresultErrorMessage(r);
1081 ecode = PQresultErrorField(r, PG_DIAG_SQLSTATE);
1084 return pg_excpt(query?query:
"pgstream", errstr.c_str(), ecode);
1095 txt.append(
"(ERR=");
1105 init(query, 2, step);
1111 init(query.c_str(), 2, step);
1116 #pragma warning( pop)