1 // Written in D programming language 2 /** 3 * Copyright: © 2014 DSoftOut 4 * License: Subject to the terms of the MIT license, as written in the included LICENSE file. 5 * Authors: NCrashed <ncrashed@gmail.com> 6 */ 7 module app; 8 9 import std.getopt; 10 import std.stdio; 11 import std.datetime; 12 import dlogg.strict; 13 import pgator.db.pq.libpq; 14 import pgator.db.pq.connection; 15 import pgator.db.async.pool; 16 17 void main(string[] args) 18 { 19 string logName = "app.log"; 20 string connString = "port=5432"; 21 bool help = false; 22 23 args.getopt("l|log", &logName, 24 "c|conn", &connString, 25 "h|help", &help 26 ); 27 28 if(help) 29 { 30 writeln("pgator-backend-example-onerow [options]\n\n", 31 "\t-l|--log=path\t - path to log, default 'app.log'\n", 32 "\t-c|--conn=string - connection string to PostgreSQL\n", 33 "\t-h|--help\t - prints this message" 34 ); 35 return; 36 } 37 38 // Initializing concurrent logger 39 auto logger = new shared StrictLogger(logName); 40 scope(exit) logger.finalize(); 41 42 // Intitializing PostgreSQL bindings and Connection factory 43 auto api = new shared PostgreSQL(logger); 44 auto connProvider = new shared PQConnProvider(logger, api); 45 46 // Creating pool 47 auto pool = new shared AsyncPool(logger, connProvider 48 , dur!"seconds"(1) // time between reconnection tries 49 , dur!"seconds"(5) // maximum time to wait for free connection appearing in the pool while quering 50 , dur!"seconds"(3) // every 3 seconds check connection 51 ); 52 scope(failure) pool.finalize(); 53 54 // Adding server to the pool 55 pool.addServer(connString, 1); 56 57 // Quering a server from pool 58 auto bsonRange = pool.execTransaction(["select 10 as field"], 59 [], // no parameters 60 [], // no parameters 61 null, // no vars 62 [true] // indicates that first query should be one-row query 63 ); 64 foreach(bson; bsonRange) writeln(bson); 65 66 // Invalid query with rollback 67 auto bsonRange2 = pool.execTransaction(["select 10 as field union select 11 as field"], 68 [], // no parameters 69 [], // no parameters 70 null, // no vars 71 [true] // indicates that first query should be one-row query 72 ); 73 foreach(bson; bsonRange2) writeln(bson); 74 }