lidavidm commented on code in PR #679:
URL: https://github.com/apache/arrow-adbc/pull/679#discussion_r1204609465
##########
c/driver/postgresql/connection.cc:
##########
@@ -216,6 +216,85 @@ AdbcStatusCode PostgresConnection::GetInfo(struct
AdbcConnection* connection,
return BatchToArrayStream(&array, &schema, out, error);
}
+AdbcStatusCode PostgresConnectionGetSchemasImpl(PGconn* conn, int depth,
+ const char* db_name,
+ const char* db_schema,
+ struct ArrowArray*
db_schemas_list,
+ struct AdbcError* error) {
+ struct ArrowArray* db_schema_items = db_schemas_list->children[0];
+ struct ArrowArray* db_schema_names = db_schema_items->children[0];
+ struct ArrowArray* db_schema_tables_list = db_schema_items->children[1];
+
+ // inefficient to place here but better localized until we do a class-based
refactor
+ const char* curr_db;
+ PqResultHelper curr_db_helper = PqResultHelper{conn, "SELECT
current_database()"};
+ if (curr_db_helper.Status() == PGRES_TUPLES_OK) {
+ assert(curr_db_helper.NumRows() == 1);
+ auto curr_iter = curr_db_helper.begin();
+ PqResultRow db_row = *curr_iter;
+ curr_db = db_row[0].data;
Review Comment:
will this pointer stay valid? maybe we should copy to a string?
##########
c/driver/postgresql/connection.cc:
##########
@@ -216,6 +216,85 @@ AdbcStatusCode PostgresConnection::GetInfo(struct
AdbcConnection* connection,
return BatchToArrayStream(&array, &schema, out, error);
}
+AdbcStatusCode PostgresConnectionGetSchemasImpl(PGconn* conn, int depth,
+ const char* db_name,
+ const char* db_schema,
+ struct ArrowArray*
db_schemas_list,
+ struct AdbcError* error) {
+ struct ArrowArray* db_schema_items = db_schemas_list->children[0];
+ struct ArrowArray* db_schema_names = db_schema_items->children[0];
+ struct ArrowArray* db_schema_tables_list = db_schema_items->children[1];
+
+ // inefficient to place here but better localized until we do a class-based
refactor
+ const char* curr_db;
+ PqResultHelper curr_db_helper = PqResultHelper{conn, "SELECT
current_database()"};
+ if (curr_db_helper.Status() == PGRES_TUPLES_OK) {
+ assert(curr_db_helper.NumRows() == 1);
+ auto curr_iter = curr_db_helper.begin();
+ PqResultRow db_row = *curr_iter;
+ curr_db = db_row[0].data;
+ } else {
+ return ADBC_STATUS_NOT_IMPLEMENTED;
Review Comment:
nit: should probably be like STATUS_IO or something?
##########
c/driver/postgresql/connection.cc:
##########
@@ -216,6 +216,85 @@ AdbcStatusCode PostgresConnection::GetInfo(struct
AdbcConnection* connection,
return BatchToArrayStream(&array, &schema, out, error);
}
+AdbcStatusCode PostgresConnectionGetSchemasImpl(PGconn* conn, int depth,
+ const char* db_name,
+ const char* db_schema,
+ struct ArrowArray*
db_schemas_list,
+ struct AdbcError* error) {
+ struct ArrowArray* db_schema_items = db_schemas_list->children[0];
+ struct ArrowArray* db_schema_names = db_schema_items->children[0];
+ struct ArrowArray* db_schema_tables_list = db_schema_items->children[1];
+
+ // inefficient to place here but better localized until we do a class-based
refactor
+ const char* curr_db;
+ PqResultHelper curr_db_helper = PqResultHelper{conn, "SELECT
current_database()"};
+ if (curr_db_helper.Status() == PGRES_TUPLES_OK) {
+ assert(curr_db_helper.NumRows() == 1);
+ auto curr_iter = curr_db_helper.begin();
+ PqResultRow db_row = *curr_iter;
+ curr_db = db_row[0].data;
+ } else {
+ return ADBC_STATUS_NOT_IMPLEMENTED;
+ }
+
+ // postgres only allows you to list schemas for the currently connected db
+ if (strcmp(db_name, curr_db) == 0) {
+ struct StringBuilder query = {0};
+ if (StringBuilderInit(&query, /*initial_size*/ 256)) {
+ return ADBC_STATUS_INTERNAL;
+ }
+
+ const char* stmt =
+ "SELECT nspname FROM pg_catalog.pg_namespace WHERE "
+ "nspname !~ '^pg_' AND nspname <> 'information_schema'";
+
+ if (StringBuilderAppend(&query, "%s", stmt)) {
+ StringBuilderReset(&query);
+ return ADBC_STATUS_INTERNAL;
+ }
+
+ if (db_schema != NULL) {
+ char* schema_name = PQescapeIdentifier(conn, db_schema,
strlen(db_schema));
+ if (schema_name == NULL) {
+ SetError(error, "%s%s", "Failed to escape schema: ",
PQerrorMessage(conn));
+ StringBuilderReset(&query);
+ return ADBC_STATUS_INVALID_ARGUMENT;
+ }
+
+ int res =
+ StringBuilderAppend(&query, "%s%s%s", " AND nspname ='",
schema_name, "'");
Review Comment:
Eventually we should do this via query parameters instead of escaping
(especially since the spec lets you use LIKE-style patterns for these filters)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]