[libgda/metat: 6/9] provider-meta: added utility methods to run queries
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda/metat: 6/9] provider-meta: added utility methods to run queries
- Date: Wed, 7 Nov 2018 15:37:07 +0000 (UTC)
commit 7b6c2dd50a1b463f8dee633760aa85b48190a1d1
Author: Daniel Espinosa <esodan gmail com>
Date: Mon Aug 20 14:46:30 2018 -0500
provider-meta: added utility methods to run queries
libgda/gda-provider-meta.c | 77 +++++++++++++++++++++++++++++++++++++
libgda/gda-provider-meta.h | 14 +++++++
libgda/sqlite/gda-sqlite-provider.c | 2 +
3 files changed, 93 insertions(+)
---
diff --git a/libgda/gda-provider-meta.c b/libgda/gda-provider-meta.c
index 74764af1b..90c42faa7 100644
--- a/libgda/gda-provider-meta.c
+++ b/libgda/gda-provider-meta.c
@@ -18,6 +18,16 @@
*/
#include <libgda/gda-provider-meta.h>
#include <libgda/gda-connection.h>
+#include <glib/gi18n-lib.h>
+
+/* module error */
+GQuark gda_provider_meta_error_quark (void)
+{
+ static GQuark quark;
+ if (!quark)
+ quark = g_quark_from_static_string ("gda_provider_meta_error");
+ return quark;
+}
enum {
PROP_CONNECTION = 1,
@@ -42,6 +52,73 @@ gda_provider_meta_default_init (GdaProviderMetaInterface *iface) {
obj_properties[PROP_CONNECTION]);
}
+/**
+ * gda_provider_meta_execute_query:
+ * @prov: a #GdaProviderMeta
+ * @sql: a string with the SQL to execute on provider
+ * @error: place to store errors or %NULL
+ *
+ * SQL is specific for current provider.
+ *
+ * Returns: (transfer full) (nullable): a new #GdaDataModel with as a result of the query
+ * Since: 6.0
+ * Stability: Unstable
+ */
+GdaDataModel *gda_provider_meta_execute_query (GdaProviderMeta *prov,
+ const gchar *sql,
+ GError **error)
+{
+ g_return_val_if_fail (prov, NULL);
+ g_return_val_if_fail (GDA_IS_PROVIDER_META (prov), NULL);
+ g_return_val_if_fail (sql, NULL);
+
+ GdaDataModel *model = NULL;
+ GdaConnection *cnc = NULL;
+ g_object_get (G_OBJECT (prov), "connection", &cnc, NULL);
+ if (cnc == NULL) {
+ g_set_error (error, GDA_PROVIDER_META_ERROR, GDA_PROVIDER_META_NO_CONNECTION_ERROR,
+ _("No connection is set"));
+ return NULL;
+ }
+ if (!gda_connection_is_opened (cnc)) {
+ g_set_error (error, GDA_PROVIDER_META_ERROR, GDA_PROVIDER_META_NO_CONNECTION_ERROR,
+ _("Connection is no opened"));
+ return NULL;
+ }
+ model = gda_connection_execute_select_command (cnc, sql, error);
+ return model;
+}
+
+/**
+ * gda_provider_meta_execute_query:
+ * @prov: a #GdaProviderMeta
+ * @sql: a string with the SQL to execute on provider
+ * @error: place to store errors or %NULL
+ *
+ * SQL is specific for current provider.
+ *
+ * Returns: (transfer full) (nullable): a new #GdaDataModel with as a result of the query
+ * Since: 6.0
+ * Stability: Unstable
+ */
+GdaRow*
+gda_provider_meta_execute_query_row (GdaProviderMeta *prov,
+ const gchar *sql,
+ GError **error)
+{
+ GdaDataModel *model = NULL;
+ model = gda_provider_meta_execute_query (prov, sql, error);
+ if (model == NULL) {
+ return NULL;
+ }
+ if (gda_data_model_get_n_rows (model) != 1) {
+ g_set_error (error, GDA_PROVIDER_META_ERROR, GDA_PROVIDER_META_NO_CONNECTION_ERROR,
+ _("No rows or more than one row was returned from query"));
+ return NULL;
+ }
+ return gda_row_new_from_data_model (model, 0);
+}
+
/**
* gda_provider_meta_get_connection:
* @prov:
diff --git a/libgda/gda-provider-meta.h b/libgda/gda-provider-meta.h
index 35b304e6b..557c5bbe7 100644
--- a/libgda/gda-provider-meta.h
+++ b/libgda/gda-provider-meta.h
@@ -25,6 +25,13 @@
G_BEGIN_DECLS
+typedef enum {
+ GDA_PROVIDER_META_NO_CONNECTION_ERROR
+} GdaProviderMetaError;
+
+extern GQuark gda_provider_meta_error_quark (void);
+#define GDA_PROVIDER_META_ERROR gda_provider_meta_error_quark ()
+
#define GDA_TYPE_PROVIDER_META gda_provider_meta_get_type()
G_DECLARE_INTERFACE(GdaProviderMeta, gda_provider_meta, GDA, PROVIDER_META, GObject)
@@ -229,6 +236,13 @@ struct _GdaProviderMetaInterface
gpointer padding[12];
};
+GdaDataModel *gda_provider_meta_execute_query (GdaProviderMeta *prov,
+ const gchar *sql,
+ GError **error);
+
+GdaRow *gda_provider_meta_execute_query_row (GdaProviderMeta *prov,
+ const gchar *sql,
+ GError **error);
GdaConnection *gda_provider_meta_get_connection (GdaProviderMeta *prov);
diff --git a/libgda/sqlite/gda-sqlite-provider.c b/libgda/sqlite/gda-sqlite-provider.c
index c4774752f..ec36fa1e3 100644
--- a/libgda/sqlite/gda-sqlite-provider.c
+++ b/libgda/sqlite/gda-sqlite-provider.c
@@ -65,6 +65,7 @@
#include <stdio.h>
#define _GDA_PSTMT(x) ((GdaPStmt*)(x))
#include <libgda/gda-debug-macros.h>
+#include <libgda/gda-provider-meta.h>
#define FILE_EXTENSION ".db"
static gchar *get_table_nth_column_name (GdaConnection *cnc, const gchar *table_name, gint pos);
@@ -3995,3 +3996,4 @@ gda_sqlite_provider_unescape_string (G_GNUC_UNUSED GdaServerProvider *provider,
return retval;
}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]