Interpolated strings and SQL

Paolo Invernizzi paolo.invernizzi at gmail.com
Tue Jan 9 09:59:19 UTC 2024


On Tuesday, 9 January 2024 at 09:25:28 UTC, Nickolay Bukreyev 
wrote:
> On Tuesday, 9 January 2024 at 08:29:08 UTC, Walter Bright wrote:
>> It happens at runtime.
>
> No. This line is inside `enum string query = () { ... }();`. So 
> CTFE-performance considerations do apply.
>
>> I'm sorry to say, that looks like tty noise.
>
> That’s sad. In my opinion, it is at least as readable, plus I 
> see a few objective advantages in it. We don’t have to agree on 
> this though.
>
>> It will rarely need to be escaped, but when one does need it, 
>> one needs it!
>
> Yes, but I see a benefit in reducing the number of characters 
> that _have_ to be escaped in the first place. While `$` rarely 
> appeared in examples we’ve been thinking of so far, if someone 
> faces a need to create a string full of dollars, escaping them 
> all will uglify the string.
>
>> DIP1027 does not require ( ) if it's just an identifier. That 
>> makes for the shortest, simplest
>> istring syntax. The ( ) usage will be relatively rare. The 
>> idea is the most common cases should
>> require the least syntactical noise.
>
> Totally agree. Personally, I prefer omitting parentheses in 
> interpolations when a language supports such syntax, but it’s a 
> matter of taste.
>
>> Also, the reason I picked the SQL example is because that is 
>> the one most cited as being needed
>> and in showing the power of DIP1036 and because I was told 
>> that DIP1027 couldn't do it :-)
>
> DIP1027 is unable to do it _at compile time_. I cannot argue 
> that compile-time string creation doesn’t give us much if we 
> call an SQL engine afterwards. So we need another example where 
> CTFE-ability is desired. Alexandru Ermicioi asked about 
> logging; I agree it is nice to rule out format-string parsing 
> from every `log` call.

Compile time string creation when dealing with SQL give you the 
ability to validate the string for correctness at compile time.

Here an example of what we are doing internally:


```
pinver at utumno fieldmanager % bin/yab build ldc_lab_mac_i64_dg
2024-01-09T10:48:07.889 [info] melkor.d:235:executeReadyLabel 
executing ldc_lab_mac_i64_dg:
/Users/pinver/dlang/ldc-1.36.0/bin/ldc2     -preview=dip1000 -i 
-Isrc -mtriple=x86_64-apple-darwin --vcolumns 
-J/Users/pinver/Lembas --d-version=env_dev_ 
--d-version=listen_for_nx_ --d-version=disable_ssl 
--d-version=disable_fixations --d-version=disable_metrics 
--d-version=disable_aggregator --d-debug -g 
-of/Users/pinver/Projects/DeepGlance/fieldmanager/bin/lab_mac_i64_dg /Users/pinver/Projects/DeepGlance/fieldmanager/src/application.d src/sbx/raygui/c_raygui.c
2024-01-09T10:48:13.423 [error] melkor.d:247:executeReadyLabel 
build failed:
src/ops/sql/semantics.d(489,31): Error: uncaught CTFE exception 
`object.Exception("42P01: relation \"snapshotsssss\" does not 
exist. SQL: select size_mm, size_px from snapshotsssss where 
snapshot_id = $1")`
src/api3.d(41,9):        thrown from here
src/api3.d(51,43):        called from here: 
`checkSql(Schema("public", ["aggregators":Table("aggregators", 
["aggregated_till":Column("aggregated_till", Type.timestamp, 
true, false), "touchpoint_id":Column("touchpoint_id", 
Type.smallint, true, false)], [], [], ["pinver", 
"ipsos_analysis_operator", "i
/Users/pinver/Projects/DeepGlance/fieldmanager/src/application.d(644,45): Error: template instance `api3.forgeSqlCheckerForSchema!(Schema("public", ["aggregators":Table("aggregators", ["aggregated_till":Column("aggregated_till", Type.timestamp, true, false), "touchpoint_id":Column("touchpoint_id", T
```

or

```

pinver at utumno fieldmanager % bin/yab build ldc_lab_mac_i64_dg
2024-01-09T10:52:36.220 [info] melkor.d:235:executeReadyLabel 
executing ldc_lab_mac_i64_dg:
/Users/pinver/dlang/ldc-1.36.0/bin/ldc2     -preview=dip1000 -i 
-Isrc -mtriple=x86_64-apple-darwin --vcolumns 
-J/Users/pinver/Lembas --d-version=env_dev_ 
--d-version=listen_for_nx_ --d-version=disable_ssl 
--d-version=disable_fixations --d-version=disable_metrics 
--d-version=disable_aggregator --d-debug -g 
-of/Users/pinver/Projects/DeepGlance/fieldmanager/bin/lab_mac_i64_dg /Users/pinver/Projects/DeepGlance/fieldmanager/src/application.d src/sbx/raygui/c_raygui.c
2024-01-09T10:52:37.254 [error] melkor.d:247:executeReadyLabel 
build failed:

src/ops/sql/semantics.d(504,19): Error: uncaught CTFE exception 
`object.Exception("XXXX! role \"dummyuser\" can't select on table 
\"snapshots\". SQL: select size_mm, size_px from snapshots where 
snapshot_id = $1")`
src/api3.d(41,9):        thrown from here
src/api3.d(51,43):        called from here: 
`checkSql(Schema("public", ["aggregators":Table("aggregators", 
["aggregated_till":Column("aggregated_till", Type.timestamp, 
true, false), "touchpoint_id":Column("touchpoint_id", 
Type.smallint, true, false)], [], [], ["pinver", 
"ipsos_analysis_operator", "i
/Users/pinver/Projects/DeepGlance/fieldmanager/src/application.d(644,45): Error: template instance `api3.forgeSqlCheckerForSchema!(Schema("public", ["aggregators":Table("aggregators", ["aggregated_till":Column("aggregated_till", Type.timestamp, true, false), "touchpoint_id":Column("touchpoint_id", T
```

CTFE support is a must IMHO

/P




More information about the Digitalmars-d mailing list