这篇教程C++ text_to_cstring函数代码示例写得很实用,希望能帮到您。
本文整理汇总了C++中text_to_cstring函数的典型用法代码示例。如果您正苦于以下问题:C++ text_to_cstring函数的具体用法?C++ text_to_cstring怎么用?C++ text_to_cstring使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。 在下文中一共展示了text_to_cstring函数的30个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。 示例1: xmlvalidate_dtd/** * Validation function take first argument as XML type, then check if is * well formated. If so, do the same for DTD document. If both success, check * XML document against DTD schema restrictions. * @return true if pass, false otherwise */Datum xmlvalidate_dtd(PG_FUNCTION_ARGS){#ifdef USE_LIBXML int ret = -1; xmlValidCtxtPtr validctxt = NULL; xmlDtdPtr dtd = NULL; bool result = false; text *data = NULL; char *dtdstr = NULL; xmltype *xmldata = NULL; char *xmldataint = NULL; xmlChar *xmldatastr = NULL; int lenxml = -1; // length of xml data xmlDocPtr doc = NULL; // creating xmlChar * from internal xmltype of stored XML xmldata = PG_GETARG_XML_P(0); xmldataint = VARDATA(xmldata); lenxml = VARSIZE(xmldata) - VARHDRSZ; xmldatastr = (xmlChar *) palloc((lenxml + 1) * sizeof(xmlChar)); memcpy(xmldatastr, xmldataint, lenxml); xmldatastr[lenxml] = '/0'; // creating xmlChar* from text representation of DTD data = PG_GETARG_TEXT_P(1); dtdstr = text_to_cstring(data); //initialize LibXML structures, if allready done -> do nothing pg_xml_init(); xmlInitParser(); doc = xmlReadMemory((const char *)xmldatastr, lenxml, "include.xml", NULL, 0); if (doc == NULL) { elog(ERROR, "Failed to parse XML document"); PG_RETURN_BOOL (false); } // create DTD from memory, must use XML_CHAR_ENCODING_NONE dtd = xmlIOParseDTD(NULL, xmlParserInputBufferCreateMem(dtdstr, strlen(dtdstr), XML_CHAR_ENCODING_NONE), XML_CHAR_ENCODING_NONE); if (dtd == NULL) { // unable to create parser context elog(ERROR, "Error with creating DTD schema, check if schema is valid"); PG_RETURN_BOOL (false); } validctxt = xmlNewValidCtxt(); if (validctxt == NULL) { // cant create validation context elog(INFO ,"cant create validation context"); xmlFreeDtd(dtd); PG_RETURN_BOOL (false); } ret = xmlValidateDtd(validctxt, doc, dtd); if (ret == 0) { elog(INFO, "Validates"); result = true; } else if (ret > 0) { elog(INFO, "Dont validates"); result = false; } else { elog(INFO, "Validation generated an internal error"); result = false; } xmlFreeDtd(dtd); xmlFreeValidCtxt(validctxt); xmlFreeDoc(doc); // clean up document in memmory xmlCleanupParser(); // clean up stream parser PG_RETURN_BOOL (result);#else NO_XML_SUPPORT(); PG_RETURN_BOOL (false);#endif }
开发者ID:killteck,项目名称:indexing-xml,代码行数:90,
示例2: kc_expandDatum kc_expand(PG_FUNCTION_ARGS) { KC_ENTRY *search; FuncCallContext *funcctx; int call_cntr; char *kbuf; size_t ksiz, vsiz; const char *cvbuf; char *kv_kbuf = NULL; size_t kv_ksiz; int done; /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); // Make sure that there are enough args. if (PG_NARGS() < MIN_ARGS) { ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("Must run expand with at least %d args!", MIN_ARGS))); } /* Make the kcdb here. */ search = (KC_ENTRY *)palloc(sizeof(KC_ENTRY)); search->db = kcdbnew(); if (open_db (search->db, text_to_cstring(PG_GETARG_TEXT_PP(0)), text_to_cstring(PG_GETARG_TEXT_PP(1)))) { // Set the key to jump into: // Call with -- map_name, result_id, class, doctype, pop, psource // Here, map_name describes a db to open. // Otherwise, result_id:class:doctype:pop:psource (search->jump_key) = (char *) palloc(MAX_JUMP_KEY_LEN * sizeof(char)); int index_point; search->jump_key = text_to_cstring(PG_GETARG_TEXT_PP(2)); int size_left = MAX_JUMP_KEY_LEN; for (index_point = START_VARIABLE_INDEX; index_point < END_VARIABLE_INDEX; index_point++) { if (PG_NARGS() > index_point) { char *next_idx = text_to_cstring(PG_GETARG_TEXT_PP(index_point)); if (next_idx != NULL) { size_left = size_left - (2 + strlen(next_idx)); strncat (search->jump_key, CF_LABEL_SEP, size_left); strncat (search->jump_key, next_idx, size_left); } } } #ifdef CF_DUBUG ereport(NOTICE, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Setting jump buffer -- [%s]", search->jump_key)));#endif // Create a cursor, and set it to the base point looking for entries. search->cur = kcdbcursor(search->db); kccurjumpkey(search->cur, search->jump_key, MAX_JUMP_KEY_LEN); } else { search->db = NULL; } search->next_map = 0; search->msg = NULL; // Save the search struct for the subsequent calls. funcctx->user_fctx = search; MemoryContextSwitchTo(oldcontext); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); call_cntr = funcctx->call_cntr; search = (KC_ENTRY *) funcctx->user_fctx; // If no current msg, try to get the next one. done = 1;#ifdef CF_DUBUG ereport(NOTICE, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("beginning run")));#endif if (search->msg) {#ifdef CF_DUBUG ereport(NOTICE, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Incrementing next from map %d -- %zu", search->next_map, search->msg->n_map_entry)));#endif // Case if we are using the external cursor running over kv map.//.........这里部分代码省略.........
开发者ID:cloudflare,项目名称:SortaSQL,代码行数:101,
示例3: kc_deleteDatum kc_delete(PG_FUNCTION_ARGS) { char *map_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *start_time = text_to_cstring(PG_GETARG_TEXT_PP(1)); ArrayType *rids = PG_GETARG_ARRAYTYPE_P(2); int i; Datum *rid_datums; bool *rid_nulls; int rid_count; char *next_rid; KCDB *main_db; char *vbuf; size_t vsiz; int64_t num_keys_to_run; int64_t num_keys_deleted; char **keys_to_use; Cloudflare__ZoneTimeBucket *msg_new; // Open our DB. main_db = kcdbnew(); if (!open_db (main_db, map_name, start_time)) { PG_RETURN_INT64(0); } kcdbbegintran (main_db, 0); // Now run over the array. deconstruct_array(rids, TEXTOID, -1, false, 'i', &rid_datums, &rid_nulls, &rid_count); if (ARR_HASNULL(rids)) { ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("cannot work with arrays containing NULLs"))); } keys_to_use = (char **)palloc(KC_MAX_ENTRIES_PER_RID * sizeof(char)); num_keys_deleted = 0; char prefixes_to_use[rid_count][KC_MAX_RID]; for (i = 0; i < rid_count; i++) { next_rid = TextDatumGetCString(rid_datums[i]); snprintf(prefixes_to_use[i], KC_MAX_RID, "%s%s", next_rid, CF_LABEL_SEP); num_keys_to_run = kcdbmatchprefix (main_db, prefixes_to_use[i], keys_to_use, KC_MAX_ENTRIES_PER_RID); if (num_keys_to_run != -1) { num_keys_deleted += num_keys_to_run; int next_key; for (next_key=0; next_key < num_keys_to_run; next_key++) { vbuf = kcdbget(main_db, keys_to_use[next_key], strlen(keys_to_use[next_key]), &vsiz); if (vbuf) { msg_new = cloudflare__zone_time_bucket__unpack(NULL, vsiz, (const uint8_t *)vbuf); if (msg_new == NULL) { // Something failed ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("error unpacking incoming message"))); } else { if (msg_new->kv_map_file) { unlink(msg_new->kv_map_file); } kcdbremove (main_db, keys_to_use[next_key], strlen(keys_to_use[next_key])); } cloudflare__zone_time_bucket__free_unpacked(msg_new, NULL); kcfree(vbuf); kcfree(keys_to_use[next_key]); } else { ereport(NOTICE, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("get error on %s -- %s", keys_to_use[next_key], kcecodename(kcdbecode(main_db))))); } } } else { ereport(NOTICE, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("prefix error on %s -- %s", prefixes_to_use[i], kcecodename(kcdbecode(main_db))))); } } pfree(keys_to_use); // Done! kcdbendtran (main_db, 1); if (!kcdbclose(main_db)) { ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Error Closeing db: /"%s/"", kcecodename(kcdbecode(main_db))))); } PG_RETURN_INT64(num_keys_deleted);}
开发者ID:cloudflare,项目名称:SortaSQL,代码行数:87,
示例4: pg_prewarm/* * pg_prewarm(regclass, mode text, fork text, * first_block int8, last_block int8) * * The first argument is the relation to be prewarmed; the second controls * how prewarming is done; legal options are 'prefetch', 'read', and 'buffer'. * The third is the name of the relation fork to be prewarmed. The fourth * and fifth arguments specify the first and last block to be prewarmed. * If the fourth argument is NULL, it will be taken as 0; if the fifth argument * is NULL, it will be taken as the number of blocks in the relation. The * return value is the number of blocks successfully prewarmed. */Datumpg_prewarm(PG_FUNCTION_ARGS){ Oid relOid; text *forkName; text *type; int64 first_block; int64 last_block; int64 nblocks; int64 blocks_done = 0; int64 block; Relation rel; ForkNumber forkNumber; char *forkString; char *ttype; PrewarmType ptype; AclResult aclresult; /* Basic sanity checking. */ if (PG_ARGISNULL(0)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("relation cannot be null"))); relOid = PG_GETARG_OID(0); if (PG_ARGISNULL(1)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errmsg("prewarm type cannot be null")))); type = PG_GETARG_TEXT_P(1); ttype = text_to_cstring(type); if (strcmp(ttype, "prefetch") == 0) ptype = PREWARM_PREFETCH; else if (strcmp(ttype, "read") == 0) ptype = PREWARM_READ; else if (strcmp(ttype, "buffer") == 0) ptype = PREWARM_BUFFER; else { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid prewarm type"), errhint("Valid prewarm types are /"prefetch/", /"read/", and /"buffer/"."))); PG_RETURN_INT64(0); /* Placate compiler. */ } if (PG_ARGISNULL(2)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errmsg("relation fork cannot be null")))); forkName = PG_GETARG_TEXT_P(2); forkString = text_to_cstring(forkName); forkNumber = forkname_to_number(forkString); /* Open relation and check privileges. */ rel = relation_open(relOid, AccessShareLock); aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_CLASS, get_rel_name(relOid)); /* Check that the fork exists. */ RelationOpenSmgr(rel); if (!smgrexists(rel->rd_smgr, forkNumber)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("fork /"%s/" does not exist for this relation", forkString))); /* Validate block numbers, or handle nulls. */ nblocks = RelationGetNumberOfBlocksInFork(rel, forkNumber); if (PG_ARGISNULL(3)) first_block = 0; else { first_block = PG_GETARG_INT64(3); if (first_block < 0 || first_block >= nblocks) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("starting block number must be between 0 and " INT64_FORMAT, nblocks - 1))); } if (PG_ARGISNULL(4)) last_block = nblocks - 1; else { last_block = PG_GETARG_INT64(4); if (last_block < 0 || last_block >= nblocks) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ending block number must be between 0 and " INT64_FORMAT,//.........这里部分代码省略.........
开发者ID:BioBD,项目名称:Hypothetical_Indexes,代码行数:101,
示例5: withPoints_kspPGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; /*******************************************************************************/ /* MODIFY AS NEEDED */ /* */ General_path_element_t *result_tuples = 0; size_t result_count = 0; /* */ /*******************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /*******************************************************************************/ /* MODIFY AS NEEDED */ // CREATE OR REPLACE FUNCTION pgr_withPoint( // edges_sql TEXT, // points_sql TEXT, // start_pid INTEGER, // end_pid BIGINT, // k BIGINT, // // directed BOOLEAN -- DEFAULT true, // heap_paths BOOLEAN -- DEFAULT false, // driving_side CHAR -- DEFAULT 'b', // details BOOLEAN -- DEFAULT false, PGR_DBG("Calling process"); PGR_DBG("initial driving side:%s", text_to_cstring(PG_GETARG_TEXT_P(7))); process( text_to_cstring(PG_GETARG_TEXT_P(0)), text_to_cstring(PG_GETARG_TEXT_P(1)), PG_GETARG_INT64(2), PG_GETARG_INT64(3), PG_GETARG_INT32(4), PG_GETARG_BOOL(5), PG_GETARG_BOOL(6), text_to_cstring(PG_GETARG_TEXT_P(7)), PG_GETARG_BOOL(8), &result_tuples, &result_count); /* */ /*******************************************************************************/#if PGSQL_VERSION > 95 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (General_path_element_t*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool* nulls; /*******************************************************************************/ /* MODIFY AS NEEDED */ values = palloc(7 * sizeof(Datum)); nulls = palloc(7 * sizeof(bool)); size_t i; for(i = 0; i < 7; ++i) { nulls[i] = false; } /* OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, OUT agg_cost FLOAT) */ // postgres starts counting from 1 values[0] = Int32GetDatum(funcctx->call_cntr + 1); values[1] = Int32GetDatum((int)(result_tuples[funcctx->call_cntr].start_id + 1)); values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq); values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);//.........这里部分代码省略.........
开发者ID:titanofold,项目名称:pgrouting,代码行数:101,
示例6: variant_get_variant_name/* * variant_get_variant_name: Return the name of a named variant */char *variant_get_variant_name(int typmod, Oid org_typid, bool ignore_storage){ Datum values[2]; MemoryContext cctx = CurrentMemoryContext; bool do_pop = _SPI_conn(); bool isnull; Oid types[2] = {INT4OID, REGTYPEOID}; char *cmd; int nargs; int ret; Datum result; char *out; values[0] = Int32GetDatum(typmod); /* * There's a race condition here; someone could be attempting to remove an * allowed type from this registered variant or even remove it entirely. We * could avoid that by taking a share/keyshare lock here and taking the * appropriate blocking lock when modifying the registration record. Doing * that would probably be quite bad though; not only are type IO and typmod * IO routines assumed to be non-volatile, taking such a lock would end up * generating a lot of lock updates to the registration rows. * * Since the whole purpose of registration is to handle the issue of someone * attempting to drop a type that has made it into a variant in a table * column, which we can't completely handle anyway, I don't think it's worth * it to lock the rows. */ if(ignore_storage) { cmd = "SELECT variant_name, variant_enabled, storage_allowed FROM variant._registered WHERE variant_typmod = $1"; nargs = 1; } else { cmd = "SELECT variant_name, variant_enabled, storage_allowed, allowed_types @> array[ $2 ] FROM variant._registered WHERE variant_typmod = $1"; nargs = 2; values[1] = ObjectIdGetDatum(org_typid); } /* command, nargs, Oid *argument_types, *values, *nulls, read_only, count */ if( (ret = SPI_execute_with_args( cmd, nargs, types, values, " ", true, 0 )) != SPI_OK_SELECT ) elog( ERROR, "SPI_execute_with_args(%s) returned %s", cmd, SPI_result_code_string(ret)); Assert( SPI_tuptable ); if ( SPI_processed > 1 ) ereport(ERROR, ( errmsg( "Got %u records for variant typmod %i", SPI_processed, typmod ), errhint( "This means _variant._registered is corrupted" ) ) ); if ( SPI_processed < 1 ) elog( ERROR, "invalid typmod %i", typmod ); /* Note 0 vs 1 based numbering */ Assert(SPI_tuptable->tupdesc->attrs[0]->atttypid == VARCHAROID); Assert(SPI_tuptable->tupdesc->attrs[1]->atttypid == BOOLOID); Assert(SPI_tuptable->tupdesc->attrs[2]->atttypid == BOOLOID); result = heap_getattr( SPI_tuptable->vals[0], 1, SPI_tuptable->tupdesc, &isnull ); if( isnull ) ereport( ERROR, ( errmsg( "Found NULL variant_name for typmod %i", typmod ), errhint( "This should never happen; is _variant._registered corrupted?" ) ) ); MemoryContextSwitchTo(cctx); out = text_to_cstring(DatumGetTextP(result)); result = heap_getattr( SPI_tuptable->vals[0], 2, SPI_tuptable->tupdesc, &isnull ); if( !DatumGetBool(result) ) ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg( "variant.variant(%s) is disabled", out ) ) ); /* * If storage is allowed, then throw an error if we don't know what our * original type is, or if that type is not listed as allowed. */ if(!ignore_storage) { result = heap_getattr( SPI_tuptable->vals[0], 3, SPI_tuptable->tupdesc, &isnull ); if( DatumGetBool(result) ) { if( org_typid == InvalidOid) ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg( "Unable to determine original type" ) ) ); result = heap_getattr( SPI_tuptable->vals[0], 4, SPI_tuptable->tupdesc, &isnull ); if( !DatumGetBool(result) )//.........这里部分代码省略.........
开发者ID:decibel,项目名称:variant,代码行数:101,
示例7: gp_truncate_error_log/* * Delete error log of the specified relation. This returns true from master * iif all segments and master find the relation. */Datumgp_truncate_error_log(PG_FUNCTION_ARGS){ text *relname; char *relname_str; RangeVar *relrv; Oid relid; bool allResults = true; relname = PG_GETARG_TEXT_P(0); relname_str = text_to_cstring(relname); if (strcmp(relname_str, "*.*") == 0) { /* * Only superuser is allowed to delete log files across database. */ if (!superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("must be superuser to delete all error log files")))); ErrorLogDelete(InvalidOid, InvalidOid); } else if (strcmp(relname_str, "*") == 0) { /* * Database owner can delete error log files. */ if (!pg_database_ownercheck(MyDatabaseId, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE, get_database_name(MyDatabaseId)); ErrorLogDelete(MyDatabaseId, InvalidOid); } else { AclResult aclresult; relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relid = RangeVarGetRelid(relrv, true); /* Return false if the relation does not exist. */ if (!OidIsValid(relid)) PG_RETURN_BOOL(false); /* * Allow only the table owner to truncate error log. */ aclresult = pg_class_aclcheck(relid, GetUserId(), ACL_TRUNCATE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_CLASS, relrv->relname); /* We don't care if this fails or not. */ ErrorLogDelete(MyDatabaseId, relid); } /* * Dispatch the work to segments. */ if (Gp_role == GP_ROLE_DISPATCH) { int i = 0; StringInfoData sql; CdbPgResults cdb_pgresults = {NULL, 0}; initStringInfo(&sql); appendStringInfo(&sql, "SELECT pg_catalog.gp_truncate_error_log(%s)", quote_literal_internal(text_to_cstring(relname))); CdbDispatchCommand(sql.data, DF_WITH_SNAPSHOT, &cdb_pgresults); for (i = 0; i < cdb_pgresults.numResults; i++) { Datum value; bool isnull; struct pg_result *pgresult = cdb_pgresults.pg_results[i]; if (PQresultStatus(pgresult) != PGRES_TUPLES_OK) { cdbdisp_clearCdbPgResults(&cdb_pgresults); ereport(ERROR, (errmsg("unexpected result from segment: %d", PQresultStatus(pgresult)))); } value = ResultToDatum(pgresult, 0, 0, boolin, &isnull); allResults &= (!isnull && DatumGetBool(value)); } cdbdisp_clearCdbPgResults(&cdb_pgresults); pfree(sql.data); }//.........这里部分代码省略.........
开发者ID:LJoNe,项目名称:gpdb,代码行数:101,
示例8: convert_to_UTF8Datumconvert_to_UTF8(PG_FUNCTION_ARGS){ // things we need to deal with constructing our composite type TupleDesc tupdesc; Datum values[3]; bool nulls[3]; HeapTuple tuple; // for char_set_detect function returns text *encoding = NULL; text *lang = NULL; int32_t confidence = 0; UErrorCode status = U_ZERO_ERROR; // output buffer for conversion to Unicode UChar* uBuf = NULL; int32_t uBuf_len = 0; // output of this function text *text_out; bool converted = false; bool dropped_bytes = false; bool dropped_bytes_toU = false; bool dropped_bytes_fromU = false; // temporary buffer for converted string char* converted_buf = NULL; // input args const text *buffer = PG_GETARG_TEXT_P(0); const bool force = PG_GETARG_BOOL(1); // C string of text* buffer const char* cbuffer = NULL; int cbuffer_len = 0; // Convert output values into a PostgreSQL composite type. if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record./n"))); // BlessTupleDesc for Datums BlessTupleDesc(tupdesc); // return if string to convert is NULL if (NULL == buffer) { // return input string, // converted to true, // dropped_bytes to false text_out = (text *) buffer; converted = true; dropped_bytes = false; } else { // extract string from text* to C string cbuffer = text_to_cstring(buffer); cbuffer_len = strlen(cbuffer); // bail on zero-length strings // return if cbuffer has zero length or contains a blank space if ((0 == cbuffer_len) || (0 == strcmp("", cbuffer))) { text_out = (text *) buffer; converted = true; dropped_bytes = false; } else { // UTF8 output can be up to 6 bytes per input byte // palloc0 allocates and zeros bytes in array int32_t converted_buf_len = cbuffer_len * 6 * sizeof(char); converted_buf = (char *) palloc0(converted_buf_len); // int32_t converted_len = 0; // detect encoding with ICU status = detect_ICU(buffer, &encoding, &lang, &confidence); ereport(DEBUG1, (errcode(ERRCODE_SUCCESSFUL_COMPLETION), errmsg("ICU detection status: %d/n", status))); ereport(DEBUG1, (errcode(ERRCODE_SUCCESSFUL_COMPLETION), errmsg("Detected encoding: %s, language: %s, confidence: %d/n", text_to_cstring(encoding), text_to_cstring(lang), confidence))); // return without attempting a conversion if UTF8 is detected if ( (0 == strcmp("UTF-8", text_to_cstring(encoding))) || (0 == strcmp("utf-8", text_to_cstring(encoding))) || (0 == strcmp("UTF8", text_to_cstring(encoding))) || (0 == strcmp("utf8", text_to_cstring(encoding)))//.........这里部分代码省略.........
开发者ID:aweber,项目名称:pg_chardetect,代码行数:101,
示例9: detect_ICUUErrorCodedetect_ICU(const text* buffer, text** encoding, text** lang, int32_t* confidence){ const char* cbuffer = text_to_cstring(buffer); //int cbuffer_len = strlen(cbuffer); UCharsetDetector* csd; const UCharsetMatch* csm; UErrorCode status = U_ZERO_ERROR; csd = ucsdet_open(&status); // set text buffer // use -1 for string length since NUL terminated ucsdet_setText(csd, cbuffer, STRING_IS_NULL_TERMINATED, &status); //ucsdet_setText(csd, cbuffer, cbuffer_len, &status); // detect charset csm = ucsdet_detect(csd, &status); // charset match is NULL if no match if (NULL == csm) { ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("ICU error: No charset match for /"%s/" - assuming ISO-8859-1.", cbuffer))); *encoding = cstring_to_text("ISO-8859-1"); *lang = NULL; *confidence = 0; ucsdet_close(csd); pfree((void *) cbuffer); return status; } else if (U_FAILURE(status)) { ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("ICU error: %s/n", u_errorName(status)))); *encoding = NULL; *lang = NULL; *confidence = 0; ucsdet_close(csd); pfree((void *) cbuffer); return status; } *encoding = cstring_to_text(ucsdet_getName(csm, &status)); *lang = cstring_to_text(ucsdet_getLanguage(csm, &status)); *confidence = ucsdet_getConfidence(csm, &status); // close charset detector // UCharsetMatch is owned by UCharsetDetector so its memory will be // freed when the char set detector is closed ucsdet_close(csd); pfree((void *) cbuffer); return status;}
开发者ID:aweber,项目名称:pg_chardetect,代码行数:61,
示例10: worker_merge_files_and_run_query/* * worker_merge_files_and_run_query creates a merge task table within the job's * schema, which should have already been created by the task tracker protocol. * It copies files in its task directory into this table. Then it runs final * query to create result table of the job. * * Note that here we followed a different approach to create a task table for merge * files than worker_merge_files_into_table(). In future we should unify these * two approaches. For this purpose creating a directory_fdw extension and using * it would make sense. Then we can merge files with a query or without query * through directory_fdw. */Datumworker_merge_files_and_run_query(PG_FUNCTION_ARGS){ uint64 jobId = PG_GETARG_INT64(0); uint32 taskId = PG_GETARG_UINT32(1); text *createMergeTableQueryText = PG_GETARG_TEXT_P(2); text *createIntermediateTableQueryText = PG_GETARG_TEXT_P(3); const char *createMergeTableQuery = text_to_cstring(createMergeTableQueryText); const char *createIntermediateTableQuery = text_to_cstring(createIntermediateTableQueryText); StringInfo taskDirectoryName = TaskDirectoryName(jobId, taskId); StringInfo jobSchemaName = JobSchemaName(jobId); StringInfo intermediateTableName = TaskTableName(taskId); StringInfo mergeTableName = makeStringInfo(); StringInfo setSearchPathString = makeStringInfo(); bool schemaExists = false; int connected = 0; int setSearchPathResult = 0; int createMergeTableResult = 0; int createIntermediateTableResult = 0; int finished = 0; /* * If the schema for the job isn't already created by the task tracker * protocol, we fall to using the default 'public' schema. */ schemaExists = JobSchemaExists(jobSchemaName); if (!schemaExists) { resetStringInfo(jobSchemaName); appendStringInfoString(jobSchemaName, "public"); } appendStringInfo(setSearchPathString, SET_SEARCH_PATH_COMMAND, jobSchemaName->data); /* Add "public" to search path to access UDFs in public schema */ appendStringInfo(setSearchPathString, ",public"); connected = SPI_connect(); if (connected != SPI_OK_CONNECT) { ereport(ERROR, (errmsg("could not connect to SPI manager"))); } setSearchPathResult = SPI_exec(setSearchPathString->data, 0); if (setSearchPathResult < 0) { ereport(ERROR, (errmsg("execution was not successful /"%s/"", setSearchPathString->data))); } createMergeTableResult = SPI_exec(createMergeTableQuery, 0); if (createMergeTableResult < 0) { ereport(ERROR, (errmsg("execution was not successful /"%s/"", createMergeTableQuery))); } appendStringInfo(mergeTableName, "%s%s", intermediateTableName->data, MERGE_TABLE_SUFFIX); CopyTaskFilesFromDirectory(jobSchemaName, mergeTableName, taskDirectoryName); createIntermediateTableResult = SPI_exec(createIntermediateTableQuery, 0); if (createIntermediateTableResult < 0) { ereport(ERROR, (errmsg("execution was not successful /"%s/"", createIntermediateTableQuery))); } finished = SPI_finish(); if (finished != SPI_OK_FINISH) { ereport(ERROR, (errmsg("could not disconnect from SPI manager"))); } PG_RETURN_VOID();}
开发者ID:AlexaPopov,项目名称:citus,代码行数:91,
示例11: convert_to_unicodeUErrorCodeconvert_to_unicode(const text* buffer, const text* encoding, UChar** uBuf, int32_t *uBuf_len, bool force, bool* dropped_bytes){ UErrorCode status = U_ZERO_ERROR; UConverter *conv; int32_t uConvertedLen = 0; // used to set dropped_bytes flag if force is true ToUFLAGContext * context = NULL; size_t uBufSize = 0; const char* encoding_cstr = text_to_cstring(encoding); // open converter for detected encoding conv = ucnv_open(encoding_cstr, &status); if (U_FAILURE(status)) { ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Cannot open %s converter - error: %s./n", (const char *) encoding_cstr, u_errorName(status)))); if (NULL != encoding_cstr) pfree((void *) encoding_cstr); ucnv_close(conv); return status; } if (force) { // set callback to skip illegal, irregular or unassigned bytes // set converter to use SKIP callback // contecxt will save and call it after calling custom callback ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_SKIP, NULL, NULL, NULL, &status); //TODO: refactor warning and error message reporting if (U_FAILURE(status)) { ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Cannot set callback on converter - error: %s./n", u_errorName(status)))); if (NULL != encoding_cstr) pfree((void *) encoding_cstr); ucnv_close(conv); return status; } // initialize flagging callback context = flagCB_toU_openContext(); /* Set our special callback */ ucnv_setToUCallBack(conv, flagCB_toU, context, &(context->subCallback), &(context->subContext), &status ); if (U_FAILURE(status)) { ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Cannot set callback on converter - error: %s./n", u_errorName(status)))); if (NULL != encoding_cstr) pfree((void *) encoding_cstr); ucnv_close(conv); return status; } } // allocate unicode buffer // must pfree before exiting calling function uBufSize = (VARSIZE_ANY_EXHDR(buffer)/ucnv_getMinCharSize(conv) + 1); *uBuf = (UChar*) palloc0(uBufSize * sizeof(UChar)); if (*uBuf == NULL) { status = U_MEMORY_ALLOCATION_ERROR; ereport(WARNING, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Cannot allocate %d bytes for Unicode pivot buffer - error: %s./n", (int) uBufSize, u_errorName(status)))); if (NULL != encoding_cstr) pfree((void *) encoding_cstr); ucnv_close(conv); return status; } ereport(DEBUG1,//.........这里部分代码省略.........
开发者ID:aweber,项目名称:pg_chardetect,代码行数:101,
示例12: MY_FUNCTION_NAMEPGDLLEXPORT Datum MY_FUNCTION_NAME(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; /**************************************************************************/ /* MODIFY AS NEEDED */ /* */ MY_RETURN_VALUE_TYPE *result_tuples = NULL; size_t result_count = 0; /* */ /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /**********************************************************************/ /* MODIFY AS NEEDED */ /* MY_QUERY_LINE1 **********************************************************************/ PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_INT64(1), PG_GETARG_INT64(2),#if 0 /* * handling arrays example */ PG_GETARG_ARRAYTYPE_P(1), PG_GETARG_ARRAYTYPE_P(2),#endif PG_GETARG_BOOL(3), PG_GETARG_BOOL(4), &result_tuples, &result_count); /* */ /**********************************************************************/#if PGSQL_VERSION > 94 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); } funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (MY_RETURN_VALUE_TYPE*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool* nulls; /**********************************************************************/ /* MODIFY AS NEEDED */ /* MY_QUERY_LINE2 ***********************************************************************/ values = palloc(6 * sizeof(Datum)); nulls = palloc(6 * sizeof(bool)); size_t i; for (i = 0; i < 6; ++i) { nulls[i] = false; } // postgres starts counting from 1 values[0] = Int32GetDatum(funcctx->call_cntr + 1); values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls);//.........这里部分代码省略.........
开发者ID:chahidinho,项目名称:pgrouting,代码行数:101,
示例13: max_flow_many_to_manyPGDLLEXPORT Datummax_flow_many_to_many(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; /**************************************************************************/ pgr_flow_t *result_tuples = 0; size_t result_count = 0; /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /**********************************************************************/ process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), PG_GETARG_ARRAYTYPE_P(2), text_to_cstring(PG_GETARG_TEXT_P(3)), PG_GETARG_BOOL(4), &result_tuples, &result_count); /* */ /**********************************************************************/#if PGSQL_VERSION > 95 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); } funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (pgr_flow_t *) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool *nulls; size_t call_cntr = funcctx->call_cntr; /**********************************************************************/ /* MODIFY AS NEEDED */ values = palloc(6 * sizeof(Datum)); nulls = palloc(6 * sizeof(bool)); size_t i; for (i = 0; i < 6; ++i) { nulls[i] = false; } values[0] = Int32GetDatum(call_cntr + 1); values[1] = Int64GetDatum(result_tuples[call_cntr].edge); values[2] = Int64GetDatum(result_tuples[call_cntr].source); values[3] = Int64GetDatum(result_tuples[call_cntr].target); values[4] = Int64GetDatum(result_tuples[call_cntr].flow); values[5] = Int64GetDatum(result_tuples[call_cntr].residual_capacity); /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { SRF_RETURN_DONE(funcctx); }}
开发者ID:titanofold,项目名称:pgrouting,代码行数:84,
示例14: pg_resgroup_get_status_kv/* * Get status of resource groups in key-value style */Datumpg_resgroup_get_status_kv(PG_FUNCTION_ARGS){ FuncCallContext *funcctx; StringInfoData str; bool do_dump; do_dump = (strncmp(text_to_cstring(PG_GETARG_TEXT_P(0)), "dump", 4) == 0); if (do_dump) { /* Only super user can call this function with para=dump. */ if (!superuser()) { ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg("only superusers can call this function"))); } initStringInfo(&str); /* dump info in QD and collect info from QEs to form str.*/ dumpResGroupInfo(&str); } if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; TupleDesc tupdesc; int nattr = 3; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); tupdesc = CreateTemplateTupleDesc(nattr, false); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "groupid", OIDOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "prop", TEXTOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "value", TEXTOID, -1, 0); funcctx->tuple_desc = BlessTupleDesc(tupdesc); funcctx->max_calls = do_dump ? 1 : 0; MemoryContextSwitchTo(oldcontext); } /* stuff done on every call of the function */ funcctx = SRF_PERCALL_SETUP(); if (funcctx->call_cntr < funcctx->max_calls) { if (do_dump) { Datum values[3]; bool nulls[3]; HeapTuple tuple; MemSet(values, 0, sizeof(values)); MemSet(nulls, 0, sizeof(nulls)); nulls[0] = nulls[1] = true; values[2] = CStringGetTextDatum(str.data); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); } else { SRF_RETURN_DONE(funcctx); } } else { /* nothing left */ SRF_RETURN_DONE(funcctx); }}
开发者ID:adam8157,项目名称:gpdb,代码行数:80,
示例15: xpath_tableDatumxpath_table(PG_FUNCTION_ARGS){ /* Function parameters */ char *pkeyfield = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *xmlfield = text_to_cstring(PG_GETARG_TEXT_PP(1)); char *relname = text_to_cstring(PG_GETARG_TEXT_PP(2)); char *xpathset = text_to_cstring(PG_GETARG_TEXT_PP(3)); char *condition = text_to_cstring(PG_GETARG_TEXT_PP(4)); /* SPI (input tuple) support */ SPITupleTable *tuptable; HeapTuple spi_tuple; TupleDesc spi_tupdesc; /* Output tuple (tuplestore) support */ Tuplestorestate *tupstore = NULL; TupleDesc ret_tupdesc; HeapTuple ret_tuple; ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; AttInMetadata *attinmeta; MemoryContext per_query_ctx; MemoryContext oldcontext; char **values; xmlChar **xpaths; char *pos; const char *pathsep = "|"; int numpaths; int ret; int proc; int i; int j; int rownr; /* For issuing multiple rows from one original * document */ bool had_values; /* To determine end of nodeset results */ StringInfoData query_buf; PgXmlErrorContext *xmlerrcxt; volatile xmlDocPtr doctree = NULL; /* We only have a valid tuple description in table function mode */ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that cannot accept a set"))); if (rsinfo->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("xpath_table must be called as a table function"))); /* * We want to materialise because it means that we don't have to carry * libxml2 parser state between invocations of this function */ if (!(rsinfo->allowedModes & SFRM_Materialize)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("xpath_table requires Materialize mode, but it is not " "allowed in this context"))); /* * The tuplestore must exist in a higher context than this function call * (per_query_ctx is used) */ per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; oldcontext = MemoryContextSwitchTo(per_query_ctx); /* * Create the tuplestore - work_mem is the max in-memory size before a * file is created on disk to hold it. */ tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random, false, work_mem); MemoryContextSwitchTo(oldcontext); /* get the requested return tuple description */ ret_tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc); /* must have at least one output column (for the pkey) */ if (ret_tupdesc->natts < 1) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("xpath_table must have at least one output column"))); /* * At the moment we assume that the returned attributes make sense for the * XPath specififed (i.e. we trust the caller). It's not fatal if they get * it wrong - the input function for the column type will raise an error * if the path result can't be converted into the correct binary * representation. */ attinmeta = TupleDescGetAttInMetadata(ret_tupdesc); /* Set return mode and allocate value space. */ rsinfo->returnMode = SFRM_Materialize;//.........这里部分代码省略.........
开发者ID:42penguins,项目名称:postgres,代码行数:101,
示例16: crosstabDatumcrosstab(PG_FUNCTION_ARGS){ char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; Tuplestorestate *tupstore; TupleDesc tupdesc; uint64 call_cntr; uint64 max_calls; AttInMetadata *attinmeta; SPITupleTable *spi_tuptable; TupleDesc spi_tupdesc; bool firstpass; char *lastrowid; int i; int num_categories; MemoryContext per_query_ctx; MemoryContext oldcontext; int ret; uint64 proc; /* check to see if caller supports us returning a tuplestore */ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that cannot accept a set"))); if (!(rsinfo->allowedModes & SFRM_Materialize)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("materialize mode required, but it is not " / "allowed in this context"))); per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; /* Connect to SPI manager */ if ((ret = SPI_connect()) < 0) /* internal error */ elog(ERROR, "crosstab: SPI_connect returned %d", ret); /* Retrieve the desired rows */ ret = SPI_execute(sql, true, 0); proc = SPI_processed; /* If no qualifying tuples, fall out early */ if (ret != SPI_OK_SELECT || proc == 0) { SPI_finish(); rsinfo->isDone = ExprEndResult; PG_RETURN_NULL(); } spi_tuptable = SPI_tuptable; spi_tupdesc = spi_tuptable->tupdesc; /*---------- * The provided SQL query must always return three columns. * * 1. rowname * the label or identifier for each row in the final result * 2. category * the label or identifier for each column in the final result * 3. values * the value for each column in the final result *---------- */ if (spi_tupdesc->natts != 3) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid source data SQL statement"), errdetail("The provided SQL must return 3 " "columns: rowid, category, and values."))); /* get a tuple descriptor for our result type */ switch (get_call_result_type(fcinfo, NULL, &tupdesc)) { case TYPEFUNC_COMPOSITE: /* success */ break; case TYPEFUNC_RECORD: /* failed to determine actual type of RECORD */ ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); break; default: /* result type isn't composite */ ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("return type must be a row type"))); break; } /* * Check that return tupdesc is compatible with the data we got from SPI, * at least based on number and type of attributes */ if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),//.........这里部分代码省略.........
开发者ID:eubide,项目名称:postgres,代码行数:101,
示例17: bdDijkstraPGDLLEXPORT Datum bdDijkstra(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; /**************************************************************************/ /* MODIFY AS NEEDED */ /* */ General_path_element_t *result_tuples = NULL; size_t result_count = 0; /* */ /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /**********************************************************************/ /* MODIFY AS NEEDED */ /* edges_sql TEXT, start_vid BIGINT, end_vid BIGINT, directed BOOLEAN DEFAULT true, only_cost BOOLEAN DEFAULT false, **********************************************************************/ process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), PG_GETARG_ARRAYTYPE_P(2), PG_GETARG_BOOL(3), PG_GETARG_BOOL(4), &result_tuples, &result_count); /* */ /**********************************************************************/#if PGSQL_VERSION > 95 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); } funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (General_path_element_t*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool* nulls; size_t call_cntr = funcctx->call_cntr; /**********************************************************************/ /* MODIFY AS NEEDED */ /* OUT seq INTEGER, OUT path_seq INTEGER, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, OUT agg_cost FLOAT ***********************************************************************/ size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); size_t i; for (i = 0; i < numb; ++i) { nulls[i] = false; } values[0] = Int32GetDatum(call_cntr + 1); values[1] = Int32GetDatum(result_tuples[call_cntr].seq); values[2] = Int64GetDatum(result_tuples[call_cntr].start_id); values[3] = Int64GetDatum(result_tuples[call_cntr].end_id); values[4] = Int64GetDatum(result_tuples[call_cntr].node); values[5] = Int64GetDatum(result_tuples[call_cntr].edge); values[6] = Float8GetDatum(result_tuples[call_cntr].cost);//.........这里部分代码省略.........
开发者ID:chahidinho,项目名称:pgrouting,代码行数:101,
示例18: crosstab_hashDatumcrosstab_hash(PG_FUNCTION_ARGS){ char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; TupleDesc tupdesc; MemoryContext per_query_ctx; MemoryContext oldcontext; HTAB *crosstab_hash; /* check to see if caller supports us returning a tuplestore */ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that cannot accept a set"))); if (!(rsinfo->allowedModes & SFRM_Materialize) || rsinfo->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("materialize mode required, but it is not " / "allowed in this context"))); per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; oldcontext = MemoryContextSwitchTo(per_query_ctx); /* get the requested return tuple description */ tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc); /* * Check to make sure we have a reasonable tuple descriptor * * Note we will attempt to coerce the values into whatever the return * attribute type is and depend on the "in" function to complain if * needed. */ if (tupdesc->natts < 2) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("query-specified return tuple and " / "crosstab function are not compatible"))); /* load up the categories hash table */ crosstab_hash = load_categories_hash(cats_sql, per_query_ctx); /* let the caller know we're sending back a tuplestore */ rsinfo->returnMode = SFRM_Materialize; /* now go build it */ rsinfo->setResult = get_crosstab_tuplestore(sql, crosstab_hash, tupdesc, per_query_ctx, rsinfo->allowedModes & SFRM_Materialize_Random); /* * SFRM_Materialize mode expects us to return a NULL Datum. The actual * tuples are in our tuplestore and passed back through rsinfo->setResult. * rsinfo->setDesc is set to the tuple description that we actually used * to build our tuples with, so the caller can verify we did what it was * expecting. */ rsinfo->setDesc = tupdesc; MemoryContextSwitchTo(oldcontext); return (Datum) 0;}
开发者ID:eubide,项目名称:postgres,代码行数:67,
示例19: gp_read_error_log/* * gp_read_error_log * * Returns set of error log tuples. */Datumgp_read_error_log(PG_FUNCTION_ARGS){ FuncCallContext *funcctx; ReadErrorLogContext *context; HeapTuple tuple; Datum result; /* * First call setup */ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; FILE *fp; text *relname; funcctx = SRF_FIRSTCALL_INIT(); relname = PG_GETARG_TEXT_P(0); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); context = palloc0(sizeof(ReadErrorLogContext)); funcctx->user_fctx = (void *) context; funcctx->tuple_desc = BlessTupleDesc(GetErrorTupleDesc()); /* * Though this function is usually executed on segment, we dispatch * the execution if it happens to be on QD, and combine the results * into one set. */ if (Gp_role == GP_ROLE_DISPATCH) { struct CdbPgResults cdb_pgresults = {NULL, 0}; StringInfoData sql; int i; initStringInfo(&sql); /* * construct SQL */ appendStringInfo(&sql, "SELECT * FROM pg_catalog.gp_read_error_log(%s) ", quote_literal_internal(text_to_cstring(relname))); CdbDispatchCommand(sql.data, DF_WITH_SNAPSHOT, &cdb_pgresults); for (i = 0; i < cdb_pgresults.numResults; i++) { if (PQresultStatus(cdb_pgresults.pg_results[i]) != PGRES_TUPLES_OK) { cdbdisp_clearCdbPgResults(&cdb_pgresults); elog(ERROR, "unexpected result from segment: %d", PQresultStatus(cdb_pgresults.pg_results[i])); } context->numTuples += PQntuples(cdb_pgresults.pg_results[i]); } pfree(sql.data); context->segResults = cdb_pgresults.pg_results; context->numSegResults = cdb_pgresults.numResults; } else { /* * In QE, read the error log. */ RangeVar *relrv; Oid relid; relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relid = RangeVarGetRelid(relrv, true); /* * If the relation has gone, silently return no tuples. */ if (OidIsValid(relid)) { AclResult aclresult; /* * Requires SELECT priv to read error log. */ aclresult = pg_class_aclcheck(relid, GetUserId(), ACL_SELECT); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_CLASS, relrv->relname); ErrorLogFileName(context->filename, MyDatabaseId, relid); fp = AllocateFile(context->filename, "r"); context->fp = fp; } }//.........这里部分代码省略.........
开发者ID:LJoNe,项目名称:gpdb,代码行数:101,
示例20: connectby_textDatumconnectby_text(PG_FUNCTION_ARGS){ char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1)); char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2)); char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3)); int max_depth = PG_GETARG_INT32(4); char *branch_delim = NULL; bool show_branch = false; bool show_serial = false; ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; TupleDesc tupdesc; AttInMetadata *attinmeta; MemoryContext per_query_ctx; MemoryContext oldcontext; /* check to see if caller supports us returning a tuplestore */ if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that cannot accept a set"))); if (!(rsinfo->allowedModes & SFRM_Materialize) || rsinfo->expectedDesc == NULL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("materialize mode required, but it is not " / "allowed in this context"))); if (fcinfo->nargs == 6) { branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5)); show_branch = true; } else /* default is no show, tilde for the delimiter */ branch_delim = pstrdup("~"); per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; oldcontext = MemoryContextSwitchTo(per_query_ctx); /* get the requested return tuple description */ tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc); /* does it meet our needs */ validateConnectbyTupleDesc(tupdesc, show_branch, show_serial); /* OK, use it then */ attinmeta = TupleDescGetAttInMetadata(tupdesc); /* OK, go to work */ rsinfo->returnMode = SFRM_Materialize; rsinfo->setResult = connectby(relname, key_fld, parent_key_fld, NULL, branch_delim, start_with, max_depth, show_branch, show_serial, per_query_ctx, rsinfo->allowedModes & SFRM_Materialize_Random, attinmeta); rsinfo->setDesc = tupdesc; MemoryContextSwitchTo(oldcontext); /* * SFRM_Materialize mode expects us to return a NULL Datum. The actual * tuples are in our tuplestore and passed back through rsinfo->setResult. * rsinfo->setDesc is set to the tuple description that we actually used * to build our tuples with, so the caller can verify we did what it was * expecting. */ return (Datum) 0;}
开发者ID:eubide,项目名称:postgres,代码行数:77,
示例21: tsquery_rewrite_queryDatumtsquery_rewrite_query(PG_FUNCTION_ARGS){ TSQuery query = PG_GETARG_TSQUERY_COPY(0); text *in = PG_GETARG_TEXT_P(1); TSQuery rewrited = query; MemoryContext outercontext = CurrentMemoryContext; MemoryContext oldcontext; QTNode *tree; char *buf; SPIPlanPtr plan; Portal portal; bool isnull; if (query->size == 0) { PG_FREE_IF_COPY(in, 1); PG_RETURN_POINTER(rewrited); } tree = QT2QTN(GETQUERY(query), GETOPERAND(query)); QTNTernary(tree); QTNSort(tree); buf = text_to_cstring(in); SPI_connect(); if ((plan = SPI_prepare(buf, 0, NULL)) == NULL) elog(ERROR, "SPI_prepare(/"%s/") failed", buf); if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL) elog(ERROR, "SPI_cursor_open(/"%s/") failed", buf); SPI_cursor_fetch(portal, true, 100); if (SPI_tuptable == NULL || SPI_tuptable->tupdesc->natts != 2 || SPI_gettypeid(SPI_tuptable->tupdesc, 1) != TSQUERYOID || SPI_gettypeid(SPI_tuptable->tupdesc, 2) != TSQUERYOID) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ts_rewrite query must return two tsquery columns"))); while (SPI_processed > 0 && tree) { uint64 i; for (i = 0; i < SPI_processed && tree; i++) { Datum qdata = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull); Datum sdata; if (isnull) continue; sdata = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &isnull); if (!isnull) { TSQuery qtex = DatumGetTSQuery(qdata); TSQuery qtsubs = DatumGetTSQuery(sdata); QTNode *qex, *qsubs = NULL; if (qtex->size == 0) { if (qtex != (TSQuery) DatumGetPointer(qdata)) pfree(qtex); if (qtsubs != (TSQuery) DatumGetPointer(sdata)) pfree(qtsubs); continue; } qex = QT2QTN(GETQUERY(qtex), GETOPERAND(qtex)); QTNTernary(qex); QTNSort(qex); if (qtsubs->size) qsubs = QT2QTN(GETQUERY(qtsubs), GETOPERAND(qtsubs)); oldcontext = MemoryContextSwitchTo(outercontext); tree = findsubquery(tree, qex, qsubs, NULL); MemoryContextSwitchTo(oldcontext); QTNFree(qex); if (qtex != (TSQuery) DatumGetPointer(qdata)) pfree(qtex); QTNFree(qsubs); if (qtsubs != (TSQuery) DatumGetPointer(sdata)) pfree(qtsubs); if (tree) { /* ready the tree for another pass */ QTNClearFlags(tree, QTN_NOCHANGE); QTNSort(tree); } }//.........这里部分代码省略.........
开发者ID:0x0FFF,项目名称:postgres,代码行数:101,
示例22: astarOneToOnePGDLLEXPORT DatumastarOneToOne(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; General_path_element_t *result_tuples = 0; size_t result_count = 0; if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /********************************************************************** edges_sql TEXT, start_vid BIGINT, end_vid BIGINT, directed BOOLEAN DEFAULT true, heuristic INTEGER DEFAULT 0, factor FLOAT DEFAULT 1.0, epsilon FLOAT DEFAULT 1.0, **********************************************************************/ PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_INT64(1), PG_GETARG_INT64(2), PG_GETARG_BOOL(3), PG_GETARG_INT32(4), PG_GETARG_FLOAT8(5), PG_GETARG_FLOAT8(6), PG_GETARG_BOOL(7), &result_tuples, &result_count);#if PGSQL_VERSION > 95 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record"))); funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (General_path_element_t*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool* nulls; /********************************************************************** OUT seq INTEGER, OUT path_seq INTEGER, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, OUT agg_cost FLOAT *********************************************************************/ values = palloc(6 * sizeof(Datum)); nulls = palloc(6 * sizeof(bool)); size_t i; for (i = 0; i < 6; ++i) { nulls[i] = false; } values[0] = Int32GetDatum(funcctx->call_cntr + 1); values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { SRF_RETURN_DONE(funcctx); }}
开发者ID:titanofold,项目名称:pgrouting,代码行数:99,
示例23: kshortest_pathPGDLLEXPORT Datumkshortest_path(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; General_path_element_t *path = NULL; size_t result_count = 0; if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); /* CREATE OR REPLACE FUNCTION _pgr_ksp( sql text, start_vid bigint, end_vid bigint, k integer, directed boolean, heap_paths boolean */ PGR_DBG("Calling process"); compute( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_INT64(1), PG_GETARG_INT64(2), PG_GETARG_INT32(3), PG_GETARG_BOOL(4), PG_GETARG_BOOL(5), &path, &result_count); PGR_DBG("Total number of tuples to be returned %ld /n", result_count);#if PGSQL_VERSION > 95 funcctx->max_calls = result_count;#else funcctx->max_calls = (uint32_t)result_count;#endif funcctx->user_fctx = path; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " "that cannot accept type record/n"))); funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; path = (General_path_element_t*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; Datum *values; bool* nulls; values = palloc(7 * sizeof(Datum)); nulls = palloc(7 * sizeof(bool)); size_t i; for (i = 0; i < 7; ++i) { nulls[i] = false; } values[0] = Int32GetDatum(funcctx->call_cntr + 1); values[1] = Int32GetDatum(path[funcctx->call_cntr].start_id + 1); values[2] = Int32GetDatum(path[funcctx->call_cntr].seq); values[3] = Int64GetDatum(path[funcctx->call_cntr].node); values[4] = Int64GetDatum(path[funcctx->call_cntr].edge); values[5] = Float8GetDatum(path[funcctx->call_cntr].cost); values[6] = Float8GetDatum(path[funcctx->call_cntr].agg_cost); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { /* do when there is no more left */ SRF_RETURN_DONE(funcctx); }}
开发者ID:cvvergara,项目名称:pgrouting,代码行数:89,
示例24: datum_to_json/* * Turn a scalar Datum into JSON, appending the string to "result". * * Hand off a non-scalar datum to composite_to_json or array_to_json_internal * as appropriate. */static voiddatum_to_json(Datum val, bool is_null, StringInfo result, TYPCATEGORY tcategory, Oid typoutputfunc){ char *outputstr; text *jsontext; if (is_null) { appendStringInfoString(result, "null"); return; } switch (tcategory) { case TYPCATEGORY_ARRAY: array_to_json_internal(val, result, false); break; case TYPCATEGORY_COMPOSITE: composite_to_json(val, result, false); break; case TYPCATEGORY_BOOLEAN: if (DatumGetBool(val)) appendStringInfoString(result, "true"); else appendStringInfoString(result, "false"); break; case TYPCATEGORY_NUMERIC: outputstr = OidOutputFunctionCall(typoutputfunc, val); /* * Don't call escape_json here if it's a valid JSON number. * Numeric output should usually be a valid JSON number and JSON * numbers shouldn't be quoted. Quote cases like "Nan" and * "Infinity", however. */ if (strpbrk(outputstr, NON_NUMERIC_LETTER) == NULL) appendStringInfoString(result, outputstr); else escape_json(result, outputstr); pfree(outputstr); break; case TYPCATEGORY_JSON: /* JSON will already be escaped */ outputstr = OidOutputFunctionCall(typoutputfunc, val); appendStringInfoString(result, outputstr); pfree(outputstr); break; case TYPCATEGORY_JSON_CAST: jsontext = DatumGetTextP(OidFunctionCall1(typoutputfunc, val)); outputstr = text_to_cstring(jsontext); appendStringInfoString(result, outputstr); pfree(outputstr); pfree(jsontext); break; default: outputstr = OidOutputFunctionCall(typoutputfunc, val); escape_json(result, outputstr); pfree(outputstr); break; }}
开发者ID:sqlparser,项目名称:postgres,代码行数:68,
示例25: tuple_data_splitDatumtuple_data_split(PG_FUNCTION_ARGS){ Oid relid; bytea *raw_data; uint16 t_infomask; uint16 t_infomask2; char *t_bits_str; bool do_detoast = false; bits8 *t_bits = NULL; Datum res; relid = PG_GETARG_OID(0); raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1); t_infomask = PG_GETARG_INT16(2); t_infomask2 = PG_GETARG_INT16(3); t_bits_str = PG_ARGISNULL(4) ? NULL : text_to_cstring(PG_GETARG_TEXT_PP(4)); if (PG_NARGS() >= 6) do_detoast = PG_GETARG_BOOL(5); if (!superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg("must be superuser to use raw page functions"))); if (!raw_data) PG_RETURN_NULL(); /* * Convert t_bits string back to the bits8 array as represented in the * tuple header. */ if (t_infomask & HEAP_HASNULL) { int bits_str_len; int bits_len; bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE; if (!t_bits_str) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("argument of t_bits is null, but it is expected to be null and %d character long", bits_len))); bits_str_len = strlen(t_bits_str); if (bits_len != bits_str_len) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("unexpected length of t_bits %u, expected %d", bits_str_len, bits_len))); /* do the conversion */ t_bits = text_to_bits(t_bits_str, bits_str_len); } else { if (t_bits_str) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes length", strlen(t_bits_str)))); } /* Split tuple data */ res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ, VARSIZE(raw_data) - VARHDRSZ, t_infomask, t_infomask2, t_bits, do_detoast); if (t_bits) pfree(t_bits); PG_RETURN_ARRAYTYPE_P(res);}
开发者ID:adityavs,项目名称:postgres,代码行数:76,
示例26: ts_stat_sqlstatic TSVectorStat *ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws){ char *query = text_to_cstring(txt); int i; TSVectorStat *stat; bool isnull; Portal portal; SPIPlanPtr plan; if ((plan = SPI_prepare(query, 0, NULL)) == NULL) /* internal error */ elog(ERROR, "SPI_prepare(/"%s/") failed", query); if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL) /* internal error */ elog(ERROR, "SPI_cursor_open(/"%s/") failed", query); SPI_cursor_fetch(portal, true, 100); if (SPI_tuptable == NULL || SPI_tuptable->tupdesc->natts != 1 || !is_expected_type(SPI_gettypeid(SPI_tuptable->tupdesc, 1), TSVECTOROID)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ts_stat query must return one tsvector column"))); stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat)); stat->maxdepth = 1; if (ws) { char *buf; buf = VARDATA(ws); while (buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ) { if (pg_mblen(buf) == 1) { switch (*buf) { case 'A': case 'a': stat->weight |= 1 << 3; break; case 'B': case 'b': stat->weight |= 1 << 2; break; case 'C': case 'c': stat->weight |= 1 << 1; break; case 'D': case 'd': stat->weight |= 1; break; default: stat->weight |= 0; } } buf += pg_mblen(buf); } } while (SPI_processed > 0) { for (i = 0; i < SPI_processed; i++) { Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull); if (!isnull) stat = ts_accum(persistentContext, stat, data); } SPI_freetuptable(SPI_tuptable); SPI_cursor_fetch(portal, true, 100); } SPI_freetuptable(SPI_tuptable); SPI_cursor_close(portal); SPI_freeplan(plan); pfree(query); return stat;}
开发者ID:hl0103,项目名称:pgxc,代码行数:87,
示例27: kc_shrinkDatum kc_shrink(PG_FUNCTION_ARGS) { char *map_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *start_time = text_to_cstring(PG_GETARG_TEXT_PP(1)); // Start time + uid!!! char *new_rid = text_to_cstring(PG_GETARG_TEXT_PP(2)); ArrayType *old_rids = PG_GETARG_ARRAYTYPE_P(3); char *classification = text_to_cstring(PG_GETARG_TEXT_PP(4)); char *doctype = text_to_cstring(PG_GETARG_TEXT_PP(5)); char *pop = text_to_cstring(PG_GETARG_TEXT_PP(6)); char *psource = text_to_cstring(PG_GETARG_TEXT_PP(7)); text *tout; int i,j; Datum *rid_datums; bool *rid_nulls; int rid_count; char *next_rid; KCDB *main_db; char *vbuf; size_t vsiz; // Open our DB. main_db = kcdbnew(); if (!open_db (main_db, map_name, start_time)) { tout = cstring_to_text(new_rid); PG_RETURN_TEXT_P(tout); } kcdbbegintran (main_db, 0); // First fill in what we can from the input. Cloudflare__ZoneTimeBucket msg = CLOUDFLARE__ZONE_TIME_BUCKET__INIT; msg.map_name = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.doctype = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.classification = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.pop = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.psource = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.result_id = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.db_key = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.db_path = (char *)palloc(MAX_KC_ROW_ENTRY * sizeof(char)); msg.map_entry = palloc(MAX_KEYS_BEFORE_KV_MAP * sizeof(Cloudflare__ZoneTimeBucket__Counter)); msg.n_map_entry = 0; strncpy(msg.map_name, map_name, MAX_KC_ROW_ENTRY); strncpy(msg.classification, classification, MAX_KC_ROW_ENTRY); strncpy(msg.doctype, doctype, MAX_KC_ROW_ENTRY); strncpy(msg.pop, pop, MAX_KC_ROW_ENTRY); strncpy(msg.psource, psource, MAX_KC_ROW_ENTRY); strncpy(msg.result_id, new_rid, KC_MAX_RID); snprintf(msg.db_path, MAX_KC_ROW_ENTRY, "%s%s%s", map_name, "/", start_time); snprintf(msg.db_key, KC_MAX_RID, "%s%s%s%s%s%s%s%s%s%s%s", new_rid, CF_LABEL_SEP, classification, CF_LABEL_SEP, doctype, CF_LABEL_SEP, pop, CF_LABEL_SEP, psource, CF_LABEL_SEP, map_name); // Now run over the array. deconstruct_array(old_rids, TEXTOID, -1, false, 'i', &rid_datums, &rid_nulls, &rid_count); if (ARR_HASNULL(old_rids)) { ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("cannot work with arrays containing NULLs"))); } int num_new_keys = 0; int num_entries = 0; char keys_to_use[rid_count][KC_MAX_RID]; Cloudflare__ZoneTimeBucket *msg_new[rid_count]; j=0; for (i = 0; i < rid_count; i++) { next_rid = TextDatumGetCString(rid_datums[i]); snprintf(keys_to_use[i], KC_MAX_RID, "%s%s%s%s%s%s%s%s%s%s%s", next_rid, CF_LABEL_SEP, classification, CF_LABEL_SEP, doctype, CF_LABEL_SEP, pop, CF_LABEL_SEP, psource, CF_LABEL_SEP, map_name); vbuf = kcdbget(main_db, keys_to_use[i], strlen(keys_to_use[i]), &vsiz); if (vbuf) { msg_new[j] = cloudflare__zone_time_bucket__unpack(NULL, vsiz, (const uint8_t *)vbuf); if (msg_new[j] == NULL) { // Something failed ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("error unpacking incoming message"))); } else { if (msg_new[j]->kv_map_file) { num_entries = MAX_KEYS_BEFORE_KV_MAP + 1; } else { num_entries += msg_new[j]->n_map_entry; } j++; } kcfree(vbuf); } else {//.........这里部分代码省略.........
开发者ID:cloudflare,项目名称:SortaSQL,代码行数:101,
示例28: pgxml_texttoxmlchar/* Translate a PostgreSQL "varlena" -i.e. a variable length parameter * into the libxml2 representation */static xmlChar *pgxml_texttoxmlchar(text *textstring){ return (xmlChar *) text_to_cstring(textstring);}
开发者ID:42penguins,项目名称:postgres,代码行数:8,
示例29: xmlelementDatumxmlelement(PG_FUNCTION_ARGS){ Datum nameText; ArrayType *attrs = NULL; char *elName; unsigned int nameLen, resSizeMax; unsigned int childSize = 0; char *c, *result, *resData, *resCursor, *nameDst; XMLCompNodeHdr element; XMLNodeOffset *rootOffPtr; bool nameFirstChar = true; char **attrNames = NULL; char **attrValues = NULL; char *attrValFlags = NULL; XMLNodeHdr *attrNodes = NULL; XMLNodeHdr child = NULL; char **newNds = NULL; char *newNd = NULL; unsigned int attrCount = 0; unsigned int attrsSizeTotal = 0; unsigned short childCount = 0; if (PG_ARGISNULL(0)) { elog(ERROR, "invalid element name"); } nameText = PG_GETARG_DATUM(0); elName = TextDatumGetCString(nameText); nameLen = strlen(elName); if (nameLen == 0) { elog(ERROR, "invalid element name"); } if (!PG_ARGISNULL(1)) { int *dims; Oid elType, arrType; int16 arrLen, elLen; bool elByVal, elIsNull; char elAlign; unsigned int i; attrs = PG_GETARG_ARRAYTYPE_P(1); if (ARR_NDIM(attrs) != 2) { elog(ERROR, "attributes must be passed in 2 dimensional array"); } dims = ARR_DIMS(attrs); if (dims[1] != 2) { elog(ERROR, "the second dimension of attribute array must be 2"); } attrCount = dims[0]; Assert(attrCount > 0); elType = attrs->elemtype; arrType = get_array_type(elType); arrLen = get_typlen(arrType); Assert(arrType != InvalidOid); get_typlenbyvalalign(elType, &elLen, &elByVal, &elAlign); attrNames = (char **) palloc(attrCount * sizeof(char *)); attrValues = (char **) palloc(attrCount * sizeof(char *)); attrValFlags = (bool *) palloc(attrCount * sizeof(char)); for (i = 1; i <= attrCount; i++) { int subscrName[] = {i, 1}; int subscrValue[] = {i, 2}; Datum elDatum; char *nameStr, *valueStr; bool valueHasRefs = false; elDatum = array_ref(attrs, 2, subscrName, arrLen, elLen, elByVal, elAlign, &elIsNull); if (elIsNull) { elog(ERROR, "attribute name must not be null"); } nameStr = text_to_cstring(DatumGetTextP(elDatum)); if (strlen(nameStr) == 0) { elog(ERROR, "attribute name must be a string of non-zero length"); } else { /* Check validity of characters. */ char *c = nameStr; int cWidth = pg_utf_mblen((unsigned char *) c);//.........这里部分代码省略.........
开发者ID:andreypopp,项目名称:pg_xnode,代码行数:101,
示例30: xmlvalidate_rng/** * Validation function take first argument as XML type, then check if is * well formated. If so, do the same for RNG document. If both success, check * XML document against RNG schema restrictions. * @return true if pass, false otherwise */Datumxmlvalidate_rng(PG_FUNCTION_ARGS){#ifdef USE_LIBXML text *data = NULL; char *rng = NULL; xmlChar *utf8rng = NULL; xmltype *xmldata = NULL; char *xmldataint = NULL; xmlChar *xmldatastr = NULL; bool result = false; int lenxml = -1; // length of xml data int lenrng = -1; // length of xsd data xmlDocPtr doc = NULL; int ret = -1; xmlRelaxNGParserCtxtPtr ctxt = NULL; xmlRelaxNGPtr schema = NULL; xmlRelaxNGValidCtxtPtr validctxt = NULL; // creating xmlChar * from internal xmltype of stored XML xmldata = PG_GETARG_XML_P(0); xmldataint = VARDATA(xmldata); lenxml = VARSIZE(xmldata) - VARHDRSZ; xmldatastr = (xmlChar *) palloc((lenxml + 1) * sizeof(xmlChar)); memcpy(xmldatastr, xmldataint, lenxml); xmldatastr[lenxml] = '/0'; // creating xmlChar* from text representation of XSD data = PG_GETARG_TEXT_P(1); lenrng = VARSIZE(data) - VARHDRSZ; rng = text_to_cstring(data); //encode XML to internal representation with UTF-8, only one used in LibXML utf8rng = pg_do_encoding_conversion((unsigned char*)rng, lenrng, GetDatabaseEncoding(), PG_UTF8); //initialize LibXML structures, if allready done -> do nothing pg_xml_init(); xmlInitParser(); doc = xmlReadMemory((const char *)xmldatastr, lenxml, "include.xml", NULL, 0); if (doc == NULL) { elog(ERROR, "Failed to parse XML document"); PG_RETURN_BOOL (false); } ctxt = xmlRelaxNGNewMemParserCtxt(rng, lenrng); if (ctxt == NULL) { // unable to create parser context elog(ERROR, "Error with creating schema, check if RelaxNG schema is valid"); PG_RETURN_BOOL (false); } schema = xmlRelaxNGParse(ctxt); // parse schema xmlRelaxNGFreeParserCtxt(ctxt); // realease parser context validctxt = xmlRelaxNGNewValidCtxt(schema); if (validctxt == NULL) { // cant create validation context xmlRelaxNGFree(schema); elog(ERROR, "Cant create validation context"); PG_RETURN_BOOL (false); } // set errors to SQL errors xmlRelaxNGSetValidErrors(validctxt, xml_error_handler, NULL, 0); ret = xmlRelaxNGValidateDoc(validctxt, doc); if (ret == 0) { elog(INFO, "Validates"); result = true; } else if (ret > 0) { elog(INFO, "Dont validates"); result = false; } else { elog(INFO, "Validation generated an internal error"); result = false; } xmlRelaxNGFree(schema); xmlRelaxNGFreeValidCtxt(validctxt);//.........这里部分代码省略.........
开发者ID:killteck,项目名称:indexing-xml,代码行数:101,
注:本文中的text_to_cstring函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 C++ text_width函数代码示例 C++ text_layer_set_text_alignment函数代码示例 |