Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #2

Merged
merged 5 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/src/single_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'query/query_stream_handler.dart';
import 'results/field.dart';
import 'results/row.dart';

final Logger _log = new Logger("SingleConnection");
final Logger _log = new Logger("MySqlConnection");

class ConnectionSettings {
String host;
Expand Down Expand Up @@ -143,6 +143,9 @@ class MySqlConnection {
return new MySqlConnection(c.timeout, conn);
}

/// Run [sql] query on the database using [values] as positional sql parameters.
///
/// eg. ```query("SELECT FROM users WHERE id = ?", [userId])```.
Future<Results> query(String sql, [Iterable<Object> values]) async {
if (values == null || values.isEmpty) {
return _conn.processHandlerWithResults(
Expand All @@ -152,6 +155,9 @@ class MySqlConnection {
return (await queryMulti(sql, [values])).first;
}

/// Run [sql] query multiple times for each set of positional sql parameters in [values].
///
/// e.g. ```queryMulti("INSERT INTO USERS (name) VALUES (?)", ["Adam", "Eve"])```.
Future<List<Results>> queryMulti(
String sql, Iterable<Iterable<Object>> values) async {
PreparedQuery prepared;
Expand Down Expand Up @@ -202,23 +208,22 @@ class TransactionContext {

class _RollbackError {}

/// An iterable of result rows returned by [MySqlConnection.query] or [MySqlConnection.queryMulti].
class Results extends IterableBase<Row> {
final int insertId;
final int affectedRows;
final List<Field> fields;
final List<Row> _rows;

Results(this._rows, this.fields, this.insertId, this.affectedRows);
Results._(this._rows, this.fields, this.insertId, this.affectedRows);

static Future<Results> read(ResultsStream r) async {
static Future<Results> _read(ResultsStream r) async {
var rows = await r.toList();
return new Results(rows, r.fields, r.insertId, r.affectedRows);
return new Results._(rows, r.fields, r.insertId, r.affectedRows);
}

@override
Iterator<Row> get iterator {
return _rows.iterator;
}
Iterator<Row> get iterator => _rows.iterator;
}

class ReqRespConnection {
Expand Down Expand Up @@ -457,7 +462,7 @@ class ReqRespConnection {
ResultsStream results = await _processHandler(handler).timeout(timeout);
// Read all of the results. This is so we can close the handler before returning to the
// user. Obviously this is not super efficient but it guarantees correct api use.
Results ret = await Results.read(results).timeout(timeout);
Results ret = await Results._read(results).timeout(timeout);
return ret;
} finally {
_handler = null;
Expand Down
5 changes: 0 additions & 5 deletions test/integration/row_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

library mysql1.test.row_test;

import 'dart:async';

import 'package:mysql1/mysql1.dart';
import 'package:test/test.dart';

import '../test_infrastructure.dart';

const tableName = 'row1';

void main() {}
Expand Down
9 changes: 0 additions & 9 deletions test/integration_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
library integrationtests;

import 'dart:async';

import 'package:logging/logging.dart';
import 'package:options_file/options_file.dart';
import 'package:mysql1/mysql1.dart';
import 'package:test/test.dart';

import 'test_util.dart';

part 'integration/errors.dart';
part 'integration/largeblob.dart';
part 'integration/nullmap.dart';
Expand Down
8 changes: 0 additions & 8 deletions test/interleave_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import 'dart:async';
import 'dart:math';

import 'package:logging/logging.dart';
import 'package:options_file/options_file.dart';
import 'package:mysql1/mysql1.dart';
import 'package:test/test.dart';

/*
* This example drops a couple of tables if they exist, before recreating them.
* It then stores some data in the database and reads it back out.
Expand Down
6 changes: 3 additions & 3 deletions test/unit/binary_data_packet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
var buffer = new Buffer.fromList([1, 32]);
var value = packet.readField(field, buffer);

expect(value, new isInstanceOf<Blob>());
expect(value, TypeMatcher<Blob>());
expect((value as Blob).toString(), equals(" "));
});

Expand All @@ -31,7 +31,7 @@ void main() {
}
var value = packet.readField(field, buffer);

expect(value, new isInstanceOf<Blob>());
expect(value, TypeMatcher<Blob>());
expect((value as Blob).toString(), hasLength(500));
});

Expand All @@ -46,7 +46,7 @@ void main() {
}
var value = packet.readField(field, buffer);

expect(value, new isInstanceOf<Blob>());
expect(value, TypeMatcher<Blob>());
expect((value as Blob).toString(), hasLength(50000));
});
});
Expand Down
20 changes: 8 additions & 12 deletions test/unit/field_by_name_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
library mysql1.test.unit.field_by_name_test;

import 'package:mysql1/src/prepared_statements/binary_data_packet.dart';
import 'package:mysql1/src/constants.dart';
import 'package:mysql1/src/prepared_statements/execute_query_handler.dart';
import 'package:mysql1/src/query/query_stream_handler.dart';
import 'package:mysql1/src/query/standard_data_packet.dart';
import 'package:mysql1/src/results/field.dart';
import 'package:mysql1/src/constants.dart';
import 'package:mysql1/src/results/row.dart';

import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -42,18 +38,18 @@ void main() {
expect(fieldIndex.keys, contains(new Symbol("a123")));
});

test('should call noSuchMethod', () {
var fieldIndex = new Map<Symbol, int>();
fieldIndex[new Symbol("one")] = 0;
fieldIndex[new Symbol("two")] = 1;
fieldIndex[new Symbol("three")] = 2;
var values = [5, "hello", null];
// test('should call noSuchMethod', () {
// var fieldIndex = new Map<Symbol, int>();
// fieldIndex[new Symbol("one")] = 0;
// fieldIndex[new Symbol("two")] = 1;
// fieldIndex[new Symbol("three")] = 2;
// var values = [5, "hello", null];

// Row row = new StandardDataPacket.forTests(values, fieldIndex);
// expect(row.one, equals(5));
// expect(row.two, equals("hello"));
// expect(row.three, equals(null));
});
// });

// test('should fail for non-existent properties', () {
// var fieldIndex = new Map<Symbol, int>();
Expand Down