1 /*
2  * Copyright (c) 2017-2018 Nikola Kolev <koue@chaosophia.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *    - Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    - Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 #include "cez_fossil.h"
32 #include "cez_misc.h"
33 
34 Global g;
35 
36 const char TestSchema[] =
37 "CREATE TABLE tbl_test("
38 " id INTEGER PRIMARY KEY,"
39 " name TEXT"
40 ");";
41 
42 const char *dbname = "/tmp/testme-xzfadget48.db";
43 const char *sqltracefile = "/tmp/testme-4trwerfsdfrf89.log";
44 
45 Stmt q;
46 
main(void)47 int main(void){
48   char command[256];
49 
50   test_start();
51 
52   /* create database */
53   db_init_database(dbname, TestSchema, (char*)0);
54   test_ok("init database");
55   test_ok("create schema");
56 
57   /* open database */
58   if (sqlite3_open(dbname, &g.db) != SQLITE_OK) {
59     fprintf(stderr, "Cannot open database file: %s\n", dbname);
60     return (1);
61   }
62   sqlite3_trace_v2(g.db, SQLITE_TRACE_STMT, db_sql_trace, 0);
63   if ((g.sqltrace = fopen(sqltracefile, "w+")) == NULL) {
64     fprintf(stderr, "Cannot open sql trace file: %s\n", sqltracefile);
65     sqlite3_close(g.db);
66     return (1);
67   }
68 
69   /* insert record */
70   db_multi_exec("INSERT INTO tbl_test(name) VALUES (%Q)", "testuser0");
71   db_multi_exec("INSERT INTO tbl_test(name) VALUES (%Q)", "testuser1");
72   db_multi_exec("INSERT INTO tbl_test(name) VALUES (%Q)", "testuser2");
73   db_multi_exec("INSERT INTO tbl_test(name) VALUES (%Q)", "testuser3");
74   test_ok("insert");
75   /* get text */
76   char *zDb_text = db_text(0, "SELECT name FROM tbl_test "
77 				"WHERE name='testuser1'");
78   printf("%20d, %s\n", 0, zDb_text);
79   free(zDb_text);
80   test_ok("db_text");
81   /* get int */
82   printf("%20d\n", db_int(0, "SELECT id FROM tbl_test WHERE name='testuser2'"));
83   test_ok("db_int");
84   /* multiple results */
85   db_prepare(&q, "SELECT id, name FROM tbl_test");
86   test_ok("db_prepare");
87   while(db_step(&q)==SQLITE_ROW){
88     printf("%20d, %s\n", db_column_int(&q, 0), db_column_text(&q, 1));
89   }
90   db_finalize(&q);
91   test_ok("db_step");
92   test_ok("db_finalize");
93 
94   sqlite3_close(g.db);
95   snprintf(command, sizeof(command), "rm %s", dbname);
96   system(command);
97   fclose(g.sqltrace);
98   snprintf(command, sizeof(command), "echo && cat %s", sqltracefile);
99   system(command);
100   snprintf(command, sizeof(command), "rm %s", sqltracefile);
101   system(command);
102 
103   printf("\n");
104   test_ok("sql_trace");
105 
106   test_succeed();
107 
108   test_end();
109 
110   return (0);
111 }
112