Skip to content

Commit 4d54e6f

Browse files
authored
Ensure example works as a standalone script (#236)
1 parent 66e48fc commit 4d54e6f

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

.github/workflows/dart.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ jobs:
4545
- name: Run tests
4646
run: dart test --coverage=coverage
4747

48+
- name: Test example
49+
run: |
50+
docker run --detach --name postgres_for_dart_test -p 127.0.0.1:5432:5432 -e POSTGRES_USER=user -e POSTGRES_DATABASE=database -e POSTGRES_PASSWORD=pass postgres
51+
dart run example/example.dart
52+
docker container rm --force postgres_for_dart_test
53+
4854
# https://www.bradcypert.com/how-to-upload-coverage-to-codecov-for-dart/
4955
- name: Install coverage tools
5056
run: dart pub global activate coverage

example/example.dart

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
1+
/// Example for the `postgres` package.
2+
///
3+
/// Running the example requires access to a postgres server. If you have docker
4+
/// installed, you can start a postgres server to run this example with
5+
///
6+
/// ```
7+
/// docker run --detach --name postgres_for_dart_test -p 127.0.0.1:5432:5432 -e POSTGRES_USER=user -e POSTGRES_DATABASE=database -e POSTGRES_PASSWORD=pass postgres
8+
/// ```
9+
///
10+
/// To stop and clear the database server once you're done testing, run
11+
///
12+
/// ```
13+
/// docker container rm --force postgres_for_dart_test
14+
/// ```
15+
library;
16+
117
import 'package:postgres/postgres.dart';
218

319
void main() async {
4-
final conn = await Connection.open(Endpoint(
5-
host: 'localhost',
6-
database: 'postgres',
7-
username: 'user',
8-
password: 'pass',
9-
));
20+
final conn = await Connection.open(
21+
Endpoint(
22+
host: 'localhost',
23+
database: 'postgres',
24+
username: 'user',
25+
password: 'pass',
26+
),
27+
// The postgres server hosted locally doesn't have SSL by default. If you're
28+
// accessing a postgres server over the Internet, the server should support
29+
// SSL and you should swap out the mode with `SslMode.verifyFull`.
30+
settings: ConnectionSettings(sslMode: SslMode.disable),
31+
);
1032
print('has connection!');
1133

34+
// Simple query without results
35+
await conn.execute('CREATE TABLE IF NOT EXISTS a_table ('
36+
' id TEXT NOT NULL, '
37+
' totals INTEGER NOT NULL DEFAULT 0'
38+
')');
39+
1240
// simple query
1341
final result0 = await conn.execute("SELECT 'foo'");
1442
print(result0[0][0]); // first row, first column
1543

16-
// name parameter query
44+
// Using prepared statements to supply values
1745
final result1 = await conn.execute(
46+
r'INSERT INTO a_table (id) VALUES ($1)',
47+
parameters: ['example row'],
48+
);
49+
print('Inserted ${result1.affectedRows} rows');
50+
51+
// name parameter query
52+
final result2 = await conn.execute(
1853
Sql.named('SELECT * FROM a_table WHERE id=@id'),
19-
parameters: {'id': 'xyz'},
54+
parameters: {'id': 'example row'},
2055
);
21-
print(result1.first.toColumnMap());
56+
print(result2.first.toColumnMap());
2257

2358
// transaction
2459
await conn.runTx((s) async {
25-
final rs = await s.execute('SELECT count(*) FROM foo');
60+
final rs = await s.execute('SELECT count(*) FROM a_table');
2661
await s.execute(
2762
r'UPDATE a_table SET totals=$1 WHERE id=$2',
2863
parameters: [rs[0][0], 'xyz'],
@@ -31,8 +66,8 @@ void main() async {
3166

3267
// prepared statement
3368
final statement = await conn.prepare(Sql("SELECT 'foo';"));
34-
final result2 = await statement.run([]);
35-
print(result2);
69+
final result3 = await statement.run([]);
70+
print(result3);
3671
await statement.dispose();
3772

3873
// preared statement with types

0 commit comments

Comments
 (0)