Commit 9715bc33 authored by Dave Griffiths's avatar Dave Griffiths
Browse files

sync fixes, and open/close the database in main activity

parent dfdb53b9
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foam.mongoose"
android:versionCode="8"
android:versionCode="9"
android:versionName="1.0">
<application android:label="@string/app_name"
android:icon="@drawable/logo"
......
......@@ -72,17 +72,21 @@
;; persistent database
(define db "/sdcard/mongoose/local-mongoose.db")
(db-open db)
(setup db "local")
(setup db "sync")
(setup db "stream")
(insert-entity-if-not-exists
(define (setup-database!)
(msg "setting up database")
(db-close db) ;; close just in case (sorts out db file delete while running problem)
(db-open db)
(msg "setting up tables")
(setup db "local")
(setup db "sync")
(setup db "stream")
(msg (db-status db))
(insert-entity-if-not-exists
db "local" "app-settings" "null" 1
(list
(ktv "user-id" "varchar" "No name yet...")))
(display (db-all db "local" "app-settings"))(newline)
(msg (db-all db "local" "app-settings")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; stuff in memory
......@@ -241,7 +245,7 @@
'()
entities))
(define (suck-entity-from-server db table unique-id exists)
(define (suck-entity-from-server db table unique-id)
;; ask for the current version
(http-request
(string-append unique-id "-update-new")
......@@ -249,7 +253,8 @@
(lambda (data)
;; check "sync-insert" in sync.ss raspberry pi-side for the contents of 'entity'
(let ((entity (list-ref data 0))
(ktvlist (list-ref data 1)))
(ktvlist (list-ref data 1))
(exists (entity-exists? db table unique-id)))
(if (not exists)
(insert-entity-wholesale
db table
......@@ -286,7 +291,7 @@
#f)))
;; if we don't have this entity or the version on the server is newer
(if (or (not exists) old)
(cons (suck-entity-from-server db table unique-id exists) r)
(cons (suck-entity-from-server db table unique-id) r)
r)))
'()
data)))
......@@ -310,9 +315,11 @@
(define (build-dirty)
(let ((sync (get-dirty-stats db "sync"))
(stream (get-dirty-stats db "stream")))
(if (or (not sync) (not stream))
"No data yet"
(string-append
"Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
"Focal data: " (number->string (car stream)) "/" (number->string (cadr stream)))))
"Focal data: " (number->string (car stream)) "/" (number->string (cadr stream))))))
(define (upload-dirty db)
(let ((r (append
......@@ -603,7 +610,7 @@
(suck-new db "sync")))))
(else '()))
(list
(delayed "debug-timer" (+ 5000 (random 5000)) debug-timer-cb)
(delayed "debug-timer" (+ 10000 (random 5000)) debug-timer-cb)
(update-debug))))
......@@ -1424,6 +1431,7 @@
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg)
(setup-database!)
(let ((user-id (ktv-get (get-entity db "local" 1) "user-id")))
(set-current! 'user-id user-id)
(list
......
......@@ -29,6 +29,12 @@ db::db(const char *fn) :
db::~db()
{
// v2 in case of pending transactions
int rc = sqlite3_close_v2(m_db);
if( rc != SQLITE_OK )
{
// do something...
}
}
sqlite3_stmt *db::prepare(const char *sql)
......
......@@ -24,6 +24,20 @@ public:
m_dbs.add_to_end(new db_node(fn,d));
}
void remove(const char *fn)
{
db_node *cur=(db_node*)m_dbs.m_head;
while (cur!=NULL)
{
if (!strcmp(fn,cur->m_fn))
{
m_dbs.remove(cur);
return;
}
cur=(db_node*)cur->m_next;
}
}
db *get(const char *fn)
{
db_node *cur=(db_node*)m_dbs.m_head;
......
......@@ -194,6 +194,7 @@
_OP_DEF(opexe_6, "alog", 1, 1, TST_NONE, OP_ALOG )
_OP_DEF(opexe_6, "send", 1, 1, TST_NONE, OP_SEND )
_OP_DEF(opexe_6, "db-open", 1, 1, TST_NONE, OP_OPEN_DB )
_OP_DEF(opexe_6, "db-close", 1, 1, TST_NONE, OP_CLOSE_DB )
_OP_DEF(opexe_6, "db-exec", 2, INF_ARG, TST_NONE, OP_EXEC_DB )
_OP_DEF(opexe_6, "db-insert", 2, INF_ARG, TST_NONE, OP_INSERT_DB )
_OP_DEF(opexe_6, "db-status", 1, 1, TST_NONE, OP_STATUS_DB )
......
......@@ -4337,6 +4337,13 @@ static pointer opexe_6(scheme *sc, enum scheme_opcodes op) {
}
s_return(sc,sc->F);
}
case OP_CLOSE_DB: {
if (is_string(car(sc->args))) {
the_db_container.remove(string_value(car(sc->args)));
s_return(sc,sc->T);
}
s_return(sc,sc->F);
}
case OP_EXEC_DB: {
if (is_string(car(sc->args)) &&
is_string(cadr(sc->args))) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment