* InsertDocument adds a new document into the specified collection. */
| 1291 | * InsertDocument adds a new document into the specified collection. |
| 1292 | */ |
| 1293 | bool |
| 1294 | InsertDocument(uint64 collectionId, const char *shardTableName, |
| 1295 | int64 shardKeyValue, pgbson *objectId, |
| 1296 | pgbson *document) |
| 1297 | { |
| 1298 | StringInfoData query; |
| 1299 | const int argCount = 3; |
| 1300 | Oid argTypes[3]; |
| 1301 | Datum argValues[3]; |
| 1302 | int spiStatus PG_USED_FOR_ASSERTS_ONLY = 0; |
| 1303 | |
| 1304 | SPI_connect(); |
| 1305 | |
| 1306 | initStringInfo(&query); |
| 1307 | appendStringInfo(&query, "INSERT INTO %s.", ApiDataSchemaName); |
| 1308 | |
| 1309 | if (shardTableName != NULL && shardTableName[0] != '\0') |
| 1310 | { |
| 1311 | appendStringInfoString(&query, shardTableName); |
| 1312 | } |
| 1313 | else |
| 1314 | { |
| 1315 | appendStringInfo(&query, "documents_" UINT64_FORMAT, collectionId); |
| 1316 | } |
| 1317 | |
| 1318 | appendStringInfo(&query, " (shard_key_value, object_id, document) " |
| 1319 | " VALUES ($1, %s.bson_from_bytea($2), " |
| 1320 | "%s.bson_from_bytea($3))", |
| 1321 | CoreSchemaName, CoreSchemaName); |
| 1322 | |
| 1323 | argTypes[0] = INT8OID; |
| 1324 | argValues[0] = Int64GetDatum(shardKeyValue); |
| 1325 | argTypes[1] = BYTEAOID; |
| 1326 | argValues[1] = PointerGetDatum(CastPgbsonToBytea(objectId)); |
| 1327 | argTypes[2] = BYTEAOID; |
| 1328 | argValues[2] = PointerGetDatum(CastPgbsonToBytea(document)); |
| 1329 | |
| 1330 | SPIPlanPtr plan = GetSPIQueryPlanWithLocalShard(collectionId, shardTableName, |
| 1331 | QUERY_ID_INSERT, query.data, argTypes, |
| 1332 | argCount); |
| 1333 | |
| 1334 | spiStatus = SPI_execute_plan(plan, argValues, NULL, false, 1); |
| 1335 | pfree(query.data); |
| 1336 | |
| 1337 | SPI_finish(); |
| 1338 | |
| 1339 | return spiStatus == SPI_OK_INSERT; |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* |
no test coverage detected