tableName

This commit is contained in:
Tobe O 2019-02-13 00:00:30 -05:00
parent 3fb360147d
commit d2d3e7b93d
13 changed files with 52 additions and 20 deletions

View file

@ -1,3 +1,6 @@
# 2.0.0-dev.21
* Add tableName to query
# 2.0.0-dev.20
* Join updates.

View file

@ -23,7 +23,7 @@ class _FakeExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[returningFields]) async {
var now = new DateTime.now();
print(

View file

@ -13,6 +13,9 @@ abstract class QueryBase<T> {
/// Values to insert into a prepared statement.
final Map<String, dynamic> substitutionValues = {};
/// The table against which to execute this query.
String get tableName;
/// The list of fields returned by this query.
///
/// If it's `null`, then this query will perform a `SELECT *`.
@ -32,7 +35,7 @@ abstract class QueryBase<T> {
Future<List<T>> get(QueryExecutor executor) async {
var sql = compile(Set());
return executor
.query(sql, substitutionValues)
.query(tableName, sql, substitutionValues)
.then((it) => it.map(deserialize).toList());
}
@ -116,9 +119,6 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
String _crossJoin, _groupBy;
int _limit, _offset;
/// The table against which to execute this query.
String get tableName;
/// A reference to an abstract query builder.
///
/// This is usually a generated class.
@ -320,15 +320,17 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
if (_joins.isEmpty) {
return executor
.query(
sql, substitutionValues, fields.map(adornWithTableName).toList())
.query(tableName, sql, substitutionValues,
fields.map(adornWithTableName).toList())
.then((it) => it.map(deserialize).toList());
} else {
return executor.transaction(() async {
// TODO: Can this be done with just *one* query?
var existing = await get(executor);
//var sql = compile(preamble: 'SELECT $tableName.id', withFields: false);
return executor.query(sql, substitutionValues).then((_) => existing);
return executor
.query(tableName, sql, substitutionValues)
.then((_) => existing);
});
}
}
@ -348,7 +350,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var sql = compile(Set());
sql = 'WITH $tableName as ($insertion RETURNING $returning) ' + sql;
return executor
.query(sql, substitutionValues)
.query(tableName, sql, substitutionValues)
.then((it) => it.isEmpty ? null : deserialize(it.first));
}
}
@ -370,7 +372,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
sql = 'WITH $tableName as ($updateSql RETURNING $returning) ' + sql;
return executor
.query(sql, substitutionValues)
.query(tableName, sql, substitutionValues)
.then((it) => it.map(deserialize).toList());
}
}
@ -504,10 +506,17 @@ abstract class QueryWhere {
/// Represents the `UNION` of two subqueries.
class Union<T> extends QueryBase<T> {
/// The subject(s) of this binary operation.
final QueryBase<T> left, right;
/// Whether this is a `UNION ALL` operation.
final bool all;
Union(this.left, this.right, {this.all: false}) {
@override
final String tableName;
Union(this.left, this.right, {this.all: false, String tableName})
: this.tableName = tableName ?? left.tableName {
substitutionValues
..addAll(left.substitutionValues)
..addAll(right.substitutionValues);
@ -598,9 +607,11 @@ class JoinOn {
abstract class QueryExecutor {
const QueryExecutor();
/// Executes a single query.
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]);
/// Begins a database transaction.
Future<T> transaction<T>(FutureOr<T> f());
}

View file

@ -1,5 +1,5 @@
name: angel_orm
version: 2.0.0-dev.20
version: 2.0.0-dev.21
description: Runtime support for Angel's ORM. Includes base classes for queries.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm

View file

@ -23,7 +23,7 @@ class _FakeExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[returningFields]) async {
var now = new DateTime.now();
print(

View file

@ -24,7 +24,7 @@ class PostgresExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
if (returningFields != null) {
var fields = returningFields.join(', ');

View file

View file

View file

@ -0,0 +1,14 @@
name: angel_orm_mysql
version: 1.0.0-dev
description: MySQL support for Angel's ORM. Includes functionality for querying and transactions.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm
environment:
sdk: '>=2.0.0-dev.1.2 <3.0.0'
dependencies:
angel_orm: ^2.0.0-dev
logging: ^0.11.0
pool: ^1.0.0
sqljocky5: ^2.0.0
dev_dependencies:
test: ^1.0.0

View file

@ -1,3 +1,6 @@
# 1.0.0-dev.3
* Support for `tableName`.
# 1.0.0-dev.2
* Add optional logging.

View file

@ -7,6 +7,6 @@ main() async {
return new PostgreSQLConnection('localhost', 5432, 'angel_orm_test');
});
var rows = await executor.query('SELECT * FROM users', {});
var rows = await executor.query('users', 'SELECT * FROM users', {});
print(rows);
}

View file

@ -21,7 +21,7 @@ class PostgreSQLExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
if (returningFields != null) {
var fields = returningFields.join(', ');
@ -104,11 +104,12 @@ class PostgreSQLExecutorPool extends QueryExecutor {
@override
Future<List<List>> query(
String query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String> returningFields]) {
return _pool.withResource(() async {
var executor = await _next();
return executor.query(query, substitutionValues, returningFields);
return executor.query(
tableName, query, substitutionValues, returningFields);
});
}

View file

@ -1,5 +1,5 @@
name: angel_orm_postgres
version: 1.0.0-dev.2
version: 1.0.0-dev.3
description: PostgreSQL support for Angel's ORM. Includes functionality for querying and transactions.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm