| | |
| | | std::string columnType_; |
| | | }; |
| | | |
| | | struct MetadataEntry |
| | | { |
| | | Oid relationOid_; |
| | | std::string jsonColumnName_; |
| | | std::string path_; |
| | | std::string jsonType_; |
| | | std::string preferredName_; |
| | | }; |
| | | |
| | | static std::vector<MetadataEntry> metadataEntries_; |
| | | |
| | | static JsonbValue * |
| | | getobjectfield(JsonbContainer *container, const char *field_name, JsonbValue *value_buffer) |
| | | { |
| | |
| | | } |
| | | |
| | | static std::string |
| | | buildmetadataqualifiedname(const std::string &owner_name) |
| | | { |
| | | return buildqualifiedname(owner_name, "dbms_json_metadata"); |
| | | } |
| | | |
| | | static std::string |
| | | resolveownername() |
| | | { |
| | | List *searchPath = fetch_search_path(false); |
| | |
| | | } |
| | | |
| | | static void |
| | | ensuremetadatatable(const std::string &owner_name) |
| | | { |
| | | int spiStatus = SPI_OK_CONNECT; |
| | | int executeStatus = SPI_OK_UTILITY; |
| | | std::string createSql = |
| | | "CREATE TABLE IF NOT EXISTS " + buildmetadataqualifiedname(owner_name) + " (" |
| | | "tablename text NOT NULL, " |
| | | "jcolname text NOT NULL, " |
| | | "path text NOT NULL, " |
| | | "json_type text NOT NULL, " |
| | | "preferred_name text NOT NULL, " |
| | | "PRIMARY KEY(tablename, jcolname, path, json_type)" |
| | | ")"; |
| | | |
| | | spiStatus = SPI_connect(); |
| | | if (spiStatus != SPI_OK_CONNECT) { |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("SPI_connect failed in DBMS_JSON metadata initialization"), |
| | | errdetail("spi_status=%d", spiStatus))); |
| | | } |
| | | |
| | | executeStatus = SPI_execute(createSql.c_str(), false, 0); |
| | | if (executeStatus != SPI_OK_UTILITY) { |
| | | SPI_finish(); |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("DBMS_JSON metadata table initialization failed"), |
| | | errdetail("spi_status=%d", executeStatus))); |
| | | } |
| | | |
| | | SPI_finish(); |
| | | } |
| | | |
| | | static void |
| | | applypreferrednames(const std::string &owner_name, |
| | | const std::string &table_name, |
| | | applypreferrednames(Oid relation_oid, |
| | | const std::string &json_column_name, |
| | | std::vector<GuideEntry> *entries) |
| | | { |
| | | Oid argTypes[2] = {TEXTOID, TEXTOID}; |
| | | Datum values[2]; |
| | | const char nulls[2] = {' ', ' '}; |
| | | int spiStatus = SPI_OK_CONNECT; |
| | | int executeStatus = SPI_OK_SELECT; |
| | | std::string selectSql = |
| | | "SELECT path, json_type, preferred_name " |
| | | "FROM " + buildmetadataqualifiedname(owner_name) + |
| | | " WHERE tablename = $1 AND jcolname = $2"; |
| | | |
| | | if (entries->empty()) { |
| | | return; |
| | | } |
| | | |
| | | values[0] = CStringGetTextDatum(table_name.c_str()); |
| | | values[1] = CStringGetTextDatum(json_column_name.c_str()); |
| | | |
| | | spiStatus = SPI_connect(); |
| | | if (spiStatus != SPI_OK_CONNECT) { |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("SPI_connect failed in DBMS_JSON preferred name lookup"), |
| | | errdetail("spi_status=%d", spiStatus))); |
| | | } |
| | | |
| | | executeStatus = SPI_execute_with_args(selectSql.c_str(), |
| | | 2, |
| | | argTypes, |
| | | values, |
| | | nulls, |
| | | true, |
| | | 0); |
| | | if (executeStatus != SPI_OK_SELECT) { |
| | | SPI_finish(); |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("DBMS_JSON preferred name lookup failed"), |
| | | errdetail("spi_status=%d", executeStatus))); |
| | | } |
| | | |
| | | for (uint64 rowIndex = 0; rowIndex < SPI_processed; ++rowIndex) { |
| | | HeapTuple rowTuple = SPI_tuptable->vals[rowIndex]; |
| | | TupleDesc rowDesc = SPI_tuptable->tupdesc; |
| | | bool isNull = false; |
| | | char *pathCString = TextDatumGetCString(SPI_getbinval(rowTuple, rowDesc, 1, &isNull)); |
| | | char *typeCString = TextDatumGetCString(SPI_getbinval(rowTuple, rowDesc, 2, &isNull)); |
| | | char *preferredCString = TextDatumGetCString(SPI_getbinval(rowTuple, rowDesc, 3, &isNull)); |
| | | |
| | | for (const MetadataEntry &metadataEntry : metadataEntries_) { |
| | | if (metadataEntry.relationOid_ != relation_oid || |
| | | metadataEntry.jsonColumnName_ != json_column_name) { |
| | | continue; |
| | | } |
| | | for (GuideEntry &entry : *entries) { |
| | | if (entry.path_ == pathCString && entry.jsonType_ == typeCString) { |
| | | entry.columnName_ = preferredCString; |
| | | if (entry.path_ == metadataEntry.path_ && |
| | | entry.jsonType_ == metadataEntry.jsonType_) { |
| | | entry.columnName_ = metadataEntry.preferredName_; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | pfree(pathCString); |
| | | pfree(typeCString); |
| | | pfree(preferredCString); |
| | | static void |
| | | upsertmetadataentry(Oid relation_oid, |
| | | const std::string &json_column_name, |
| | | const std::string &path_value, |
| | | const std::string &json_type, |
| | | const std::string &preferred_name) |
| | | { |
| | | for (MetadataEntry &metadataEntry : metadataEntries_) { |
| | | if (metadataEntry.relationOid_ == relation_oid && |
| | | metadataEntry.jsonColumnName_ == json_column_name && |
| | | metadataEntry.path_ == path_value && |
| | | metadataEntry.jsonType_ == json_type) { |
| | | metadataEntry.preferredName_ = preferred_name; |
| | | return; |
| | | } |
| | | } |
| | | |
| | | SPI_finish(); |
| | | metadataEntries_.push_back({relation_oid, |
| | | json_column_name, |
| | | path_value, |
| | | json_type, |
| | | preferred_name}); |
| | | } |
| | | |
| | | static void |
| | |
| | | std::string tableOwnerName; |
| | | std::string tableName; |
| | | std::string jsonColumnName; |
| | | std::string metadataOwnerName = DbmsJson::resolveownername(); |
| | | Oid relationOid = InvalidOid; |
| | | Jsonb *guideJsonb = nullptr; |
| | | text *guideText = nullptr; |
| | | |
| | |
| | | &tableOwnerName, |
| | | &tableName, |
| | | &jsonColumnName); |
| | | relationOid = DbmsJson::resolvetablerelid(tableOwnerName, tableName); |
| | | guideJsonb = DbmsJson::builddataguidefortable(tableOwnerName, |
| | | tableName, |
| | | jsonColumnName); |
| | | DbmsJson::ensuremetadatatable(metadataOwnerName); |
| | | |
| | | { |
| | | std::vector<DbmsJson::GuideEntry> guideEntries; |
| | | DbmsJson::parseguideentries(guideJsonb, &guideEntries); |
| | | DbmsJson::applypreferrednames(metadataOwnerName, |
| | | tableName, |
| | | DbmsJson::applypreferrednames(relationOid, |
| | | jsonColumnName, |
| | | &guideEntries); |
| | | guideJsonb = DbmsJson::buildguidefromentries(guideEntries); |
| | |
| | | char *_jsonColumnNameCString = nullptr; |
| | | char *_pathCString = nullptr; |
| | | char *_preferredNameCString = nullptr; |
| | | Oid argTypes[5] = {TEXTOID, TEXTOID, TEXTOID, TEXTOID, TEXTOID}; |
| | | Datum values[5]; |
| | | const char nulls[5] = {' ', ' ', ' ', ' ', ' '}; |
| | | int spiStatus = SPI_OK_CONNECT; |
| | | int executeStatus = SPI_OK_INSERT; |
| | | std::string metadataOwnerName; |
| | | std::string tableOwnerName; |
| | | std::string tableName; |
| | | std::string jsonColumnName; |
| | | std::string jsonType; |
| | | std::string upsertSql; |
| | | Oid relationOid = InvalidOid; |
| | | |
| | | ereport(LOG, |
| | | (errmsg("DBMS_JSON.RENAME_COLUMN call"), |
| | |
| | | &tableOwnerName, |
| | | &tableName, |
| | | &jsonColumnName); |
| | | metadataOwnerName = DbmsJson::resolveownername(); |
| | | DbmsJson::ensuremetadatatable(metadataOwnerName); |
| | | |
| | | upsertSql = |
| | | "INSERT INTO " + DbmsJson::buildmetadataqualifiedname(metadataOwnerName) + |
| | | " (tablename, jcolname, path, json_type, preferred_name) " |
| | | "VALUES ($1, $2, $3, $4, $5) " |
| | | "ON CONFLICT (tablename, jcolname, path, json_type) " |
| | | "DO UPDATE SET preferred_name = EXCLUDED.preferred_name"; |
| | | |
| | | values[0] = CStringGetTextDatum(tableName.c_str()); |
| | | values[1] = CStringGetTextDatum(jsonColumnName.c_str()); |
| | | values[2] = CStringGetTextDatum(_pathCString); |
| | | values[3] = CStringGetTextDatum(jsonType.c_str()); |
| | | values[4] = CStringGetTextDatum(_preferredNameCString); |
| | | |
| | | spiStatus = SPI_connect(); |
| | | if (spiStatus != SPI_OK_CONNECT) { |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("SPI_connect failed in DBMS_JSON.RENAME_COLUMN"), |
| | | errdetail("spi_status=%d", spiStatus))); |
| | | } |
| | | |
| | | executeStatus = SPI_execute_with_args(upsertSql.c_str(), |
| | | 5, |
| | | argTypes, |
| | | values, |
| | | nulls, |
| | | false, |
| | | 0); |
| | | if (executeStatus != SPI_OK_INSERT && executeStatus != SPI_OK_UPDATE) { |
| | | SPI_finish(); |
| | | ereport(ERROR, |
| | | (errcode(ERRCODE_INTERNAL_ERROR), |
| | | errmsg("DBMS_JSON.RENAME_COLUMN failed"), |
| | | errdetail("spi_status=%d", executeStatus))); |
| | | } |
| | | |
| | | SPI_finish(); |
| | | relationOid = DbmsJson::resolvetablerelid(tableOwnerName, tableName); |
| | | DbmsJson::upsertmetadataentry(relationOid, |
| | | jsonColumnName, |
| | | std::string(_pathCString), |
| | | jsonType, |
| | | std::string(_preferredNameCString)); |
| | | |
| | | if (_tableNameCString != nullptr) { |
| | | pfree(_tableNameCString); |
| | |
| | | char *_pathCString = nullptr; |
| | | std::string viewOwnerName; |
| | | std::string tableOwnerName; |
| | | std::string metadataOwnerName; |
| | | Oid relationOid = InvalidOid; |
| | | Jsonb *guideJsonb = nullptr; |
| | | std::vector<DbmsJson::GuideEntry> guideEntries; |
| | | std::vector<DbmsJson::ColumnSpec> columns; |
| | |
| | | &tableOwnerName, |
| | | &tableName, |
| | | &jsonColumnName); |
| | | metadataOwnerName = viewOwnerName; |
| | | DbmsJson::ensuremetadatatable(metadataOwnerName); |
| | | relationOid = DbmsJson::resolvetablerelid(tableOwnerName, tableName); |
| | | guideJsonb = DbmsJson::builddataguidefortable(tableOwnerName, |
| | | tableName, |
| | | jsonColumnName); |
| | | DbmsJson::parseguideentries(guideJsonb, &guideEntries); |
| | | DbmsJson::applypreferrednames(metadataOwnerName, |
| | | tableName, |
| | | DbmsJson::applypreferrednames(relationOid, |
| | | jsonColumnName, |
| | | &guideEntries); |
| | | DbmsJson::buildcolumnsfromguide(guideEntries, |