Skip to content

Commit

Permalink
update cache
Browse files Browse the repository at this point in the history
  • Loading branch information
juicycleff committed Mar 5, 2019
1 parent 0c03adb commit 09ecb32
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/src/cache/in_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ class InMemoryCache implements Cache {
return HashMap<String, dynamic>();
}
}

@override
Future<bool> remove(String key, bool cascade) {
// TODO: implement remove
return null;
}
}
29 changes: 29 additions & 0 deletions lib/src/cache/normalized/record_field_json_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:convert';

class RecordFieldJsonAdapter {

static RecordFieldJsonAdapter create() {
return new RecordFieldJsonAdapter();
}

RecordFieldJsonAdapter() {
}

dynamic toJson(Map<String, dynamic> fields) {
assert(fields != null);
return json.encode(fields);
}

Map<String, Object> from(dynamic jsonObj) {
assert(jsonObj != null);
return json.decode(jsonObj);
}

/*
private Map<String, Object> fromBufferSource(BufferedSource bufferedFieldSource) throws IOException {
final CacheJsonStreamReader cacheJsonStreamReader =
cacheJsonStreamReader(bufferedSourceJsonReader(bufferedFieldSource));
return cacheJsonStreamReader.toMap();
}
*/
}
67 changes: 67 additions & 0 deletions lib/src/cache/normalized/sql/sql-normalized-cache.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> _inMemoryCache = HashMap<String, dynamic>();

@override
Object read(String key) {
// TODO: implement read
return null;
}

Future<List<HashMap<String, dynamic>>> _readFromStorage() async {
List<HashMap<String, dynamic>> 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<bool> 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;
}

}
33 changes: 33 additions & 0 deletions lib/src/cache/normalized/sql/sql_helper.dart
Original file line number Diff line number Diff line change
@@ -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();
}

}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <rex.raphael@outlook.com>
homepage: https://github.com/juicycleff/graphql-flutter/tree/master
Expand Down

0 comments on commit 09ecb32

Please sign in to comment.