File: dbdecl.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 (496 lines) | stat: -rw-r--r-- 10,118 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use core:lang;
use lang:bs;
use lang:bs:macro;
use lang:bs:unsafe;

/**
 * A column declaration.
 */
class Column {
	// Name of the column.
	Str name;

	// Datatype.
	SQLType datatype;

	// Is this column a primary key?
	Bool primaryKey;

	// Is this column allowed to contain NULL?
	Bool allowNull;

	// Unique?
	Bool unique;

	// Auto-increment?
	Bool autoIncrement;

	// Default value?
	SQLLiteral? default;


	init(SStr name, SQLType type) {
		init {
			name = name.v;
			datatype = type;
		}
	}

	// Called from the syntax.
	void setPrimary(Str x) { primaryKey = true; }
	void setAllowNull(Str x) { allowNull = true; }
	void setUnique(Str x) { unique = true; }
	void setAutoIncrement(Str x) { autoIncrement = true; }
	void setDefault(SQLLiteral x) { default = x; }

	// Called from the syntax, gives a decent error message on how we're different from SQL.
	void setNotNull(SStr x) {
		throw SyntaxError(x.pos, "Columns are NOT NULL by default. Use ALLOW NULL to allow nullable columns.");
	}

	// Create an SQL part for this column.
	void toSQL(QueryStrBuilder to) {
		to.name(name);
		to.put(" ");
		to.type(datatype);
		modifiers(to);
	}

	void toS(StrBuf to) : override {
		QueryStrBuilder b;
		toSQL(b);
		to << b.build;
		if (primaryKey)
			to << " (primary key)";
	}

	// To schema.
	Schema:Column toSchema() {
		Schema:Attributes attrs;
		if (primaryKey)
			attrs += Schema:Attributes:primaryKey;
		if (!allowNull)
			attrs += Schema:Attributes:notNull;
		if (unique)
			attrs += Schema:Attributes:unique;
		if (autoIncrement)
			attrs += Schema:Attributes:autoIncrement;

		Schema:Column out(name, datatype.sqlType, attrs);
		if (default)
			out.default = default.toSQL();

		out;
	}

	// See if this column has some kind of default value. I.e. it is OK to leave one without a value?
	Bool hasDefault(Bool multiPK) {
		if (default)
			return true;
		if (primaryKey & !multiPK)
			return true;
		if (allowNull)
			return true;
		if (autoIncrement)
			return true;
		false;
	}

	private void modifiers(QueryStrBuilder to) {
		if (!allowNull) {
			to.put(" NOT NULL");
		}
		if (unique) {
			to.put(" UNIQUE");
		}
		if (autoIncrement) {
			to.put(" ");
			to.autoIncrement();
		}
		if (default) {
			to.put(" DEFAULT " + default.toSQL());
		}
	}
}


/**
 * Index for a table.
 */
class Index {
	// Name of the index (might be auto-generated).
	Str name;

	// Columns.
	Str[] columns;

	// Create.
	init(Str name, Str[] columns) {
		init {
			name = name;
			columns = columns;
		}
	}

	// To schema.
	Schema:Index toSchema()	{
		return Schema:Index(name, columns);
	}

	// To SQL statement.
	void toSQL(QueryStrBuilder to, Str table) {
		to.put("CREATE INDEX ");
		to.name(name);
		to.put(" ON ");
		to.name(table);
		to.put("(");
		for (i, c in columns) {
			if (i > 0)
				to.put(", ");
			to.name(c);
		}
		to.put(");");
	}

	// To string. No SQL escaping.
	void toS(StrBuf to) : override {
		to << "INDEX " << name << " ON ?(" << join(columns, ", ") << ")";
	}
}

/**
 * Declaration of an entire table.
 */
class Table {
	// Name of the table.
	Str name;

	// Columns in the table.
	Column[] columns;

	// Indices for this table.
	Index[] indices;

	init(SStr name) {
		init {
			name = name.v;
		}
	}

	// Find a column.
	Column? find(Str name) {
		for (c in columns)
			if (c.name == name)
				return c;
		null;
	}

	// Add column (called from syntax).
	void add(Column col) {
		columns << col;
	}

	// Add primary key(s) (called from syntax).
	void add(Array<SStr> cols) {
		for (c in columns)
			if (c.primaryKey)
				throw SyntaxError(cols[0].pos, "Only one instance of the PRIMARY KEY keyword may be present for each table.");

		for (c in cols) {
			unless (col = find(c.v))
				throw SyntaxError(c.pos, "No column named ${c.v} was declared in this table.");
			col.primaryKey = true;
		}
	}

	// Add index.
	void add(SrcPos pos, Index index) {
		for (i in indices)
			if (i.name == index.name)
				throw SyntaxError(pos, "The index ${index.name} already exists.");

		indices << index;
	}

	// Check if there are multiple primary keys.
	Bool multiplePK() {
		Nat count = 0;
		for (c in columns)
			if (c.primaryKey)
				count++;
		count > 1;
	}

	// Figure out what index, if any, has an implicit AUTOINCREMENT. Returns "count" if none has it.
	Nat implicitAutoIncrementColumn() {
		Nat count = columns.count();
		Nat firstPrimary = count();

		for (i, c in columns) {
			if (c.primaryKey) {
				if (firstPrimary < count)
					return count;
				firstPrimary = i;
			}
		}

		var col = columns[firstPrimary];
		// Reset if it is either already marked as autoincrement, or if it is of the improper type.
		if (col.autoIncrement) {
			firstPrimary = count;
		} else if (!col.datatype.sqlType.sameType(QueryType:integer)) {
			firstPrimary = count;
		}

		return firstPrimary;
	}

	// Create a Schema from this table declaration.
	Schema toSchema() {
		Schema:Column[] cols;
		for (c in columns)
			cols << c.toSchema();

		Schema:Index[] inds;
		for (i in indices)
			inds << i.toSchema();

		return Schema(name, cols, inds);
	}

	// Create an SQL statement for this table declaration.
	void toSQL(QueryStrBuilder to, Bool ifNotExists, Bool implicitAutoIncrement, Str[] options) {
		to.put("CREATE TABLE ");
		if (ifNotExists)
			to.put("IF NOT EXISTS ");
		to.name(name);
		to.put(" (");

		Str[] pk;
		for (col in columns)
			if (col.primaryKey)
				pk << col.name;

		Nat implicitCol = if (implicitAutoIncrement) { columns.count; } else { implicitAutoIncrementColumn(); };
		for (i, col in columns) {
			if (i > 0)
				to.put(", ");
			col.toSQL(to);

			// Note: Some databases (e.g. SQLite) require that the PRIMARY KEY is specified here in
			// order for AUTOINCREMENT to work properly.
			if (pk.count == 1 & col.primaryKey)
				to.put(" PRIMARY KEY");

			if (implicitCol == i) {
				to.put(" ");
				to.autoIncrement();
			}
		}

		if (pk.count > 1) {
			to.put(", PRIMARY KEY(");
			for (i, k in pk) {
				if (i > 0)
					to.put(", ");
				to.name(k);
			}
			to.put(")");
		}

		to.put(")");
		if (options.any) {
			to.put(" ");
			for (i, k in options) {
				if (i > 0)
					to.put(", ");
				to.put(k);
			}
		}
		to.put(";");
	}
}

/**
 * Helper type for creating indices.
 */
class IndexDecl {
	SrcPos pos;
	SStr table;
	Index index;

	init(SrcPos pos, SStr name, SStr table, SStr[] cols) {
		Str[] c;
		for (x in cols)
			c << x.v;

		init {
			pos = pos;
			table = table;
			index(name.v, c);
		}
	}

	init(SrcPos pos, SStr table, SStr[] cols) {
		StrBuf name;
		name << table.v;

		Str[] c;
		for (x in cols) {
			c << x.v;
			name << "_" << x.v;
		}

		init {
			pos = pos;
			table = table;
			index(name.toS, c);
		}
	}
}

/**
 * Database description.
 */
class Database {
	// Tables declared (indices are stored inside each table).
	Table[] tables;

	// Add a table.
	void add(Table decl) {
		tables.push(decl);
	}

	// Add an index.
	void add(IndexDecl decl) {
		unless (table = find(decl.table.v))
			throw SyntaxError(decl.table.pos, "The table ${decl.table.v} was not declared (yet).");

		table.add(decl.pos, decl.index);
	}

	// Find a table.
	Table? find(Str name) {
		// TODO: Speedier lookup?
		for (table in tables)
			if (table.name == name)
				return table;
		null;
	}
}

/**
 * Declaration of a database.
 */
class DatabaseDecl extends NamedDecl {
	SStr name;
	Scope scope;
	Database contents;

	init(SStr name, Scope scope, Database contents) {
		init() {
			name = name;
			scope = scope;
			contents = contents;
		}
	}

	Named doCreate() {
		DatabaseType(name, contents);
	}
}

/**
 * Type stored in the name tree.
 */
class DatabaseType extends Type {
	// Contents of the database.
	Database contents;

	// All queries that operate on this database. This allows us to prepare all statements up-front,
	// which avoids surprises for applications down the line, both in terms of performance and in
	// the case some query results in an error due to incorrect database contents.
	CachedQuery[] queries;

	// Create.
	init(SStr name, Database contents) {
		init(name.v, TypeFlags:typeClass) {
			contents = contents;
		}

		setSuper(named{TypedConnection});
		addCtors();
	}

	private void addCtors() {
		{
			BSTreeCtor ctor([thisParam(this), ValParam(named{DBConnection}, "db")], SrcPos());
			CtorBody body(ctor, Scope());

			Actuals params;
			params.add(LocalVarAccess(SrcPos(), body.parameters[1]));
			params.add(lang:bs:util:TObjectLiteral(SrcPos(), this));
			body.add(InitBlock(SrcPos(), body, params));

			ctor.body = body;
			add(ctor);
		}
		{
			BSTreeCtor ctor([
								thisParam(this),
								ValParam(named{DBConnection}, "db"),
								ValParam(named{MigrationPolicy}, "policy")
							], SrcPos());
			CtorBody body(ctor, Scope());

			Actuals params;
			params.add(LocalVarAccess(SrcPos(), body.parameters[1]));
			params.add(lang:bs:util:TObjectLiteral(SrcPos(), this));
			params.add(LocalVarAccess(SrcPos(), body.parameters[2]));
			body.add(InitBlock(SrcPos(), body, params));

			ctor.body = body;
			add(ctor);
		}
	}
}


/**
 * Base class inherited from the DBType class.
 */
class TypedConnection {
	// Underlying database connection.
	DBConnection connection;

	// Create and verify the database structure.
	init(DBConnection conn, DatabaseType t) {
		self(conn, t, MigrationPolicy:default);
	}

	// Create and verify the database structure, also supply a migration policy.
	init(DBConnection conn, DatabaseType t, MigrationPolicy policy) {
		init { connection = conn; }

		// Make sure the database is in the state we expect it to be.
		verifyDatabaseSchema(conn, t.contents, policy);

		// Prepare statements now to avoid performance problems and to detect errors early. Note
		// that we use 'as thread' to avoid the implicit copy here (we do the same for the objects
		// we embed in the generated machine code, so this is not too bad).
		var queries = as thread Compiler { t.queries; };
		for (q in t.queries) {
			conn.prepare(q);
		}
	}

	// Close.
	void close() {
		connection.close();
	}

	// Get a cached prepared statement based on its ID.
	Statement prepare(CachedQuery cached) {
		connection.prepare(cached);
	}
}