dargres 1.0.0
dargres: ^1.0.0 copied to clipboard
Dargres is a pure-Dart PostgreSQL driver based on Python pg8000
dargres #
an attempt to port Tony Locke's pg8000 python library https://github.com/tlocke/pg8000
Dargres is a pure-Dart PostgreSQL driver
this is still experimental, that said, I believe it already works in several scenarios, but it is not yet well tested, the idea is to have as few dependencies as possible and be very fast, at the moment it only depends on the "crypto" and "convert" package , this code was heavily inspired by other PostgreSQL driver implementations and other related projects like
- ✅ https://github.com/tlocke/pg8000
- ✅ https://github.com/wulczer/postgres-on-the-wire
- ✅ https://github.com/jasync-sql/jasync-sql
- ✅ https://github.com/npgsql/npgsql
- ✅ https://github.com/tomyeh/postgresql
- ✅ https://github.com/isoos/postgresql-dart
- ✅ https://github.com/pgjdbc/pgjdbc
- ✅ https://github.com/will/crystal-pg
- ✅ https://github.com/lib/pq
I'm only able to do this implementation thanks to several open source projects, thanks to the entire open source community for creating all these tools.
Currently supports: #
Authentication:
- ✅ CleartextPassword
- ✅ MD5Password
- ✅ SASL SCRAM-SHA-256
Connection:
- ✅ No SSL
- ✅ With SSL
query statement:
- ✅ simple
- ✅ unnamed prepared statement
- ✅ named prepared statement
Transaction:
- ✅ PHP PDO style
- ✅ Closure
Notices And Notifications:
- ✅ Working
Charset supported:
- ✅ latin1
- ✅ utf8
- ✅ ascii
extreme experimental
- ✅ Allow reconnection attempt if postgresql was restarted (allowAttemptToReconnect: true)
Creating a connection with SSL and executing a simple select #
var sslContext = SslContext.createDefaultContext();
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
sslContext: sslContext,
);
await con.connect();
var results = await con.querySimple('select 1');
print('results: $results');
//results: [[1]]
await con.close();
Creating a connection and executing a simple select #
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
);
await con.connect();
var results = await con.querySimple('select 1');
print('results: $results');
//result [[1]]
await con.close();
Create a connection and execute a PHP PDO style transaction #
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
);
await con.connect();
final transaction = await con.beginTransaction();
try {
await transaction.querySimple(
"""INSERT INTO "people" ("name", "dateRegister") VALUES ('Alex', '2022-11-30 16:22:03') returning id""");
await con.commit(transaction);
} catch (e) {
await con.rollBack(transaction);
}
await con.close();
Create a connection and perform a transaction in a closure #
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
);
await con.connect();
await con.runInTransaction((ctx) async {
return ctx.querySimple(
"""INSERT INTO "people" ("name", "dateRegister") VALUES ('Alex', '2022-11-30 16:22:03') returning id""");
});
await con.close();
Executing a prepared statement like PDO #
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
);
await con.connect();
var query = await con.prepareStatement(r'select * from people limit $1',[1]);
var results = await con.executeStatement(query);
print('results: $results');
print('sql: ${query.sql}');
//result: [[1, Alex, 2021-12-31 21:00:00.000]]
//sql: select * from crud_teste.people limit $1
// server log:
//2022-12-12 19:47:22.877 -03 [1956] LOG: execute dargres_statement_0: select * from crud_teste.people limit $1
//2022-12-12 19:47:22.877 -03 [1956] DETAIL: parameters: $1 = '1'
await con.close();
Notices And Notifications it is ideal that creates a dedicated connection to listen to database notifications #
var con = CoreConnection(
'user',
database: 'dataBaseTest',
host: 'localhost',
port: 5432,
password: '123456',
);
await con.connect();
con.notifications.listen((event) async {
// LISTEN
print('$event');
//Result: {backendPid: 9188, channel: db_change_event, payload: This is the payload}
});
await con.execute('LISTEN "db_change_event"');
// NOTIFY
Timer.periodic(Duration(seconds: 2), (t) async {
await con.execute("NOTIFY db_change_event, 'This is the payload'");
});
await con.close();