-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample.c
182 lines (165 loc) · 7.18 KB
/
example.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <crossdb.h>
int main (int argc, char **argv)
{
xdb_res_t *pRes;
xdb_row_t *pRow;
#if 1
xdb_conn_t *pConn = xdb_open (argc > 1 ? argv[1] : ":memory:");
#else
xdb_conn_t *pConn = xdb_connect (NULL, NULL, NULL, "memory", 7777);
XDB_CHECK (NULL != pConn, printf ("failed to create DB\n"); return -1;);
#endif
// Create Table
pRes = xdb_exec (pConn, "CREATE TABLE IF NOT EXISTS student (id INT PRIMARY KEY, name CHAR(16), age INT, class CHAR(16), score FLOAT, info VARCHAR(255))");
XDB_RESCHK(pRes, printf ("Can't create table student\n"); goto error;);
pRes = xdb_exec (pConn, "CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))");
XDB_RESCHK(pRes, printf ("Can't create table teacher\n"); goto error;);
pRes = xdb_exec (pConn, "CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))");
XDB_RESCHK(pRes, printf ("Can't create table book\n"); goto error;);
// clean table
pRes = xdb_exec (pConn, "DELETE FROM student");
pRes = xdb_exec (pConn, "DELETE FROM teacher");
pRes = xdb_exec (pConn, "DELETE FROM book");
// Insert
pRes = xdb_exec (pConn, "INSERT INTO student (id,name,age,class,score) VALUES (1,'jack',10,'3-1',90),(2,'tom',11,'2-5',91),(3,'jack',11,'1-6',92),(4,'rose',10,'4-2',90),(5,'tim',10,'3-1',95)");
XDB_RESCHK(pRes, printf ("Can't insert table student\n"); goto error;);
pRes = xdb_pexec (pConn, "INSERT INTO student (id,name,age,class,score,info) VALUES (6,'Tony',10,'3-1',95,'%s')", "He is a boy.\nHe likes playing football.\nWe all like him!");
XDB_RESCHK(pRes, printf ("Can't insert table student\n"); goto error;);
pRes = xdb_pexec (pConn, "INSERT INTO student (id,name,age,class,score,info) VALUES (7,'Wendy',10,'3-1',95,'%s')", "She is a girl.\nShe likes cooking.\nWe all love her!");
XDB_RESCHK(pRes, printf ("Can't insert table student\n"); goto error;);
pRes = xdb_exec (pConn, "INSERT INTO teacher (id,name,age) VALUES (1,'Tomas',40),(2,'Steven',50),(3,'Bill',31),(4,'Lucy',29)");
XDB_RESCHK(pRes, printf ("Can't insert table teacher\n"); goto error;);
pRes = xdb_exec (pConn, "INSERT INTO book (id,name,author,count) VALUES (1,'Romeo and Juliet','Shakespeare',10),(2,'Pride and Prejudice','Austen',5),(3,'Great Expectations','Dickens',8),(4,'Sorrows of Young Werther','Von Goethe',4)");
XDB_RESCHK(pRes, printf ("Can't insert table book\n"); goto error;);
// Select
pRes = xdb_exec (pConn, "SELECT * from student");
printf ("=== Select all %d rows\n", (int)pRes->row_count);
while (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
// Update
printf ("\n=== Update age = 9 for id = 2\n");
pRes = xdb_exec (pConn, "UPDATE student set age=9 WHERE id = 2");
XDB_RESCHK(pRes, printf ("Can't update id=%d\n",2); goto error;);
pRes = xdb_exec (pConn, "SELECT id,name,age,class,score from student WHERE id = 2");
printf (" select %d rows\n ", (int)pRes->row_count);
while (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
printf (" id=%d name='%s' age=%d class='%s' score=%f\n",
xdb_column_int (pRes->col_meta, pRow, 0),
xdb_column_str (pRes->col_meta, pRow, 1),
xdb_column_int (pRes->col_meta, pRow, 2),
xdb_column_str (pRes->col_meta, pRow, 3),
xdb_column_float(pRes->col_meta, pRow, 4));
printf (" id=%d name='%s' age=%d class='%s' score=%f\n",
*(int*)pRow[0],
(char*)pRow[1],
*(int*)pRow[2],
(char*)pRow[3],
*(float*)pRow[4]);
}
xdb_free_result (pRes);
// Delete
printf ("\n=== Delete id = 3\n");
pRes = xdb_exec (pConn, "DELETE FROM student WHERE id = 3");
XDB_RESCHK(pRes, printf ("Can't delete id=%d\n",3); goto error;);
pRes = xdb_exec (pConn, "SELECT * from student WHERE id = 3");
printf (" select %d rows\n", (int)pRes->row_count);
while (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
// Aggregation function
printf ("\n=== AGG COUNT,MIN,MAX,SUM,AVG\n");
pRes = xdb_exec (pConn, "SELECT COUNT(*),MIN(score),MAX(score),SUM(score),AVG(score) FROM student");
printf (" --- select %d rows\n ", (int)pRes->row_count);
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
printf (" COUNT(*)=%d MIN(score)=%f MAX(score)=%f SUM(score)=%f AVG(score)=%f\n",
xdb_column_int (pRes->col_meta, pRow, 0),
xdb_column_float(pRes->col_meta, pRow, 1),
xdb_column_float(pRes->col_meta, pRow, 2),
xdb_column_double(pRes->col_meta, pRow, 3),
xdb_column_double(pRes->col_meta, pRow, 4));
printf (" COUNT(*)=%d MIN(score)=%f MAX(score)=%f SUM(score)=%f AVG(score)=%f\n",
(int)*(int64_t*)pRow[0],
*(float*)pRow[1],
*(float*)pRow[2],
*(double*)pRow[3],
*(double*)pRow[4]);
}
xdb_free_result (pRes);
// Transaction rollback
printf ("\n=== Rollback\n");
xdb_begin (pConn);
printf (" update age=15 for id = 2\n");
pRes = xdb_exec (pConn, "UPDATE student set age=15 WHERE id = 2");
pRes = xdb_exec (pConn, "SELECT id,name,age from student WHERE id = 2");
printf (" select %d rows: ", (int)pRes->row_count);
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
printf (" -- rollback\n");
xdb_rollback (pConn);
pRes = xdb_exec (pConn, "SELECT id,name,age from student WHERE id = 2");
printf (" select %d rows: ", (int)pRes->row_count);
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
// Transaction commit
printf ("\n=== Commit\n");
xdb_begin (pConn);
printf (" update age=15 for id = 2\n");
pRes = xdb_exec (pConn, "UPDATE student set age=15 WHERE id = 2");
pRes = xdb_exec (pConn, "SELECT * from student WHERE id = 2");
printf (" select %d rows: ", (int)pRes->row_count);
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
printf (" -- commit\n");
xdb_commit (pConn);
pRes = xdb_exec (pConn, "SELECT * from student WHERE id = 2");
printf (" select %d rows: ", (int)pRes->row_count);
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
// Multi-Statements
printf ("\n=== Muti-Statements\n");
pRes = xdb_exec (pConn, "SELECT COUNT(*) FROM student; SELECT id,name FROM student WHERE id=2");
printf (" -- 1st result: ");
// count(*)
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
// select
printf (" -- 2nd result: ");
pRes = xdb_next_result (pConn);
if (NULL != pRes) {
if (NULL != (pRow = xdb_fetch_row (pRes))) {
xdb_print_row (pRes->col_meta, pRow, 0);
printf ("\n");
}
xdb_free_result (pRes);
}
// Embedded shell
printf ("\n=== Enter interactive embedded shell\n");
xdb_exec (pConn, "SHELL");
error:
xdb_close (pConn);
return 0;
}