Skip to content

Commit

Permalink
Added rowsAffected to PostgreSQLResult (#143)
Browse files Browse the repository at this point in the history
* Added rowsAffected to PostgreSQLResult which is returned by `PostgreSQLExecutionContext.query`


Currently `PostgreSQLExecutionContext.query` does not have a way to know how many rows did that particular
query affected. The only way to get that information is to use `PostgreSQLExecutionContext.execute`.

Unfortunately `execute` uses slower, less efficient encoding to talk to the PostgreSQL
server and does not support all data types that `query` supports.

This allows you to use `PostgreSQLExecutionContext.query` for INSERT, UPDATE, DELETE statements
and get the affected row count.

* Renamed rowsAffected -> affectedRowCount

Asked in #143 (comment)

* dartfmt

Co-authored-by: Arturas Slajus <[email protected]>
  • Loading branch information
arturaz and arturaz authored Sep 7, 2020
1 parent 29a4155 commit 64c6ed2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
21 changes: 14 additions & 7 deletions lib/src/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ abstract class _PostgreSQLExecutionContextMixin
query.statementIdentifier = _connection._cache.identifierForQuery(query);
}

final rows = await _enqueue(query, timeoutInSeconds: timeoutInSeconds);
final queryResult =
await _enqueue(query, timeoutInSeconds: timeoutInSeconds);
var columnDescriptions = query.fieldDescriptions;
if (resolveOids) {
columnDescriptions = await _connection._oidCache
Expand All @@ -453,8 +454,9 @@ abstract class _PostgreSQLExecutionContextMixin
final metaData = _PostgreSQLResultMetaData(columnDescriptions);

return _PostgreSQLResult(
queryResult.affectedRowCount,
metaData,
rows
queryResult.value
.map((columns) => _PostgreSQLResultRow(metaData, columns))
.toList());
}
Expand All @@ -476,24 +478,26 @@ abstract class _PostgreSQLExecutionContextMixin

@override
Future<int> execute(String fmtString,
{Map<String, dynamic> substitutionValues, int timeoutInSeconds}) {
{Map<String, dynamic> substitutionValues, int timeoutInSeconds}) async {
timeoutInSeconds ??= _connection.queryTimeoutInSeconds;
if (_connection.isClosed) {
throw PostgreSQLException(
'Attempting to execute query, but connection is not open.');
}

final query = Query<int>(
final query = Query<void>(
fmtString, substitutionValues, _connection, _transaction,
onlyReturnAffectedRowCount: true);

return _enqueue(query, timeoutInSeconds: timeoutInSeconds);
final result = await _enqueue(query, timeoutInSeconds: timeoutInSeconds);
return result.affectedRowCount;
}

@override
void cancelTransaction({String reason});

Future<T> _enqueue<T>(Query<T> query, {int timeoutInSeconds = 30}) async {
Future<QueryResult<T>> _enqueue<T>(Query<T> query,
{int timeoutInSeconds = 30}) async {
if (_queue.add(query)) {
_connection._transitionToState(_connection._connectionState.awake());

Expand Down Expand Up @@ -535,9 +539,12 @@ class _PostgreSQLResultMetaData {

class _PostgreSQLResult extends UnmodifiableListView<PostgreSQLResultRow>
implements PostgreSQLResult {
@override
final int affectedRowCount;
final _PostgreSQLResultMetaData _metaData;

_PostgreSQLResult(this._metaData, List<PostgreSQLResultRow> rows)
_PostgreSQLResult(
this.affectedRowCount, this._metaData, List<PostgreSQLResultRow> rows)
: super(rows);

@override
Expand Down
2 changes: 2 additions & 0 deletions lib/src/execution_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ abstract class PostgreSQLResultRow implements List {
///
/// Rows can be accessed through the `[]` operator.
abstract class PostgreSQLResult implements List<PostgreSQLResultRow> {
/// How many rows did this query affect?
int get affectedRowCount;
List<ColumnDescription> get columnDescriptions;
}
15 changes: 11 additions & 4 deletions lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Query<T> {

String statementIdentifier;

Future<T> get future => _onComplete.future;
Future<QueryResult<T>> get future => _onComplete.future;

final String statement;
final Map<String, dynamic> substitutionValues;
Expand All @@ -38,7 +38,7 @@ class Query<T> {

CachedQuery cache;

final _onComplete = Completer<T>.sync();
final _onComplete = Completer<QueryResult<T>>.sync();
List<FieldDescription> _fieldDescriptions;

List<FieldDescription> get fieldDescriptions => _fieldDescriptions;
Expand Down Expand Up @@ -154,11 +154,11 @@ class Query<T> {
}

if (onlyReturnAffectedRowCount) {
_onComplete.complete(rowsAffected as T);
_onComplete.complete(QueryResult(rowsAffected, null));
return;
}

_onComplete.complete(rows as T);
_onComplete.complete(QueryResult(rowsAffected, rows as T));
}

void completeError(dynamic error, [StackTrace stackTrace]) {
Expand All @@ -173,6 +173,13 @@ class Query<T> {
String toString() => statement;
}

class QueryResult<T> {
final int affectedRowCount;
final T value;

const QueryResult(this.affectedRowCount, this.value);
}

class CachedQuery {
CachedQuery(this.preparedStatementName, this.orderedParameters);

Expand Down

0 comments on commit 64c6ed2

Please sign in to comment.