libpq93_bindings 0.0.1
libpq93_bindings: ^0.0.1 copied to clipboard
PostgreSQL 9.3 database bindings to "libpq" library for Dart.
example/example.dart
import 'dart:io';
import 'package:binary_interop/binary_interop.dart';
import 'package:libc/headers.dart';
import 'package:libpq93_bindings/headers.dart';
import 'package:libpq93_bindings/libpq93_bindings.dart';
/**
* Example 31-1. libpq Example Program 1
*
* http://www.postgresql.org/docs/9.1/static/libpq-example.html *
*/
void main(List<String> args) {
var t = new BinaryTypes();
var h = new BinaryTypeHelper(t);
h.addHeaders(LIBC_HEADERS);
h.addHeaders(LIBPQ93_HEADERS);
var libpq = loadLibpq93Library(t);
var conninfo = "dbname = postgres";
if (args.length == 2) {
var user = args[0];
var pass = args[1];
conninfo += " user=$user password=$pass";
}
/* Make a connection to the database */
var conn = libpq.PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (libpq.PQstatus(conn) != Libpq93Def.CONNECTION_OK) {
var message = h.readString(libpq.PQerrorMessage(conn));
print("Connection to database failed: $message");
exit_nicely(libpq, conn);
}
/*
* Our test case here involves using a cursor, for which we must be inside
* a transaction block. We could do the whole thing with a single
* PQexec() of "select * from pg_database", but that's too trivial to make
* a good example.
*/
/* Start a transaction block */
var res = libpq.PQexec(conn, "BEGIN");
if (libpq.PQresultStatus(res) != Libpq93Def.PGRES_COMMAND_OK) {
var message = h.readString(libpq.PQerrorMessage(conn));
print("BEGIN command failed: $message");
libpq.PQclear(res);
exit_nicely(libpq, conn);
}
/*
* Should PQclear PGresult whenever it is no longer needed to avoid memory
* leaks
*/
libpq.PQclear(res);
/*
* Fetch rows from pg_database, the system catalog of databases
*/
res = libpq.PQexec(
conn, "DECLARE myportal CURSOR FOR select * from pg_database");
if (libpq.PQresultStatus(res) != Libpq93Def.PGRES_COMMAND_OK) {
var message = h.readString(libpq.PQerrorMessage(conn));
print("DECLARE CURSOR failed: $message");
libpq.PQclear(res);
exit_nicely(libpq, conn);
}
libpq.PQclear(res);
res = libpq.PQexec(conn, "FETCH ALL in myportal");
if (libpq.PQresultStatus(res) != Libpq93Def.PGRES_TUPLES_OK) {
var message = h.readString(libpq.PQerrorMessage(conn));
print("FETCH ALL failed: $message");
libpq.PQclear(res);
exit_nicely(libpq, conn);
}
/* first, print out the attribute names */
var nFields = libpq.PQnfields(res);
for (var i = 0; i < nFields; i++) {
var value = h.readString(libpq.PQfname(res, i));
print(value);
}
print("\n");
/* next, print out the rows */
for (var i = 0; i < libpq.PQntuples(res); i++) {
for (var j = 0; j < nFields; j++) {
var value = h.readString(libpq.PQgetvalue(res, i, j));
print(value);
}
print("");
}
libpq.PQclear(res);
/* close the portal ... we don't bother to check for errors ... */
res = libpq.PQexec(conn, "CLOSE myportal");
libpq.PQclear(res);
/* end the transaction */
res = libpq.PQexec(conn, "END");
libpq.PQclear(res);
/* close the connection to the database and cleanup */
libpq.PQfinish(conn);
}
exit_nicely(Libpq93Lib libpq, BinaryData conn) {
libpq.PQfinish(conn);
exit(1);
}