-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
126 lines (120 loc) · 4.59 KB
/
example.c
File metadata and controls
126 lines (120 loc) · 4.59 KB
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
/*
* SQLiteCLIPS - a SQLite virtual table for CLIPS template facts
* Copyright (C) 2021-2023 G. David Butler <gdb@dbSystems.com>
*
* This file is part of SQLiteCLIPS
*
* SQLiteCLIPS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SQLiteCLIPS 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "sqlite3.h"
#include "clips.h"
int
main(
void
){
extern int sqlite3_clips_init(sqlite3 *, Environment *);
Environment *ev;
sqlite3 *db;
sqlite3_stmt *st;
sqlite3_initialize();
if (!(ev = CreateEnvironment())) {
fprintf(stderr, "CreateEnvironment fail\n");
return (-1);
}
SetConserveMemory(ev, 1);
if (!LoadFromString(ev
,"(deftemplate MAIN::t1"
"(slot s1 (type INTEGER))" /* t1.s1 INTEGER NOT NULL */
"(slot s2 (type SYMBOL STRING))" /* t1.s2 TEXT */
"(slot s3)" /* t1.s3 */
")"
,SIZE_MAX)) {
fprintf(stderr, "LoadFromString fail\n");
return (-1);
}
if (sqlite3_open(":memory:", &db)) {
fprintf(stderr, "sqlite3_open fail\n");
return (-1);
}
if (sqlite3_clips_init(db, ev)) {
fprintf(stderr, "sqlite3_create_module fail\n");
return (-1);
}
if (sqlite3_exec(db
,"CREATE VIRTUAL TABLE \"t1\" USING CLIPS(\"MAIN::t1\");"
,0,0,0)) {
fprintf(stderr, "sqlite3_exec %s\n", sqlite3_errmsg(db));
return (-1);
}
if (sqlite3_prepare(db, "SELECT ROWID,\"s1\",\"s2\",\"s3\" FROM \"t1\"", -1, &st, 0)) {
fprintf(stderr, "sqlite3_prepare %s\n", sqlite3_errmsg(db));
return (-1);
}
if (sqlite3_exec(db, "INSERT INTO \"t1\"(\"s1\",\"s2\")VALUES(1,CAST('a' AS BLOB)),(2,'b');", 0,0,0)) {
fprintf(stderr, "sqlite3_exec %s\n", sqlite3_errmsg(db));
return (-1);
}
while (sqlite3_step(st) == SQLITE_ROW)
printf("%lld %s %s %s\n", sqlite3_column_int64(st, 0)
,sqlite3_column_type(st, 1) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 1)
,sqlite3_column_type(st, 2) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 2)
,sqlite3_column_type(st, 3) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 3)
);
sqlite3_reset(st);
putchar('\n');
if (sqlite3_exec(db, "UPDATE \"t1\" SET \"s3\"=1 WHERE \"s2\"='b';", 0,0,0)) {
fprintf(stderr, "sqlite3_exec %s\n", sqlite3_errmsg(db));
return (-1);
}
while (sqlite3_step(st) == SQLITE_ROW)
printf("%lld %s %s %s\n", sqlite3_column_int64(st, 0)
,sqlite3_column_type(st, 1) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 1)
,sqlite3_column_type(st, 2) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 2)
,sqlite3_column_type(st, 3) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 3)
);
sqlite3_reset(st);
putchar('\n');
if (sqlite3_exec(db, "DELETE FROM \"t1\" WHERE \"s3\" IS NULL;", 0,0,0)) {
fprintf(stderr, "sqlite3_exec %s\n", sqlite3_errmsg(db));
return (-1);
}
while (sqlite3_step(st) == SQLITE_ROW)
printf("%lld %s %s %s\n", sqlite3_column_int64(st, 0)
,sqlite3_column_type(st, 1) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 1)
,sqlite3_column_type(st, 2) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 2)
,sqlite3_column_type(st, 3) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 3)
);
sqlite3_reset(st);
putchar('\n');
if (sqlite3_exec(db, "UPDATE \"t1\" SET \"s3\"=NULL WHERE \"s1\"=2;", 0,0,0)) {
fprintf(stderr, "sqlite3_exec %s\n", sqlite3_errmsg(db));
return (-1);
}
while (sqlite3_step(st) == SQLITE_ROW)
printf("%lld %s %s %s\n", sqlite3_column_int64(st, 0)
,sqlite3_column_type(st, 1) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 1)
,sqlite3_column_type(st, 2) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 2)
,sqlite3_column_type(st, 3) == SQLITE_NULL ? "NULL" : (const char *)sqlite3_column_text(st, 3)
);
sqlite3_finalize(st);
if (sqlite3_close(db)) {
fprintf(stderr, "sqlite3_close fail\n");
return (-1);
}
if (!DestroyEnvironment(ev)) {
fprintf(stderr, "DestroyEnvironment fail\n");
return (-1);
}
return (0);
}