Skip to content

Commit 90a4c07

Browse files
committed
fix edges
1 parent aed6ad6 commit 90a4c07

File tree

4 files changed

+16
-33
lines changed

4 files changed

+16
-33
lines changed

lib/src/adapter/adapter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
391391

392392
Set<String> _keysFor(String key, String name) {
393393
final result = db.select(
394-
'SELECT src, dest FROM _edges WHERE (src = ? AND name = ?) OR (dest = ? AND inverse = ?)',
394+
'SELECT key_, _key FROM _edges WHERE (key_ = ? AND name_ = ?) OR (_key = ? AND _name = ?)',
395395
[key, name, key, name]);
396-
return {for (final r in result) r['src'] == key ? r['dest'] : r['src']};
396+
return {for (final r in result) r['key_'] == key ? r['_key'] : r['key_']};
397397
}
398398

399399
void _initializeRelationships(T model, {String? fromKey}) {

lib/src/model/relationship/relationship.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ sealed class Relationship<E extends DataModelMixin<E>, N> with EquatableMixin {
6565

6666
void _addAll(Iterable<String> keys, {bool notify = true}) {
6767
final ps = db.prepare(
68-
'REPLACE INTO _edges (src, name, dest, inverse) VALUES (?, ?, ?, ?)');
68+
'REPLACE INTO _edges (key_, name_, _key, _name) VALUES (?, ?, ?, ?)');
6969
final additions = [];
7070
for (final key in keys) {
7171
final order = ownerKey.compareTo(key);
7272
final args = order == -1
7373
? [ownerKey, name, key, inverseName]
7474
: [key, inverseName, ownerKey, name];
75-
// TODO if we dont have inverseName?
7675
ps.execute(args);
7776
additions.add(key);
7877
}
@@ -100,11 +99,11 @@ sealed class Relationship<E extends DataModelMixin<E>, N> with EquatableMixin {
10099

101100
if (order == -1) {
102101
db.execute(
103-
'UPDATE _edges SET dest = ? WHERE src = ? AND name = ? AND dest = ?',
102+
'UPDATE _edges SET dest = ? WHERE key_ = ? AND name_ = ? AND _key = ?',
104103
args);
105104
} else {
106105
db.execute(
107-
'UPDATE _edges SET src = ? WHERE dest = ? AND inverse = ? AND src = ?',
106+
'UPDATE _edges SET key_ = ? WHERE _key = ? AND _name = ? AND key_ = ?',
108107
args);
109108
}
110109

@@ -118,7 +117,7 @@ sealed class Relationship<E extends DataModelMixin<E>, N> with EquatableMixin {
118117

119118
void _removeAll({bool notify = true}) {
120119
db.execute(
121-
'DELETE FROM _edges WHERE (src = ? AND name = ?) OR (dest = ? AND inverse = ?)',
120+
'DELETE FROM _edges WHERE (key_ = ? AND name_ = ?) OR (_key = ? AND _name = ?)',
122121
[_ownerKey!, _name!, _ownerKey!, _name!]);
123122
}
124123

@@ -130,11 +129,10 @@ sealed class Relationship<E extends DataModelMixin<E>, N> with EquatableMixin {
130129

131130
if (order == -1) {
132131
db.execute(
133-
'DELETE FROM _edges WHERE src = ? AND name = ? AND dest = ?', args);
132+
'DELETE FROM _edges WHERE key_ = ? AND name_ = ? AND _key = ?', args);
134133
} else {
135134
db.execute(
136-
'DELETE FROM _edges WHERE dest = ? AND inverse = ? AND src = ?',
137-
args);
135+
'DELETE FROM _edges WHERE _key = ? AND _name = ? AND key_ = ?', args);
138136
}
139137

140138
_adapter.core._notify(

lib/src/storage/in_memory_local_storage.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@ class InMemoryLocalStorage extends LocalStorage {
88

99
try {
1010
db = sqlite3.openInMemory();
11-
12-
db.execute('''
13-
CREATE TABLE IF NOT EXISTS _edges (
14-
src INTEGER NOT NULL,
15-
name TEXT NOT NULL,
16-
dest INTEGER NOT NULL,
17-
inverse TEXT
18-
);
19-
20-
CREATE TABLE IF NOT EXISTS _keys (
21-
key INTEGER PRIMARY KEY AUTOINCREMENT,
22-
type TEXT NOT NULL,
23-
id TEXT,
24-
is_int INTEGER DEFAULT 0
25-
);
26-
''');
2711
} catch (e, stackTrace) {
2812
print('[flutter_data] Failed to open:\n$e\n$stackTrace');
2913
}

lib/src/storage/local_storage.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class LocalStorage {
3434
VACUUM;
3535
3636
CREATE TABLE IF NOT EXISTS _edges (
37-
src INTEGER NOT NULL,
38-
name TEXT NOT NULL,
39-
dest INTEGER NOT NULL,
40-
inverse TEXT,
41-
PRIMARY KEY (src, dest)
37+
key_ INTEGER NOT NULL,
38+
name_ TEXT,
39+
_key INTEGER NOT NULL,
40+
_name TEXT,
41+
UNIQUE (key_, name_, _key)
42+
UNIQUE (_key, _name, key_)
4243
);
43-
CREATE INDEX IF NOT EXISTS src_name_idx ON _edges(src, name);
44-
CREATE INDEX IF NOT EXISTS dest_inverse_idx ON _edges(dest, inverse);
44+
CREATE INDEX IF NOT EXISTS key_name_idx ON _edges(key_, name_);
45+
CREATE INDEX IF NOT EXISTS inv_key_name_idx ON _edges(_key, _name);
4546
4647
CREATE TABLE IF NOT EXISTS _keys (
4748
key INTEGER PRIMARY KEY AUTOINCREMENT,

0 commit comments

Comments
 (0)