From 09ecb32a84d8744405300755179932d09389d6a7 Mon Sep 17 00:00:00 2001 From: Rex Momoh Date: Tue, 5 Mar 2019 23:30:44 +0100 Subject: [PATCH] update cache --- lib/src/cache/in_memory.dart | 6 ++ .../normalized/record_field_json_adapter.dart | 29 ++++++++ .../normalized/sql/sql-normalized-cache.dart | 67 +++++++++++++++++++ lib/src/cache/normalized/sql/sql_helper.dart | 33 +++++++++ pubspec.yaml | 2 +- 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 lib/src/cache/normalized/record_field_json_adapter.dart create mode 100644 lib/src/cache/normalized/sql/sql-normalized-cache.dart create mode 100644 lib/src/cache/normalized/sql/sql_helper.dart diff --git a/lib/src/cache/in_memory.dart b/lib/src/cache/in_memory.dart index 6b4126364..4be115de7 100644 --- a/lib/src/cache/in_memory.dart +++ b/lib/src/cache/in_memory.dart @@ -102,4 +102,10 @@ class InMemoryCache implements Cache { return HashMap(); } } + + @override + Future remove(String key, bool cascade) { + // TODO: implement remove + return null; + } } diff --git a/lib/src/cache/normalized/record_field_json_adapter.dart b/lib/src/cache/normalized/record_field_json_adapter.dart new file mode 100644 index 000000000..9ef48e643 --- /dev/null +++ b/lib/src/cache/normalized/record_field_json_adapter.dart @@ -0,0 +1,29 @@ +import 'dart:convert'; + +class RecordFieldJsonAdapter { + + static RecordFieldJsonAdapter create() { + return new RecordFieldJsonAdapter(); + } + + RecordFieldJsonAdapter() { + } + + dynamic toJson(Map fields) { + assert(fields != null); + return json.encode(fields); + } + + Map from(dynamic jsonObj) { + assert(jsonObj != null); + return json.decode(jsonObj); + } + +/* + private Map fromBufferSource(BufferedSource bufferedFieldSource) throws IOException { + final CacheJsonStreamReader cacheJsonStreamReader = + cacheJsonStreamReader(bufferedSourceJsonReader(bufferedFieldSource)); + return cacheJsonStreamReader.toMap(); + } +*/ +} \ No newline at end of file diff --git a/lib/src/cache/normalized/sql/sql-normalized-cache.dart b/lib/src/cache/normalized/sql/sql-normalized-cache.dart new file mode 100644 index 000000000..e131b72ee --- /dev/null +++ b/lib/src/cache/normalized/sql/sql-normalized-cache.dart @@ -0,0 +1,67 @@ +import 'dart:collection'; + +import 'package:flutter_graphql/src/cache/normalized/record_field_json_adapter.dart'; +import 'package:flutter_graphql/src/cache/normalized/sql/sql_helper.dart'; +import 'package:sqflite/sqflite.dart'; + +import '../../cache.dart'; + +class SqlNormalizedCache implements Cache { + + SqlNormalizedCache(this.dbHelper, this.recordFieldAdapter) { + dbHelper.open(); + database = dbHelper.db; + } + + static const String UPDATE_STATEMENT = '''UPDATE ${SqlHelper.TABLE_RECORDS} SET ${SqlHelper.COLUMN_KEY}=?, ${SqlHelper.COLUMN_RECORD}=? WHERE ${SqlHelper.COLUMN_KEY}=?'''; + static const String DELETE_STATEMENT = '''DELETE FROM ${SqlHelper.TABLE_RECORDS} WHERE ${SqlHelper.COLUMN_KEY}=?'''; + static const String DELETE_ALL_RECORD_STATEMENT = '''DELETE FROM ${SqlHelper.TABLE_RECORDS}'''; + + Database database; + final SqlHelper dbHelper; + final allColumns = [ + SqlHelper.COLUMN_ID, + SqlHelper.COLUMN_KEY, + SqlHelper.COLUMN_RECORD]; + final RecordFieldJsonAdapter recordFieldAdapter; + HashMap _inMemoryCache = HashMap(); + + @override + Object read(String key) { + // TODO: implement read + return null; + } + + Future>> _readFromStorage() async { + List> records = await database.query(SqlHelper.TABLE_RECORDS); + return records; + } + + @override + void reset() { + // TODO: implement reset + } + + @override + void restore() { + // TODO: implement restore + } + + @override + void save() { + // TODO: implement save + } + + @override + void write(String key, dynamic values) { + database.insert(SqlHelper.TABLE_RECORDS, values); + } + + @override + Future remove(String key, bool cascade) async { + assert(key != null); + final deletedObj = await database.delete(SqlHelper.TABLE_RECORDS, where: '${SqlHelper.COLUMN_KEY}=?', whereArgs: [key].toList()); + return true; + } + +} \ No newline at end of file diff --git a/lib/src/cache/normalized/sql/sql_helper.dart b/lib/src/cache/normalized/sql/sql_helper.dart new file mode 100644 index 000000000..86820f87a --- /dev/null +++ b/lib/src/cache/normalized/sql/sql_helper.dart @@ -0,0 +1,33 @@ +import 'package:path/path.dart'; +import 'package:sqflite/sqflite.dart'; + +class SqlHelper { + + static const String TABLE_RECORDS = 'records'; + static const String COLUMN_ID = '_id'; + static const String COLUMN_RECORD = 'record'; + static const String COLUMN_KEY = 'key'; + + static const String DATABASE_NAME = 'graphql-flutter.db'; + static const int DATABASE_VERSION = 1; + + static const String DATABASE_CREATE = '''CREATE TABLE $TABLE_RECORDS ($COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COLUMN_KEY TEXT NOT NULL, $COLUMN_RECORD TEXT NOT NULL'''; + static const String IDX_RECORDS_KEY = 'idx_records_key'; + static const String CREATE_KEY_INDEX = '''CREATE INDEX $IDX_RECORDS_KEY ON $TABLE_RECORDS ($COLUMN_KEY)'''; + + Database db; + + Future open() async { + final databasesPath = await getDatabasesPath(); + String path = join(databasesPath, DATABASE_NAME); + db = await openDatabase(path, version: DATABASE_VERSION, onCreate: (Database db, int version) async { + await db.execute(DATABASE_CREATE); + await db.execute(CREATE_KEY_INDEX); + }); + } + + Future close() async { + await db.close(); + } + +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 516e6cb1e..9b2b3512e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_graphql description: A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package. -version: 1.0.0-alpha.12 +version: 1.0.0-alpha.13 authors: - Rex Raphael homepage: https://github.com/juicycleff/graphql-flutter/tree/master