Skip to content

Commit 081d37c

Browse files
committed
example and readme: rangify() explained, duplicate Bson sample removed
1 parent f6ab11d commit 081d37c

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ void main(string[] args)
7171
writeln( "Text query result by name: ", answer[0]["current_time"].as!string );
7272
writeln( "Text query result by index: ", answer[0][3].as!string );
7373
74-
// It is possible to read values of unknown type using BSON:
75-
auto firstRow = answer[0];
76-
foreach(cell; rangify(firstRow))
77-
{
78-
writeln("bson: ", cell.as!Bson);
79-
}
80-
8174
// Binary arguments query with binary result:
8275
QueryParams p;
8376
p.sqlCommand = "SELECT "~
@@ -142,6 +135,20 @@ void main(string[] args)
142135
// Signal that the COPY is finished. Let Postgresql finalize the command
143136
// and return any errors with the data.
144137
conn.putCopyEnd();
138+
139+
import std.range: enumerate;
140+
141+
// rangify() template helps to iterate over Answer and Row:
142+
auto few_rows = conn.exec("SELECT v1, v2 FROM test_dpq2_copy");
143+
foreach(row_num, row; few_rows.rangify.enumerate)
144+
{
145+
foreach(col_num, cell; row.rangify.enumerate)
146+
writeln(
147+
"row_num: ", row_num,
148+
" col_num: ", col_num,
149+
" value: ", cell.as!Bson
150+
);
151+
}
145152
}
146153
```
147154

@@ -175,6 +182,14 @@ column: 'null_field', Variant: Nullable.null, Bson: null
175182
column: 'array_field', Variant: [first, second, Nullable.null], Bson: ["first","second",null]
176183
column: 'multi_array', Variant: [[1, 2, 3], [4, 5, 6]], Bson: [[1,2,3],[4,5,6]]
177184
column: 'json_value', Variant: {"text_str":"text string","float_value":123.456}, Bson: {"text_str":"text string","float_value":123.456}
185+
row_num: 0 col_num: 0 value: "This, right here, is a test"
186+
row_num: 0 col_num: 1 value: "8"
187+
row_num: 1 col_num: 0 value: "Wow! it works"
188+
row_num: 1 col_num: 1 value: "13"
189+
row_num: 2 col_num: 0 value: "Horray!"
190+
row_num: 2 col_num: 1 value: "3456"
191+
row_num: 3 col_num: 0 value: "Super fast!"
192+
row_num: 3 col_num: 1 value: "325"
178193
```
179194

180195
## Using dynamic version of libpq

example/example.d

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ void main(string[] args)
2323
writeln( "Text query result by name: ", answer[0]["current_time"].as!string );
2424
writeln( "Text query result by index: ", answer[0][3].as!string );
2525

26-
// It is possible to read values of unknown type using BSON:
27-
auto firstRow = answer[0];
28-
foreach(cell; rangify(firstRow))
29-
{
30-
writeln("bson: ", cell.as!Bson);
31-
}
32-
3326
// Binary arguments query with binary result:
3427
QueryParams p;
3528
p.sqlCommand = "SELECT "~
@@ -94,4 +87,18 @@ void main(string[] args)
9487
// Signal that the COPY is finished. Let Postgresql finalize the command
9588
// and return any errors with the data.
9689
conn.putCopyEnd();
90+
91+
import std.range: enumerate;
92+
93+
// rangify() template helps to iterate over Answer and Row:
94+
auto few_rows = conn.exec("SELECT v1, v2 FROM test_dpq2_copy");
95+
foreach(row_num, row; few_rows.rangify.enumerate)
96+
{
97+
foreach(col_num, cell; row.rangify.enumerate)
98+
writeln(
99+
"row_num: ", row_num,
100+
" col_num: ", col_num,
101+
" value: ", cell.as!Bson
102+
);
103+
}
97104
}

src/dpq2/result.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ immutable class Answer : Result
326326
}
327327
}
328328

329-
/// Creates forward range from immutable Answer
329+
/// Creates forward range from Answer, Row or from any immutable indexed type
330330
auto rangify(T)(T obj)
331331
{
332332
struct Rangify(T)

0 commit comments

Comments
 (0)