Original text
Here are DIP1036
the SQL
support methods:
here
auto execi(Args...)(Sqlite db, InterpolationHeader header, Args args, InterpolationFooter footer) {
import arsd.sqlite;
//`SQLite`允许你执行`?1,?2`等操作
enum string query = () {
string sql;
int number;
import std.conv;
foreach(idx, arg; Args)
static if(is(arg == InterpolatedLiteral!str, string str))
sql ~= str;
else static if(is(arg == InterpolationHeader) || is(arg == InterpolationFooter))
throw new Exception("Nested interpolation not supported");
else static if(is(arg == InterpolatedExpression!code, string code))
{
} //只需跳过它
else
sql ~= " " ~ to!string(++number);
return sql;
}();
auto statement = Statement(db, query);
int number;
foreach(arg; args) {
static if(!isInterpolatedMetadata!(typeof(arg)))
statement.bind(++number, arg);
}
return statement.execute();
}
Here:
1
after converting the parameter tuple istring
, it passes it to execi
the template.
2
It 循环
converts the parameters, (ironically!) 再转换
into 格式串
a format %s
instead of ?1,?2,?3
etc.
3
It skips DIP1036
all insertions 插值参数
and
4
puts them 其余参数
separately 绑定
into 1,2,3
indexes.
5
.Then it executes sql
the statement.
Note that this is not 嵌套
supported istring
.
See how it DIP1027
works with:
auto execi(Args...)(Sqlite db, Args args) {
import arsd.sqlite;
//`SQLite`允许你执行`?1,?2`等操作
enum string query = () {
string sql;
int number;
import std.conv;
auto fmt = arg[0];
for (size_t i = 0; i < fmt.length, ++i)
{
char c = fmt[i];
if (c == '%' && i + 1 < fmt.length && fmt[i + 1] == 's')
{
sql ~= " " ~ to!string(++number);
++i;
}
else if (c == '%' && i + 1 < fmt.length && fmt[i + 1] == '%')
++i; //跳过转义百分比
else
sql ~= c;
}
return sql;
}();
auto statement = Statement(db, query);
int number;
foreach(arg; args[1 .. args.length]) {
statement.bind(++number, arg);
}
return statement.execute();
}
This: After
1
converting istring
to 参数元组
, 传递
give execi
the template,
2
the 第一个元组
elements are 格式串
, replace
3
with the corresponding arg
index , put the bindings to , put the bindings to them , and then execute the statement, it is ."n"
"%s"
所有实例
4
替换格式串
语句
参数
索引
5
sql
等价