From 9983cf004116cc84b3fbc311f0ca69eb87622136 Mon Sep 17 00:00:00 2001 From: Arto Bendiken Date: Thu, 15 Nov 2018 18:05:44 +0200 Subject: [PATCH] Implemented SQLiteCursor. --- lib/src/cursor.dart | 62 +++++++++++++++++++++++++++---------------- pubspec.yaml | 2 +- test/sqlite_test.dart | 6 +++++ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/lib/src/cursor.dart b/lib/src/cursor.dart index 4c0e04c..9a24138 100644 --- a/lib/src/cursor.dart +++ b/lib/src/cursor.dart @@ -1,10 +1,10 @@ /* This is free and unencumbered software released into the public domain. */ import 'dart:async' show Future; -import 'dart:typed_data' show ByteBuffer; import 'package:flutter/services.dart' show MethodChannel; -import 'package:flutter_android/android_database.dart' show Cursor; +import 'package:flutter_android/android_database.dart' show Cursor, CursorIndexOutOfBoundsException; +import 'package:meta/meta.dart' show experimental, required; /// Exposes results from a query on a [SQLiteDatabase]. /// @@ -17,43 +17,59 @@ class SQLiteCursor extends Cursor { static const MethodChannel _channel = MethodChannel('flutter_sqlcipher/SQLiteCursor'); bool _isClosed = false; + List _columns = const []; + List> _rows = const >[]; + int _rowIndex = -1; + + /// Constructs an empty cursor. + SQLiteCursor.empty(); + + /// Constructs a cursor from the provided column/row data. + @experimental + SQLiteCursor.from({@required List columns, @required List> rows}) + : assert(columns != null), + assert(rows != null), + _columns = List.unmodifiable(columns), + _rows = rows; @override Future close() { _isClosed = true; + _columns = null; + _rows = null; + _rowIndex = -1; return Future.value(); } @override - ByteBuffer getBlob(final int columnIndex) => null; // TODO - - @override - List getColumnNames() => []; // TODO - - @override - int getCount() => 0; // TODO - - @override - double getDouble(final int columnIndex) => null; // TODO - - @override - int getInt(final int columnIndex) => null; // TODO - - @override - int getPosition() => -1; // TODO + dynamic get(final int columnIndex) { + if (_rowIndex < 0 || _rowIndex >= _rows.length) { + throw CursorIndexOutOfBoundsException(_rowIndex, _rows.length); + } + if (columnIndex < 0 || columnIndex >= _columns.length) { + throw CursorIndexOutOfBoundsException(columnIndex, _columns.length); + } + return _rows[_rowIndex][columnIndex]; + } @override - String getString(final int columnIndex) => null; // TODO + List getColumnNames() => _columns; @override - int getType(final int columnIndex) => null; // TODO + int getCount() => _rows.length; @override - bool get isClosed => _isClosed; // TODO + int getPosition() => _rowIndex; @override - bool isNull(final int columnIndex) => null; // TODO + bool get isClosed => _isClosed; @override - bool moveToPosition(final int position) => false; // TODO + bool moveToPosition(final int position) { + if (position >= -1 && position <= _rows.length) { + _rowIndex = position; + return true; // request accepted + } + return false; // request rejected + } } diff --git a/pubspec.yaml b/pubspec.yaml index 7f58a55..ea46dd1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,7 @@ documentation: dependencies: flutter: sdk: flutter - flutter_android: ^0.1.6 + flutter_android: ^0.1.7 meta: ^1.1.6 dev_dependencies: flutter_test: diff --git a/test/sqlite_test.dart b/test/sqlite_test.dart index 167cb39..4278f5b 100644 --- a/test/sqlite_test.dart +++ b/test/sqlite_test.dart @@ -8,4 +8,10 @@ void main() { group("sqlite", () { // TODO }); + group("sqlite.SQLiteCursor", () { + // TODO + }); + group("sqlite.SQLiteDatabase", () { + // TODO + }); }