File: joins.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (271 lines) | stat: -rw-r--r-- 5,175 bytes parent folder | download | duplicates (2)
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
use test;
use sql;
use core:io;

DATABASE JoinDB {
	TABLE textA(
		id INTEGER PRIMARY KEY,
		b INTEGER,
		value TEXT
		);

	TABLE textB(
		id INTEGER PRIMARY KEY,
		c INTEGER,
		value TEXT
		);

	TABLE textC(
		id INTEGER PRIMARY KEY,
		value TEXT
		);
}

JoinDB joinDB() {
	SQLite db;
	JoinDB c(db);

	WITH c {
		INSERT INTO textC VALUES (1, "i");
		INSERT INTO textC VALUES (2, "ii");
		INSERT INTO textC VALUES (3, "iii");
		INSERT INTO textC VALUES (4, "iv");

		INSERT INTO textB VALUES (1, 2, "one");
		INSERT INTO textB VALUES (2, 3, "two");
		INSERT INTO textB VALUES (3, 4, "three");
		INSERT INTO textB VALUES (4, 5, "four");

		INSERT INTO textA VALUES (1, 2, "1");
		INSERT INTO textA VALUES (2, 3, "2");
		INSERT INTO textA VALUES (3, 4, "3");
		INSERT INTO textA VALUES (4, 5, "4");
	};

	return c;
}

test JoinInner {
	var db = joinDB();
	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value
			FROM textA
			JOIN textB ON textA.b == textB.id;
		Nat rows;
		for (row in res) {
			rows++;
			// print(row.toS);
		}
		check rows == 3;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			JOIN textB ON textA.b == textB.id
			JOIN textC ON textB.c == textC.id;
		Nat rows;
		for (row in res) {
			rows++;
			// print(row.toS);
		}
		check rows == 2;
	}
}

test JoinLeft {
	var db = joinDB();
	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value
			FROM textA
			LEFT JOIN textB ON textA.b == textB.id;
		Nat rows;
		Nat rightNull;
		for (row in res) {
			rows++;
			if (row.textB.empty)
				rightNull++;
			// print(row.toS);
		}
		check rows == 4;
		check rightNull == 1;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			LEFT JOIN textB ON textA.b == textB.id
			LEFT JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat bNull;
		Nat cNull;
		for (row in res) {
			rows++;
			if (row.textB.empty)
				bNull++;
			if (row.textC.empty)
				cNull++;
			// print(row.toS);
		}
		check rows == 4;
		check bNull == 1;
		check cNull == 2;
	}
}

test JoinRight {
	var db = joinDB();
	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value
			FROM textA
			RIGHT JOIN textB ON textA.b == textB.id;
		Nat rows;
		Nat leftNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				leftNull++;
			// print(row.toS);
		}
		check rows == 4;
		check leftNull == 1;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			RIGHT JOIN textB ON textA.b == textB.id
			RIGHT JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat aNull;
		Nat bNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				aNull++;
			if (row.textB.empty)
				bNull++;
			// print(row.toS);
		}
		check rows == 4;
		check aNull == 2;
		check bNull == 1;
	}
}

test JoinFull {
	var db = joinDB();
	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value
			FROM textA
			FULL JOIN textB ON textA.b == textB.id;
		Nat rows;
		Nat leftNull;
		Nat rightNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				leftNull++;
			if (row.textB.empty)
				rightNull++;
			// print(row.toS);
		}
		check rows == 5;
		check leftNull == 1;
		check rightNull == 1;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			FULL JOIN textB ON textA.b == textB.id
			FULL JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat aNull;
		Nat bNull;
		Nat cNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				aNull++;
			if (row.textB.empty)
				bNull++;
			if (row.textC.empty)
				cNull++;
			// print(row.toS);
		}
		check rows == 6;
		check aNull == 2;
		check bNull == 2;
		check cNull == 2;
	}
}

test JoinMixed {
	var db = joinDB();
	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			LEFT JOIN textB ON textA.b == textB.id
			RIGHT JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat aNull;
		Nat bNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				aNull++;
			if (row.textB.empty)
				bNull++;
			// print(row.toS);
		}

		check rows == 4;
		check aNull == 2;
		check bNull == 2;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			RIGHT JOIN textB ON textA.b == textB.id
			LEFT JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat aNull;
		Nat cNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				aNull++;
			if (row.textC.empty)
				cNull++;
			// print(row.toS);
		}

		check rows == 4;
		check aNull == 1;
		check cNull == 1;
	}

	{
		var res = WITH db: SELECT textA.id, textA.value, textB.id, textB.value, textC.id, textC.value
			FROM textA
			FULL JOIN textB ON textA.b == textB.id
			RIGHT JOIN textC ON textB.c == textC.id;
		Nat rows;
		Nat aNull;
		Nat bNull;
		for (row in res) {
			rows++;
			if (row.textA.empty)
				aNull++;
			if (row.textB.empty)
				bNull++;
			// print(row.toS);
		}

		check rows == 4;
		check aNull == 2;
		check bNull == 1;
	}
}