summaryrefslogtreecommitdiff
path: root/ephy-query-history.c
blob: e9bd91466c434f740586d0744320ab2ec2cfcfd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  Copyright © 2009 Benjamin Otte <otte@gnome.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>
#include "ephy-history.h"

static gboolean quiet = FALSE;

static void
ephy_query_history_print_results (GObject *history, GAsyncResult *res, gpointer loop)
{
  GError *error = NULL;
  GSList *list;
  guint i;

  list = ephy_history_select_finish (EPHY_HISTORY (history), res, &error);

  if (list == NULL)
    g_main_loop_quit (loop);
  if (error)
    {
      g_print ("Error: %s\n", error->message);
      g_error_free (error);
      return;
    }

  if (!quiet)
    {
      for (; list; list = list->next)
        {
          GValueArray *array = list->data;
          for (i = 0; i < array->n_values; i++)
            {
              char *s = g_strdup_value_contents (&array->values[i]);
              g_print ("%s\t", s);
            }
          g_print ("\n");
        }
    }
}

int 
main (int argc, char *argv[])
{
  static char *filename = NULL;
  static GOptionEntry options[] = {
    { "filename", 'f', 0, G_OPTION_ARG_FILENAME, &filename, "file to use as history instead of default", "FILENAME" },
    { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "don't print output", NULL },
    { NULL }
  };
  GOptionContext *ctx;
  GError *error = NULL;
  EphyHistory *history;
  GMainLoop *loop;
  GTimer *timer;

  g_thread_init (NULL);
  g_type_init ();

  ctx = g_option_context_new ("");
  g_option_context_add_main_entries (ctx, options, "options");
  g_option_context_parse (ctx, &argc, &argv, &error);
  g_option_context_free (ctx);

  if (error) 
    {
      g_printerr ("Error parsing command line arguments: %s\n", error->message);
      g_error_free (error);
      return 1;
    }

  if (argc < 2) 
    {
      g_print ("usage: %s [OPTIONS] SQL\n", argv[0]);
      return 1;
    }

  history = ephy_history_new (filename);
  loop = g_main_loop_new (NULL, FALSE);
  
  timer = g_timer_new ();
  g_timer_start (timer);
  ephy_history_select_async (history,
                             50,
                             1000,
                             ephy_query_history_print_results,
                             loop,
                             NULL,
                             "%s",
                             argv[1]);

  g_main_loop_run (loop);
  g_print ("Query took %gs\n", g_timer_elapsed (timer, NULL));
  g_timer_destroy (timer);
  g_main_loop_unref (loop);
  g_object_unref (history);
  
  return 0;
}