eavdb.ss 30.1 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
1 2
#lang racket

Dave Griffiths's avatar
Dave Griffiths committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
;; MongooseWeb Copyright (C) 2013 Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

Dave Griffiths's avatar
Dave Griffiths committed
18 19 20
(require (planet jaymccarthy/sqlite:5:1/sqlite))
(require "utils.ss")
(provide (all-defined-out))
Dave Griffiths's avatar
Dave Griffiths committed
21

Dave Griffiths's avatar
Dave Griffiths committed
22 23
;; tinyscheme
;(define db-select db-exec)
Dave Griffiths's avatar
Dave Griffiths committed
24

Dave Griffiths's avatar
Dave Griffiths committed
25 26 27 28 29 30
;; racket
(define db-exec exec/ignore)
(define db-select select)
(define db-insert insert)
(define (db-status db) (errmsg db))
(define (time) (list (random) (random))) ; ahem
Dave Griffiths's avatar
Dave Griffiths committed
31 32 33 34 35 36


;; create eav tables (add types as required)
(define (setup db table)
  (db-exec db (string-append "create table " table "_entity ( entity_id integer primary key autoincrement, entity_type varchar(256), unique_id varchar(256), dirty integer, version integer)"))
  (db-exec db (string-append "create table " table "_attribute ( id integer primary key autoincrement, attribute_id varchar(256), entity_type varchar(256), attribute_type varchar(256))"))
37 38 39 40
  (db-exec db (string-append "create table " table "_value_varchar ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)"))
  (db-exec db (string-append "create table " table "_value_int ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value integer, dirty integer, version integer)"))
  (db-exec db (string-append "create table " table "_value_real ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value real, dirty integer, version integer)"))
  (db-exec db (string-append "create table " table "_value_file ( id integer primary key autoincrement, entity_id integer, attribute_id varchar(255), value varchar(4096), dirty integer, version integer)")))
Dave Griffiths's avatar
Dave Griffiths committed
41 42 43 44

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; basic key/type/value structure
45 46 47 48
;; used for all data internally, and maps to the eavdb types

(define (ktv key type value) (list key type value -999))
(define (ktv-with-version key type value version) (list key type value version))
Dave Griffiths's avatar
Dave Griffiths committed
49 50 51
(define ktv-key car)
(define ktv-type cadr)
(define ktv-value caddr)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
(define (ktv-version ktv) (list-ref ktv 3))

(define (ktv-eq? a b)
  (and
   (equal? (ktv-key a) (ktv-key b))
   (equal? (ktv-type a) (ktv-type b))
   (cond
    ((or
      (equal? (ktv-type a) "int")
      (equal? (ktv-type a) "real"))
     (eqv? (ktv-value a) (ktv-value b)))
    ((or
      (equal? (ktv-type a) "varchar")
      (equal? (ktv-type a) "file"))
     (equal? (ktv-value a) (ktv-value b)))
    (else
     (msg "unsupported ktv type in ktv-eq?: " (ktv-type a))
     #f))))
Dave Griffiths's avatar
Dave Griffiths committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

;; stringify based on type (for url)
(define (stringify-value ktv)
  (cond
   ((null? (ktv-value ktv)) "NULL")
   ((equal? (ktv-type ktv) "varchar") (string-append "'" (ktv-value ktv) "'"))
   (else
    (if (not (string? (ktv-value ktv)))
        (number->string (ktv-value ktv))
        (ktv-value ktv)))))

;; stringify based on type (for url)
(define (stringify-value-url ktv)
  (cond
   ((null? (ktv-value ktv)) "NULL")
   ((equal? (ktv-type ktv) "varchar") (ktv-value ktv))
   (else
    (if (not (string? (ktv-value ktv)))
        (number->string (ktv-value ktv))
        (ktv-value ktv)))))

91
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dave Griffiths's avatar
Dave Griffiths committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

;; helper to return first instance from a select
(define (select-first db str . args)
  (let ((s (apply db-select (append (list db str) args))))
    (if (or (null? s) (eq? s #t))
        '()
        (vector-ref (cadr s) 0))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; putting data in

;; get the type from the attribute table with an entity/key
(define (get-attribute-type db table entity-type key)
  (let ((sql (string-append
              "select attribute_type from " table
              "_attribute where entity_type = ? and attribute_id = ?")))
    (select-first db sql entity-type key)))

;; search for a type and add it if it doesn't exist
(define (find/add-attribute-type db table entity-type key type)
  (let ((t (get-attribute-type db table entity-type key)))
    ;; add and return passed in type if not exist
    (cond
      ((null? t)
       (msg "adding new attribute for" entity-type " called " key " of type " type)
       (db-insert
        db (string-append "insert into " table "_attribute values (null, ?, ?, ?)")
        key entity-type type)
       type)
      (else
       (cond
         ((equal? type t) t)
         (else
          (msg "type has changed for" entity-type key "from" t "to" type "???")
          ;; wont work
          ;; what do we do?
          ;; some kind of coercion for existing data???
          type))))))

;; low level insert of a ktv
132
(define (insert-value db table entity-id ktv dirty)
Dave Griffiths's avatar
Dave Griffiths committed
133 134
  ;; use type to dispatch insert to correct value table
  (db-insert db (string-append "insert into " table "_value_" (ktv-type ktv)
135 136
                               " values (null, ?, ?, ?, ?, ?)")
             entity-id (ktv-key ktv) (ktv-value ktv) (if dirty 1 0) (ktv-version ktv)))
Dave Griffiths's avatar
Dave Griffiths committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

(define (get-unique user)
  (let ((t (time)))
    (string-append
     user "-" (number->string (car t)) ":" (number->string (cadr t)))))

;; insert an entire entity
(define (insert-entity db table entity-type user ktvlist)
  (insert-entity-wholesale db table entity-type (get-unique user) 1 0 ktvlist))

;; insert an entire entity
(define (insert-entity/get-unique db table entity-type user ktvlist)
  (let ((uid (get-unique user)))
    (insert-entity-wholesale db table entity-type uid 1 0 ktvlist)
    uid))

Dave Griffiths's avatar
Dave Griffiths committed
153 154
(define sema (make-semaphore 1))

Dave Griffiths's avatar
Dave Griffiths committed
155 156
;; all the parameters - for syncing purposes
(define (insert-entity-wholesale db table entity-type unique-id dirty version ktvlist)
Dave Griffiths's avatar
Dave Griffiths committed
157 158
  (semaphore-wait sema)
  (db-exec db "begin transaction")
Dave Griffiths's avatar
Dave Griffiths committed
159 160 161 162
  (let ((id (db-insert
             db (string-append
                 "insert into " table "_entity values (null, ?, ?, ?, ?)")
             entity-type unique-id dirty version)))
Dave Griffiths's avatar
Dave Griffiths committed
163

Dave Griffiths's avatar
Dave Griffiths committed
164 165 166 167 168 169 170 171
    ;; create the attributes if they are new, and validate them if they exist
    (for-each
     (lambda (ktv)
       (find/add-attribute-type db table entity-type (ktv-key ktv) (ktv-type ktv)))
     ktvlist)
    ;; add all the keys
    (for-each
     (lambda (ktv)
172
       (insert-value db table id ktv dirty))
Dave Griffiths's avatar
Dave Griffiths committed
173 174
     ktvlist)

Dave Griffiths's avatar
Dave Griffiths committed
175 176 177 178
    (db-exec db "end transaction")
    (semaphore-post sema)

    id))
Dave Griffiths's avatar
Dave Griffiths committed
179

180

Dave Griffiths's avatar
Dave Griffiths committed
181
;; update the value given an entity type, a attribute type and it's key (= attriute_id)
182
;; creates the value if it doesn't already exist, updates it otherwise if it's different
Dave Griffiths's avatar
Dave Griffiths committed
183
(define (update-value db table entity-id ktv)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  (let ((s (select-first
            db (string-append
                "select value from " table "_value_" (ktv-type ktv) " where entity_id = ? and attribute_id = ?")
            entity-id (ktv-key ktv))))
    (if (null? s)
        (insert-value db table entity-id ktv #t)
        ;; only update if the are different
        (if (not (ktv-eq? ktv (list (ktv-key ktv) (ktv-type ktv) s)))
            (begin
              (msg "incrementing value version in update-value")
              (db-exec
               db (string-append "update " table "_value_" (ktv-type ktv)
                                 " set value=?, dirty=1, version=version+1  where entity_id = ? and attribute_id = ?")
               (ktv-value ktv) entity-id (ktv-key ktv)))
            '())))) ;;(msg "values for" (ktv-key ktv) "are the same (" (ktv-value ktv) "==" s ")")))))

;; don't make dirty or update version here
(define (update-value-from-sync db table entity-id ktv)
  (let ((s (select-first
            db (string-append
                "select value from " table "_value_" (ktv-type ktv) " where entity_id = ? and attribute_id = ?")
            entity-id (ktv-key ktv))))
    (msg "update-value-from-sync" s)
    (msg ktv)
    (msg entity-id)
    (if (null? s)
        (insert-value db table entity-id ktv #t)
        (db-exec
         db (string-append "update " table "_value_" (ktv-type ktv)
                           " set value=?, dirty=0, version=? where entity_id = ? and attribute_id = ?")
         (ktv-value ktv) (ktv-version ktv) entity-id (ktv-key ktv)))))
Dave Griffiths's avatar
Dave Griffiths committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; getting data out

(define (entity-exists? db table unique-id)
  (not (null? (select-first
               db (string-append
                   "select * from " table "_entity where unique_id = ?")
               unique-id))))

(define (get-entity-type db table entity-id)
  (select-first
   db (string-append
       "select entity_type from " table "_entity where entity_id = ?")
       entity-id))

(define (get-all-entity-types db table)
  (cdr (db-select db (string-append "select distinct entity_type from " table "_entity;"))))

;; get all the (current) attributes for an entity type
(define (get-attribute-ids/types db table entity-type)
  (let ((s (db-select
            db (string-append
                "select * from " table "_attribute where entity_type = ?")
                entity-type)))
    (if (null? s) '()
        (map
         (lambda (row)
           (list (vector-ref row 1)    ;; id
                 (vector-ref row 3)))  ;; type
         (cdr s)))))

247
;; get the value, dirty and version given an entity type, a attribute type and it's key (= attriute_id)
Dave Griffiths's avatar
Dave Griffiths committed
248
(define (get-value db table entity-id kt)
249 250 251 252 253 254 255 256
  (let ((s (db-select
            db (string-append "select value, dirty, version from " table "_value_" (ktv-type kt)
                              " where entity_id = ? and attribute_id = ?")
            entity-id (ktv-key kt))))
    (if (null? s) '()
	(list (vector-ref (cadr s) 0)
	      (vector-ref (cadr s) 1)
	      (vector-ref (cadr s) 2)))))
Dave Griffiths's avatar
Dave Griffiths committed
257 258 259 260 261 262 263 264 265

;; get an entire entity, as a list of key/value pairs
(define (get-entity-plain db table entity-id)
  (let* ((entity-type (get-entity-type db table entity-id)))
    (cond
      ((null? entity-type) (msg "entity" entity-id "not found!") '())
      (else
       (map
        (lambda (kt)
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
          (let ((vdv (get-value db table entity-id kt)))
            (if (null? vdv) 
		(msg "ERROR: get-entity-plain: no value found for " entity-id " " (ktv-key kt))		
		(list (ktv-key kt) (ktv-type kt) 
		      (list-ref vdv 0) (list-ref vdv 2)))))
        (get-attribute-ids/types db table entity-type))))))

;; get an entire entity, as a list of key/value pairs, only dirty values                               
(define (get-entity-plain-for-sync db table entity-id)
  (let* ((entity-type (get-entity-type db table entity-id)))
    (cond
      ((null? entity-type) (msg "entity" entity-id "not found!") '())
      (else
       (foldl
        (lambda (kt r)
          (let ((vdv (get-value db table entity-id kt)))
            (cond
             ((null? vdv)
              (msg "ERROR: get-entity-plain: no value found for " entity-id " " (ktv-key kt))         
              r)
             ;; only return if dirty                                                                   
             ((zero? (cadr vdv))
              (cons
               (list (ktv-key kt) (ktv-type kt) (list-ref vdv 0) (list-ref vdv 2))
               r))
             (else r))))
        '()
Dave Griffiths's avatar
Dave Griffiths committed
293 294 295 296 297 298 299 300 301 302 303
        (get-attribute-ids/types db table entity-type))))))

;; get an entire entity, as a list of key/value pairs (includes entity id)
(define (get-entity db table entity-id)
  (let ((unique-id (get-unique-id db table entity-id)))
    (cons
     (list "unique_id" "varchar" unique-id)
     (get-entity-plain db table entity-id))))

(define (all-entities db table type)
  (let ((s (db-select
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
            db (string-append "select e.entity_id from " table "_entity as e "
                              "join " table "_value_varchar "
                              " as n on n.entity_id = e.entity_id and n.attribute_id = ?"
                              "left join " table "_value_int "
                              "as d on d.entity_id = e.entity_id and d.attribute_id = ? "
                              "where e.entity_type = ? "
                              "and (d.value='NULL' or d.value is NULL or d.value = 0) "
                              "order by n.value")
            "name" "deleted" type)))
    (msg (db-status db))
    (if (null? s)
        '()
        (map
         (lambda (i)
           (vector-ref i 0))
         (cdr s)))))

(define (all-entities-with-parent db table type parent)
  (let ((s (db-select
            db (string-append "select e.entity_id from " table "_entity as e "
                              "join " table "_value_varchar "
                              " as n on n.entity_id = e.entity_id and n.attribute_id = ?"
                              "join " table "_value_varchar "
                              " as p on p.entity_id = e.entity_id and p.attribute_id = ?"
                              "left join " table "_value_int "
                              "as d on d.entity_id = e.entity_id and d.attribute_id = ? "
                              "where e.entity_type = ? and "
                              "p.value = ? and "
                              "(d.value='NULL' or d.value is NULL or d.value = 0) "
                              "order by n.value")
            "name" "parent" "deleted" type parent)))
    (msg (db-status db))
    (if (null? s)
        '()
        (map
         (lambda (i)
           (vector-ref i 0))
         (cdr s)))))


;; filter is list of (attribute-key type op arg) e.g. ("gender" "varchar" "=" "Female")
;; note: only one filter per key..

(define (make-filter k t o a) (list k t o a))
(define (filter-key f) (list-ref f 0))
(define (filter-type f) (list-ref f 1))
(define (filter-op f) (list-ref f 2))
(define (filter-arg f) (list-ref f 3))

(define (merge-filter f fl)
  (cond
   ((null? fl) (list f))
   ((equal? (filter-key (car fl)) (filter-key f))
    (cons f (cdr fl)))
   (else (cons (car fl) (merge-filter f (cdr fl))))))

(define (delete-filter key fl)
  (cond
   ((null? fl) '())
   ((equal? (filter-key (car fl)) key)
    (cdr fl))
   (else (cons (car fl) (delete-filter key (cdr fl))))))

(define (build-query table filter)
  (string-append
   (foldl
    (lambda (i r)
      (let ((var (string-append (filter-key i) "_var")))
        ;; add a query chunk
        (string-append
         r "join " table "_value_" (filter-type i) " "
         "as " var " on "
         var ".entity_id = e.entity_id and " var ".attribute_id = '" (filter-key i) "' and "
         var ".value " (filter-op i) " ? ")))

    ;; boilerplate query start
    (string-append
     "select e.entity_id from " table "_entity as e "
     ;; order by name
     "join " table "_value_varchar "
     "as n on n.entity_id = e.entity_id and n.attribute_id = 'name' "
     ;; ignore deleted
     "join " table "_value_int "
     "as d on d.entity_id = e.entity_id and d.attribute_id = 'deleted' and "
     "d.value = 0 ")
    filter)
   "where e.entity_type = ? order by n.value"))

(define (build-args filter)
  (map
   (lambda (i)
     (filter-arg i))
   filter))

(define (filter-entities db table type filter)
  (let ((s (apply
            db-select
            (dbg (append
                  (list db (build-query table filter))
                  (build-args filter)
                  (list type))))))
    (msg (db-status db))
Dave Griffiths's avatar
Dave Griffiths committed
406 407 408 409 410 411 412
    (if (null? s)
        '()
        (map
         (lambda (i)
           (vector-ref i 0))
         (cdr s)))))

413

Dave Griffiths's avatar
Dave Griffiths committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427
(define (validate db)
  ;; check attribute for duplicate entity-id/attribute-ids
  0)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helpers

(define (ktv-get ktv-list key)
  (cond
   ((null? ktv-list) #f)
   ((equal? (ktv-key (car ktv-list)) key)
    (ktv-value (car ktv-list)))
   (else (ktv-get (cdr ktv-list) key))))

428 429 430 431 432 433 434
(define (ktv-get-type ktv-list key)
  (cond
   ((null? ktv-list) #f)
   ((equal? (ktv-key (car ktv-list)) key)
    (ktv-type (car ktv-list)))
   (else (ktv-get-type (cdr ktv-list) key))))

Dave Griffiths's avatar
Dave Griffiths committed
435 436 437 438 439 440 441 442
(define (ktv-set ktv-list ktv)
  (cond
   ((null? ktv-list) (list ktv))
   ((equal? (ktv-key (car ktv-list)) (ktv-key ktv))
    (cons ktv (cdr ktv-list)))
   (else (cons (car ktv-list) (ktv-set (cdr ktv-list) ktv)))))

(define (db-all db table type)
Dave Griffiths's avatar
Dave Griffiths committed
443
  (map
Dave Griffiths's avatar
Dave Griffiths committed
444 445
   (lambda (i)
     (get-entity db table i))
Dave Griffiths's avatar
Dave Griffiths committed
446 447
   (all-entities db table type)))

448 449 450 451 452 453 454 455 456 457 458
(define (db-with-parent db table type parent)
  (map
   (lambda (i)
     (get-entity db table i))
   (all-entities-with-parent db table type parent)))

(define (db-filter db table type filter)
  (map
   (lambda (i)
     (get-entity db table i))
   (filter-entities db table type filter)))
Dave Griffiths's avatar
Dave Griffiths committed
459 460 461 462 463 464

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; updating data

;; update an entire entity (version incl), via a (possibly partial) list of key/value pairs
(define (update-to-version db table entity-id version ktvlist)
465 466
  ;; not dirty
  (update-entity-values db table entity-id ktvlist #f)
Dave Griffiths's avatar
Dave Griffiths committed
467 468 469 470
  (update-entity-version db table entity-id version))

;; auto update version
(define (update-entity db table entity-id ktvlist)
471
  ;; dirty
Dave Griffiths's avatar
Dave Griffiths committed
472
  (update-entity-changed db table entity-id)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  (update-entity-values db table entity-id ktvlist #t))

(define (clean-value db table entity-id kt)
  (db-exec db (string-append "update " table "_value_" (ktv-type kt)
                             " set dirty=0  where entity_id = ? and attribute_id = ?")
           entity-id (ktv-key kt)))

(define (clean-entity-values db table entity-id)
  (msg "clean-entity-values")
  (let* ((entity-type (get-entity-type db table entity-id)))
    (cond
     ((null? entity-type)
      (msg "clean-entity-values: entity" entity-id "not found!") '())
     (else
      (for-each
       (lambda (kt)
         (msg "cleaning" kt)
         (clean-value db table entity-id (list (ktv-key kt) (ktv-type kt))))
       (get-attribute-ids/types db table entity-type))))))
Dave Griffiths's avatar
Dave Griffiths committed
492 493

;; update an entity, via a (possibly partial) list of key/value pairs
494 495
;; if dirty is not true, this is coming from a sync
(define (update-entity-values db table entity-id ktvlist dirty)
Dave Griffiths's avatar
Dave Griffiths committed
496 497 498 499 500 501 502 503 504 505 506 507
  (let* ((entity-type (get-entity-type db table entity-id)))
    (cond
     ((null? entity-type) (msg "entity" entity-id "not found!") '())
     (else
      ;; update main entity type
      (for-each
       (lambda (ktv)
         (when (not (equal? (ktv-key ktv) "unique_id"))
               (find/add-attribute-type db table entity-type (ktv-key ktv) (ktv-type ktv))))
       ktvlist)
      (for-each
       (lambda (ktv)
508 509 510
         (if dirty
             (update-value db table entity-id ktv)
             (update-value-from-sync db table entity-id ktv)))
Dave Griffiths's avatar
Dave Griffiths committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
       ktvlist)))))

;; update or create an entire entity if it doesn't exist
;; will return the new entity id if it's created
(define (update/insert-entity db table entity-type user entity-id ktvlist)
  (let* ((entity-type (get-entity-type db table entity-id)))
    (cond
     ((null? entity-type)
      (insert-entity db table entity-type user ktvlist))
     (else
      (update-entity db table entity-id ktvlist)
      #f))))

(define (insert-entity-if-not-exists db table entity-type user entity-id ktvlist)
  (let ((found (get-entity-type db table entity-id)))
    (if (null? found)
        (insert-entity db table entity-type user ktvlist)
        #f)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; versioning

(define (get-entity-version db table entity-id)
  (select-first
   db (string-append "select version from " table "_entity where entity_id = ?")
   entity-id))

(define (get-entity-dirty db table entity-id)
  (select-first
   db (string-append "select dirty from " table "_entity where entity_id = ?")
   entity-id))

(define (update-entity-changed db table entity-id)
  (db-exec
   db (string-append
546 547
       "update " table "_entity set dirty=?, version=version+1 where entity_id = ?")
   1 entity-id))
Dave Griffiths's avatar
Dave Griffiths committed
548 549 550 551

(define (update-entity-version db table entity-id version)
  (db-exec
   db (string-append
552 553
       "update " table "_entity set dirty=0, version=? where entity_id = ?")
   version entity-id))
Dave Griffiths's avatar
Dave Griffiths committed
554 555

(define (update-entity-clean db table unique-id)
556 557
  (msg "cleaning")
  ;; clean entity table
Dave Griffiths's avatar
Dave Griffiths committed
558 559
  (db-exec
   db (string-append "update " table "_entity set dirty=? where unique_id = ?")
560 561 562 563
   0 unique-id)
  ;; clean value tables for this entity
  (msg "cleaning values")
  (clean-entity-values db table (entity-id-from-unique db table unique-id))  )
Dave Griffiths's avatar
Dave Griffiths committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

(define (get-dirty-stats db table)
  (list
   (select-first
    db (string-append "select count(entity_id) from " table "_entity where dirty=1"))
   (select-first
    db (string-append "select count(entity_id) from " table "_entity;"))))

(define (dirty-entities db table)
  (let ((de (db-select
             db (string-append
                 "select entity_id, entity_type, unique_id, dirty, version from " table "_entity where dirty=1;"))))
    (if (null? de)
        '()
        (map
         (lambda (i)
580
           (msg "dirty-entities")
Dave Griffiths's avatar
Dave Griffiths committed
581 582 583 584
           (list
            ;; build according to url ([table] entity-type unique-id dirty version)
            (cdr (vector->list i))
            ;; data entries (todo - only dirty values!)
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
            (dbg (get-entity-plain-for-sync db table (vector-ref i 0)))))
         (cdr de)))))

;; todo: BROKEN...
;; used for sync-all
(define (dirty-and-all-entities db table)
  (let ((de (db-select
             db (string-append
                 "select entity_id, entity_type, unique_id, dirty, version from " table "_entity"))))
    (if (null? de)
        '()
        (map
         (lambda (i)
           (list
            ;; build according to url ([table] entity-type unique-id dirty version)
            (cdr (vector->list i))
            ;; data entries (todo - only dirty values!)???????????
Dave Griffiths's avatar
Dave Griffiths committed
602 603 604
            (get-entity-plain db table (vector-ref i 0))))
         (cdr de)))))

605

Dave Griffiths's avatar
Dave Griffiths committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syncing

(define (stringify-list l)
  (foldl
   (lambda (i r)
     (string-append r " " i))
   "" l))

(define (stringify-ktvlist ktvlist)
  (foldl
   (lambda (i r)
     (string-append r " " (ktv-key i) ":" (stringify-value i)))
   ""
   ktvlist))

(define (build-sync-debug db table)
  (foldl
   (lambda (i r)
     (string-append
      r "\n" (vector-ref i 0) " " (vector-ref i 1) " "
      (stringify-ktvlist (get-entity db table (vector-ref i 0)))))
   ""
   (cdr (db-select
         db (string-append "select * from " table "_entity where dirty=1;")))))


(define (build-sync db table)
  (map
   (lambda (i)
     (list
      (vector->list i)
      (get-entity db table (vector-ref i 0))))
   (cdr (db-select
         db (string-append "select * from " table "_entity where dirty=1;")))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; doing things with unique ids

(define (entity-id-from-unique db table unique-id)
  (select-first
   db (string-append "select entity_id from " table "_entity where unique_id = ?")
   unique-id))

(define (entity-version-from-unique db table unique-id)
  (select-first
   db (string-append "select version from " table "_entity where unique_id = ?")
   unique-id))


(define (get-unique-id db table entity-id)
  (select-first
   db (string-append
       "select unique_id from " table "_entity where entity_id = ?")
       entity-id))

(define (get-entity-id db table unique-id)
  (select-first
   db (string-append
       "select entity_id from " table "_entity where unique_id = ?")
   unique-id))

668 669 670
(define (get-entity-by-unique db table unique-id)
  (get-entity db table (get-entity-id db table unique-id)))

Dave Griffiths's avatar
Dave Griffiths committed
671
(define (get-entity-name db table unique-id)
672
  (ktv-get (get-entity-by-unique db table unique-id) "name"))
Dave Griffiths's avatar
Dave Griffiths committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

(define (get-entity-names db table id-list)
  (foldl
   (lambda (id r)
     (if (equal? r "")
         (get-entity-name db table id)
         (string-append r ", " (get-entity-name db table id))))
   ""
   id-list))

(define (csv-titles db table entity-type)
  (foldl
   (lambda (kt r)
     (if (equal? r "") (string-append "\"" (ktv-key kt) "\"")
         (string-append r ", \"" (ktv-key kt) "\"")))
688
   "id, "
Dave Griffiths's avatar
Dave Griffiths committed
689 690 691 692 693 694 695 696 697 698 699 700 701
   (get-attribute-ids/types db table entity-type)))

(define (csv db table entity-type)
  (foldl
   (lambda (res r)
     (let ((entity (get-entity db table (vector-ref res 0))))
       (string-append
        r "\n"
        (foldl
         (lambda (ktv r)
           (cond
            ((equal? (ktv-key ktv) "unique_id") r)
            ((null? (ktv-value ktv))
702
             (msg "value not found in csv for " (ktv-key ktv))
Dave Griffiths's avatar
Dave Griffiths committed
703 704 705 706 707 708
             r)
            ;; dereferences lists of ids
            ((and
              (> (string-length (ktv-key ktv)) 8)
              (equal? (substring (ktv-key ktv) 0 8) "id-list-"))
             (string-append r ", \"" (get-entity-names db "sync" (string-split (ktv-value ktv) '(#\,))) "\""))
709
            ;; look for unique ids and dereference them
Dave Griffiths's avatar
Dave Griffiths committed
710 711 712 713 714 715
            ((and
              (> (string-length (ktv-key ktv)) 3)
              (equal? (substring (ktv-key ktv) 0 3) "id-"))
             (string-append r ", \"" (get-entity-name db "sync" (ktv-value ktv)) "\""))
            (else
             (string-append r ", \"" (stringify-value-url ktv) "\""))))
716
         (vector-ref res 1) ;; unique_id
Dave Griffiths's avatar
Dave Griffiths committed
717 718
         entity))))
   (csv-titles db table entity-type)
719 720 721 722
   (cdr (db-select
         db (string-append
             "select entity_id, unique_id from "
             table "_entity where entity_type = ?") entity-type))))
Dave Griffiths's avatar
Dave Griffiths committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

(define (db-open db-name)
  (cond
    ((file-exists? (string->path db-name))
     (display "open existing db")(newline)
     (open (string->path db-name)))
    (else
     (display "making new db")(newline)
     (let ((db (open (string->path db-name))))
       ;; todo, dynamically create these tables
       (setup db "sync")
       (setup db "stream")
       db))))


(define (unit-tests)
  ;; db
(msg "testing db")
(define db "unit-test.db")
(set! db (db-open db))

(define (feq a b)
  (< (abs (- a b)) 0.001))

;;(msg (db-status db))

;; test low level sql
(db-exec db "create table unittest ( id integer primary key autoincrement, name varchar(256), num int, r real )")

(define id (db-insert db "insert into unittest values (null, ?, ?, ?)" "hello" 23 1.1))
(asserteq "sql autoinc" (+ id 1) (db-insert db "insert into unittest values (null, ?, ?, ?)" "hello2" 26 2.3))

(let ((q (db-select db "select * from unittest")))
  (assert "sql length" (> (length q) 2)))

(let ((q (db-select db "select * from unittest where id = ?" id)))
  (asserteq "sql select one" (length q) 2)
  (assert "sql select two" (vector? (car q)))
  (asserteq "sql select 3" (vector-ref (cadr q) 2) 23)
  (assert "sql select 4" (feq (vector-ref (cadr q) 3) 1.1)))

(db-exec db "update unittest set name=? where id = ?" "bob" id)

(let ((q (db-select db "select * from unittest where id = ?" id)))
  (asserteq "sql update" (vector-ref (cadr q) 1) "bob"))

(db-exec db "update unittest set name=? where id = ?" "Robert'); DROP TABLE unittest;--" id)

(let ((q (db-select db "select * from unittest where id = ?" id)))
  (asserteq "bobby tables sql injection" (vector-ref (cadr q) 1) "Robert'); DROP TABLE unittest;--"))

;; test the entity attribute value system
(define table "eavunittest")
(setup db table)

(asserteq "ktv one" (stringify-value (ktv "one" "varchar" "two")) "'two'")
(asserteq "ktv 2" (stringify-value (ktv "one" "int" 3)) "3")
(asserteq "ktv 3" (stringify-value-url (ktv "one" "varchar" "two")) "two")
(asserteq "ktv 4" (stringify-value-url (ktv "one" "int" 3)) "3")

(asserteq "select first" (select-first db "select name from unittest where id = ?" (+ id 1))
          "hello2")

(define e (insert-entity db table "thing" "me" (list (ktv "param1" "varchar" "bob")
                                                     (ktv "param2" "int" 30)
788 789 790
                                                     (ktv "param3" "real" 3.141)
						     (ktv "name" "varchar" "name")
						     (ktv "deleted" "int" 0))))
Dave Griffiths's avatar
Dave Griffiths committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

(asserteq "eav ent type" (get-entity-type db table e) "thing")

(let ((e (get-entity db table e)))
  (asserteq "entity get 1" (ktv-get e "param1") "bob")
  (asserteq "entity get 2" (ktv-get e "param2") 30)
  (assert "entity get 3" (feq (ktv-get e "param3") 3.141)))

(update-value db table e (ktv "param1" "varchar" "fred"))

(let ((e (get-entity db table e)))
  (asserteq "update value 1" (ktv-get e "param1") "fred")
  (asserteq "update value 2" (ktv-get e "param2") 30))

(assert "all-entities" (> (length (all-entities db table "thing")) 0))

807 808
(msg "hello")

Dave Griffiths's avatar
Dave Griffiths committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
(update-entity db table e (list (ktv "param1" "varchar" "wotzit")
                                (ktv "param2" "int" 1)))

(let ((e (get-entity db table e)))
  (asserteq "update-entity 1" (ktv-get e "param1") "wotzit")
  (asserteq "update-entity 2" (ktv-get e "param2") 1))

(update-entity db table e (list (ktv "param3" "real" 3.3)))

(let ((e (get-entity db table e)))
  (msg e)
  (asserteq "update-entity 3" (ktv-get e "param1") "wotzit")
  (asserteq "update-entity 4" (ktv-get e "param2") 1)
  (assert "update-entity 5" (feq (ktv-get e "param3") 3.3)))

(define e2 (insert-entity db table "thing" "me"
                          (list (ktv "param1" "varchar" "bob")
                                (ktv "param2" "int" 30)
                                (ktv "param3" "real" 3.141)
                                (ktv "param4" "int" 0))))

(let ((e (get-entity db table e2)))
  (msg e)
  (asserteq "new entity 1" (ktv-get e "param1") "bob")
  (asserteq "new entity 2" (ktv-get e "param2") 30)
  (assert "new entity 3" (feq (ktv-get e "param3") 3.141))
  (asserteq "new entity 3" (ktv-get e "param4") 0))

;; test the versioning
(asserteq "dirty flag" (get-entity-dirty db table e) 1)
(asserteq "dirty flag2" (get-entity-dirty db table e2) 1)
(let ((uid (get-unique-id db table e2)))
  (update-entity-clean db table uid))
(asserteq "dirty flag post clean" (get-entity-dirty db table e2) 0)
(asserteq "versioning" (get-entity-version db table e) 2)
(asserteq "dirty flag3" (get-entity-dirty db table e) 1)
(assert "dirty" (> (length (dbg (dirty-entities db table))) 0))

(for-each
 (lambda (e)
   (update-entity-clean
    db table
    (list-ref (car e) 1)))
 (dirty-entities db table))

(asserteq "cleaning" (length (dirty-entities db table)) 0)

(msg (csv db table "thing"))

(msg (db-status db))
)

;(unit-tests)