File: tables.bs

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (313 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use test;
use sql;
use core:io;

DATABASE SimpleDB {
	TABLE test(
		id INTEGER PRIMARY KEY,
		name TEXT
	);

	// Auto name of the index.
	INDEX ON test(name);
}

test Simple {
	SQLite baseDb; // (cwdUrl / ".." / "test.db");
	SimpleDB c(baseDb);

	Int count = 0;
	for (row in WITH c: SELECT * FROM test)
		count++;

	check count == 0;

	Str test = "variable";
	WITH c: INSERT INTO test VALUES (3, "test's");
	WITH c: INSERT INTO test(id, name) VALUES (4, test);
	var insertedId = WITH c: INSERT INTO test(name) VALUES ("auto-id");

	check insertedId == 5;

	WITH c: UPDATE test SET name = name || " " || test WHERE id == 2 + 1;

	Nat numRows = WITH c: COUNT FROM test WHERE id > 3;
	check numRows == 2.nat;

	Bool b = false;
	var removed = WITH c: DELETE FROM test WHERE id == insertedId AND NOT b;

	check removed == 1.nat;

	StrBuf data;
	for (row in WITH c: SELECT * FROM test) {
		data << row.id << ", " << row.name << "\n";
	}

	check data.toS == "3, test's variable\n4, variable\n";
}

test BSEscape {
	SQLite db;
	SimpleDB c(db);

	WITH c {
		INSERT INTO test VALUES (3, ${"test" # 3});

		StrBuf data;
		for (row in SELECT name AS n FROM test)
			data << row.n << "|";

		check data.toS == "test3|";
	}
}

test Repeat {
	// Statements are cached, make sure that we can re-run them more than once.
	SQLite baseDB;
	SimpleDB c(baseDB);

	for (Int i = 0; i < 3; i++) {
		Str v = "Value: ${i}";
		WITH c: INSERT INTO test VALUES (i, v);
	}

	Int rows = 0;
	for (row in WITH c: SELECT * FROM test) {
		rows++;
	}

	check rows == 3;
}

test Like {
	SQLite db;
	SimpleDB c(db);

	WITH c {
		INSERT INTO test(name) VALUES ("test");
		INSERT INTO test(name) VALUES ("best");
		INSERT INTO test(name) VALUES ("beaver");

		StrBuf all;
		for (row in SELECT * FROM test WHERE name LIKE "_est") {
			all << row.name << "|";
		}

		for (row in SELECT * FROM test WHERE name LIKE "be%") {
			all << row.name << "!";
		}

		check all.toS == "test|best|best!beaver!";
	}
}

test ComplexCondition {
	SQLite db;
	SimpleDB c(db);

	WITH c {
		INSERT INTO test VALUES (1, "test");
		INSERT INTO test VALUES (2, "best");
		INSERT INTO test VALUES (3, "beaver");

		StrBuf all;
		for (row in SELECT * FROM test WHERE name LIKE "_est" AND id >= 2) {
			all << row.name << "|";
		}

		check all.toS == "best|";
	}
}


DATABASE TwoTables {
	TABLE test(
		id INTEGER PRIMARY KEY,
		name TEXT,
		city TEXT DEFAULT "none"
	);

	INDEX ON test(name, city);

	TABLE extra(
		id INTEGER PRIMARY KEY,
		test INTEGER,
		data TEXT
	);

	INDEX ON extra(test);
}

test AutoMigrate {
	SQLite baseDb;
	{
		SimpleDB c(baseDb);

		WITH c: INSERT INTO test VALUES (3, "test1");
	}

	{
		TwoTables c(baseDb);
		Int rows = 0;

		WITH c {
			INSERT INTO test VALUES (4, "test2", "city");
			INSERT INTO extra VALUES (1, 4, "more data");

			for (x in SELECT city FROM test WHERE id == 4) {
				rows++;
				check x.city == "city";
			}
		}

		// Outside the block to ensure it is actually executed.
		check rows == 1;
	}
}

test Join {
	SQLite db;
	TwoTables c(db);

	WITH c {
		INSERT INTO test VALUES (3, "a", "a");
		INSERT INTO test VALUES (4, "b", "b");

		INSERT INTO extra(test, data) VALUES (3, "more");
		INSERT INTO extra(test, data) VALUES (3, "even more");
		INSERT INTO extra(test, data) VALUES (4, "some more");

		StrBuf all;
		for (x in SELECT test.id AS id, data FROM test JOIN extra ON extra.test == test.id WHERE test.id == 3) {
			all << x.id << "|" << x.data << "|";
		}

		check all.toS == "3|more|3|even more|";

		all.clear();

		if (x = SELECT ONE test.id AS id FROM test JOIN extra ON extra.test == test.id WHERE test.id == 3) {
			all << x.id;
		}
		if (x = SELECT ONE test.id AS id FROM test WHERE test.id == 8) {
			all << x.id;
		}

		check all.toS == "3";

		all.clear();

		for (x in SELECT * FROM test JOIN extra ON extra.test == test.id WHERE test.id == 4) {
			all << x.test.id << "|" << x.extra.data << "|";
		}

		check all.toS == "4|some more|";
	}
}

test OrderBy {
	SQLite db;
	TwoTables c(db);

	WITH c {
		INSERT INTO test(id, name) VALUES (3, "c");
		INSERT INTO test(id, name) VALUES (1, "g");
		INSERT INTO test(id, name) VALUES (2, "g");

		StrBuf all;
		for (x in SELECT id, name FROM test ORDER BY name DESC, id ASC)
			all << x.id << x.name << "|";

		check all.toS == "1g|2g|3c|";

		all = StrBuf();
		for (x in SELECT id, name FROM test ORDER BY name ASC, id ASC)
			all << x.id << x.name << "|";

		check all.toS == "3c|1g|2g|";
	}
}

test TableAlias {
	SQLite db;
	TwoTables c(db);
	WITH c {
		INSERT INTO extra VALUES(1, 2, "a");
		INSERT INTO extra VALUES(2, 3, "b");
		INSERT INTO extra VALUES(3, 4, "c");

		StrBuf all;
		for (x in SELECT p.id AS a, q.id AS b, p.data AS c, q.data AS d FROM extra AS p JOIN extra AS q ON p.test == q.id) {
			all << x.a << x.b << x.c << x.d << "|";
		}

		check all.toS == "12ab|23bc|";
	}
}

DATABASE MultiPK {
	TABLE test(
		id1 INTEGER,
		id2 INTEGER,
		PRIMARY KEY(id1, id2)
	);
}

test MultiPKTest {
	SQLite db;
	MultiPK c(db);

	WITH c {
		INSERT INTO test VALUES (1, 2);
		INSERT INTO test VALUES (1, 3);

		try {
			INSERT INTO test VALUES (1, 2);
		} catch (SQLError e) {
			// This should happen!
		}

		StrBuf all;
		for (x in SELECT * FROM test)
			all << x.id1 << x.id2 << "|";

		check all.toS == "12|13|";
	}

	// Try re-creating the typed connection to test that we parse the declaration from SQLite properly.
	MultiPK d(db);
}


DATABASE NullDB {
	TABLE test(
		id INTEGER PRIMARY KEY,
		value1 TEXT ALLOW NULL,
		value2 TEXT,
		value3 TEXT DEFAULT "empty"
	);
}

test NullTest {
	SQLite db;
	NullDB c(db);

	WITH c {
		Str? val;

		INSERT INTO test(value2, value3) VALUES ("a", "i");
		INSERT INTO test(value1, value2) VALUES ("b", "c");

		INSERT INTO test(value1, value2) VALUES (val || "z", "d");
		val = "z";
		INSERT INTO test(value1, value2) VALUES (val || "z", "e");

		StrBuf data;
		for (row in SELECT * FROM test) {
			data << row.value1.toS << "|" << row.value2 << "|" << row.value3 << "!";
		}

		check data.toS == "null|a|i!b|c|empty!null|d|empty!zz|e|empty!";
	}
}