gpe-expenses  0.1.9
Files | Data Structures | Macros | Typedefs | Functions | Variables
QOF Command Line Interface

Files

file  qof-main.c
 Common functions for the QOF external framework.
 
file  qof-main.h
 Common functions for the QOF external framework.
 

Data Structures

struct  QofMain_s
 The qof-main context struct. More...
 

Macros

#define _GNU_SOURCE
 
#define MAX_LINE   79
 
#define ERR_INDENT   strlen(PACKAGE) + 2
 Indent error messages as paragraphs.
 
#define QOF_DATE_STRING_LENGTH   MAX_DATE_LENGTH
 
#define QOF_MAIN_CLI   "QOF-mod-command-line"
 
#define CATEGORY_NAME   "category"
 Category name. More...
 
#define QSF_COMPRESS   "compression_level"
 
#define QSF_ENCODING   "encoding_string"
 
#define QSF_DATE_CONVERT   "convert_date_to_time"
 
#define QOF_SQL_SUPPORTED   "^SELECT|INSERT"
 
#define QOF_CLI_OPTIONS
 Common QOF CLI options. More...
 
#define QOF_MAIN_OP
 
#define QOF_OP_VARS
 
#define QOF_OP_INIT
 

Typedefs

typedef struct QofMain_s QofMainContext
 The qof-main context struct. More...
 

Functions

void qof_main_wrap_line (FILE *fp, gint indent, const gchar *template_str,...) __attribute__((format(printf
 Wrap long lines in popt-style. More...
 
static void qof_main_run_sql (QofMainContext *context)
 
static void qof_main_run_query (QofMainContext *context)
 
void qof_main_free (QofMainContext *context)
 
static void find_param_cb (QofParam *param, gpointer user_data)
 
static void build_database_list (QofIdTypeConst obj_type, QofMainContext *context)
 
static void select_cb (QofObject *obj, gpointer data)
 
void qof_main_moderate_query (QofMainContext *context)
 Assemble the components of the query. More...
 
static void option_cb (QofBackendOption *option, gpointer data)
 
void qof_mod_compression (gint64 gz_level, QofMainContext *context)
 
void qof_mod_encoding (const gchar *encoding, QofMainContext *context)
 
void qof_mod_convert_deprecated (gint64 convert, QofMainContext *context)
 
void qof_cmd_xmlfile (QofMainContext *context)
 
static void qof_main_list (QofObject *obj, gpointer G_GNUC_UNUSED data)
 
void qof_main_select (QofMainContext *context)
 
void qof_cmd_list (void)
 Lists all databases supported by the current QOF framework. More...
 
static void explain_cb (QofParam *param, gpointer G_GNUC_UNUSED user_data)
 
void qof_cmd_explain (QofMainContext *context)
 
void qof_mod_category (const gchar *category, QofMainContext *data)
 Shorthand to only query objects that are set to the specified category. More...
 
glong qof_mod_get_local_offset (void)
 Get the gmt_off offset for this locale. More...
 
void qof_mod_database (const gchar *database, QofMainContext *data)
 Shorthand to only query objects within one specific supported database. More...
 
void qof_mod_time (const gchar *date_time, QofMainContext *data)
 Shorthand to only query objects that contain the specified date. More...
 
void qof_mod_exclude (const gchar *exclude, QofMainContext *data)
 Shorthand to exclude a supported database from the query. More...
 
void qof_mod_sql (const gchar *sql_query, QofMainContext *data)
 Specify a SQL query on the command line. More...
 
void qof_mod_sql_file (const gchar *sql_file, QofMainContext *data)
 Specify one or more SQL queries contained in a file. More...
 
void qof_mod_write (const gchar *write_file, QofMainContext *data)
 Write the results of any query to the file. More...
 
void qof_main_show_error (QofSession *session)
 Output error messages from QOF. More...
 
GSListqof_main_get_param_list (QofIdTypeConst object_type, QofType param_type)
 List of all parameters for this object of one QOF type. More...
 

Variables

static QofLogModule log_module = "QOF-mod-command-line"
 

Detailed Description

Includes common functions for all QOF CLI programs and provides generic functions to implement command line and interactive shell options.

QOF provides an outline CLI that is easily patched from the qof-generator project to make it easier to keep various QOF projects updated.

This CLI is easily extended to support your own functions and options and includes macros to help you keep up to date with changes in main QOF options. It is recommended that you do not edit this file, instead please feed patches back to the QOF-devel mailing list at http://lists.sourceforge.net/mailman/listinfo/qof-devel so that other projects can be updated.

Macro Definition Documentation

#define CATEGORY_NAME   "category"

Category name.

The name of the parameter that holds the category of the entity.

Many CLI data sources categorise data by user-editable category strings. If your program does not, simply implement a modified QOF_CLI_OPTIONS in your code without the category option:

{"category", 'c', POPT_ARG_STRING, &category, qof_op_category,
_("Shorthand to only query objects that are set to the specified category."),
"string"},

Definition at line 118 of file qof-main.h.

#define QOF_CLI_OPTIONS

Common QOF CLI options.

These are definitions for popt support in the CLI. Every program's popt table should start with QOF_CLI_OPTIONS or a replacement to insert the standard options into it. Also enables autohelp. End your popt option list with POPT_TABLEEND. If you want to remove any of these options, simply copy QOF_CLI_OPTIONS into a macro of your own and remove the options you do not need.

Definition at line 419 of file qof-main.h.

#define QOF_DATE_STRING_LENGTH   MAX_DATE_LENGTH
Maximum length of the UTC timestamp used by QSF

QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ"

Definition at line 100 of file qof-main.h.

#define QOF_MAIN_CLI   "QOF-mod-command-line"

Debug module for qof-main

Definition at line 103 of file qof-main.h.

#define QOF_MAIN_OP
Value:
_(qof_op_noop, = 0) \
_(qof_op_list,) \
_(qof_op_xmlfile,) \
_(qof_op_category,) \
_(qof_op_database,) \
_(qof_op_time,) \
_(qof_op_exclude,) \
_(qof_op_sql,) \
_(qof_op_sql_file,) \
_(qof_op_write, ) \
_(qof_op_explain,) \
_(qof_op_vers,) \
_(qof_op_compress,) \
_(qof_op_debug,)

use only if you have no extended options, otherwise use as a template.

Definition at line 456 of file qof-main.h.

#define QOF_OP_INIT
Value:
category = NULL; \
database = NULL; \
sql_file = NULL; \
write_file = NULL; \
sql_query = NULL; \
filename = NULL;
initialise the standard QOF CLI option variables. 

A simple convenience macro.

Definition at line 485 of file qof-main.h.

#define QOF_OP_VARS
Value:
const gchar *date_time, *category, *database; \
const gchar *sql_file, *write_file, *sql_query, *filename;
Define the variables for the standard QOF CLI options.

If you remove any QOF CLI options, ensure you also remove the option variable and it's initialiser.

Definition at line 477 of file qof-main.h.

#define QOF_SQL_SUPPORTED   "^SELECT|INSERT"
 The SQL commands supported by QOF

A regular expression used to exclude unsupported commands from SQL files. Anything that does not match the expression will be silently ignored. This allows genuine SQL dump files to be parsed without errors.

A QOF object is similar to a definition of a SQL table.
A QOF entity is similar to an instance of a SQL record.
A QOF parameter is similar to data in a SQL field.

Certain SQL commands have no QOF equivalent and should always be ignored silently:

  • ALTER (the object parameters cannot be changed at runtime)
  • CREATE (new tables - new objects - cannot be created at runtime)
  • DROP (an object cannot be "de-registered" without re-compiling)
  • FLUSH (QOF has no permissions system)
  • GRANT
  • KILL
  • LOCK
  • OPTIMIZE
  • REVOKE
  • USE (QOF only has one database, itself.)

Definition at line 159 of file qof-main.h.

Referenced by qof_mod_sql_file().

#define QSF_COMPRESS   "compression_level"
backend configuration index string for QSF 

The identifier for the configuration option within QSF supported by the CLI. Matches the QofBackendOption->option_name in the KvpFrame holding the options.

Definition at line 127 of file qof-main.h.

#define QSF_DATE_CONVERT   "convert_date_to_time"

convert deprecated date fields into times.

Definition at line 133 of file qof-main.h.

#define QSF_ENCODING   "encoding_string"

backend configuration encoding string

Definition at line 130 of file qof-main.h.

Typedef Documentation

typedef struct QofMain_s QofMainContext

The qof-main context struct.

Intended as a core type for QOF-based CLI programs, wrap your own context struct around qof_main_context

Function Documentation

void qof_cmd_explain ( QofMainContext context)
Print a list of available parameters for a database.

Used with qof_mod_database to print a list of QofParam for the QofObject set in context->database.

Definition at line 444 of file qof-main.c.

References QofMain_s::database, and QofMain_s::error.

445 {
446  if (context->error)
447  return;
448  fprintf (stdout, _("\nParameters of the %s database:\n\n"),
449  context->database);
450  qof_class_param_foreach (context->database, explain_cb, NULL);
451  fprintf (stdout, _("\nThank you for using %s\n\n"), PACKAGE);
452 }
void qof_cmd_list ( void  )

Lists all databases supported by the current QOF framework.

Prints the name and description for each object type registered with this instance of QOF. No options are used.

Definition at line 416 of file qof-main.c.

References qof_main_wrap_line().

417 {
418  qof_main_wrap_line (stdout, 0,
419  /* Translators: This line is wrapped by the program -
420  please make sure you do NOT add line endings here. */
421  _("\n%s: You can use the supported database names with '%s -d' "
422  "and in SQL queries (as the table name) with '%s -s|f'. "
423  "Descriptions are shown only for readability.\n"),
424  PACKAGE, PACKAGE, PACKAGE);
425  fprintf (stdout, "%-20s%-20s\n", _("Name"), _("Description"));
426  qof_object_foreach_type (qof_main_list, NULL);
427  qof_main_wrap_line (stdout, 0,
428  /* Translators: This line is wrapped by the program -
429  please make sure you do NOT add line endings here. */
430  _("\nUse '%s -d <database> --explain' to see the list of fields "
431  "within any supported database."), PACKAGE);
432  fprintf (stdout, _("\nThank you for using %s\n\n"), PACKAGE);
433 }
void qof_cmd_xmlfile ( QofMainContext context)

query the QSF XML data

Definition at line 354 of file qof-main.c.

References QofMain_s::database, QofMain_s::encoding, ERR_INDENT, QofMain_s::exclude, QofMain_s::export_session, QofMain_s::filename, QofMain_s::gz_level, QofMain_s::input_session, qof_main_moderate_query(), qof_main_show_error(), qof_main_wrap_line(), qof_mod_compression(), qof_mod_encoding(), and QofMain_s::write_file.

355 {
356  QofSession *input_session, *export_session;
357 
358  ENTER (" ");
359  input_session = context->input_session;
360  if (0 == safe_strcmp (context->exclude, context->database)
361  && (context->exclude != NULL))
362  {
364  /* Translators: This line is wrapped by the program -
365  please make sure you do NOT add line endings here. */
366  _("%s: Error: Cannot exclude database \"%s\" with option -e "
367  "because option -d is set to include the database: \"%s\". "
368  "Use the \'-l\' command to see the full list of supported "
369  "databases.\n"), PACKAGE, context->exclude,
370  context->database);
371  qof_session_end (input_session);
372  LEAVE (" conflicting options");
373  return;
374  }
375  qof_session_begin (input_session, context->filename, TRUE, FALSE);
376  qof_main_show_error (input_session);
377  if (0 != safe_strcmp (QOF_STDOUT, context->filename))
378  qof_session_load (input_session, NULL);
379  qof_main_show_error (input_session);
380  export_session = qof_session_new ();
381  context->export_session = export_session;
382  if (context->write_file)
383  {
384  qof_session_begin (export_session, context->write_file, TRUE,
385  TRUE);
386  qof_mod_compression (context->gz_level, context);
387  }
388  else
389  qof_session_begin (export_session, QOF_STDOUT, TRUE, FALSE);
390  qof_main_show_error (export_session);
391  /* ensure encoding value is set in the new export_session */
392  qof_mod_encoding (context->encoding, context);
393  qof_main_moderate_query (context);
394  qof_session_save (export_session, NULL);
395  qof_main_show_error (export_session);
396  qof_main_show_error (input_session);
397  qof_session_end (input_session);
398  qof_session_end (export_session);
399  LEAVE (" ");
400 }
void qof_main_free ( QofMainContext context)

Free qof_main_context values when work is done.

Definition at line 136 of file qof-main.c.

References QofMain_s::category, QofMain_s::database, QofMain_s::filename, QofMain_s::sql_file, and QofMain_s::write_file.

Referenced by gpe_expense_close().

137 {
138  g_free (context->filename);
139  g_free (context->write_file);
140  g_free (context->sql_file);
141  g_free (context->database);
142  g_free (context->category);
143 }
GSList* qof_main_get_param_list ( QofIdTypeConst  object_type,
QofType  param_type 
)

List of all parameters for this object of one QOF type.

Return a GSList of all parameters of this object that are a particular QOF type, QOF_TYPE_STRING, QOF_TYPE_BOOLEAN etc.

The returned GSList should be freed by the caller.

Note
The return list is a singly linked list - GSList - not the doubly-linked list - GList - returned by ::qof_class_get_referenceList.
Parameters
object_typeobject->e_type for the relevant object.
param_typeThe type of parameter to match, QOF_TYPE_STRING etc.
Returns
GSList of all matching parameters or NULL if none exist.
void qof_main_moderate_query ( QofMainContext context)

Assemble the components of the query.

If any SQL statements are found, run separately from any -c, -d or -t options.

All queries are additive: Successive queries add more entities to the result set but no entity is set more than once.

Definition at line 224 of file qof-main.c.

References QofMain_s::database, QofMain_s::exclude, QofMain_s::query, QofMain_s::sql_list, and QofMain_s::sql_str.

Referenced by qof_cmd_xmlfile().

225 {
226  gboolean all;
227 
228  ENTER (" ");
229  all = TRUE;
230  context->query = qof_query_create ();
231  while (context->sql_list)
232  {
233  PINFO ("running sql_list");
234  context->sql_str = g_strdup (context->sql_list->data);
235  qof_main_run_sql (context);
236  qof_main_run_query (context);
237  if (context->query)
238  qof_query_clear (context->query);
239  g_free (context->sql_str);
240  context->sql_str = NULL;
241  all = FALSE;
242  context->sql_list = g_list_next (context->sql_list);
243  }
244  if (0 < g_list_length (context->sql_list))
245  {
246  context->sql_str = NULL;
247  g_list_free (context->sql_list);
248  all = FALSE;
249  }
250  if (context->sql_str != NULL)
251  {
252  PINFO ("running sql_str");
253  qof_main_run_sql (context);
254  qof_main_run_query (context);
255  if (context->query)
256  qof_query_clear (context->query);
257  all = FALSE;
258  }
259  if ((context->exclude != NULL)
260  && (qof_class_is_registered (context->exclude)))
261  {
262  qof_object_foreach_type (select_cb, context);
263  all = FALSE;
264  }
265  if ((context->database != NULL)
266  && (qof_class_is_registered (context->database)))
267  {
268  build_database_list (context->database, context);
269  all = FALSE;
270  }
271  if (all == TRUE)
272  qof_object_foreach_type (select_cb, context);
273  LEAVE (" ");
274 }
void qof_main_show_error ( QofSession *  session)

Output error messages from QOF.

QOF will set errors in the QofSession. The application determines how to output those messages and for the CLI, this will be to stderr. Not all these messages are implemented in any one QOF CLI.

Parameters
sessionAny current session.

Definition at line 596 of file qof-main.c.

References ERR_INDENT, and qof_main_wrap_line().

Referenced by qof_cmd_xmlfile().

597 {
598  const gchar *fmt;
599 
600  if (qof_error_check (session))
601  {
602  fmt = "%s: %s\n";
603  qof_main_wrap_line (stderr, ERR_INDENT, fmt, PACKAGE,
604  qof_error_get_message (session));
605  }
606 }
void qof_main_wrap_line ( FILE fp,
gint  indent,
const gchar *  template_str,
  ... 
)

Wrap long lines in popt-style.

Defaults to a line width of 79 characters. Indents lines following the first according to the value of indent.

Parameters
fpA file stream, including stdout or stderr.
indentThe number of characters to indent from the left. Values over or equal to 79 or less than zero are adjusted.
template_strString to be wrapped, containing positional parameters, as specified in the Single Unix Specification. Can be translated but should not contain newline characters except at the beginning or end of the message.
Note
Once wrapped, the line has a newline appended. Adding a newline to the incoming string will generate a blank line.

Definition at line 56 of file qof-main.c.

Referenced by qof_cmd_list(), qof_cmd_xmlfile(), qof_main_show_error(), and qof_mod_sql_file().

58 {
59  gint line_length, msg_length;
60  va_list wraps;
61  gchar *message;
62 
63  line_length = MAX_LINE;
64  /* note the modulus. Don't use CLAMP here */
65  /* indent != line_length or particularly close to it. */
66  indent = indent >= line_length ? indent % line_length : indent;
67  indent = indent < 0 ? 0 : indent;
68  message = NULL;
69  g_return_if_fail (template);
70  va_start (wraps, template);
71  message = g_strdup_vprintf (template, wraps);
72  va_end (wraps);
73  g_return_if_fail (message);
74  msg_length = strlen (message);
75  while (msg_length > line_length)
76  {
77  gchar *chunk;
78  gchar format[16];
79 
80  chunk = message + line_length - 1;
81  while (chunk > message && !g_ascii_isspace (*chunk))
82  chunk--;
83  if (chunk == message)
84  break; /* give up */
85  while (chunk > (message + 1) && g_ascii_isspace (*chunk))
86  chunk--;
87  chunk++;
88  g_sprintf (format, "%%.%ds\n%%%ds", (gint) (chunk - message),
89  indent);
90  g_fprintf (fp, format, message, "");
91  message = chunk;
92  while (g_ascii_isspace (*message) && *message)
93  message++;
94  msg_length = strlen (message);
95  if (line_length == MAX_LINE)
96  line_length -= indent;
97  }
98  if (msg_length)
99  g_fprintf (fp, "%s\n", message);
100 }
void qof_mod_category ( const gchar *  category,
QofMainContext data 
)

Shorthand to only query objects that are set to the specified category.

Modifies the QOF query to only query objects that are set to category.

Definition at line 455 of file qof-main.c.

References QofMain_s::category.

456 {
457  data->category = g_strdup (category);
458 }
void qof_mod_compression ( gint64  gz_level,
QofMainContext context 
)
Pass the requested compression to QSF
Parameters
gz_levelInteger between 0 and 9, 9 highest compression, 0 for none.
contextThe QofMain context.

Definition at line 298 of file qof-main.c.

References QofMain_s::export_session, and QofMain_s::gz_level.

Referenced by qof_cmd_xmlfile().

299 {
300  KvpFrame *be_config;
301  QofBook *book;
302  QofBackend *be;
303 
304  ENTER (" compression=%" G_GINT64_FORMAT, gz_level);
305  if ((gz_level > 0) && (gz_level <= 9))
306  {
307  book = qof_session_get_book (context->export_session);
308  be = qof_book_get_backend (book);
309  be_config = qof_backend_get_config (be);
310  context->gz_level = gz_level;
311  qof_backend_option_foreach (be_config, option_cb, context);
312  qof_backend_load_config (be, be_config);
313  }
314  LEAVE (" ");
315 }
void qof_mod_convert_deprecated ( gint64  convert,
QofMainContext context 
)

configure handling of deprecated date fields.

Definition at line 335 of file qof-main.c.

References QofMain_s::convert, and QofMain_s::export_session.

336 {
337  KvpFrame *be_config;
338  QofBook *book;
339  QofBackend *be;
340  gboolean set;
341 
342  set = (convert == 0) ? FALSE : TRUE;
343  ENTER (" convert deprecated date values? %i No if 0.", set);
344  book = qof_session_get_book (context->export_session);
345  be = qof_book_get_backend (book);
346  be_config = qof_backend_get_config (be);
347  context->convert = convert;
348  qof_backend_option_foreach (be_config, option_cb, context);
349  qof_backend_load_config (be, be_config);
350  LEAVE (" ");
351 }
void qof_mod_database ( const gchar *  database,
QofMainContext data 
)

Shorthand to only query objects within one specific supported database.

Used to only query objects within the specified database.

Definition at line 475 of file qof-main.c.

References QofMain_s::database.

476 {
477  if (qof_class_is_registered (database))
478  data->database = g_strdup (database);
479 }
void qof_mod_encoding ( const gchar *  encoding,
QofMainContext context 
)

Pass an encoding string to the backend.

Definition at line 318 of file qof-main.c.

References QofMain_s::encoding, and QofMain_s::export_session.

Referenced by qof_cmd_xmlfile().

319 {
320  KvpFrame *be_config;
321  QofBook *book;
322  QofBackend *be;
323 
324  ENTER (" encode to %s", encoding);
325  book = qof_session_get_book (context->export_session);
326  be = qof_book_get_backend (book);
327  be_config = qof_backend_get_config (be);
328  context->encoding = encoding;
329  qof_backend_option_foreach (be_config, option_cb, context);
330  qof_backend_load_config (be, be_config);
331  LEAVE (" ");
332 }
void qof_mod_exclude ( const gchar *  exclude,
QofMainContext data 
)

Shorthand to exclude a supported database from the query.

Excludes the (single) specified database from the query.

Definition at line 528 of file qof-main.c.

References QofMain_s::exclude.

529 {
530  if (qof_class_is_registered (exclude))
531  data->exclude = g_strdup (exclude);
532 }
glong qof_mod_get_local_offset ( void  )

Get the gmt_off offset for this locale.

User specified strings can be assumed to be in localtime, but values are stored as UTC. This offset allows the application to modify the minimum and maximum time settings for queries so that the user gets the expected results.

If the timezone is +0100 and the date is 24th July 2006, values could be stored as 11pm on 23rd July 2006 UTC. qof_mod_get_local_offset returns -86400. Add this value to the minimum and maximum time passed to the query to ensure queries select expected timeframe: 2006-07-23T23:00:00Z to 2006-07-24T22:59:59Z.

Returns
the number of seconds difference between localtime and UTC.

Definition at line 461 of file qof-main.c.

Referenced by qof_mod_time().

462 {
463  glong local_offset;
464  struct tm local;
465  time_t now;
466 
467  local_offset = 0; /* UTC */
468  now = time (NULL);
469  local = *localtime_r (&now, &local);
470  local_offset -= local.tm_gmtoff;
471  return local_offset;
472 }
void qof_mod_sql ( const gchar *  sql_query,
QofMainContext data 
)

Specify a SQL query on the command line.

For SELECT, the returned list is a list of all of the instances of 'SomeObj' that match the query. The 'SORT' term is optional. The 'WHERE' term is optional; but if you don't include 'WHERE', you will get a list of all of the object instances. The Boolean operations 'AND' and 'OR' together with parenthesis can be used to construct arbitrarily nested predicates.

For INSERT, the returned list is a list containing the newly created instance of 'SomeObj'.

Date queries handle full date and time strings, using the format exported by the QSF backend. To query dates and times, convert user input into UTC time using qof_date_print and QOF_DATE_FORMAT_UTC

If the param is a KVP frame, then we use a special markup to indicate frame values. The markup should look like /some/kvp/path:value. Thus, for example,
SELECT * FROM SomeObj WHERE (param_a < '/some/kvp:10.0')
will search for the object where param_a is a KVP frame, and this KVP frame contains a path '/some/kvp' and the value stored at that path is floating-point and that float value is less than 10.0.

Parameters
sql_queryExamples:

"select * from pilot_address"

"select * from pilot_expenses where type_of_expense = 'Mileage';"

"SELECT * from pilot_datebook where start_time > '2004-04-06T00:00Z' and end_time < '2005-04-05T23:59:59Z';"

"insert into pilot_todo (description, date_due, todo_priority) values ('put the cat out', '2005-11-24T21:30:00Z', 1)"

Parameters
dataThe QofMain context.

Definition at line 535 of file qof-main.c.

References QofMain_s::sql_str.

536 {
537  data->sql_str = g_strdup (sql_query);
538 }
void qof_mod_sql_file ( const gchar *  sql_file,
QofMainContext data 
)

Specify one or more SQL queries contained in a file.

The rules for single SQL commands also apply with regard to the lack of explicit support for joins and the pending support for selecting only certain parameters from a certain object.

See qof_mod_sql for information on the queries supported.

Definition at line 541 of file qof-main.c.

References ERR_INDENT, qof_main_wrap_line(), QOF_SQL_SUPPORTED, QofMain_s::sql_file, and QofMain_s::sql_list.

542 {
543  FILE *filehandle;
544 #ifndef HAVE_GETLINE
545  gchar lineptr[1024];
546 #else
547  gchar *lineptr;
548 #endif
549  gchar *buf;
550  size_t n;
551  regex_t *r;
552  gint reg_exp_check;
553  const gchar *fmt;
554  static gchar *pattern = QOF_SQL_SUPPORTED;
555 
556  ENTER (" ");
557  data->sql_file = g_strdup (sql_file);
558  n = 0;
559  data->sql_list = NULL;
560  filehandle = fopen (sql_file, "r");
561  if (!filehandle)
562  {
563  fmt = _("%s: There was an error reading the file '%s'.\n");
564  qof_main_wrap_line (stderr, ERR_INDENT, fmt, PACKAGE, sql_file);
565  return;
566  }
567  r = g_new (regex_t, 1);
568 #ifndef HAVE_GETLINE
569  while (NULL != (fgets (lineptr, sizeof (lineptr), filehandle)))
570 #else
571  lineptr = NULL;
572  while (0 < getline (&lineptr, &n, filehandle))
573 #endif
574  {
575  reg_exp_check =
576  regcomp (r, pattern, REG_ICASE | REG_NOSUB | REG_EXTENDED);
577  g_return_if_fail (reg_exp_check == 0);
578  if (0 != regexec (r, lineptr, 0, NULL, 0))
579  continue;
580  buf = g_strdup (g_strchomp (lineptr));
581  data->sql_list = g_list_prepend (data->sql_list, buf);
582  }
583  regfree (r);
584  g_free (r);
585  fclose (filehandle);
586  LEAVE (" sql_list=%d", g_list_length (data->sql_list));
587 }
void qof_mod_time ( const gchar *  date_time,
QofMainContext data 
)

Shorthand to only query objects that contain the specified date.

Used to modify the QOF query to only query objects that contain at least one parameter containing a QOF_TYPE_TIME that matches the range specified. Dates need to be specified as YY-MM-DD, i.e. QOF_DATE_FORMAT_ISO.

You can specify a UTC timestring, just as normally output by QSF, but the time will not be matched when using the shorthand option, only the year, month and day.

For more precise time matches or to set a defined period that doesn't follow whole calendar months, (e.g. the UK financial year) use a SQL statement:

"SELECT * from pilot_datebook where start_time > '2004-04-06T00:00Z'\n and end_time < '2005-04-05T23:59:59Z';"

Partial matches are allowed, so YY-MM matches any object where a date is within the specified month and year, YY matches any object where a date is within the specified year.

The query range starts at midnight on the first day of the range and ends at 1 second to midnight on the last day of the range.

Definition at line 482 of file qof-main.c.

References QofMain_s::max_qt, QofMain_s::min_qt, and qof_mod_get_local_offset().

483 {
484  QofDate *qd;
485  gint adding_days;
486  gchar *info;
487 
488  /* incoming date is assumed to be localtime */
489  ENTER (" date_time=%s", date_time);
490  g_return_if_fail (date_time);
491  qd = qof_date_parse (date_time, QOF_DATE_FORMAT_ISO);
492  if (!qd)
493  qd = qof_date_parse (date_time, QOF_DATE_FORMAT_UTC);
494  info = qof_date_print (qd, QOF_DATE_FORMAT_ISO8601);
495  PINFO (" parsed start_time=%s", info);
496  g_free (info);
497  /* set first second of day, UTC */
498  qof_date_set_day_start (qd);
499  data->min_qt = qof_date_to_qtime (qd);
500  /* adjust for incoming localtime */
501  qof_time_add_secs (data->min_qt,
503  /* year specified but no month or day, select the entire year */
504  if (strlen (date_time) == 4)
505  {
506  PINFO (" match entire year %s", date_time);
507  /* go to end of this year, not first day of next. */
508  adding_days = qof_date_isleap(qd->qd_year) ? 365 : 364;
509  qof_date_adddays (qd, adding_days);
510  }
511  /* month specified, but no day, select entire month */
512  if (strlen (date_time) == 7)
513  {
514  PINFO (" match entire month %s", date_time);
515  adding_days = qof_date_get_mday (qd->qd_mon, qd->qd_year);
516  qof_date_adddays (qd, adding_days - 1);
517  }
518  /* set last second of day */
519  qof_date_set_day_end (qd);
520  data->max_qt = qof_date_to_qtime (qd);
521  /* adjust for incoming localtime */
522  qof_time_add_secs (data->max_qt,
524  LEAVE (" ");
525 }
void qof_mod_write ( const gchar *  write_file,
QofMainContext data 
)

Write the results of any query to the file.

filename of the file to be written out.

Definition at line 590 of file qof-main.c.

References QofMain_s::write_file.

591 {
592  data->write_file = g_strdup (write_file);
593 }