| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2017 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBLIITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include <stdio.h> |
| |
30 #include <sqlite3.h> |
| |
31 |
| |
32 #include <dbutils/dbutils.h> |
| |
33 #include <dbutils/sqlite.h> |
| |
34 |
| |
35 const char *sql_create_table_person = |
| |
36 "create table if not exists Person (" |
| |
37 "person_id integer primary key autoincrement, " |
| |
38 "name text, " |
| |
39 "email text, " |
| |
40 "age integer, " |
| |
41 "iscustomer integer , " |
| |
42 "hash integer);"; |
| |
43 |
| |
44 const char *sql_check_table = |
| |
45 "select person_id from Person limit 1;"; |
| |
46 |
| |
47 const char *sql_create_test_data = |
| |
48 "insert into person (name, email, age, iscustomer, hash) " |
| |
49 "values " |
| |
50 "('alice', 'alice@example.com', 30, 1, 123456789), " |
| |
51 "('bob', 'bob@example.com', 25, 0, 987654321);"; |
| |
52 |
| |
53 typedef struct Person { |
| |
54 int64_t person_id; |
| |
55 |
| |
56 cxmutstr name; |
| |
57 cxmutstr email; |
| |
58 int age; |
| |
59 bool iscustomer; |
| |
60 uint64_t hash; |
| |
61 } Person; |
| |
62 |
| |
63 static int create_test_data(sqlite3 *db); |
| |
64 |
| |
65 int main(int argc, char **argv) { |
| |
66 |
| |
67 |
| |
68 DBUContext *ctx = dbuContextCreate(); |
| |
69 |
| |
70 DBUClass *person = dbuRegisterClass(ctx, "person", Person, person_id); |
| |
71 dbuClassAdd(person, Person, name); |
| |
72 dbuClassAdd(person, Person, email); |
| |
73 dbuClassAdd(person, Person, age); |
| |
74 dbuClassAdd(person, Person, iscustomer); |
| |
75 dbuClassAdd(person, Person, hash); |
| |
76 |
| |
77 |
| |
78 |
| |
79 // Open or create the database |
| |
80 sqlite3 *db; |
| |
81 int rc = sqlite3_open("test.db", &db); |
| |
82 if(rc != SQLITE_OK) { |
| |
83 fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); |
| |
84 sqlite3_close(db); |
| |
85 return 1; |
| |
86 } |
| |
87 |
| |
88 if(create_test_data(db)) { |
| |
89 return 1; |
| |
90 } |
| |
91 |
| |
92 CxList *persons = dbuSQLiteQuerySingleTable(ctx, db, "person", "select * from Person;"); |
| |
93 if(persons) { |
| |
94 CxIterator i = cxListIterator(persons); |
| |
95 cx_foreach(Person *, p, i) { |
| |
96 printf("{ person_id = %" PRId64 ", name = \"%s\", email = \"%s\", age = %d, iscustomer = %s, hash = %" PRIu64 " }\n", |
| |
97 p->person_id, p->name.ptr, p->email.ptr, p->age, p->iscustomer ? "true" : "false", p->hash); |
| |
98 } |
| |
99 } else { |
| |
100 fprintf(stderr, "Error\n"); |
| |
101 } |
| |
102 |
| |
103 sqlite3_close(db); |
| |
104 |
| |
105 return 0; |
| |
106 } |
| |
107 |
| |
108 static int create_test_data(sqlite3 *db) { |
| |
109 char *err_msg = NULL; |
| |
110 int rc = sqlite3_exec(db, sql_create_table_person, 0, 0, &err_msg); |
| |
111 if(rc != SQLITE_OK) { |
| |
112 fprintf(stderr, "SQLite error: %s\n", err_msg); |
| |
113 sqlite3_free(err_msg); |
| |
114 sqlite3_close(db); |
| |
115 return 1; |
| |
116 } |
| |
117 |
| |
118 |
| |
119 sqlite3_stmt *stmt; |
| |
120 rc = sqlite3_prepare_v2(db, sql_check_table, -1, &stmt, 0); |
| |
121 if(rc != SQLITE_OK) { |
| |
122 fprintf(stderr, "SQLite error: %s\n", sqlite3_errmsg(db)); |
| |
123 sqlite3_close(db); |
| |
124 return 1; |
| |
125 } |
| |
126 |
| |
127 int exists = 0; |
| |
128 if(sqlite3_step(stmt) == SQLITE_ROW) { |
| |
129 exists = 1; |
| |
130 } |
| |
131 sqlite3_finalize(stmt); |
| |
132 |
| |
133 if(exists) { |
| |
134 return 0; |
| |
135 } |
| |
136 |
| |
137 rc = sqlite3_exec(db, sql_create_test_data, 0, 0, &err_msg); |
| |
138 if(rc != SQLITE_OK) { |
| |
139 fprintf(stderr, "SQLite error: %s\n", err_msg); |
| |
140 sqlite3_free(err_msg); |
| |
141 sqlite3_close(db); |
| |
142 return 1; |
| |
143 } |
| |
144 |
| |
145 return 0; |
| |
146 } |