postgresql 0.1.0

Yeat Another PostgreSQL driver


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Another try write PostgreSQL driver, first try fails with ugly db, unified db library is utopia, which provides incomplete features of all, and overhead abstraction.

Where D power, is templates, library should provide, different results for different goals. Also, result is local storage (within currenct context), available even when connection is destroyed. Also u can simply write own result storage Struct or Class based. See example bellow.

TODO

  1. Libpq wrapper
  2. API stability
  3. Native protocol implementation
  4. Connection pool
  5. Reconnect on network issues
  6. Multimaster support (PostgreSQL10)

Note

  1. Link.perform used for function with no result
  2. Link.call used for function returned result
  3. Link.exec used for statement with no result (e.g. UPDATE)
  4. Link.query used for statement with results (e.g. SELECT)

Example

import std.stdio;
import postgresql;
import postgresql.result.array;
import postgresql.result.dict;

void main()
{
    // Create connection
    auto db = new Link("postgresql://postgres@127.0.0.1:5432/postgres?sslmode=disable&connect_timeout=20");

    // Disconnect at exit
    scope(exit)
    {
        db.detach();
    }

    // Try connect
    if (!db.attach())
    {
        // Fails
        writeln("error: ", db.error);
        return;
    }

    // Define array result storage, where row is number indexed
    ResultArray result1;

    if (db.query(result1, "SELECT generate_series($1::INT, $2::INT) AS gen", 0, 9))
    {
        writeln(" cols: ", result1.cols);
        writeln(" rows: ", result1.rows);
        writeln("names: ", result1.names);
        writeln("types: ", result1.types);
        writeln(" data: ", result1.data);
        foreach(row; result1.data)
        {
            writeln(row);
        }
    }
    else
    {
        writeln(db.error);
    }

    // Separator
    writeln("\n===\n");

    // Define map result storage, where row is string indexed
    ResultDict result2;

    if (db.query(result2, "SELECT generate_series($1::INT, $2::INT) AS gen", 10, 19))
    {
        writeln(" cols: ", result2.cols);
        writeln(" rows: ", result2.rows);
        writeln("names: ", result2.names);
        writeln("types: ", result2.types);
        writeln(" data: ", result2.data);
        foreach(row; result2.data)
        {
            writeln(row);
        }
    }
    else
    {
        writeln(db.error);
    }

}

Output

 cols: 1
 rows: 10
names: ["gen"]
types: [INT4OID]
 data: [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]

===

 cols: 1
 rows: 10
names: ["gen"]
types: [INT4OID]
 data: [{"gen":10}, {"gen":11}, {"gen":12}, {"gen":13}, {"gen":14}, {"gen":15}, {"gen":16}, {"gen":17}, {"gen":18}, {"gen":19}]
{"gen":10}
{"gen":11}
{"gen":12}
{"gen":13}
{"gen":14}
{"gen":15}
{"gen":16}
{"gen":17}
{"gen":18}
{"gen":19}
Authors:
Anton Dutov
Dependencies:
none
Versions:
0.2.1 2017-Sep-15
0.1.0 2017-Sep-10
~master 2017-Sep-15
Show all 3 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 18 downloads total