41 #include <glib/gi18n.h>
42 #include <glib/gprintf.h>
57 const gchar *
template, ...)
59 gint line_length, msg_length;
63 line_length = MAX_LINE;
66 indent = indent >= line_length ? indent % line_length : indent;
67 indent = indent < 0 ? 0 : indent;
69 g_return_if_fail (
template);
70 va_start (wraps,
template);
71 message = g_strdup_vprintf (
template, wraps);
73 g_return_if_fail (message);
74 msg_length = strlen (message);
75 while (msg_length > line_length)
80 chunk = message + line_length - 1;
81 while (chunk > message && !g_ascii_isspace (*chunk))
85 while (chunk > (message + 1) && g_ascii_isspace (*chunk))
88 g_sprintf (format,
"%%.%ds\n%%%ds", (gint) (chunk - message),
90 g_fprintf (fp, format, message,
"");
92 while (g_ascii_isspace (*message) && *message)
94 msg_length = strlen (message);
95 if (line_length == MAX_LINE)
96 line_length -= indent;
99 g_fprintf (fp,
"%s\n", message);
110 q = qof_sql_query_new ();
111 sql = g_strdup (context->
sql_str);
113 qof_sql_query_set_book (q, book);
114 qof_sql_query_run (q, sql);
115 context->
query = qof_sql_query_get_query (q);
128 qof_query_set_book (context->
query, book);
129 results = qof_query_run (context->
query);
146 find_param_cb (
QofParam * param, gpointer user_data)
153 if ((param->param_getfcn == NULL) ||
154 (param->param_setfcn == NULL))
156 if (0 == safe_strcmp (context->
param_type, param->param_type))
158 time_pred_data = qof_query_time_predicate (QOF_COMPARE_GTE,
159 QOF_DATE_MATCH_NORMAL, context->
min_qt);
160 tmp = g_strdup (param->param_name);
161 qof_query_add_term (context->
query,
162 qof_query_build_param_list (tmp, NULL), time_pred_data, QOF_QUERY_AND);
163 time_pred_data = qof_query_time_predicate (QOF_COMPARE_LTE,
164 QOF_DATE_MATCH_NORMAL, context->
max_qt);
165 qof_query_add_term (context->
query,
166 qof_query_build_param_list (tmp, NULL), time_pred_data, QOF_QUERY_AND);
167 qof_main_run_query (context);
168 qof_query_purge_terms (context->
query,
169 qof_query_build_param_list (tmp, QOF_ID_BOOK, QOF_TYPE_GUID, NULL));
170 PINFO (
" param_name=%s added", tmp);
177 build_database_list (QofIdTypeConst obj_type,
QofMainContext * context)
179 if (!obj_type || !context)
181 if (!qof_class_is_registered (obj_type))
183 ENTER (
" object_type=%s", obj_type);
184 context->
query = qof_query_create_for (obj_type);
190 qof_query_string_predicate (QOF_COMPARE_EQUAL,
191 context->
category, QOF_STRING_MATCH_CASEINSENSITIVE,
193 qof_query_add_term (context->
query,
195 category_pred, QOF_QUERY_AND);
199 PINFO (
" Preparing a time based queryset.");
201 qof_class_param_foreach (obj_type, find_param_cb, context);
205 qof_main_run_query (context);
207 qof_query_clear (context->
query);
213 select_cb (
QofObject * obj, gpointer data)
218 g_return_if_fail (context);
219 if (0 != safe_strcmp (context->
exclude, obj->e_type))
220 build_database_list (obj->e_type, context);
230 context->
query = qof_query_create ();
233 PINFO (
"running sql_list");
235 qof_main_run_sql (context);
236 qof_main_run_query (context);
238 qof_query_clear (context->
query);
244 if (0 < g_list_length (context->
sql_list))
252 PINFO (
"running sql_str");
253 qof_main_run_sql (context);
254 qof_main_run_query (context);
256 qof_query_clear (context->
query);
260 && (qof_class_is_registered (context->
exclude)))
262 qof_object_foreach_type (select_cb, context);
266 && (qof_class_is_registered (context->
database)))
268 build_database_list (context->
database, context);
272 qof_object_foreach_type (select_cb, context);
282 g_return_if_fail (context);
286 ENTER (
" compression=%" G_GINT64_FORMAT
" encoding=%s",
288 if (0 == safe_strcmp (
QSF_COMPRESS, option->option_name))
289 option->value = (gpointer) & context->
gz_level;
290 if (0 == safe_strcmp (
QSF_ENCODING, option->option_name))
291 option->value = (gpointer) g_strdup(context->
encoding);
293 option->value = (gpointer) & context->
convert;
304 ENTER (
" compression=%" G_GINT64_FORMAT, gz_level);
305 if ((gz_level > 0) && (gz_level <= 9))
308 be = qof_book_get_backend (book);
309 be_config = qof_backend_get_config (be);
311 qof_backend_option_foreach (be_config, option_cb, context);
312 qof_backend_load_config (be, be_config);
324 ENTER (
" encode to %s", encoding);
326 be = qof_book_get_backend (book);
327 be_config = qof_backend_get_config (be);
329 qof_backend_option_foreach (be_config, option_cb, context);
330 qof_backend_load_config (be, be_config);
342 set = (convert == 0) ? FALSE : TRUE;
343 ENTER (
" convert deprecated date values? %i No if 0.",
set);
345 be = qof_book_get_backend (book);
346 be_config = qof_backend_get_config (be);
348 qof_backend_option_foreach (be_config, option_cb, context);
349 qof_backend_load_config (be, be_config);
356 QofSession *input_session, *export_session;
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,
371 qof_session_end (input_session);
372 LEAVE (
" conflicting options");
375 qof_session_begin (input_session, context->
filename, TRUE, FALSE);
377 if (0 != safe_strcmp (QOF_STDOUT, context->
filename))
378 qof_session_load (input_session, NULL);
380 export_session = qof_session_new ();
384 qof_session_begin (export_session, context->
write_file, TRUE,
389 qof_session_begin (export_session, QOF_STDOUT, TRUE, FALSE);
394 qof_session_save (export_session, NULL);
397 qof_session_end (input_session);
398 qof_session_end (export_session);
403 qof_main_list (
QofObject * obj, gpointer G_GNUC_UNUSED data)
405 fprintf (stdout,
"%-20s%-20s\n", obj->e_type, obj->type_label);
411 g_return_if_fail (context);
412 qof_object_foreach_type (select_cb, context);
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);
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);
436 explain_cb (
QofParam * param, gpointer G_GNUC_UNUSED user_data)
438 if (param->param_getfcn && param->param_setfcn)
439 fprintf (stdout, _(
"Type: %s\tName: %s\n"),
440 param->param_type, param->param_name);
448 fprintf (stdout, _(
"\nParameters of the %s database:\n\n"),
450 qof_class_param_foreach (context->
database, explain_cb, NULL);
451 fprintf (stdout, _(
"\nThank you for using %s\n\n"), PACKAGE);
457 data->
category = g_strdup (category);
469 local = *localtime_r (&now, &local);
470 local_offset -= local.tm_gmtoff;
477 if (qof_class_is_registered (database))
478 data->
database = g_strdup (database);
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);
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);
498 qof_date_set_day_start (qd);
499 data->
min_qt = qof_date_to_qtime (qd);
501 qof_time_add_secs (data->
min_qt,
504 if (strlen (date_time) == 4)
506 PINFO (
" match entire year %s", date_time);
508 adding_days = qof_date_isleap(qd->qd_year) ? 365 : 364;
509 qof_date_adddays (qd, adding_days);
512 if (strlen (date_time) == 7)
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);
519 qof_date_set_day_end (qd);
520 data->
max_qt = qof_date_to_qtime (qd);
522 qof_time_add_secs (data->
max_qt,
530 if (qof_class_is_registered (exclude))
531 data->
exclude = g_strdup (exclude);
537 data->
sql_str = g_strdup (sql_query);
557 data->
sql_file = g_strdup (sql_file);
560 filehandle = fopen (sql_file,
"r");
563 fmt = _(
"%s: There was an error reading the file '%s'.\n");
567 r = g_new (regex_t, 1);
569 while (NULL != (fgets (lineptr,
sizeof (lineptr), filehandle)))
572 while (0 < getline (&lineptr, &n, filehandle))
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))
580 buf = g_strdup (g_strchomp (lineptr));
586 LEAVE (
" sql_list=%d", g_list_length (data->
sql_list));
600 if (qof_error_check (session))
604 qof_error_get_message (session));