QOF  0.8.7
Macros | Typedefs | Functions | Variables
qofsql.c File Reference

QOF client-side SQL parser - interfaces with libgda. More...

#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <glib.h>
#include <libintl.h>
#include "sql_parser.h"
#include <time.h>
#include "qof.h"
#include "qofsql-p.h"
#include "qofquery-p.h"

Go to the source code of this file.

Macros

#define _(String)   dgettext (GETTEXT_PACKAGE, String)
 
#define QSQL_KVP_TABLE   "sql_kvp"
 
#define END_DB_VERSION   " dbversion int );"
 

Typedefs

typedef struct ent_and_string eas
 

Functions

QofSqlQuery * qof_sql_query_new (void)
 
void qof_sql_query_destroy (QofSqlQuery *q)
 
QofQueryqof_sql_query_get_query (QofSqlQuery *q)
 
void qof_sql_query_set_book (QofSqlQuery *q, QofBook *book)
 
void qof_sql_query_set_kvp (QofSqlQuery *q, KvpFrame *kvp)
 
static void get_table_and_param (char *str, char **tab, char **param)
 
static char * dequote_string (char *str)
 
static QofQueryhandle_single_condition (QofSqlQuery *query, sql_condition *cond)
 
static QofQueryhandle_where (QofSqlQuery *query, sql_where *swear)
 
static void handle_sort_order (QofSqlQuery *query, GList *sorder_list)
 
static void qof_sql_insertCB (const QofParam *param, const gchar *insert_string, QofSqlQuery *query)
 
static void qof_query_set_insert_table (QofSqlQuery *query)
 
static QofEntityqof_query_insert (QofSqlQuery *query)
 
static const char * sql_type_as_string (sql_statement_type type)
 
void qof_sql_query_parse (QofSqlQuery *query, const char *str)
 
GList * qof_sql_query_run (QofSqlQuery *query, const char *str)
 
GList * qof_sql_query_rerun (QofSqlQuery *query)
 
static void create_sql_from_param_cb (QofParam *param, gpointer user_data)
 
static gchar * string_param_to_sql (QofParam *param)
 
static void string_param_foreach (QofParam *param, gpointer user_data)
 
static void create_param_list (QofParam *param, gpointer user_data)
 list just the parameter names More...
 
static void kvpvalue_to_sql_insert (const gchar *key, KvpValue *val, gpointer user_data)
 
static void kvpvalue_to_sql_update (const gchar *key, KvpValue *val, gpointer user_data)
 
gchar * qof_sql_object_create_table (QofObject *obj)
 Build a SQL 'CREATE' statement for this object. More...
 
gchar * qof_sql_entity_create_table (QofEntity *ent)
 Build a SQL 'CREATE' statement for this entity. More...
 
gchar * qof_sql_entity_insert (QofEntity *ent)
 Build a SQL 'INSERT' statement for this entity. More...
 
static void collect_kvp (QofEntity *ent, gpointer user_data)
 
gchar * qof_sql_entity_update (QofEntity *ent)
 Build a SQL 'UPDATE' statement for the current entity parameter. More...
 
gchar * qof_sql_entity_update_kvp (QofEntity *ent)
 Build a SQL 'UPDATE' statement for the KVP data in this entity. More...
 
gchar * qof_sql_entity_update_list (QofEntity *ent, GList **params)
 Build a SQL 'UPDATE' statement for a list of parameters. More...
 
gchar * qof_sql_entity_delete (QofEntity *ent)
 Build a SQL 'DELETE' statement for this entity. More...
 
gchar * qof_sql_entity_drop_table (QofEntity *ent)
 Build a SQL 'DROP' statement for this entity type. More...
 
void qof_sql_entity_set_kvp_tablename (const gchar *name)
 Set a default KVP table name for each backend. More...
 
void qof_sql_entity_set_kvp_id (gulong id)
 Set the initial index value of the KVP table. More...
 
gulong qof_sql_entity_get_kvp_id (void)
 Get the index value of the KVP table after the operation(s). More...
 
void qof_sql_entity_set_kvp_exists (gboolean exist)
 Set or clear a flag that the KVP table exists or not. More...
 

Variables

static gchar * kvp_table_name = NULL
 
static QofLogModule log_module = "qof-query"
 
static gulong kvp_id = 0
 
static gboolean kvp_table_exists = FALSE
 

Detailed Description

QOF client-side SQL parser - interfaces with libgda.

Author
Copyright (C) 2004 Linas Vepstas linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g
Copyright 2008 Neil Williams linux.nosp@m.@cod.nosp@m.ehelp.nosp@m..co..nosp@m.uk

Intended to parse incoming SQL into QOF queries (SELECT or INSERT) and prepare SQL commands from QOF entities (CREATE, UPDATE, INSERT, DELETE and DROP) for use in SQL-based backends.

Definition in file qofsql.c.

Typedef Documentation

typedef struct ent_and_string eas

simple helper struct for passing between SQL-generating functions

Function Documentation

static void create_param_list ( QofParam param,
gpointer  user_data 
)
static

list just the parameter names

Note
Must match the number and order of the list of parameter values from ::create_each_param

Definition at line 1220 of file qofsql.c.

1221 {
1222  eas * data = (eas *)user_data;
1223  g_return_if_fail (data->str);
1224  /* avoid creating database fields for calculated values */
1225  if (!param->param_setfcn)
1226  return;
1227  /* avoid setting KVP even if a param_setfcn has been set
1228  because a QofSetterFunc for KVP is quite pointless. */
1229  if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
1230  {
1231  PINFO (" kvp support tag");
1232  return;
1233  }
1234  if (!g_str_has_suffix (data->str, "("))
1235  {
1236  gchar *add;
1237  add = g_strconcat (data->str, ", ", param->param_name, NULL);
1238  g_free (data->str);
1239  data->str = add;
1240  }
1241  else
1242  {
1243  gchar * add;
1244  add = g_strjoin ("", data->str, param->param_name, NULL);
1245  g_free (data->str);
1246  data->str = add;
1247  }
1248 }
#define PINFO(format, args...)
Definition: qoflog.h:199
gint safe_strcmp(const gchar *da, const gchar *db)
Definition: qofutil.c:75
struct ent_and_string eas
static void kvpvalue_to_sql_insert ( const gchar *  key,
KvpValue val,
gpointer  user_data 
)
static

returns the VALUES for INSERT in pre-defined order

Definition at line 1252 of file qofsql.c.

1253 {
1254  eas * data;
1255  KvpValueType n;
1256  gchar * path;
1257 
1258  path = g_strdup("");
1259  ENTER (" ");
1260  data = (eas*)user_data;
1261  g_return_if_fail (key && val && data);
1262  n = kvp_value_get_type (val);
1263  switch (n)
1264  {
1265  case KVP_TYPE_GINT64:
1266  case KVP_TYPE_DOUBLE:
1267  case KVP_TYPE_NUMERIC:
1268  case KVP_TYPE_STRING:
1269  case KVP_TYPE_GUID:
1270  case KVP_TYPE_TIME:
1271  case KVP_TYPE_BOOLEAN:
1272  {
1273  path = g_strjoin ("/", data->full_kvp_path, key, NULL);
1274  data->str =
1275  g_strdup_printf ("'%s', '%s', '%s'", key, path,
1276  kvp_value_to_bare_string (val));
1277  DEBUG (" %s", data->str);
1278  break;
1279  }
1280  case KVP_TYPE_FRAME:
1281  {
1282  path = g_strjoin ("/", data->full_kvp_path, key, NULL);
1283  g_free (data->full_kvp_path);
1284  data->full_kvp_path = path;
1286  kvpvalue_to_sql_insert, data);
1287  break;
1288  }
1289  default:
1290  {
1291  PERR (" unsupported value = %d", kvp_value_get_type (val));
1292  break;
1293  }
1294  }
1295  LEAVE (" %s", data->str);
1296 }
#define PERR(format, args...)
Definition: qoflog.h:183
KvpValueType
possible types in the union KvpValue
Definition: kvpframe.h:87
Unique identifier.
Definition: kvpframe.h:118
#define LEAVE(format, args...)
Definition: qoflog.h:227
128bit denominator/numerator maths.
Definition: kvpframe.h:106
64bit integer
Definition: kvpframe.h:94
#define DEBUG(format, args...)
Definition: qoflog.h:208
gchar * kvp_value_to_bare_string(const KvpValue *val)
General purpose function to convert any KvpValue to a string.
Definition: kvpframe.c:1834
void kvp_frame_for_each_slot(KvpFrame *f, KvpValueForeachCB proc, gpointer data)
Definition: kvpframe.c:1642
Simple boolean type.
Definition: kvpframe.h:136
standard C string
Definition: kvpframe.h:112
KvpFrame * kvp_value_get_frame(const KvpValue *value)
Definition: kvpframe.c:1546
standard C double type
Definition: kvpframe.h:100
struct ent_and_string eas
#define ENTER(format, args...)
Definition: qoflog.h:217
64bit time/date handling.
Definition: kvpframe.h:124
static void kvpvalue_to_sql_insert(const gchar *key, KvpValue *val, gpointer user_data)
Definition: qofsql.c:1252
static void kvpvalue_to_sql_update ( const gchar *  key,
KvpValue val,
gpointer  user_data 
)
static

returns the VALUES for UPDATE in pre-defined order

Definition at line 1300 of file qofsql.c.

1301 {
1302  eas * data;
1303  KvpValueType n;
1304  gchar * path;
1305 
1306  ENTER (" key=%s", key);
1307  data = (eas*)user_data;
1308  g_return_if_fail (key && val && data);
1309  n = kvp_value_get_type (val);
1310  switch (n)
1311  {
1312  case KVP_TYPE_GINT64:
1313  case KVP_TYPE_DOUBLE:
1314  case KVP_TYPE_NUMERIC:
1315  case KVP_TYPE_STRING:
1316  case KVP_TYPE_GUID:
1317  case KVP_TYPE_TIME:
1318  case KVP_TYPE_BOOLEAN:
1319  {
1320  path = g_strjoin ("/", data->full_kvp_path, key, NULL);
1321  data->str =
1322  g_strdup_printf ("type='%s', value='%s' WHERE path='%s' and ",
1323  key, kvp_value_to_bare_string (val), path);
1324  DEBUG (" %s", data->str);
1325  break;
1326  }
1327  case KVP_TYPE_FRAME:
1328  {
1329  path = g_strjoin ("/", data->full_kvp_path, key, NULL);
1330  g_free (data->full_kvp_path);
1331  data->full_kvp_path = path;
1333  kvpvalue_to_sql_update, data);
1334  break;
1335  }
1336  default:
1337  {
1338  PERR (" unsupported value = %d", kvp_value_get_type (val));
1339  break;
1340  }
1341  }
1342  LEAVE (" %s", data->str);
1343 }
#define PERR(format, args...)
Definition: qoflog.h:183
KvpValueType
possible types in the union KvpValue
Definition: kvpframe.h:87
Unique identifier.
Definition: kvpframe.h:118
static void kvpvalue_to_sql_update(const gchar *key, KvpValue *val, gpointer user_data)
Definition: qofsql.c:1300
#define LEAVE(format, args...)
Definition: qoflog.h:227
128bit denominator/numerator maths.
Definition: kvpframe.h:106
64bit integer
Definition: kvpframe.h:94
#define DEBUG(format, args...)
Definition: qoflog.h:208
gchar * kvp_value_to_bare_string(const KvpValue *val)
General purpose function to convert any KvpValue to a string.
Definition: kvpframe.c:1834
void kvp_frame_for_each_slot(KvpFrame *f, KvpValueForeachCB proc, gpointer data)
Definition: kvpframe.c:1642
Simple boolean type.
Definition: kvpframe.h:136
standard C string
Definition: kvpframe.h:112
KvpFrame * kvp_value_get_frame(const KvpValue *value)
Definition: kvpframe.c:1546
standard C double type
Definition: kvpframe.h:100
struct ent_and_string eas
#define ENTER(format, args...)
Definition: qoflog.h:217
64bit time/date handling.
Definition: kvpframe.h:124
static gchar* string_param_to_sql ( QofParam param)
static
Note
These strings are fairly standard SQL but SQL means different things to different programs. Keep these strings as simple and plain as possible. Hopefully, what works in SQLite0 will work in most other SQL based programs.
Bug:
This won't work - collect types can vary unpredictably - need a KVP implementation.

Definition at line 1134 of file qofsql.c.

1135 {
1142  /* Handle the entity GUID. Ensure that reference GUIDs
1143  must not also try to be primary keys and can be NULL. */
1144  if ((0 == safe_strcmp (param->param_type, QOF_TYPE_GUID)) &&
1145  (0 == safe_strcmp (param->param_name, QOF_PARAM_GUID)))
1146  return g_strdup_printf (" %s char(32) primary key not null",
1147  param->param_name);
1148  if (0 == safe_strcmp (param->param_type, QOF_TYPE_GUID))
1149  return g_strdup_printf (" %s char(32)", param->param_name);
1150  /* avoid creating database fields for calculated values */
1151  if (!param->param_setfcn)
1152  return NULL;
1153  if (0 == safe_strcmp (param->param_type, QOF_TYPE_STRING))
1154  return g_strdup_printf (" %s mediumtext", param->param_name);
1155  if (0 == safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
1156  return g_strdup_printf (" %s int", param->param_name);
1157  if ((0 == safe_strcmp (param->param_type, QOF_TYPE_NUMERIC))
1158  || (0 == safe_strcmp (param->param_type, QOF_TYPE_DOUBLE))
1159  || (0 == safe_strcmp (param->param_type, QOF_TYPE_DEBCRED)))
1160  {
1161  return g_strdup_printf (" %s text", param->param_name);
1162  }
1163  if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT32))
1164  return g_strdup_printf (" %s int", param->param_name);
1165  if (0 == safe_strcmp (param->param_type, QOF_TYPE_TIME))
1166  return g_strdup_printf (" %s datetime", param->param_name);
1167  if (0 == safe_strcmp (param->param_type, QOF_TYPE_CHAR))
1168  return g_strdup_printf (" %s char(1)", param->param_name);
1169  /* kvp data is stored separately - actually this is really
1170  a no-op because entities do not need a param_setfcn for kvp data. */
1171  if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
1172  return g_strdup ("");
1175  if (0 == safe_strcmp (param->param_type, QOF_TYPE_COLLECT))
1176  return g_strdup ("");
1177 // return g_strdup_printf (" %s char(32)", param->param_name);
1178  /* catch references */
1179  return g_strdup_printf (" %s char(32)", param->param_name);
1180 }
#define QOF_TYPE_COLLECT
secondary collections are used for one-to-many references between entities and are implemented using ...
Definition: qofclass.h:121
gint safe_strcmp(const gchar *da, const gchar *db)
Definition: qofutil.c:75

Variable Documentation

gchar* kvp_table_name = NULL
static

One KVP table per file for all instances.

Definition at line 51 of file qofsql.c.