-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlpp11dv5.d
executable file
·65 lines (52 loc) · 1.85 KB
/
sqlpp11dv5.d
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
#!/usr/bin/env tdmd
import std.conv : to;
import std.format : format;
import std.string : replace;
import std.algorithm : canFind, startsWith;
/// sqlpp11dv5-beta - SQL read and passed as template parameter ...
unittest {
enum string foo = "42";
static assert(
mixin(QueryBuilderSqlTemplate!"select foo, bar from bazTable where foo='{foo}'")
== "select foo, bar from bazTable where foo='42'"
);
}
auto QueryBuilderSqlTemplate(string input)() {
return format!`"%s"`(
input
.replace("{", `" ~ (`)
.replace("}", `).to!string ~ "`));
}
/// sqlpp11dv5 - SQL read and checked for syntax at compile time
unittest {
const string foo = "42";
// Eponymous templates - "alias-this for templates"
// https://p0nce.github.io/d-idioms/#Eponymous-templates
const ok(string query) = __traits(compiles, mixin(QueryBuilderSqlTemplateAndConstrains!query));
static assert(ok!"select foo, bar from bazTable where foo='{foo}'");
static assert(!ok!"foo, bar from bazTable where foo='{foo}'");
// ok is shorthand for okay
template okay(string query) {
import std.traits;
enum bool okay = __traits(compiles, mixin(QueryBuilderSqlTemplateAndConstrains!query));
}
static assert(okay!"select foo, bar from bazTable where foo='{foo}'");
static assert(!okay!"foo, bar from bazTable where foo='{foo}'");
}
void ensureSelect(string input)() {
static if (!input.startsWith("select")) {
static assert(0, "no select used");
}
}
void ensureFrom(string input)() {
static if (!input.canFind("from")) {
static assert(0, "no from clause used");
}
}
auto QueryBuilderSqlTemplateAndConstrains(string input)() {
ensureSelect!input;
ensureFrom!input;
return format!`"%s"`(
input
.replace("{", `" ~ (`).replace("}", `).to!string ~ "`));
}