29
29
def __init__(self, sqlite_file):
30
30
"""Create a new sqlite file."""
31
31
self._db_handle = dbapi2.connect(sqlite_file)
32
self._real_machine_id = None
32
self._real_replica_uid = None
33
33
self._ensure_schema()
35
35
def get_sync_target(self):
107
107
c.execute("CREATE INDEX document_fields_field_value_doc_idx"
108
108
" ON document_fields(field_name, value, doc_id)")
109
109
c.execute("CREATE TABLE sync_log ("
110
" machine_id TEXT PRIMARY KEY,"
110
" replica_uid TEXT PRIMARY KEY,"
111
111
" known_generation INTEGER)")
112
112
c.execute("CREATE TABLE conflicts ("
136
136
def _extra_schema_init(self, c):
137
137
"""Add any extra fields, etc to the basic table definitions."""
139
def _set_machine_id(self, machine_id):
140
"""Force the machine_id to be set."""
139
def _set_replica_uid(self, replica_uid):
140
"""Force the replica_uid to be set."""
141
141
with self._db_handle:
142
142
c = self._db_handle.cursor()
143
c.execute("INSERT INTO u1db_config VALUES ('machine_id', ?)",
145
self._real_machine_id = machine_id
143
c.execute("INSERT INTO u1db_config VALUES ('replica_uid', ?)",
145
self._real_replica_uid = replica_uid
147
def _get_machine_id(self):
148
if self._real_machine_id is not None:
149
return self._real_machine_id
147
def _get_replica_uid(self):
148
if self._real_replica_uid is not None:
149
return self._real_replica_uid
150
150
c = self._db_handle.cursor()
151
c.execute("SELECT value FROM u1db_config WHERE name = 'machine_id'")
151
c.execute("SELECT value FROM u1db_config WHERE name = 'replica_uid'")
152
152
val = c.fetchone()
155
self._real_machine_id = val[0]
156
return self._real_machine_id
155
self._real_replica_uid = val[0]
156
return self._real_replica_uid
158
_machine_id = property(_get_machine_id)
158
_replica_uid = property(_get_replica_uid)
160
160
def _get_generation(self):
161
161
c = self._db_handle.cursor()
295
295
this_doc_rev, this_doc = self._get_doc(doc_id)
296
296
return [(this_doc_rev, this_doc)] + conflict_docs
298
def get_sync_generation(self, other_db_id):
298
def get_sync_generation(self, other_replica_uid):
299
299
c = self._db_handle.cursor()
300
c.execute("SELECT known_generation FROM sync_log WHERE machine_id = ?",
300
c.execute("SELECT known_generation FROM sync_log WHERE replica_uid = ?",
301
(other_replica_uid,))
302
302
val = c.fetchone()
306
306
other_gen = val[0]
309
def set_sync_generation(self, other_db_id, other_generation):
309
def set_sync_generation(self, other_replica_uid, other_generation):
310
310
with self._db_handle:
311
311
c = self._db_handle.cursor()
312
312
my_gen = self._get_generation()
313
313
c.execute("INSERT OR REPLACE INTO sync_log VALUES (?, ?)",
314
(other_db_id, other_generation))
314
(other_replica_uid, other_generation))
316
316
def _compare_and_insert_doc(self, doc_id, doc_rev, doc):
317
317
with self._db_handle:
456
456
class SQLiteSyncTarget(CommonSyncTarget):
458
def get_sync_info(self, other_machine_id):
459
other_gen = self._db.get_sync_generation(other_machine_id)
458
def get_sync_info(self, other_replica_uid):
459
other_gen = self._db.get_sync_generation(other_replica_uid)
460
460
my_gen = self._db._get_generation()
461
return self._db._machine_id, my_gen, other_gen
461
return self._db._replica_uid, my_gen, other_gen
463
def record_sync_info(self, other_machine_id, other_machine_generation):
464
self._db.set_sync_generation(other_machine_id,
465
other_machine_generation)
463
def record_sync_info(self, other_replica_uid, other_replica_generation):
464
self._db.set_sync_generation(other_replica_uid,
465
other_replica_generation)
468
468
class SQLiteExpandedDatabase(SQLiteDatabase):