From b2210aab948caf9a8d45486a14a53d86c5415406 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 22 Mar 2016 21:29:27 +0100 Subject: [PATCH] Added funcions GdaExSqlBuilder::get_sql_* (closes #1001). --- src/sqlbuilder.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++ src/sqlbuilder.h | 8 ++- 2 files changed, 167 insertions(+), 2 deletions(-) diff --git a/src/sqlbuilder.c b/src/sqlbuilder.c index 939ca36..feaf3d6 100644 --- a/src/sqlbuilder.c +++ b/src/sqlbuilder.c @@ -674,6 +674,167 @@ gchar return ret; } +/** + * gdaex_sql_builder_get_sql_select: + * @sqlb: + * @cnc: + * @params: + * + */ +gchar +*gdaex_sql_builder_get_sql_select (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params) +{ + gchar *ret; + gchar *sql; + + gchar *start; + gchar *end; + + ret = NULL; + + sql = gdaex_sql_builder_get_sql (sqlb, cnc, params); + if (sql == NULL) + { + return ret; + } + + start = g_strstr_len (sql, -1, "SELECT"); + if (start == NULL) + { + return ret; + } + + end = g_strstr_len (sql, -1, "FROM"); + if (end == NULL) + { + return ret; + } + + ret = g_strndup (start + 7, strlen (sql) - 8 - strlen (end)); + + g_free (sql); + + return ret; +} + +/** + * gdaex_sql_builder_get_sql_from: + * @sqlb: + * @cnc: + * @params: + * + */ +gchar +*gdaex_sql_builder_get_sql_from (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params) +{ + gchar *ret; + gchar *sql; + + gchar *start; + gchar *end; + + ret = NULL; + + sql = gdaex_sql_builder_get_sql (sqlb, cnc, params); + if (sql == NULL) + { + return ret; + } + + start = g_strstr_len (sql, -1, "FROM"); + if (start == NULL) + { + return ret; + } + + end = g_strstr_len (sql, -1, "WHERE"); + if (end == NULL) + { + end = g_strstr_len (sql, -1, "ORDER BY"); + } + + ret = g_strndup (start + 5, strlen (start) - (end != NULL ? 6 : 5) - (end != NULL ? strlen (end) : 0)); + + g_free (sql); + + return ret; +} + +/** + * gdaex_sql_builder_get_sql_where: + * @sqlb: + * @cnc: + * @params: + * + */ +gchar +*gdaex_sql_builder_get_sql_where (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params) +{ + gchar *ret; + gchar *sql; + + gchar *start; + gchar *end; + + ret = NULL; + + sql = gdaex_sql_builder_get_sql (sqlb, cnc, params); + if (sql == NULL) + { + return ret; + } + + start = g_strstr_len (sql, -1, "WHERE"); + if (start == NULL) + { + return ret; + } + + end = g_strstr_len (sql, -1, "ORDER BY"); + + ret = g_strndup (start + 6, strlen (start) - (end != NULL ? 7 : 6) - (end != NULL ? strlen (end) : 0)); + + g_free (sql); + + return ret; +} + +/** + * gdaex_sql_builder_get_sql_order: + * @sqlb: + * @cnc: + * @params: + * + */ +gchar +*gdaex_sql_builder_get_sql_order (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params) +{ + gchar *ret; + gchar *sql; + + gchar *start; + + ret = NULL; + + sql = gdaex_sql_builder_get_sql (sqlb, cnc, params); + if (sql == NULL) + { + return ret; + } + + start = g_strstr_len (sql, -1, "ORDER BY"); + if (start == NULL) + { + return ret; + } + + ret = g_strndup (start + 9, strlen (start) - 9); + + g_free (sql); + + return ret; +} + /** * gdaex_sql_builder_query: * @sqlb: diff --git a/src/sqlbuilder.h b/src/sqlbuilder.h index fb69f33..af3343c 100644 --- a/src/sqlbuilder.h +++ b/src/sqlbuilder.h @@ -1,7 +1,7 @@ /* * sqlbuilder.h * - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2016 Andrea Zagli * * This file is part of libgdaex. * @@ -68,13 +68,17 @@ void gdaex_sql_builder_join (GdaExSqlBuilder *sqlb, void gdaex_sql_builder_field (GdaExSqlBuilder *sqlb, const gchar *table_name, const gchar *field_name, const gchar *field_alias, GValue *gval); void gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...); -GdaSqlBuilderId gdaex_sql_builder_where (GdaExSqlBuilder *sqln, GdaSqlOperatorType op, +GdaSqlBuilderId gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...); void gdaex_sql_builder_order (GdaExSqlBuilder *sqlb, ...); GdaSqlBuilder *gdaex_sql_builder_get_gda_sql_builder (GdaExSqlBuilder *sqlb); gchar *gdaex_sql_builder_get_sql (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params); +gchar *gdaex_sql_builder_get_sql_select (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params); +gchar *gdaex_sql_builder_get_sql_from (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params); +gchar *gdaex_sql_builder_get_sql_where (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params); +gchar *gdaex_sql_builder_get_sql_order (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params); GdaDataModel *gdaex_sql_builder_query (GdaExSqlBuilder *sqlb, GdaEx *gdaex, GdaSet *params); gint gdaex_sql_builder_execute (GdaExSqlBuilder *sqlb, GdaEx *gdaex, GdaSet *params); -- 2.49.0