Skip to content

Commit

Permalink
[Streaming Replication - 2nd] Fix affected rows parsing in CommandCom…
Browse files Browse the repository at this point in the history
…pleteMessage (#52)

* Fix affected rows parsing in CommandCompleteMessage

When using non-standard command such as 'IDENTIFY_SYSTEM', the parsing had an issue where it'll parse `SYSTEM` as the integer. A new reliable regex was added to parse the affected rows correctly if there's any

* rename RegExp from identifierExpression to _affectedRowsExp
  • Loading branch information
osaxma committed Sep 9, 2022
1 parent 02161ad commit 047e59c
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/src/server_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,33 @@ class NotificationResponseMessage extends ServerMessage {
class CommandCompleteMessage extends ServerMessage {
final int rowsAffected;

static RegExp identifierExpression = RegExp(r'[A-Z ]*');
/// Match the digits at the end of the string.
/// Possible values are:
/// ```
/// command-tag | #rows
/// SELECT 1
/// UPDATE 1234
/// DELETE 568
/// MOVE 42
/// FETCH 60
/// COPY 314
/// ```
/// For INSERT, there are three columns:
/// ```
/// | command tag | oid* | #rows |
/// INSERT 0 42
/// ```
/// *oid is only used with `INSERT` and it's always 0.
static final _affectedRowsExp = RegExp(r'\d+$');

CommandCompleteMessage._(this.rowsAffected);

factory CommandCompleteMessage(Uint8List bytes) {
final str = utf8.decode(bytes.sublist(0, bytes.length - 1));
final match = identifierExpression.firstMatch(str);
final match = _affectedRowsExp.firstMatch(str);
var rowsAffected = 0;
if (match != null && match.end < str.length) {
rowsAffected = int.parse(str.split(' ').last);
if (match != null) {
rowsAffected = int.parse(match.group(0)!);
}
return CommandCompleteMessage._(rowsAffected);
}
Expand Down

0 comments on commit 047e59c

Please sign in to comment.