1 #include <glib-object.h>
2 #include <glib.h>
3 #include <gio/gio.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sqlite3.h>
7 
8 #define HASH_VALUES_FILE "../data/hash_values.log"
9 #define DB_FILE "example_db.db"
10 
11 GQueue *values_queue;
12 
13 static void
create_table(sqlite3 * db)14 create_table (sqlite3 *db)
15 {
16 	gchar *sql = "CREATE TABLE sym_type (type_id integer PRIMARY KEY AUTOINCREMENT,"
17                    "type_type text not null,"
18                    "type_name text not null,"
19                    "unique (type_type, type_name))"	;
20 
21 	sqlite3_exec(db, sql, NULL, 0, NULL);
22 
23 }
24 
25 static void
delete_previous_db(void)26 delete_previous_db (void)
27 {
28 	GFile *file;
29 
30 	g_message ("deleting file "DB_FILE"...");
31 
32 	file = g_file_new_for_path (DB_FILE);
33 
34 	g_file_delete (file, NULL, NULL);
35 
36 	g_object_unref (file);
37 
38 	g_message ("..OK");
39 }
40 
41 static gint
open_connection(sqlite3 ** db)42 open_connection (sqlite3 **db)
43 {
44 	gint rc = 0;
45   	sqlite3_open (DB_FILE, &(*db));
46 
47   	if (rc)
48 	{
49     	g_message ("Can't open database: %s\n", sqlite3_errmsg(*db));
50     	sqlite3_close(*db);
51     	return -1;
52   	}
53 	return 0;
54 }
55 
56 static void
load_queue_values(void)57 load_queue_values (void)
58 {
59 	gchar line[80];
60 	values_queue = g_queue_new ();
61 
62 	FILE *file = fopen (HASH_VALUES_FILE, "r");
63 
64 	while( fgets(line,sizeof(line),file) )
65 	{
66 		/*g_message ("got %s", line);*/
67 		g_queue_push_tail (values_queue, g_strdup (line));
68 	}
69 
70 	fclose (file);
71 }
72 
73 static void
insert_data(sqlite3 * db)74 insert_data (sqlite3 *db)
75 {
76 	sqlite3_stmt *stmt;
77 	gint i;
78 	gdouble elapsed_DEBUG;
79 	GTimer *sym_timer_DEBUG  = g_timer_new ();
80 
81 	gchar *sql_str = "INSERT INTO sym_type (type_type, type_name) VALUES (?, ?)";
82 
83 	gint queue_length = g_queue_get_length (values_queue);
84 
85 	g_message ("begin transaction...");
86 	sqlite3_exec(db, "BEGIN", 0, 0, 0);
87 	g_message ("..OK");
88 
89 	g_message ("populating transaction..");
90 	for (i = 0; i < queue_length; i++)
91 	{
92 		gchar * value = g_queue_pop_head (values_queue);
93 		gchar **tokens = g_strsplit (value, "|", 2);
94 
95 		if ( sqlite3_prepare(db,
96 				sql_str,  // stmt
97 				-1, // If than zero, then stmt is read up to the first nul terminator
98 				&stmt,
99 				0  /* Pointer to unused portion of stmt */
100 		    ) != SQLITE_OK)
101 		{
102 			printf("\nCould not prepare statement.");
103 			return;
104 		}
105 
106 		if (sqlite3_bind_text(
107 			stmt,
108 			1,  /* Index of wildcard */
109 			tokens[0],
110 			strlen (tokens[0]),
111 			SQLITE_STATIC) != SQLITE_OK)
112 		{
113 			printf("\nCould not bind int.\n");
114 			return;
115 		}
116 
117 		if (sqlite3_bind_text(
118 			stmt,
119 			2,  /* Index of wildcard */
120 			tokens[1],
121 			strlen (tokens[1]),
122 			SQLITE_STATIC) != SQLITE_OK)
123 		{
124 			printf("\nCould not bind int.\n");
125 			return;
126 		}
127 
128 		if (sqlite3_step(stmt) != SQLITE_DONE) {
129 			printf("\nCould not step (execute) stmt.\n");
130 			return;
131 		}
132 
133 		g_strfreev(tokens);
134 
135 		sqlite3_reset(stmt);
136 	}
137 	elapsed_DEBUG = g_timer_elapsed (sym_timer_DEBUG, NULL);
138 	g_message ("..OK (elapsed %f)", elapsed_DEBUG);
139 
140 	g_message ("committing...");
141 
142 	sqlite3_exec(db, "COMMIT", 0, 0, 0);
143 
144 	elapsed_DEBUG = g_timer_elapsed (sym_timer_DEBUG, NULL);
145 	g_message ("..OK (elapsed %f)", elapsed_DEBUG);
146 }
147 
148 gint
main(gint argc,gchar ** argv)149 main(gint argc, gchar **argv)
150 {
151 	sqlite3 *db = NULL;
152 
153 	g_type_init();
154 
155 	delete_previous_db ();
156 
157 	if (open_connection (&db) < 0)
158 		return -1;
159 
160 	create_table (db);
161 
162 	/* load        $ wc -l hash_values.log
163 	 * 20959 hash_values.log
164 	 * into our queue.
165 	 */
166 	load_queue_values ();
167 
168 
169 	insert_data (db);
170 
171 
172   	sqlite3_close(db);
173   	return 0;
174 }
175