kou commented on code in PR #5108:
URL: https://github.com/apache/arrow-adbc/pull/5108#discussion_r4807052656


##########
glib/adbc-glib/connection.c:
##########
@@ -228,6 +228,51 @@ gboolean gadbc_connection_set_option(GADBCConnection* 
connection, const gchar* k
   return gadbc_error_check(error, status_code, &adbc_error, context);
 }
 
+/**
+ * gadbc_connection_get_option:
+ * @connection: A #GADBCConnection.
+ * @key: An option key.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable):
+ *   The option value or %NULL on error.
+ *
+ *   It should be freed with g_free() when no longer needed.
+ *
+ * Since: 1.14.0
+ */
+gchar* gadbc_connection_get_option(GADBCConnection* connection, const gchar* 
key,
+                                   GError** error) {
+  const gchar* context = "[adbc][connection][get-option]";
+  struct AdbcConnection* adbc_connection =
+      gadbc_connection_get_raw(connection, context, error);
+  if (!adbc_connection) {
+    return NULL;
+  }
+  size_t length = 256;
+  gchar* value = g_new0(gchar, length);
+  for (gint i = 0; i < 2; i++) {
+    size_t capacity = length;
+    struct AdbcError adbc_error = {};
+    AdbcStatusCode status_code =
+        AdbcConnectionGetOption(adbc_connection, key, value, &length, 
&adbc_error);
+    if (!gadbc_error_check(error, status_code, &adbc_error, context)) {
+      g_free(value);
+      return NULL;
+    }
+    if (length <= capacity) {
+      return value;
+    }
+    if (i == 0) {
+      value = g_realloc(value, length);
+    }
+  }
+  g_free(value);
+  g_set_error(error, GADBC_ERROR, GADBC_ERROR_INVALID_STATE,
+              "%s option length changed while reading", context);
+  return NULL;

Review Comment:
   How about trying a stack buffer first and allocating a heap buffer only when 
the value doesn't fit?
   
   ```c
       gchar buffer[256];
       size_t length = sizeof(buffer);
       struct AdbcError adbc_error = {};
       AdbcStatusCode status_code =
           AdbcConnectionGetOption(adbc_connection, key, buffer, &length, 
&adbc_error);
       if (!gadbc_error_check(error, status_code, &adbc_error, context)) {
         return NULL;
       }
       if (length <= sizeof(buffer)) {
         gchar* value = g_new(gchar, length);
         memcpy(value, buffer, length);
         return value;
       }
   
       size_t capacity = length;
       gchar* value = g_new0(gchar, capacity);
       adbc_error = (struct AdbcError){};
       status_code =
           AdbcConnectionGetOption(adbc_connection, key, value, &length, 
&adbc_error);
       if (!gadbc_error_check(error, status_code, &adbc_error, context)) {
         g_free(value);
         return NULL;
       }
       if (length > capacity) {
         g_free(value);
         g_set_error(error, GADBC_ERROR, GADBC_ERROR_INVALID_STATE,
                     "%s option length changed while reading", context);
         return NULL;
       }
       return value;
   ```



-- 
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]

Reply via email to