本文整理汇总了C++中SPI_getvalue函数的典型用法代码示例。如果您正苦于以下问题:C++ SPI_getvalue函数的具体用法?C++ SPI_getvalue怎么用?C++ SPI_getvalue使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SPI_getvalue函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: __table_log_restore_table_update
void __table_log_restore_table_update(SPITupleTable *spi_tuptable, char *table_restore, char *table_orig_pkey, char *col_query_start, int col_pkey, int number_columns, int i, char *old_pkey_string) {
int size_of_values, j, ret;
char *tmp, *tmp2;
/* memory for dynamic query */
int d_query_size;
char *d_query;
char *d_query_start;
/* get the size of names and values */
size_of_values = 0;
/* go through all columns in this result */
for (j = 1; j <= number_columns; j++) {
/* get value */
tmp = SPI_getvalue(spi_tuptable->vals[i], spi_tuptable->tupdesc, j);
/* and get name of column */
tmp2 = SPI_fname(spi_tuptable->tupdesc, j);
if (tmp == NULL) {
size_of_values += 6 + strlen(do_quote_ident(tmp2)) + 2;
} else {
size_of_values += strlen(do_quote_literal(tmp)) + strlen(do_quote_ident(tmp2)) + 3;
}
}
/* reserve memory */
d_query_size = 250 + size_of_values + NAMEDATALEN + strlen(do_quote_literal(old_pkey_string));
d_query_start = (char *) palloc((d_query_size + 1) * sizeof(char));
d_query = d_query_start;
/* build query */
sprintf(d_query, "UPDATE %s SET ", do_quote_ident(table_restore));
d_query = d_query_start + strlen(d_query_start);
for (j = 1; j <= number_columns; j++) {
if (j > 1) {
strncat(d_query_start, (const char *)", ", d_query_size);
d_query += 2;
}
tmp = SPI_getvalue(spi_tuptable->vals[i], spi_tuptable->tupdesc, j);
tmp2 = SPI_fname(spi_tuptable->tupdesc, j);
if (tmp == NULL) {
snprintf(d_query, d_query_size, "%s=NULL", do_quote_ident(tmp2));
} else {
snprintf(d_query, d_query_size, "%s=%s", do_quote_ident(tmp2), do_quote_literal(tmp));
}
d_query = d_query_start + strlen(d_query_start);
}
snprintf(d_query, d_query_size, " WHERE %s=%s", do_quote_ident(table_orig_pkey), do_quote_literal(old_pkey_string));
d_query = d_query_start + strlen(d_query_start);
#ifdef TABLE_LOG_DEBUG_QUERY
elog(NOTICE, "query: %s", d_query_start);
#endif /* TABLE_LOG_DEBUG_QUERY */
ret = SPI_exec(d_query_start, 0);
if (ret != SPI_OK_UPDATE) {
elog(ERROR, "could not update data in: %s", table_restore);
}
/* done */
}
示例2: __table_log_restore_table_insert
void __table_log_restore_table_insert(SPITupleTable *spi_tuptable, char *table_restore, char *table_orig_pkey, char *col_query_start, int col_pkey, int number_columns, int i) {
int size_of_values, j, ret;
char *tmp;
/* memory for dynamic query */
int d_query_size;
char *d_query;
char *d_query_start;
/* get the size of values */
size_of_values = 0;
/* go through all columns in this result */
for (j = 1; j <= number_columns; j++) {
tmp = SPI_getvalue(spi_tuptable->vals[i], spi_tuptable->tupdesc, j);
if (tmp == NULL) {
size_of_values += 6;
} else {
size_of_values += strlen(do_quote_literal(tmp)) + 3;
}
}
/* reserve memory */
d_query_size = 250 + strlen(col_query_start) + size_of_values;
d_query_start = (char *) palloc((d_query_size + 1) * sizeof(char));
d_query = d_query_start;
/* build query */
sprintf(d_query, "INSERT INTO %s (%s) VALUES (", do_quote_ident(table_restore), col_query_start);
d_query = d_query_start + strlen(d_query_start);
for (j = 1; j <= number_columns; j++) {
if (j > 1) {
strncat(d_query_start, (const char *)", ", d_query_size);
}
tmp = SPI_getvalue(spi_tuptable->vals[i], spi_tuptable->tupdesc, j);
if (tmp == NULL) {
strncat(d_query_start, (const char *)"NULL", d_query_size);
} else {
strncat(d_query_start, do_quote_literal(tmp), d_query_size);
}
}
strncat(d_query_start, (const char *)")", d_query_size);
#ifdef TABLE_LOG_DEBUG_QUERY
elog(NOTICE, "query: %s", d_query_start);
#endif /* TABLE_LOG_DEBUG_QUERY */
ret = SPI_exec(d_query_start, 0);
if (ret != SPI_OK_INSERT) {
elog(ERROR, "could not insert data into: %s", table_restore);
}
/* done */
}
示例3: makeStringInfo
char *lookup_primary_key(char *schemaName, char *tableName, bool failOnMissing) {
StringInfo sql = makeStringInfo();
char *keyname;
SPI_connect();
appendStringInfo(sql, "SELECT column_name FROM information_schema.key_column_usage WHERE table_schema = '%s' AND table_name = '%s'", schemaName, tableName);
SPI_execute(sql->data, true, 1);
if (SPI_processed == 0) {
if (failOnMissing)
elog(ERROR, "Cannot find primary key column for: %s.%s", schemaName, tableName);
else {
SPI_finish();
return NULL;
}
}
keyname = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
if (keyname == NULL)
elog(ERROR, "Primary Key field is null for: %s.%s", schemaName, tableName);
keyname = MemoryContextStrdup(TopTransactionContext, keyname);
SPI_finish();
return keyname;
}
示例4: process_delete
static void process_delete(PgqTriggerEvent *ev, StringInfo sql)
{
TriggerData *tg = ev->tgdata;
HeapTuple old_row = tg->tg_trigtuple;
TupleDesc tupdesc = tg->tg_relation->rd_att;
char *col_ident;
char *col_value;
int i;
int need_and = false;
int attkind_idx;
for (i = 0, attkind_idx = -1; i < tupdesc->natts; i++) {
if (tupdesc->attrs[i]->attisdropped)
continue;
attkind_idx++;
if (!pgqtriga_is_pkey(ev, i, attkind_idx))
continue;
col_ident = SPI_fname(tupdesc, i + 1);
col_value = SPI_getvalue(old_row, tupdesc, i + 1);
if (need_and)
appendStringInfoString(sql, " and ");
else
need_and = true;
append_key_eq(sql, col_ident, col_value);
}
}
示例5: get_database_count
/*
* Returns a count of the number of non-template databases from the catalog.
*/
int get_database_count(void) {
int retval, processed;
StringInfoData buf;
SPITupleTable *coltuptable;
int database_count = 0;
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
initStringInfo(&buf);
appendStringInfo(&buf, "SELECT count(*) FROM pg_database WHERE datname NOT IN ('template0', 'template1') AND datallowconn IS TRUE;");
retval = SPI_execute(buf.data, false, 0);
if (retval != SPI_OK_SELECT) {
elog(FATAL, "Database information collection failed");
// FAIL RETURN 1
}
processed = SPI_processed;
if (processed > 0) {
coltuptable = SPI_tuptable;
database_count = atoi(SPI_getvalue(coltuptable->vals[0], coltuptable->tupdesc, 1));
}
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
return database_count;
}
示例6: plphp_zval_from_tuple
/*
* plphp_zval_from_tuple
* Build a PHP hash from a tuple.
*/
zval *
plphp_zval_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
{
int i;
char *attname = NULL;
zval *array;
MAKE_STD_ZVAL(array);
array_init(array);
for (i = 0; i < tupdesc->natts; i++)
{
char *attdata;
/* Get the attribute name */
attname = tupdesc->attrs[i]->attname.data;
/* and get its value */
if ((attdata = SPI_getvalue(tuple, tupdesc, i + 1)) != NULL)
{
/* "true" means strdup the string */
add_assoc_string(array, attname, attdata, true);
pfree(attdata);
}
else
add_assoc_null(array, attname);
}
return array;
}
示例7: plj_get_configvalue_string
char *
plj_get_configvalue_string(const char *paramName)
{
char *sql;
int proc, ret;
/*
* no SPI_connect, we are already connected.
*/
sql = SPI_palloc(strlen(paramName) + strlen(get_sql));
sprintf(sql, get_sql, paramName);
ret = SPI_exec(sql, 1);
proc = SPI_processed;
if (ret == SPI_OK_SELECT && proc > 0)
{
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
return SPI_getvalue(tuptable->vals[0], tupdesc, 1);
}
elog(WARNING, "[config db] config value not set: %s", paramName);
return "";
}
示例8: fetch_restriction
static
void fetch_restriction(
HeapTuple *tuple,
TupleDesc *tupdesc,
Column_info_t info[4],
Restrict_t *restriction) {
restriction->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
restriction->cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
char *str = DatumGetCString(
SPI_getvalue(*tuple, *tupdesc, info[2].colNumber));
// TODO(someone) because its text, no guarantee the text read is correct
// move this code to c++ to tokenize the integers.
int i = 0;
for (i = 0; i < MAX_RULE_LENGTH; ++i) restriction->restricted_edges[i] = -1;
str[0] = ',';
if (str != NULL) {
char *token = NULL;
int i = 0;
token = (char *)strtok(str, " ,");
while (token != NULL && i < MAX_RULE_LENGTH) {
restriction->restricted_edges[i] = atoi(token);
i++;
token = (char *)strtok(NULL, " ,");
}
}
}
示例9: __table_log_restore_table_delete
void __table_log_restore_table_delete(SPITupleTable *spi_tuptable, char *table_restore, char *table_orig_pkey, char *col_query_start, int col_pkey, int number_columns, int i) {
int ret;
char *tmp;
/* memory for dynamic query */
int d_query_size;
char *d_query;
char *d_query_start;
/* get the size of value */
tmp = SPI_getvalue(spi_tuptable->vals[i], spi_tuptable->tupdesc, col_pkey);
if (tmp == NULL) {
elog(ERROR, "pkey cannot be NULL");
}
/* reserve memory */
d_query_size = 250 + strlen(do_quote_ident(table_restore)) + strlen(do_quote_ident(table_orig_pkey)) + strlen(do_quote_literal(tmp));
d_query_start = (char *) palloc((d_query_size + 1) * sizeof(char));
d_query = d_query_start;
/* build query */
sprintf(d_query, "DELETE FROM %s WHERE %s=%s", do_quote_ident(table_restore), do_quote_ident(table_orig_pkey), do_quote_literal(tmp));
d_query = d_query_start + strlen(d_query_start);
#ifdef TABLE_LOG_DEBUG_QUERY
elog(NOTICE, "query: %s", d_query_start);
#endif /* TABLE_LOG_DEBUG_QUERY */
ret = SPI_exec(d_query_start, 0);
if (ret != SPI_OK_DELETE) {
elog(ERROR, "could not delete data from: %s", table_restore);
}
/* done */
}
示例10: getExtensionLoadPath
static void getExtensionLoadPath()
{
MemoryContext curr;
Datum dtm;
bool isnull;
/*
* Check whether sqlj.loadpath exists before querying it. I would more
* happily just PG_CATCH() the error and compare to ERRCODE_UNDEFINED_TABLE
* but what's required to make that work right is "not terribly well
* documented, but the exception-block handling in plpgsql provides a
* working model" and that code is a lot more fiddly than you would guess.
*/
if ( InvalidOid == get_relname_relid("loadpath",
GetSysCacheOid1(NAMESPACENAME, CStringGetDatum("sqlj"))) )
return;
SPI_connect();
curr = CurrentMemoryContext;
if ( SPI_OK_SELECT == SPI_execute(
"SELECT path, exnihilo FROM sqlj.loadpath", true, 1)
&& 1 == SPI_processed )
{
MemoryContextSwitchTo(TopMemoryContext);
pljavaLoadPath = (char const *)SPI_getvalue(
SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
MemoryContextSwitchTo(curr);
dtm = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2,
&isnull);
if ( isnull )
elog(ERROR, "defect in CREATE EXTENSION script");
extensionExNihilo = DatumGetBool(dtm);
}
SPI_finish();
}
示例11: getPlaceSpecificationFromDatabase
struct PlaceSpecification * getPlaceSpecificationFromDatabase(long long placeid)
{
const char * query = build_placeSpecQuery(placeid);
if ( SPI_execute(query, true, 1) != SPI_OK_SELECT )
ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg(
"Error when performing placeid query")));
if ( SPI_processed < 1 )
ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg(
"unable to find placespecification")));
else if ( SPI_processed > 1 )
ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg(
"too many placespecifications returned!")));
HeapTuple placeRow = * SPI_tuptable->vals;
struct PlaceSpecification * ret = (struct PlaceSpecification *) malloc(sizeof(struct PlaceSpecification));
ret->startX_ = DatumGetFloat4( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 1, NULL) );
ret->startY_ = DatumGetFloat4( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 2, NULL) );
ret->xNumber_ = DatumGetInt32( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 3, NULL) );
ret->yNumber_ = DatumGetInt32( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 4, NULL) );
ret->xIncrement_ = DatumGetFloat4( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 5, NULL) );
ret->yIncrement_ = DatumGetFloat4( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 6, NULL) );
ret->srid_ = DatumGetInt32( SPI_getbinval(placeRow, SPI_tuptable->tupdesc, 7, NULL) );
char * projDef = SPI_getvalue(placeRow, SPI_tuptable->tupdesc, 8);
ret->projDefinition_ = strdup(projDef);
pfree(projDef);
return ret;
}
示例12: load_queue_info
/*
* load queue info from pgq.queue table.
*/
static void load_queue_info(Datum queue_name, struct QueueState *state)
{
Datum values[1];
int res;
TupleDesc desc;
HeapTuple row;
bool isnull;
values[0] = queue_name;
res = SPI_execute_plan(queue_plan, values, NULL, false, 0);
if (res != SPI_OK_SELECT)
elog(ERROR, "Queue fetch failed");
if (SPI_processed == 0)
elog(ERROR, "No such queue");
row = SPI_tuptable->vals[0];
desc = SPI_tuptable->tupdesc;
state->queue_id = DatumGetInt32(SPI_getbinval(row, desc, COL_QUEUE_ID, &isnull));
if (isnull)
elog(ERROR, "queue id NULL");
state->cur_table = DatumGetInt32(SPI_getbinval(row, desc, COL_TBLNO, &isnull));
if (isnull)
elog(ERROR, "table nr NULL");
state->table_prefix = SPI_getvalue(row, desc, COL_PREFIX);
if (state->table_prefix == NULL)
elog(ERROR, "table prefix NULL");
state->next_event_id = SPI_getbinval(row, desc, COL_EVENT_ID, &isnull);
if (isnull)
elog(ERROR, "Seq name NULL");
}
示例13: SPI_connect
char *lookup_field_mapping(MemoryContext cxt, Oid tableRelId, char *fieldname) {
char *definition = NULL;
StringInfo query;
SPI_connect();
query = makeStringInfo();
appendStringInfo(query, "select definition from zdb_mappings where table_name = %d::regclass and field_name = %s;", tableRelId, TextDatumGetCString(DirectFunctionCall1(quote_literal, CStringGetTextDatum(fieldname))));
if (SPI_execute(query->data, true, 2) != SPI_OK_SELECT)
elog(ERROR, "Problem looking up analysis thing with query: %s", query->data);
if (SPI_processed > 1) {
elog(ERROR, "Too many mappings found");
} else if (SPI_processed == 1) {
char *json = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
Size len = strlen(json);
definition = (char *) MemoryContextAllocZero(cxt, (Size) len + 1);
memcpy(definition, json, len);
}
SPI_finish();
return definition;
}
示例14: ensure_valid_environment
/*
* Ensure that the environment is sane.
* This involves checking the Postgresql version, and if in network mode
* also establishing a connection to a receiver.
*/
int ensure_valid_environment(void) {
StringInfoData buf;
int retval;
char* pgversion;
SPITupleTable *coltuptable;
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
/* Ensure compatible version */
pgstat_report_activity(STATE_RUNNING, "verifying compatible postgres version");
initStringInfo(&buf);
appendStringInfo(&buf,
"select version();"
);
retval = SPI_execute(buf.data, false, 0);
if (retval != SPI_OK_SELECT) {
elog(FATAL, "Unable to query postgres version %d", retval);
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
return 1;
}
coltuptable = SPI_tuptable;
pgversion = SPI_getvalue(coltuptable->vals[0], coltuptable->tupdesc, 1);
if(strstr(pgversion, "PostgreSQL 9.3") == NULL) {
elog(FATAL, "Unsupported Postgresql version");
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
return 1;
}
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
/*
* Attempt to establish a connection if the output mode is network.
*/
if (strcmp(output_mode, "network") == 0) {
retval = establish_connection();
if (retval == 2) {
elog(LOG, "Error : Failed to connect to antenna please check domain is available from host.");
}
}
//TODO verify logging directory is accessible when csv mode.
elog(LOG, "Pgsampler Initialized");
return 0;
}
示例15: Persistent_ExecuteQuery
/*
* Performs Actual Query execution
*/
int
Persistent_ExecuteQuery(char const *query, bool readOnlyQuery)
{
StringInfoData sqlstmt;
int ret;
int proc = 0;
Assert (query);
Insist(connected);
/*Initializations*/
sqlstmt.data = NULL;
/* Assemble our query string */
initStringInfo(&sqlstmt);
appendStringInfo(&sqlstmt,"%s",query);
PG_TRY();
{
/*XXX: Need to set the snapshot here. Reason - Unknown*/
ActiveSnapshot = SnapshotNow;
/* Run the query. */
ret = SPI_execute(sqlstmt.data, readOnlyQuery, 0);
proc = SPI_processed;
if (ret > 0 && SPI_tuptable != NULL)
{
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable* tuptable = SPI_tuptable;
int i,j;
char localbuf[8192];
for (j = 0; j< proc; j++)
{
HeapTuple tuple = tuptable->vals[j];
for (i = 1, localbuf[0] = '\0'; i <= tupdesc->natts; i++)
{
snprintf(localbuf + strlen (localbuf), sizeof(localbuf) - strlen(localbuf), " %s%s",
SPI_getvalue(tuple, tupdesc, i),
(i == tupdesc->natts) ? " " : " |");
}
elog (LOG, "==>: %s", localbuf);
}
}
}
/* Clean up in case of error. */
PG_CATCH();
{
pfree(sqlstmt.data);
PG_RE_THROW();
}
PG_END_TRY();
pfree(sqlstmt.data);
return proc;
}