Tuples, CTFE, and Sliding Template Arguments

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sun Jan 14 08:23:47 UTC 2024


On 14/01/2024 8:56 PM, Nickolay Bukreyev wrote:
> Your idea looks attracting to me, but to be honest, I haven’t fully 
> grasped it. Could you show an example implementation of `execi` & 
> `generateSql` please? Particularly interested in how you would pass and 
> process literal text chunks.

I'm working off of:

https://forum.dlang.org/post/cbwqnanabefqjvxrzszx@forum.dlang.org

I have not thought this use case through, but the basic premise should 
be sound.

For instance suffix strings, positional arguments are not handled here 
and if something wasn't an interpolated string.

```d
db.execi(i"SELECT * FROM items WHERE id = $desiredId");
```

Becomes:

```d
db.execi(@IPrefix("SELECT * FROM items WHERE id = ") desiredId);
```

```d
auto execi(FormatString fmt, Args...)(Sqlite db, Args args) {
	enum query = () {
		string ret;

		static foreach(i; 0 .. Args.length) {
			foreach(prefix; __traits(getAttributes, args[i], IPrefix)) {
				ret ~= prefix.value;
			}
			ret ~= "?";
			ret ~= i.text;
		}

		return ret;
	}();

	auto statement = Statement(db, query);
	static foreach (i, arg; args)
		statement.bind(i + 1, arg);
	return statement.execute();
}
```


More information about the Digitalmars-d mailing list