Skip to content

Commit c758ee4

Browse files
authored
Merge pull request #215 from denizzzka/example_impr8
Variant: multidimentional arrays support fixed
2 parents 8697c22 + 278ef64 commit c758ee4

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import dpq2;
5252
import std.getopt;
5353
import std.stdio: writeln;
5454
import std.typecons: Nullable;
55+
import std.variant: Variant;
5556
import vibe.data.bson;
5657
5758
void main(string[] args)
@@ -110,10 +111,15 @@ void main(string[] args)
110111
writeln( "5.1 Json: ", r[0]["json_value"].as!Json);
111112
writeln( "5.2 Bson: ", r[0]["json_value"].as!Bson);
112113
113-
// It is possible to read values of unknown type using BSON:
114+
// It is possible to read values of unknown type
115+
// using std.variant.Variant or vibe.data.bson.Bson:
114116
for(auto column = 0; column < r.columnCount; column++)
115117
{
116-
writeln("column name: '"~r.columnName(column)~"', bson: ", r[0][column].as!Bson);
118+
writeln(
119+
"column: '", r.columnName(column), "', ",
120+
"Variant: ", r[0][column].as!Variant, ", ",
121+
"Bson: ", r[0][column].as!Bson
122+
);
117123
}
118124
119125
// It is possible to upload CSV data ultra-fast:
@@ -142,11 +148,9 @@ void main(string[] args)
142148
Compile and run:
143149
```
144150
Running ./dpq2_example --conninfo=user=postgres
145-
2018-12-09T10:08:07.862:package.d:__lambda1:19 DerelictPQ loading...
146-
2018-12-09T10:08:07.863:package.d:__lambda1:26 ...DerelictPQ loading finished
147-
Text query result by name: 2018-12-09 10:08:07.868141
151+
Text query result by name: 2025-08-22 15:33:57.417629
148152
Text query result by index: 456.78
149-
bson: "2018-12-09 10:08:07.868141"
153+
bson: "2025-08-22 15:33:57.417629"
150154
bson: "abc"
151155
bson: "123"
152156
bson: "456.78"
@@ -164,12 +168,13 @@ second line
164168
4.2: [[1, 2, 3], [4, 5, 6]]
165169
5.1 Json: {"text_str":"text string","float_value":123.456}
166170
5.2 Bson: {"text_str":"text string","float_value":123.456}
167-
column name: 'double_field', bson: -1234.56789012345
168-
column name: 'text', bson: "first line\nsecond line"
169-
column name: 'null_field', bson: null
170-
column name: 'array_field', bson: ["first","second",null]
171-
column name: 'multi_array', bson: [[1,2,3],[4,5,6]]
172-
column name: 'json_value', bson: {"text_str":"text string","float_value":123.456}
171+
column: 'double_field', Variant: -1234.57, Bson: -1234.56789012345
172+
column: 'text', Variant: first line
173+
second line, Bson: "first line\nsecond line"
174+
column: 'null_field', Variant: Nullable.null, Bson: null
175+
column: 'array_field', Variant: [first, second, Nullable.null], Bson: ["first","second",null]
176+
column: 'multi_array', Variant: [[1, 2, 3], [4, 5, 6]], Bson: [[1,2,3],[4,5,6]]
177+
column: 'json_value', Variant: {"text_str":"text string","float_value":123.456}, Bson: {"text_str":"text string","float_value":123.456}
173178
```
174179

175180
## Using dynamic version of libpq

example/example.d

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dpq2;
44
import std.getopt;
55
import std.stdio: writeln;
66
import std.typecons: Nullable;
7+
import std.variant: Variant;
78
import vibe.data.bson;
89

910
void main(string[] args)
@@ -62,10 +63,15 @@ void main(string[] args)
6263
writeln( "5.1 Json: ", r[0]["json_value"].as!Json);
6364
writeln( "5.2 Bson: ", r[0]["json_value"].as!Bson);
6465

65-
// It is possible to read values of unknown type using BSON:
66+
// It is possible to read values of unknown type
67+
// using std.variant.Variant or vibe.data.bson.Bson:
6668
for(auto column = 0; column < r.columnCount; column++)
6769
{
68-
writeln("column name: '"~r.columnName(column)~"', bson: ", r[0][column].as!Bson);
70+
writeln(
71+
"column: '", r.columnName(column), "', ",
72+
"Variant: ", r[0][column].as!Variant, ", ",
73+
"Bson: ", r[0][column].as!Bson
74+
);
6975
}
7076

7177
// It is possible to upload CSV data ultra-fast:

src/dpq2/conv/to_variant.d

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,29 @@ Variant toVariant(bool isNullablePayload = true)(in Value v) @safe
5959
template retArray__(NativeT)
6060
{
6161
static if(isNullablePayload)
62-
alias arrType = Nullable!NativeT[];
62+
alias ArrType = Nullable!NativeT;
6363
else
64-
alias arrType = NativeT[];
64+
alias ArrType = NativeT;
6565

66-
alias retArray__ = retVariant!arrType;
66+
auto retArray__() @trusted
67+
{
68+
import dpq2.conv.arrays: ab = binaryValueAs;
69+
70+
const ap = ArrayProperties(v);
71+
72+
switch(ap.dimsSize.length)
73+
{
74+
case 0: return Variant(v.ab!(ArrType[])); // PG can return zero-dimensional arrays
75+
case 1: return Variant(v.ab!(ArrType[]));
76+
case 2: return Variant(v.ab!(ArrType[][]));
77+
case 3: return Variant(v.ab!(ArrType[][][]));
78+
case 4: return Variant(v.ab!(ArrType[][][][]));
79+
default: throw new ValueConvException(
80+
ConvExceptionType.DIMENSION_MISMATCH,
81+
"Attempt to convert an array of dimension "~ap.dimsSize.length.to!string~" to type Variant: dimensions greater than 4 are not supported"
82+
);
83+
}
84+
}
6785
}
6886

6987
with(OidType)

0 commit comments

Comments
 (0)