Skip to content

Commit

Permalink
example and README
Browse files Browse the repository at this point in the history
  • Loading branch information
denizzzka committed Mar 18, 2019
1 parent 6f698f3 commit 2cbce5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ Example
#!/usr/bin/env rdmd
import dpq2;
import std.stdio: writeln;
import std.getopt;
import std.stdio: writeln;
import std.typecons: Nullable;
import vibe.data.bson;
void main(string[] args)
Expand Down Expand Up @@ -95,6 +96,7 @@ void main(string[] args)
);
auto r = conn.execParams(p);
scope(exit) destroy(r);
writeln( "0: ", r[0]["double_field"].as!PGdouble_precision );
writeln( "1: ", r[0][1].as!PGtext );
Expand All @@ -115,23 +117,26 @@ void main(string[] args)
writeln("column name: '"~r.columnName(column)~"', bson: ", r[0][column].as!Bson);
}
// It is possible to upload CSV data ultra-fast
// Re-create the necessary table
conn.exec("DROP TABLE IF EXISTS test_dpq2_copy;");
conn.exec("CREATE TABLE test_dpq2_copy (v1 TEXT, v2 INT);");
// Init the COPY command. This sets the connection in a COPY receive mode until putCopyEnd() is called
// Copy CSV data, because it's standard, ultra fast, and readable
conn.exec("COPY test_dpq2_copy FROM STDIN WITH (FORMAT csv);");
// Write 2 lines of CSV, including text that contains the delimiter. Postgresql handles it well
// It is possible to upload CSV data ultra-fast:
conn.exec("CREATE TEMP TABLE test_dpq2_copy (v1 TEXT, v2 INT)");
// Init the COPY command. This sets the connection in a COPY receive
// mode until putCopyEnd() is called. Copy CSV data, because it's standard,
// ultra fast, and readable:
conn.exec("COPY test_dpq2_copy FROM STDIN WITH (FORMAT csv)");
// Write 2 lines of CSV, including text that contains the delimiter.
// Postgresql handles it well:
string data = "\"This, right here, is a test\",8\nWow! it works,13\n";
conn.putCopyData(data);
// Write 2 more lines
data = "Horray!,3456\nSuper fast!,325\n";
conn.putCopyData(data);
// Signal that the COPY is finished. Let Postgresql finalize the command and return any errors with the data.
conn.putCopyEnd();
version(LDC) destroy(r); // before Derelict unloads its bindings (prevents SIGSEGV)
// Signal that the COPY is finished. Let Postgresql finalize the command
// and return any errors with the data.
conn.putCopyEnd();
}
```

Expand Down
25 changes: 23 additions & 2 deletions example/example.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env rdmd

import dpq2;
import std.stdio: writeln;
import std.getopt;
import std.stdio: writeln;
import std.typecons: Nullable;
import vibe.data.bson;

void main(string[] args)
Expand Down Expand Up @@ -46,6 +47,7 @@ void main(string[] args)
);

auto r = conn.execParams(p);
scope(exit) destroy(r);

writeln( "0: ", r[0]["double_field"].as!PGdouble_precision );
writeln( "1: ", r[0][1].as!PGtext );
Expand All @@ -66,5 +68,24 @@ void main(string[] args)
writeln("column name: '"~r.columnName(column)~"', bson: ", r[0][column].as!Bson);
}

version(LDC) destroy(r); // before Derelict unloads its bindings (prevents SIGSEGV)
// It is possible to upload CSV data ultra-fast:
conn.exec("CREATE TEMP TABLE test_dpq2_copy (v1 TEXT, v2 INT)");

// Init the COPY command. This sets the connection in a COPY receive
// mode until putCopyEnd() is called. Copy CSV data, because it's standard,
// ultra fast, and readable:
conn.exec("COPY test_dpq2_copy FROM STDIN WITH (FORMAT csv)");

// Write 2 lines of CSV, including text that contains the delimiter.
// Postgresql handles it well:
string data = "\"This, right here, is a test\",8\nWow! it works,13\n";
conn.putCopyData(data);

// Write 2 more lines
data = "Horray!,3456\nSuper fast!,325\n";
conn.putCopyData(data);

// Signal that the COPY is finished. Let Postgresql finalize the command
// and return any errors with the data.
conn.putCopyEnd();
}

0 comments on commit 2cbce5f

Please sign in to comment.