2401d, how does ddip1027 support sql

Original text
Here are DIP1036the SQLsupport 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:
1after converting the parameter tuple istring, it passes it to execithe template.
2It 循环converts the parameters, (ironically!) 再转换into 格式串a format %sinstead of ?1,?2,?3etc.
3It skips DIP1036all insertions 插值参数and
4puts them 其余参数separately 绑定into 1,2,3indexes.
5.Then it executes sqlthe statement.
Note that this is not 嵌套supported istring.

See how it DIP1027works 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
1converting istringto 参数元组, 传递give execithe template,
2the 第一个元组elements are 格式串, replace
3with the corresponding argindex , put the bindings to , put the bindings to them , and then execute the statement, it is ."n""%s"所有实例
4替换格式串语句参数索引
5sql
等价

Guess you like

Origin blog.csdn.net/fqbqrr/article/details/135472358