Skip to content

Commit

Permalink
Move some common functionality in both StandardDataPacket and `Bina…
Browse files Browse the repository at this point in the history
…ryDataPacket` up to their parent class `Row`
  • Loading branch information
bobjackman committed Apr 18, 2019
1 parent 1af1340 commit 1723b6d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 107 deletions.
2 changes: 1 addition & 1 deletion connection.options
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ user=root
port=3306
db=sqljockeytest
#defaults to localhost
host=localhost
host=localhost
36 changes: 10 additions & 26 deletions lib/src/prepared_statements/binary_data_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ import '../results/field.dart';
import '../results/row.dart';

class BinaryDataPacket extends Row {
List<Object> values;
final Logger log;
final Logger log = new Logger("BinaryDataPacket");

BinaryDataPacket.forTests(this.values) : log = new Logger("BinaryDataPacket");
BinaryDataPacket.forTests(List _values) { values = _values; }

BinaryDataPacket(Buffer buffer, List<Field> fields)
: log = new Logger("BinaryDataPacket") {
BinaryDataPacket(Buffer buffer, List<Field> fieldPackets) {
buffer.skip(1);
var nulls = buffer.readList(((fields.length + 7 + 2) / 8).floor().toInt());
var nulls = buffer.readList(((fieldPackets.length + 7 + 2) / 8).floor().toInt());
log.fine("Nulls: $nulls");
var nullMap = new List<bool>(fields.length);
var nullMap = new List<bool>(fieldPackets.length);
var shift = 2;
var byte = 0;
for (var i = 0; i < fields.length; i++) {
for (var i = 0; i < fieldPackets.length; i++) {
var mask = 1 << shift;
nullMap[i] = (nulls[byte] & mask) != 0;
shift++;
Expand All @@ -34,15 +32,15 @@ class BinaryDataPacket extends Row {
}
}

values = new List(fields.length);
for (var i = 0; i < fields.length; i++) {
log.fine("$i: ${fields[i].name}");
values = new List<dynamic>(fieldPackets.length);
for (var i = 0; i < fieldPackets.length; i++) {
log.fine("$i: ${fieldPackets[i].name}");
if (nullMap[i]) {
log.fine("Value: null");
values[i] = null;
continue;
}
var field = fields[i];
var field = fieldPackets[i];
values[i] = readField(field, buffer);
}
}
Expand Down Expand Up @@ -218,18 +216,4 @@ class BinaryDataPacket extends Row {
}
return null;
}

int get length => values.length;

dynamic operator [](int index) => values[index];

void operator []=(int index, dynamic value) {
throw new UnsupportedError("Cannot modify row");
}

set length(int newLength) {
throw new UnsupportedError("Cannot set length of results");
}

String toString() => "Value: $values";
}
153 changes: 74 additions & 79 deletions lib/src/query/standard_data_packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
library mysql1.standard_data_packet;

import 'dart:convert';
import 'package:logging/logging.dart';

import '../constants.dart';
import '../blob.dart';
Expand All @@ -12,93 +13,87 @@ import '../results/row.dart';
import '../results/field.dart';

class StandardDataPacket extends Row {
/// Values as List
final List<dynamic> values;
final Logger log = new Logger("StandardDataPacket");

/// Values as Map
final Map<String, dynamic> fields = <String, dynamic>{};

StandardDataPacket(Buffer buffer, List<Field> fieldPackets)
: values = new List<dynamic>(fieldPackets.length) {
StandardDataPacket(Buffer buffer, List<Field> fieldPackets) {
values = new List<dynamic>(fieldPackets.length);
for (var i = 0; i < fieldPackets.length; i++) {
var list;
int length = buffer.readLengthCodedBinary();
if (length != null) {
list = buffer.readList(length);
} else {
values[i] = null;
continue;
}
switch (fieldPackets[i].type) {
case FIELD_TYPE_TINY: // tinyint/bool
case FIELD_TYPE_SHORT: // smallint
case FIELD_TYPE_INT24: // mediumint
case FIELD_TYPE_LONGLONG: // bigint/serial
case FIELD_TYPE_LONG: // int
var s = utf8.decode(list);
values[i] = int.parse(s);
break;
case FIELD_TYPE_NEWDECIMAL: // decimal
case FIELD_TYPE_FLOAT: // float
case FIELD_TYPE_DOUBLE: // double
var s = utf8.decode(list);
values[i] = double.parse(s);
break;
case FIELD_TYPE_BIT: // bit
var value = 0;
for (var num in list) {
value = (value << 8) + num;
}
values[i] = value;
break;
case FIELD_TYPE_DATE: // date
case FIELD_TYPE_DATETIME: // datetime
case FIELD_TYPE_TIMESTAMP: // timestamp
var s = utf8.decode(list);
values[i] = DateTime.parse(s).toUtc();
break;
case FIELD_TYPE_TIME: // time
var s = utf8.decode(list);
var parts = s.split(":");
values[i] = new Duration(
days: 0,
hours: int.parse(parts[0]),
minutes: int.parse(parts[1]),
seconds: int.parse(parts[2]),
milliseconds: 0);
break;
case FIELD_TYPE_YEAR: // year
var s = utf8.decode(list);
values[i] = int.parse(s);
break;
case FIELD_TYPE_STRING: // char/binary/enum/set
case FIELD_TYPE_VAR_STRING: // varchar/varbinary
var s = utf8.decode(list);
values[i] = s;
break;
case FIELD_TYPE_BLOB: // tinytext/text/mediumtext/longtext/tinyblob/mediumblob/blob/longblob
values[i] = new Blob.fromBytes(list);
break;
case FIELD_TYPE_GEOMETRY: // geometry
var s = utf8.decode(list);
values[i] = s;
break;
}
fields[fieldPackets[i].name] = values[i];
}
}

StandardDataPacket.forTests(this.values);

int get length => values.length;
var field = fieldPackets[i];

dynamic operator [](int index) => values[index];
log.fine("$i: ${field.name}");

void operator []=(int index, dynamic value) {
throw new UnsupportedError("Cannot modify row");
values[i] = readField(field, buffer);
fields[field.name] = values[i];
}
}

set length(int newLength) {
throw new UnsupportedError("Cannot set length of results");
Object readField(Field field, Buffer buffer) {
var list;
int length = buffer.readLengthCodedBinary();
if (length != null) {
list = buffer.readList(length);
} else {
return null;
}

switch (field.type) {
case FIELD_TYPE_TINY: // tinyint/bool
case FIELD_TYPE_SHORT: // smallint
case FIELD_TYPE_INT24: // mediumint
case FIELD_TYPE_LONGLONG: // bigint/serial
case FIELD_TYPE_LONG: // int
var s = utf8.decode(list);
return int.parse(s);
break;
case FIELD_TYPE_NEWDECIMAL: // decimal
case FIELD_TYPE_FLOAT: // float
case FIELD_TYPE_DOUBLE: // double
var s = utf8.decode(list);
return double.parse(s);
break;
case FIELD_TYPE_BIT: // bit
var value = 0;
for (var num in list) {
value = (value << 8) + num;
}
return value;
break;
case FIELD_TYPE_DATE: // date
case FIELD_TYPE_DATETIME: // datetime
case FIELD_TYPE_TIMESTAMP: // timestamp
var s = utf8.decode(list);
return DateTime.parse(s).toUtc();
break;
case FIELD_TYPE_TIME: // time
var s = utf8.decode(list);
var parts = s.split(":");
return new Duration(
days: 0,
hours: int.parse(parts[0]),
minutes: int.parse(parts[1]),
seconds: int.parse(parts[2]),
milliseconds: 0);
break;
case FIELD_TYPE_YEAR: // year
var s = utf8.decode(list);
return int.parse(s);
break;
case FIELD_TYPE_STRING: // char/binary/enum/set
case FIELD_TYPE_VAR_STRING: // varchar/varbinary
var s = utf8.decode(list);
return s;
break;
case FIELD_TYPE_BLOB: // tinytext/text/mediumtext/longtext/tinyblob/mediumblob/blob/longblob
return new Blob.fromBytes(list);
break;
case FIELD_TYPE_GEOMETRY: // geometry
var s = utf8.decode(list);
return s;
break;
}
}

String toString() => "Fields: $fields";
Expand Down
33 changes: 32 additions & 1 deletion lib/src/results/row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@ library mysql1.row;

import 'dart:collection';

import 'field.dart';
import '../buffer.dart';

/**
* A row of data. Fields can be retrieved by index, or by name.
*
* When retrieving a field by name, only fields which are valid Dart
* identifiers, and which aren't part of the List object, can be used.
*/
abstract class Row extends ListBase<dynamic> {}
abstract class Row extends ListBase<dynamic> {
/// Values as List
List<dynamic> values;

/// Values as Map
final Map<String, dynamic> fields = <String, dynamic>{};

int get length => values.length;

set length(int newLength) {
throw new UnsupportedError("Cannot set length of results");
}

dynamic operator [](dynamic index) {
if (index is int) {
return values[index];
} else {
return fields[index.toString()];
}
}

void operator []=(int index, dynamic value) {
throw new UnsupportedError("Cannot modify row");
}

String toString() => "Value: $values";

Object readField(Field field, Buffer buffer);
}

0 comments on commit 1723b6d

Please sign in to comment.