Skip to content

Commit a815fbb

Browse files
committed
Merge pull request #62 from kevmoo/tweaks
Tweaks
2 parents c6db384 + eb4c8da commit a815fbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+836
-738
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ connection.options
55
*.iml
66
examples
77
docs
8+
.packages
9+
.pub
810
packages
911
pubspec.lock
1012
*~

README.md

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,116 @@ Usage
1717

1818
Create a connection pool:
1919

20-
var pool = new ConnectionPool(host: 'localhost', port: 3306, user: 'bob', password: 'wibble', db: 'stuff', max: 5);
20+
```dart
21+
var pool = new ConnectionPool(
22+
host: 'localhost', port: 3306,
23+
user: 'bob', password: 'wibble',
24+
db: 'stuff', max: 5);
25+
```
2126

2227
Execute a query:
2328

24-
var results = await pool.query('select name, email from users');
29+
```dart
30+
var results = await pool.query('select name, email from users');
31+
```
2532

2633
Use the results: (*Note: forEach is asynchronous.*)
2734

28-
results.forEach((row) {
29-
print('Name: ${row[0]}, email: ${row[1]}');
30-
});
31-
35+
```dart
36+
results.forEach((row) {
37+
print('Name: ${row[0]}, email: ${row[1]}');
38+
});
39+
```
40+
3241
Or access the fields by name:
3342

34-
results.forEach((row) {
35-
print('Name: ${row.name}, email: ${row.email}');
36-
});
43+
```dart
44+
results.forEach((row) {
45+
print('Name: ${row.name}, email: ${row.email}');
46+
});
47+
```
3748

3849
Prepare a query:
3950

40-
var query = await pool.prepare('insert into users (name, email, age) values (?, ?, ?)');
51+
```dart
52+
var query = await pool.prepare(
53+
'insert into users (name, email, age) values (?, ?, ?)');
54+
```
4155

4256
Execute the query:
4357

44-
var result = await query.execute(['Bob', '[email protected]', 25]);
58+
```dart
59+
var result = await query.execute(['Bob', '[email protected]', 25]);
60+
```
4561

4662
An insert query's results will be empty, but will have an id if there was an auto-increment column in the table:
4763

48-
print("New user's id: ${result.insertId}");
64+
```dart
65+
print("New user's id: ${result.insertId}");
66+
```
4967

5068
Execute a query with multiple sets of parameters:
5169

52-
var results = await query.executeMulti([['Bob', '[email protected]', 25],
53-
['Bill', '[email protected]', 26],
54-
['Joe', '[email protected]', 37]]);
55-
70+
```dart
71+
var results = await query.executeMulti([['Bob', '[email protected]', 25],
72+
['Bill', '[email protected]', 26],
73+
['Joe', '[email protected]', 37]]);
74+
```
75+
5676
Use the list of results:
5777

58-
for (result in results) {
59-
print("New user's id: ${result.insertId}");
60-
}
78+
```dart
79+
for (result in results) {
80+
print("New user's id: ${result.insertId}");
81+
}
82+
```
6183

6284
Use a transaction:
6385

64-
var trans = await pool.startTransaction();
65-
var result = await trans.query('...');
66-
await trans.commit();
86+
```dart
87+
var trans = await pool.startTransaction();
88+
var result = await trans.query('...');
89+
await trans.commit();
90+
```
6791

6892
Development
6993
-----------
7094

7195
To run the examples and tests, you'll need to create a 'connection.options' file by
7296
copying 'connection.options.example' and modifying the settings.
7397

98+
You can use Docker to create a temporary mysql instance for testing:
99+
100+
```
101+
docker run --name mysql_test -d -P \
102+
-e MYSQL_ROOT_PASSWORD=root \
103+
-e MYSQL_USER=test \
104+
-e MYSQL_PASSWORD=test \
105+
-e MYSQL_DATABASE=test \
106+
mysql
107+
```
108+
109+
This will create and run a Docker container with the name `mysql_test`.
110+
111+
You can determine the exposed port by running
112+
113+
```
114+
docker port mysql_test
115+
```
116+
117+
*Note: depending on your Docker configuration, you will likely have to change
118+
the host IP address.*
119+
74120
Licence
75121
-------
76122

77-
It is released under the GPL, because it uses a modified part of mysql's include/mysql_com.h in constants.dart,
123+
It is released under the GPL, because it uses a modified part of mysql's include/mysql_com.h in constants.dart,
78124
which is licensed under the GPL. I would prefer to release it under the BSD Licence, but there you go.
79125

80126
The Name
81127
--------
82128

83-
It is named after [Jocky Wilson](http://en.wikipedia.org/wiki/Jocky_Wilson), the late, great
129+
It is named after [Jocky Wilson](http://en.wikipedia.org/wiki/Jocky_Wilson), the late, great
84130
darts player. (Hence the lack of an 'e' in Jocky.)
85131

86132
Things to do
@@ -99,4 +145,3 @@ necessary to support anything else?
99145
* Decimal type should probably use a bigdecimal type of some sort
100146
* MySQL 4 types (old decimal, anything else?)
101147
* Test against multiple mysql versions
102-
Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1+
import 'dart:async';
2+
3+
import 'package:logging/logging.dart';
4+
import 'package:options_file/options_file.dart';
15
import 'package:sqljocky/sqljocky.dart';
26
import 'package:sqljocky/utils.dart';
3-
import 'package:options_file/options_file.dart';
4-
import 'package:logging/logging.dart';
5-
6-
import 'dart:async';
77

88
class SpeedTest {
99
static const SIMPLE_INSERTS = 200;
1010
static const PREPARED_INSERTS = 200;
1111
static const POOL_SIZE = 1;
12-
12+
1313
ConnectionPool pool;
1414
Logger log;
15-
16-
SpeedTest(this.pool) :
17-
log = new Logger("Speed");
18-
15+
16+
SpeedTest(this.pool) : log = new Logger("Speed");
17+
1918
Future run() async {
2019
await dropTables();
2120
await createTables();
2221
await insertSimpleData();
2322
await insertPreparedData();
2423
await pool.closeConnectionsWhenNotInUse();
2524
}
26-
25+
2726
Future dropTables() {
2827
log.fine("dropping tables");
2928
var dropper = new TableDropper(pool, ['pets', 'people']);
3029
return dropper.dropTables();
3130
}
32-
31+
3332
Future createTables() {
3433
log.fine("creating tables");
35-
var querier = new QueryRunner(pool, ['create table people (id integer not null auto_increment, '
36-
'name varchar(255), '
37-
'age integer, '
38-
'primary key (id))',
39-
40-
'create table pets (id integer not null auto_increment, '
41-
'name varchar(255), '
42-
'species varchar(255), '
43-
'owner_id integer, '
44-
'primary key (id),'
45-
'foreign key (owner_id) references people (id))'
46-
]);
34+
var querier = new QueryRunner(pool, [
35+
'create table people (id integer not null auto_increment, '
36+
'name varchar(255), '
37+
'age integer, '
38+
'primary key (id))',
39+
'create table pets (id integer not null auto_increment, '
40+
'name varchar(255), '
41+
'species varchar(255), '
42+
'owner_id integer, '
43+
'primary key (id),'
44+
'foreign key (owner_id) references people (id))'
45+
]);
4746
log.fine("executing queries");
4847
return querier.executeQueries();
4948
}
50-
49+
5150
Future insertSimpleData() async {
5251
log.fine("inserting simple data");
5352
var sw = new Stopwatch()..start();
@@ -59,7 +58,7 @@ class SpeedTest {
5958
logTime("simple insertions", sw);
6059
log.fine("inserted");
6160
}
62-
61+
6362
Future insertPreparedData() async {
6463
log.fine("inserting prepared data");
6564
var sw = new Stopwatch()..start();
@@ -72,10 +71,10 @@ class SpeedTest {
7271
logTime("prepared insertions", sw);
7372
log.fine("inserted");
7473
}
75-
74+
7675
void logTime(String operation, Stopwatch sw) {
7776
var time = sw.elapsedMicroseconds;
78-
var seconds = time/1000000;
77+
var seconds = time / 1000000;
7978
log.fine("$operation took: ${seconds}s");
8079
}
8180
}
@@ -92,7 +91,7 @@ main() async {
9291

9392
var log = new Logger("Speed");
9493
log.level = Level.ALL;
95-
94+
9695
var options = new OptionsFile('connection.options');
9796
var user = options.getString('user');
9897
var password = options.getString('password');
@@ -102,13 +101,13 @@ main() async {
102101

103102
// create a connection
104103
log.fine("opening connection");
105-
var pool = new ConnectionPool(host: host, port: port, user: user,
106-
password: password, db: db, max: SpeedTest.POOL_SIZE);
104+
var pool =
105+
new ConnectionPool(host: host, port: port, user: user, password: password, db: db, max: SpeedTest.POOL_SIZE);
107106
log.fine("connection open");
108107

109108
var stopwatch = new Stopwatch()..start();
110109
await new SpeedTest(pool).run();
111110
var time = stopwatch.elapsedMicroseconds;
112-
var seconds = time/1000000;
111+
var seconds = time / 1000000;
113112
log.fine("Time taken: ${seconds}s");
114-
}
113+
}

example/example.dart

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import 'dart:async';
1111

1212
class Example {
1313
ConnectionPool pool;
14-
14+
1515
Example(this.pool);
16-
16+
1717
Future run() async {
1818
// drop the tables if they already exist
1919
await dropTables();
@@ -32,49 +32,50 @@ class Example {
3232
var dropper = new TableDropper(pool, ['pets', 'people']);
3333
return dropper.dropTables();
3434
}
35-
35+
3636
Future createTables() {
3737
print("creating tables");
38-
var querier = new QueryRunner(pool, ['create table people (id integer not null auto_increment, '
39-
'name varchar(255), '
40-
'age integer, '
41-
'primary key (id))',
42-
43-
'create table pets (id integer not null auto_increment, '
44-
'name varchar(255), '
45-
'species text, '
46-
'owner_id integer, '
47-
'primary key (id),'
48-
'foreign key (owner_id) references people (id))'
49-
]);
38+
var querier = new QueryRunner(pool, [
39+
'create table people (id integer not null auto_increment, '
40+
'name varchar(255), '
41+
'age integer, '
42+
'primary key (id))',
43+
'create table pets (id integer not null auto_increment, '
44+
'name varchar(255), '
45+
'species text, '
46+
'owner_id integer, '
47+
'primary key (id),'
48+
'foreign key (owner_id) references people (id))'
49+
]);
5050
print("executing queries");
5151
return querier.executeQueries();
5252
}
53-
53+
5454
Future addData() async {
5555
var query = await pool.prepare("insert into people (name, age) values (?, ?)");
5656
print("prepared query 1");
5757
var parameters = [
58-
["Dave", 15],
59-
["John", 16],
60-
["Mavis", 93]
61-
];
62-
var results = await query.executeMulti(parameters);
58+
["Dave", 15],
59+
["John", 16],
60+
["Mavis", 93]
61+
];
62+
await query.executeMulti(parameters);
6363

6464
print("executed query 1");
6565
query = await pool.prepare("insert into pets (name, species, owner_id) values (?, ?, ?)");
6666

6767
print("prepared query 2");
6868
parameters = [
69-
["Rover", "Dog", 1],
70-
["Daisy", "Cow", 2],
71-
["Spot", "Dog", 2]];
69+
["Rover", "Dog", 1],
70+
["Daisy", "Cow", 2],
71+
["Spot", "Dog", 2]
72+
];
7273
// ["Spot", "D\u0000og", 2]];
73-
results = await query.executeMulti(parameters);
74+
await query.executeMulti(parameters);
7475

7576
print("executed query 2");
7677
}
77-
78+
7879
Future readData() async {
7980
print("querying");
8081
var result = await pool.query('select p.id, p.name, p.age, t.name, t.species '
@@ -101,7 +102,7 @@ main() async {
101102

102103
// create a connection
103104
print("opening connection");
104-
var pool = new ConnectionPool(host: host, port: port, user: user, password: password, db: db, max:1);
105+
var pool = new ConnectionPool(host: host, port: port, user: user, password: password, db: db, max: 1);
105106
print("connection open");
106107
// create an example class
107108
var example = new Example(pool);

0 commit comments

Comments
 (0)