ddb.postgres

PostgreSQL client implementation.

Features:

  • Standalone (does not depend on libpq)
  • Binary formatting (avoids parsing overhead)
  • Prepared statements
  • Parametrized queries (partially working)
  • Enums
  • Arrays
  • Composite types

TODOs:

  • Redesign parametrized queries
  • BigInt/Numeric types support
  • Geometric types support
  • Network types support
  • Bit string types support
  • UUID type support
  • XML types support
  • Transaction support
  • Asynchronous notifications
  • Better memory management
  • More friendly PGFields

Members

Aliases

PGFields
alias PGFields = immutable(PGField)[]

Array of fields returned by the server

Classes

PGCommand
class PGCommand

Class encapsulating prepared or non-prepared statements (commands).

PGConnection
class PGConnection

Class representing connection to PostgreSQL server.

PGParameter
class PGParameter

Class representing single query parameter

PGParameters
class PGParameters

Collection of query parameters

PGResultSet
class PGResultSet(Specs...)

Input range of DBRow!Specs

ResponseMessage
class ResponseMessage

Class encapsulating errors and notices.

ServerErrorException
class ServerErrorException

Exception thrown on server error

Structs

PGField
struct PGField

Contains information about fields returned by the server

Bugs

  • Support only cleartext and MD5 authentication
  • Unfinished parameter handling
  • interval is converted to Duration, which does not support months

Data type mapping:

<td rowspan="19">Any type convertible from default D type</td>

PostgreSQL typeAliasesDefault D typeD type mapping possibilities
smallintint2short
integerint4int
bigintint8long
oidreg***uint
decimalnumericnot yet supported
realfloat4float
double precisionfloat8double
character varying(n)varchar(n)string
character(n)char(n)string
textstring
"char"char
byteaubyte[]
timestamp without time zonetimestampDateTime
timestamp with time zonetimestamptzSysTime
dateDate
time without time zonetimeTimeOfDay
time with time zonetimetzSysTime
intervalDuration (without months and years)
booleanboolbool
enumsstringenum
arraysVariant[]dynamic/static array with compatible element type
compositesrecord, rowVariant[]dynamic/static array, struct or Tuple

Examples

with vibe.d use -version=Have_vibe_d_core and use a ConnectionPool (PostgresDB Object & lockConnection)

1 
2 auto pdb = new PostgresDB([
3 	"host" : "192.168.2.50",
4 	"database" : "postgres",
5 	"user" : "postgres",
6 	"password" : ""
7 ]);
8 auto conn = pdb.lockConnection();
9 
10 auto cmd = new PGCommand(conn, "SELECT typname, typlen FROM pg_type");
11 auto result = cmd.executeQuery;
12 
13 try
14 {
15 	foreach (row; result)
16 	{
17 		writeln(row["typname"], ", ", row[1]);
18 	}
19 }
20 finally
21 {
22 	result.close;
23 }

without vibe.d you can use std sockets with PGConnection object

1 import std.stdio;
2 import ddb.postgres;
3 
4 int main(string[] argv)
5 {
6     auto conn = new PGConnection([
7         "host" : "localhost",
8         "database" : "test",
9         "user" : "postgres",
10         "password" : "postgres"
11     ]);
12 
13     scope(exit) conn.close;
14 
15     auto cmd = new PGCommand(conn, "SELECT typname, typlen FROM pg_type");
16     auto result = cmd.executeQuery;
17 
18     try
19     {
20         foreach (row; result)
21         {
22             writeln(row[0], ", ", row[1]);
23         }
24     }
25     finally
26     {
27         result.close;
28     }
29 
30     return 0;
31 }

Meta

Authors

Piotr Szturmaj