bool PgTblFmncMUsergroup::loadRec( PGresult* res , FmncMUsergroup** rec ) { char* ptr; FmncMUsergroup* _rec = NULL; bool retval = false; if (PQntuples(res) == 1) { _rec = new FmncMUsergroup(); int fnum[] = { PQfnumber(res, "ref"), PQfnumber(res, "grp"), PQfnumber(res, "own"), PQfnumber(res, "sref"), PQfnumber(res, "comment") }; ptr = PQgetvalue(res, 0, fnum[0]); _rec->ref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[1]); _rec->grp = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[2]); _rec->own = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[3]); _rec->sref.assign(ptr, PQgetlength(res, 0, fnum[3])); ptr = PQgetvalue(res, 0, fnum[4]); _rec->Comment.assign(ptr, PQgetlength(res, 0, fnum[4])); retval = true; }; PQclear(res); *rec = _rec; return retval; };
char * CPgSQL::Fetch(char * buffer, unsigned int buffersize) { if (PQstatus(pgsql) != CONNECTION_OK || PQresultStatus(result) != PGRES_TUPLES_OK || PQntuples(result) <= CurRow) return NULL; // Calculate length of the row. unsigned long row_length = 0; for (unsigned int i = 0; i < NumCol; ++i) { // We will need one more character per a column to act as a column separator and a NULL terminating character. row_length += PQgetlength(result, CurRow, i) + 1; } if (row_length == 0) return NULL; // If the row length exceeds size of the SPACER buffer, allocate a new one. // The SPACER will be deleted automatically in nwnx2lib.cpp. char * result = row_length <= buffersize ? buffer : (char *)malloc(row_length); for (unsigned int i = 0, pos = 0; i < NumCol; ++i) { int length = PQgetlength(this->result, CurRow, i); strncpy(&result[pos], PQgetvalue(this->result, CurRow, i), length); pos += length; result[pos++] = '¬'; } result[row_length-1] = '\0'; CurRow++; return result; }
bool PgTblPlnrRMStackMStructure::loadRec( PGresult* res , PlnrRMStackMStructure** rec ) { char* ptr; PlnrRMStackMStructure* _rec = NULL; bool retval = false; if (PQntuples(res) == 1) { _rec = new PlnrRMStackMStructure(); int fnum[] = { PQfnumber(res, "ref"), PQfnumber(res, "refplnrmstack"), PQfnumber(res, "refplnrmstructure"), PQfnumber(res, "fromsrefplnrmlayer"), PQfnumber(res, "tosrefplnrmlayer") }; ptr = PQgetvalue(res, 0, fnum[0]); _rec->ref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[1]); _rec->refPlnrMStack = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[2]); _rec->refPlnrMStructure = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[3]); _rec->fromSrefPlnrMLayer.assign(ptr, PQgetlength(res, 0, fnum[3])); ptr = PQgetvalue(res, 0, fnum[4]); _rec->toSrefPlnrMLayer.assign(ptr, PQgetlength(res, 0, fnum[4])); retval = true; }; PQclear(res); *rec = _rec; return retval; };
bool PgTblFmncAMToolChar::loadRec( PGresult* res , FmncAMToolChar** rec ) { char* ptr; FmncAMToolChar* _rec = NULL; bool retval = false; if (PQntuples(res) == 1) { _rec = new FmncAMToolChar(); int fnum[] = { PQfnumber(res, "ref"), PQfnumber(res, "reffmncmtool"), PQfnumber(res, "x1osreffmncktoolchar"), PQfnumber(res, "val"), PQfnumber(res, "comment") }; ptr = PQgetvalue(res, 0, fnum[0]); _rec->ref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[1]); _rec->refFmncMTool = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[2]); _rec->x1OsrefFmncKToolchar.assign(ptr, PQgetlength(res, 0, fnum[2])); ptr = PQgetvalue(res, 0, fnum[3]); _rec->Val.assign(ptr, PQgetlength(res, 0, fnum[3])); ptr = PQgetvalue(res, 0, fnum[4]); _rec->Comment.assign(ptr, PQgetlength(res, 0, fnum[4])); retval = true; }; PQclear(res); *rec = _rec; return retval; };
ubigint PgTblFmncQPrsList::loadRst( PGresult* res , const bool append , ListFmncQPrsList& rst ) { ubigint numrow; ubigint numread = 0; char* ptr; FmncQPrsList* rec; if (!append) rst.clear(); numrow = PQntuples(res); if (numrow > 0) { rst.nodes.reserve(rst.nodes.size() + numrow); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "title"), PQfnumber(res, "firstname"), PQfnumber(res, "lastname"), PQfnumber(res, "grp"), PQfnumber(res, "own"), PQfnumber(res, "reffmncmorg"), PQfnumber(res, "reffmncmaddress"), PQfnumber(res, "ixvsex"), PQfnumber(res, "tel"), PQfnumber(res, "eml") }; while (numread < numrow) { rec = new FmncQPrsList(); ptr = PQgetvalue(res, numread, fnum[0]); rec->qref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[1]); rec->jref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[2]); rec->jnum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[3]); rec->ref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[4]); rec->Title.assign(ptr, PQgetlength(res, numread, fnum[4])); ptr = PQgetvalue(res, numread, fnum[5]); rec->Firstname.assign(ptr, PQgetlength(res, numread, fnum[5])); ptr = PQgetvalue(res, numread, fnum[6]); rec->Lastname.assign(ptr, PQgetlength(res, numread, fnum[6])); ptr = PQgetvalue(res, numread, fnum[7]); rec->grp = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[8]); rec->own = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[9]); rec->refFmncMOrg = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[10]); rec->refFmncMAddress = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[11]); rec->ixVSex = atol(ptr); ptr = PQgetvalue(res, numread, fnum[12]); rec->Tel.assign(ptr, PQgetlength(res, numread, fnum[12])); ptr = PQgetvalue(res, numread, fnum[13]); rec->Eml.assign(ptr, PQgetlength(res, numread, fnum[13])); rst.nodes.push_back(rec); numread++; }; }; PQclear(res); return numread; };
ubigint PgTblFmncQStpKSteppar::loadRst( PGresult* res , const bool append , ListFmncQStpKSteppar& rst ) { ubigint numrow; ubigint numread = 0; char* ptr; FmncQStpKSteppar* rec; if (!append) rst.clear(); numrow = PQntuples(res); if (numrow > 0) { rst.nodes.reserve(rst.nodes.size() + numrow); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "klsnum"), PQfnumber(res, "fixed"), PQfnumber(res, "sref"), PQfnumber(res, "avail"), PQfnumber(res, "implied"), PQfnumber(res, "refj"), PQfnumber(res, "title"), PQfnumber(res, "comment") }; while (numread < numrow) { rec = new FmncQStpKSteppar(); ptr = PQgetvalue(res, numread, fnum[0]); rec->qref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[1]); rec->jref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[2]); rec->jnum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[3]); rec->ref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[4]); rec->klsNum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[5]); rec->Fixed = (atoi(ptr) != 0); ptr = PQgetvalue(res, numread, fnum[6]); rec->sref.assign(ptr, PQgetlength(res, numread, fnum[6])); ptr = PQgetvalue(res, numread, fnum[7]); rec->Avail.assign(ptr, PQgetlength(res, numread, fnum[7])); ptr = PQgetvalue(res, numread, fnum[8]); rec->Implied.assign(ptr, PQgetlength(res, numread, fnum[8])); ptr = PQgetvalue(res, numread, fnum[9]); rec->refJ = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[10]); rec->Title.assign(ptr, PQgetlength(res, numread, fnum[10])); ptr = PQgetvalue(res, numread, fnum[11]); rec->Comment.assign(ptr, PQgetlength(res, numread, fnum[11])); rst.nodes.push_back(rec); numread++; }; }; PQclear(res); return numread; };
/* * This function prints a query result that is a binary-format fetch from * a table defined as in the comment above. We split it out because the * main() function uses it twice. */ static void show_binary_results(PGresult *res) { int i, j; int i_fnum, t_fnum, b_fnum; /* Use PQfnumber to avoid assumptions about field order in result */ i_fnum = PQfnumber(res, "i"); t_fnum = PQfnumber(res, "t"); b_fnum = PQfnumber(res, "b"); for (i = 0; i < PQntuples(res); i++) { char *iptr; char *tptr; char *bptr; int blen; int ival; /* Get the field values (we ignore possibility they are null!) */ iptr = PQgetvalue(res, i, i_fnum); tptr = PQgetvalue(res, i, t_fnum); bptr = PQgetvalue(res, i, b_fnum); /* * The binary representation of INT4 is in network byte order, which * we'd better coerce to the local byte order. */ ival = ntohl(*((uint32_t *) iptr)); /* * The binary representation of TEXT is, well, text, and since libpq * was nice enough to append a zero byte to it, it'll work just fine * as a C string. * * The binary representation of BYTEA is a bunch of bytes, which could * include embedded nulls so we have to pay attention to field length. */ blen = PQgetlength(res, i, b_fnum); printf("tuple %d: got\n", i); printf(" i = (%d bytes) %d\n", PQgetlength(res, i, i_fnum), ival); printf(" t = (%d bytes) '%s'\n", PQgetlength(res, i, t_fnum), tptr); printf(" b = (%d bytes) ", blen); for (j = 0; j < blen; j++) printf("\\%03o", bptr[j]); printf("\n\n"); } }
ubigint PgTblFmncMSample::loadRst( PGresult* res , const bool append , ListFmncMSample& rst ) { ubigint numrow; ubigint numread = 0; char* ptr; FmncMSample* rec; if (!append) rst.clear(); numrow = PQntuples(res); if (numrow > 0) { rst.nodes.reserve(rst.nodes.size() + numrow); int fnum[] = { PQfnumber(res, "ref"), PQfnumber(res, "grp"), PQfnumber(res, "own"), PQfnumber(res, "supreffmncmsample"), PQfnumber(res, "suplvl"), PQfnumber(res, "reffmncmarticle"), PQfnumber(res, "sref"), PQfnumber(res, "material"), PQfnumber(res, "refjstate"), PQfnumber(res, "ixvstate"), PQfnumber(res, "comment") }; while (numread < numrow) { rec = new FmncMSample(); ptr = PQgetvalue(res, numread, fnum[0]); rec->ref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[1]); rec->grp = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[2]); rec->own = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[3]); rec->supRefFmncMSample = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[4]); rec->supLvl = atoi(ptr); ptr = PQgetvalue(res, numread, fnum[5]); rec->refFmncMArticle = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[6]); rec->sref.assign(ptr, PQgetlength(res, numread, fnum[6])); ptr = PQgetvalue(res, numread, fnum[7]); rec->Material.assign(ptr, PQgetlength(res, numread, fnum[7])); ptr = PQgetvalue(res, numread, fnum[8]); rec->refJState = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[9]); rec->ixVState = atol(ptr); ptr = PQgetvalue(res, numread, fnum[10]); rec->Comment.assign(ptr, PQgetlength(res, numread, fnum[10])); rst.nodes.push_back(rec); numread++; }; }; PQclear(res); return numread; };
bool PgTblFmncQPrsList::loadRec( PGresult* res , FmncQPrsList** rec ) { char* ptr; FmncQPrsList* _rec = NULL; bool retval = false; if (PQntuples(res) == 1) { _rec = new FmncQPrsList(); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "title"), PQfnumber(res, "firstname"), PQfnumber(res, "lastname"), PQfnumber(res, "grp"), PQfnumber(res, "own"), PQfnumber(res, "reffmncmorg"), PQfnumber(res, "reffmncmaddress"), PQfnumber(res, "ixvsex"), PQfnumber(res, "tel"), PQfnumber(res, "eml") }; ptr = PQgetvalue(res, 0, fnum[0]); _rec->qref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[1]); _rec->jref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[2]); _rec->jnum = atol(ptr); ptr = PQgetvalue(res, 0, fnum[3]); _rec->ref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[4]); _rec->Title.assign(ptr, PQgetlength(res, 0, fnum[4])); ptr = PQgetvalue(res, 0, fnum[5]); _rec->Firstname.assign(ptr, PQgetlength(res, 0, fnum[5])); ptr = PQgetvalue(res, 0, fnum[6]); _rec->Lastname.assign(ptr, PQgetlength(res, 0, fnum[6])); ptr = PQgetvalue(res, 0, fnum[7]); _rec->grp = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[8]); _rec->own = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[9]); _rec->refFmncMOrg = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[10]); _rec->refFmncMAddress = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[11]); _rec->ixVSex = atol(ptr); ptr = PQgetvalue(res, 0, fnum[12]); _rec->Tel.assign(ptr, PQgetlength(res, 0, fnum[12])); ptr = PQgetvalue(res, 0, fnum[13]); _rec->Eml.assign(ptr, PQgetlength(res, 0, fnum[13])); retval = true; }; PQclear(res); *rec = _rec; return retval; };
ubigint PgTblPlnrQDtpKParKey::loadRst( PGresult* res , const bool append , ListPlnrQDtpKParKey& rst ) { ubigint numrow; ubigint numread = 0; char* ptr; PlnrQDtpKParKey* rec; if (!append) rst.clear(); numrow = PQntuples(res); if (numrow > 0) { rst.nodes.reserve(rst.nodes.size() + numrow); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "klsnum"), PQfnumber(res, "sref"), PQfnumber(res, "refj"), PQfnumber(res, "title"), PQfnumber(res, "comment") }; while (numread < numrow) { rec = new PlnrQDtpKParKey(); ptr = PQgetvalue(res, numread, fnum[0]); rec->qref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[1]); rec->jref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[2]); rec->jnum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[3]); rec->ref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[4]); rec->klsNum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[5]); rec->sref.assign(ptr, PQgetlength(res, numread, fnum[5])); ptr = PQgetvalue(res, numread, fnum[6]); rec->refJ = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[7]); rec->Title.assign(ptr, PQgetlength(res, numread, fnum[7])); ptr = PQgetvalue(res, numread, fnum[8]); rec->Comment.assign(ptr, PQgetlength(res, numread, fnum[8])); rst.nodes.push_back(rec); numread++; }; }; PQclear(res); return numread; };
bool PostgreSQLInterface::PQGetValueFromBinary(unsigned int *output, PGresult *result, int rowIndex, const char *columnName) { int columnIndex = PQfnumber(result, columnName); if (columnIndex==-1) return false; char *binaryData = PQgetvalue(result, rowIndex, columnIndex); if (binaryData==0 || PQgetlength(result, rowIndex, columnIndex)==0) return false; if (binaryData) { RakAssert(PQgetlength(result, rowIndex, columnIndex)==sizeof(unsigned int)); memcpy(output, binaryData, sizeof(unsigned int)); EndianSwapInPlace((char*)output, sizeof(unsigned int)); return true; } return false; }
bool PgTblFmncQStpKSteppar::loadRec( PGresult* res , FmncQStpKSteppar** rec ) { char* ptr; FmncQStpKSteppar* _rec = NULL; bool retval = false; if (PQntuples(res) == 1) { _rec = new FmncQStpKSteppar(); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "klsnum"), PQfnumber(res, "fixed"), PQfnumber(res, "sref"), PQfnumber(res, "avail"), PQfnumber(res, "implied"), PQfnumber(res, "refj"), PQfnumber(res, "title"), PQfnumber(res, "comment") }; ptr = PQgetvalue(res, 0, fnum[0]); _rec->qref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[1]); _rec->jref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[2]); _rec->jnum = atol(ptr); ptr = PQgetvalue(res, 0, fnum[3]); _rec->ref = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[4]); _rec->klsNum = atol(ptr); ptr = PQgetvalue(res, 0, fnum[5]); _rec->Fixed = (atoi(ptr) != 0); ptr = PQgetvalue(res, 0, fnum[6]); _rec->sref.assign(ptr, PQgetlength(res, 0, fnum[6])); ptr = PQgetvalue(res, 0, fnum[7]); _rec->Avail.assign(ptr, PQgetlength(res, 0, fnum[7])); ptr = PQgetvalue(res, 0, fnum[8]); _rec->Implied.assign(ptr, PQgetlength(res, 0, fnum[8])); ptr = PQgetvalue(res, 0, fnum[9]); _rec->refJ = atoll(ptr); ptr = PQgetvalue(res, 0, fnum[10]); _rec->Title.assign(ptr, PQgetlength(res, 0, fnum[10])); ptr = PQgetvalue(res, 0, fnum[11]); _rec->Comment.assign(ptr, PQgetlength(res, 0, fnum[11])); retval = true; }; PQclear(res); *rec = _rec; return retval; };
/* * Make a Ruby array out of the encoded values from the specified * column in the given result. */ static VALUE make_column_result_array( VALUE self, int col ) { PGresult *result = pgresult_get( self ); int rows = PQntuples( result ); int i; VALUE val = Qnil; VALUE results = rb_ary_new2( rows ); if ( col >= PQnfields(result) ) rb_raise( rb_eIndexError, "no column %d in result", col ); for ( i=0; i < rows; i++ ) { val = rb_tainted_str_new( PQgetvalue(result, i, col), PQgetlength(result, i, col) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, col) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_ary_store( results, i, val ); } return results; }
static int pgsql_get_user_key(u08bits *usname, u08bits *realm, hmackey_t key) { int ret = -1; PGconn * pqc = get_pqdb_connection(); if(pqc) { char statement[TURN_LONG_STRING_SIZE]; /* direct user input eliminated - there is no SQL injection problem (since version 4.4.5.3) */ snprintf(statement,sizeof(statement),"select hmackey from turnusers_lt where name='%s' and realm='%s'",usname,realm); PGresult *res = PQexec(pqc, statement); if(!res || (PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res)!=1)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error retrieving PostgreSQL DB information: %s\n",PQerrorMessage(pqc)); } else { char *kval = PQgetvalue(res,0,0); int len = PQgetlength(res,0,0); if(kval) { size_t sz = get_hmackey_size(SHATYPE_DEFAULT); if(((size_t)len<sz*2)||(strlen(kval)<sz*2)) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key format: %s, user %s\n",kval,usname); } else if(convert_string_key_to_binary(kval, key, sz)<0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n",kval,usname); } else { ret = 0; } } else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong hmackey data for user %s: NULL\n",usname); } } if(res) PQclear(res); } return ret; }
static VALUE result_each(VALUE self) { int r, c, rows, cols, *types, failed; PGresult *res; Data_Get_Struct(self, PGresult, res); VALUE fields = rb_ary_new(); rows = PQntuples(res); cols = PQnfields(res); types = (int*)malloc(sizeof(int)*cols); for (c = 0; c < cols; c++) { rb_ary_push(fields, ID2SYM(rb_intern(PQfname(res, c)))); types[c] = PQftype(res, c); } for (r = 0; r < rows; r++) { VALUE tuple = rb_hash_new(); for (c = 0; c < cols; c++) { rb_hash_aset(tuple, rb_ary_entry(fields, c), PQgetisnull(res, r, c) ? Qnil : typecast(PQgetvalue(res, r, c), PQgetlength(res, r, c), types[c])); } rb_protect(rb_yield, tuple, &failed); if (failed) { free(types); rb_jump_tag(failed); } } free(types); return Qnil; }
int DTTablePostgres::fieldSize(int tupleNumber, int columnNumber) const { if ((tupleNumber ) >= PQntuples(result) || tupleNumber < 0 || (columnNumber) >= PQnfields(result) || columnNumber < 0) { EXToutOfRange("Parameters out of range"); } return PQgetlength(result, tupleNumber, columnNumber); }
/************************************************************************* * * Function: sql_fetch_row * * Purpose: database specific fetch_row. Returns a SQL_ROW struct * with all the data for the query in 'sqlsocket->row'. Returns * 0 on success, -1 on failure, SQL_DOWN if 'database is down'. * *************************************************************************/ static int sql_fetch_row(SQLSOCK * sqlsocket, SQL_CONFIG *config) { int records, i, len; rlm_sql_postgres_sock *pg_sock = sqlsocket->conn; sqlsocket->row = NULL; if (pg_sock->cur_row >= PQntuples(pg_sock->result)) return 0; free_result_row(pg_sock); records = PQnfields(pg_sock->result); pg_sock->num_fields = records; if ((PQntuples(pg_sock->result) > 0) && (records > 0)) { pg_sock->row = (char **)rad_malloc((records+1)*sizeof(char *)); memset(pg_sock->row, '\0', (records+1)*sizeof(char *)); for (i = 0; i < records; i++) { len = PQgetlength(pg_sock->result, pg_sock->cur_row, i); pg_sock->row[i] = (char *)rad_malloc(len+1); memset(pg_sock->row[i], '\0', len+1); strncpy(pg_sock->row[i], PQgetvalue(pg_sock->result, pg_sock->cur_row,i),len); } pg_sock->cur_row++; sqlsocket->row = pg_sock->row; return 0; } else { return 0; } }
void ResultSet::init(PGresult* res) { int nFields = PQnfields(res); for (int i = 0; i < nFields; i = i + 1) { this->columns.push_back(PQfname(res, i)); } int nTuples = PQntuples(res); std::vector<std::string*>* v; char* value; for (int i = 0; i < nTuples; i = i + 1) { v = new std::vector<std::string*>(); for (int j = 0; j < nFields; j++) { if (PQgetisnull(res, i, j)) { v->push_back(nullptr); } else { value = PQgetvalue(res, i, j); v->push_back(new std::string(value, PQgetlength(res, i, j))); } } this->rows.push_back(v); } }
/************************************************************************* * * Function: sql_fetch_row * * Purpose: database specific fetch_row. Returns a rlm_sql_row_t struct * with all the data for the query in 'handle->row'. Returns * 0 on success, -1 on failure, RLM_SQL_RECONNECT if 'database is down'. * *************************************************************************/ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config) { int records, i, len; rlm_sql_postgres_conn_t *conn = handle->conn; handle->row = NULL; if (conn->cur_row >= PQntuples(conn->result)) return 0; free_result_row(conn); records = PQnfields(conn->result); conn->num_fields = records; if ((PQntuples(conn->result) > 0) && (records > 0)) { conn->row = (char **)rad_malloc((records+1)*sizeof(char *)); memset(conn->row, '\0', (records+1)*sizeof(char *)); for (i = 0; i < records; i++) { len = PQgetlength(conn->result, conn->cur_row, i); conn->row[i] = (char *)rad_malloc(len+1); memset(conn->row[i], '\0', len+1); strlcpy(conn->row[i], PQgetvalue(conn->result, conn->cur_row,i),len + 1); } conn->cur_row++; handle->row = conn->row; } return 0; }
/* * call-seq: * res.getvalue( tup_num, field_num ) * * Returns the value in tuple number _tup_num_, field _field_num_, * or +nil+ if the field is +NULL+. */ static VALUE pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num) { VALUE val; PGresult *result; int i = NUM2INT(tup_num); int j = NUM2INT(field_num); result = pgresult_get(self); if(i < 0 || i >= PQntuples(result)) { rb_raise(rb_eArgError,"invalid tuple number %d", i); } if(j < 0 || j >= PQnfields(result)) { rb_raise(rb_eArgError,"invalid field number %d", j); } if(PQgetisnull(result, i, j)) return Qnil; val = rb_tainted_str_new(PQgetvalue(result, i, j), PQgetlength(result, i, j)); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, j) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif return val; }
void doSQL(PGconn *conn, char *command) { PGresult *result; printf("%s\n", command); result = PQexec(conn, command); printf("status is %s\n", PQresStatus(PQresultStatus(result))); printf("#rows affected %s\n", PQcmdTuples(result)); printf("result message: %s\n", PQresultErrorMessage(result)); switch(PQresultStatus(result)) { case PGRES_TUPLES_OK: { int r, n; int nrows = PQntuples(result); int nfields = PQnfields(result); printf("number of rows returned = %d\n", nrows); printf("number of fields returned = %d\n", nfields); for(r = 0; r < nrows; r++) { for(n = 0; n < nfields; n++) printf(" %s = %s(%d),", PQfname(result, n), PQgetvalue(result, r, n), PQgetlength(result, r, n)); printf("\n"); } } } PQclear(result); }
/** * Get all of the keys in the datastore. * * @param cls closure with the `struct Plugin *` * @param proc function to call on each key * @param proc_cls closure for @a proc */ static void postgres_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls) { struct Plugin *plugin = cls; int ret; int i; struct GNUNET_HashCode key; PGresult * res; res = PQexecPrepared (plugin->dbh, "get_keys", 0, NULL, NULL, NULL, 1); ret = PQntuples (res); for (i=0;i<ret;i++) { if (sizeof (struct GNUNET_HashCode) != PQgetlength (res, i, 0)) { memcpy (&key, PQgetvalue (res, i, 0), sizeof (struct GNUNET_HashCode)); proc (proc_cls, &key, 1); } } PQclear (res); proc (proc_cls, NULL, 0); }
/* * As a "hack" to avoid extra allocation and complications by using PQunescapeBytea() * we instead unescape the buffer retrieved via PQgetvalue 'in-place'. This should * be safe as unescape will only modify internal bytes in the buffer and not change * the buffer pointer. See also unescape_bytea() above. */ const void *PostgresqlResultSet_getBlob(T R, int columnIndex, int *size) { assert(R); int i = checkAndSetColumnIndex(columnIndex, R->columnCount); if (PQgetisnull(R->res, R->currentRow, i)) return NULL; return unescape_bytea((uchar_t*)PQgetvalue(R->res, R->currentRow, i), PQgetlength(R->res, R->currentRow, i), size); }
static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned int *len) { pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; char *id = NULL; if (name == NULL) { if (H->pgoid == InvalidOid) { return NULL; } *len = spprintf(&id, 0, ZEND_LONG_FMT, (zend_long) H->pgoid); } else { PGresult *res; ExecStatusType status; const char *q[1]; q[0] = name; res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0); status = PQresultStatus(res); if (res && (status == PGRES_TUPLES_OK)) { id = estrdup((char *)PQgetvalue(res, 0, 0)); *len = PQgetlength(res, 0, 0); } else { pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res)); } if (res) { PQclear(res); } } return id; }
LispObj * Lisp_PQgetlength(LispBuiltin *builtin) /* pq-getlength result tupple field-number */ { PGresult *res; int tuple, field, length; LispObj *result, *otupple, *field_number; field_number = ARGUMENT(2); otupple = ARGUMENT(1); result = ARGUMENT(0); if (!CHECKO(result, PGresult_t)) LispDestroy("%s: cannot convert %s to PGresult*", STRFUN(builtin), STROBJ(result)); res = (PGresult*)(result->data.opaque.data); CHECK_INDEX(otupple); tuple = FIXNUM_VALUE(otupple); CHECK_INDEX(field_number); field = FIXNUM_VALUE(field_number); length = PQgetlength(res, tuple, field); return (INTEGER(length)); }
int ResultSet::getColumnSize(const QString &column_name) { int col_idx=-1; try { /* Raises an error if the user try to get the value of a column in a tuple of an empty result or generated from an INSERT, DELETE, UPDATE, that is, which command do not return lines but only do updates or removal */ if(getTupleCount()==0 || empty_result) throw Exception(ERR_REF_TUPLE_INEXISTENT, __PRETTY_FUNCTION__, __FILE__, __LINE__); else if(current_tuple < 0 || current_tuple >= getTupleCount()) throw Exception(ERR_REF_INV_TUPLE_COLUMN, __PRETTY_FUNCTION__, __FILE__, __LINE__); //Get the column index wich length must be detected col_idx=getColumnIndex(column_name); } catch(Exception &e) { //Capture and redirect any generated exception throw Exception(e.getErrorMessage(), e.getErrorType(), __PRETTY_FUNCTION__, __FILE__, __LINE__, &e); } //Returns the column value length on the current tuple return(PQgetlength(sql_result, current_tuple, col_idx)); }
static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len) { pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; char *id = NULL; PGresult *res; ExecStatusType status; if (name == NULL) { res = PQexec(H->server, "SELECT LASTVAL()"); } else { const char *q[1]; q[0] = name; res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0); } status = PQresultStatus(res); if (res && (status == PGRES_TUPLES_OK)) { id = estrdup((char *)PQgetvalue(res, 0, 0)); *len = PQgetlength(res, 0, 0); } else { pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res)); } if (res) { PQclear(res); } return id; }
VALUE pg_tmbc_result_value( t_typemap *p_typemap, VALUE result, int tuple, int field ) { t_pg_coder *p_coder = NULL; t_pg_result *p_result = pgresult_get_this(result); t_tmbc *this = (t_tmbc *) p_typemap; t_typemap *default_tm; if (PQgetisnull(p_result->pgresult, tuple, field)) { return Qnil; } p_coder = this->convs[field].cconv; if( p_coder ){ char * val = PQgetvalue( p_result->pgresult, tuple, field ); int len = PQgetlength( p_result->pgresult, tuple, field ); if( p_coder->dec_func ){ return p_coder->dec_func(p_coder, val, len, tuple, field, ENCODING_GET(result)); } else { t_pg_coder_dec_func dec_func; dec_func = pg_coder_dec_func( p_coder, PQfformat(p_result->pgresult, field) ); return dec_func(p_coder, val, len, tuple, field, ENCODING_GET(result)); } } default_tm = DATA_PTR( this->typemap.default_typemap ); return default_tm->funcs.typecast_result_value( default_tm, result, tuple, field ); }
/* * Receive a single file as a malloc'd buffer. */ char * libpqGetFile(const char *filename, size_t *filesize) { PGresult *res; char *result; int len; const char *paramValues[1]; paramValues[0] = filename; res = PQexecParams(conn, "SELECT pg_read_binary_file($1)", 1, NULL, paramValues, NULL, NULL, 1); if (PQresultStatus(res) != PGRES_TUPLES_OK) pg_fatal("could not fetch remote file \"%s\": %s", filename, PQresultErrorMessage(res)); /* sanity check the result set */ if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0)) pg_fatal("unexpected result set while fetching remote file \"%s\"\n", filename); /* Read result to local variables */ len = PQgetlength(res, 0, 0); result = pg_malloc(len + 1); memcpy(result, PQgetvalue(res, 0, 0), len); result[len] = '\0'; PQclear(res); pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len); if (filesize) *filesize = len; return result; }
ubigint PgTblPlnrQDtpAPar::loadRst( PGresult* res , const bool append , ListPlnrQDtpAPar& rst ) { ubigint numrow; ubigint numread = 0; char* ptr; PlnrQDtpAPar* rec; if (!append) rst.clear(); numrow = PQntuples(res); if (numrow > 0) { rst.nodes.reserve(rst.nodes.size() + numrow); int fnum[] = { PQfnumber(res, "qref"), PQfnumber(res, "jref"), PQfnumber(res, "jnum"), PQfnumber(res, "ref"), PQfnumber(res, "x2srefkkey"), PQfnumber(res, "refplnrmfile"), PQfnumber(res, "val") }; while (numread < numrow) { rec = new PlnrQDtpAPar(); ptr = PQgetvalue(res, numread, fnum[0]); rec->qref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[1]); rec->jref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[2]); rec->jnum = atol(ptr); ptr = PQgetvalue(res, numread, fnum[3]); rec->ref = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[4]); rec->x2SrefKKey.assign(ptr, PQgetlength(res, numread, fnum[4])); ptr = PQgetvalue(res, numread, fnum[5]); rec->refPlnrMFile = atoll(ptr); ptr = PQgetvalue(res, numread, fnum[6]); rec->Val.assign(ptr, PQgetlength(res, numread, fnum[6])); rst.nodes.push_back(rec); numread++; }; }; PQclear(res); return numread; };