valueAt<T> method

  1. @override
T? valueAt<T>(
  1. int index
)

Read an appropriate value from SQLite Value or Column based on SQLite type affinity by index.

When the method is used to read value from select query, the returned value is also determine based on type declaration such as DateTime, bool ...etc.

Implementation

@override
T? valueAt<T>(int index) {
  final type = valueType(index);
  switch (type) {
    case sqlite.TEXT:
      final val = stringValueAt(index);
      if (val == null) return null;
      try {
        if (T == Uri) return Uri.parse(val) as T;
        if (T == num) return num.parse(val) as T;
        if (T == DateTime) return DateTime.parse(val) as T;
        // bigint web case
        if (T == Duration) return Duration(microseconds: int.parse(val)) as T;
      } catch (e) {
        throw sql.DatabaseException('invalid data format: unable to convert data $val to $T.');
      }
      if (T == String || T == dynamic) return val as T;
      if (T == RegExp) return RegExp(val) as T;
      // unsupported type.
      break;
    case sqlite.INTEGER:
      final val = intValueAt(index);
      if (val == null) return null;
      if (T == bool) return (val == 1) as T;
      if (T == Duration) return Duration(microseconds: val) as T;
      if (T == int || T == num || T == dynamic) return val as T;
      // unsupported type.
      break;
    case sqlite.FLOAT:
      if (T == double || T == num || T == dynamic) return doubleValueAt(index) as T?;
      // unsupported type.
      break;
    case sqlite.BLOB:
      if (T == Uint8List || T == dynamic) return blobValueAt(index) as T?;
      // unsupported type.
      break;
    case sqlite.NULL:
      return null;
  }
  throw sql.DatabaseException('unsupported sqlite value of data type $type.');
}