work in progress: access to SQL DB: create/drop table, config via etc.lisp

This commit is contained in:
Helmut Merz 2024-05-06 11:13:53 +02:00
parent c5c6a0bc7f
commit 82038d9dd5
5 changed files with 49 additions and 14 deletions

View file

@ -6,7 +6,7 @@
:version "0.0.1" :version "0.0.1"
:homepage "https://www.cyberconcepts.org" :homepage "https://www.cyberconcepts.org"
:description "" :description ""
:depends-on (:str) :depends-on (:str :sxql)
:components ((:file "forge/forge") :components ((:file "forge/forge")
(:file "storage/storage") (:file "storage/storage")
(:file "storage/tracking" :depends-on ("storage/storage")) (:file "storage/tracking" :depends-on ("storage/storage"))

View file

@ -4,11 +4,20 @@
(defpackage :scopes/storage (defpackage :scopes/storage
(:use :common-lisp) (:use :common-lisp)
(:export #:storage)) (:export #:*db-params*
#:storage
#:drop-table))
(in-package :scopes/storage) (in-package :scopes/storage)
(defparameter *db-params* nil)
(defclass storage () (defclass storage ()
((db :initarg :db) ((db :initarg :db)
(config :initarg :config) (config :initarg :config)
(schema :initarg :schema))) (schema :initarg :schema)))
(defun drop-table (st tn)
st
(print (sxql:yield
(sxql:drop-table tn :if-exists t))))

View file

@ -6,7 +6,7 @@
(:use :common-lisp) (:use :common-lisp)
(:export #:track #:time-stamp #:data (:export #:track #:time-stamp #:data
#:container #:container
#:crtable)) #:create-table))
(in-package :scopes/storage/tracking) (in-package :scopes/storage/tracking)
@ -19,9 +19,15 @@
(defclass container () (defclass container ()
((storage :initarg :storage))) ((storage :initarg :storage)))
(defun crtable (tn) (defun create-table (storage table-name head-fields)
(sxql:yield storage
(sxql:make-statement :create-table tn (let
(list ((id-type 'bigserial)
(list 'trackid :type 'bigserial :primary-key t) (hf-def (mapcar #'(lambda (x) (list x :type 'text)) head-fields)))
(list 'taskid :type 'text))))) (print (sxql:yield
(sxql:make-statement :create-table table-name
(nconc
`((trackid :type ,id-type :primary-key t))
hf-def
'((timestamp :type timestamp)
(data :type jsonb))))))))

10
test/etc.lisp Normal file
View file

@ -0,0 +1,10 @@
;;; cl-scopes/test/etc.lisp
(defparameter db-params-sqlite
'(:db-type :sqlite3))
(defparameter db-params-postgres
'(:db-type :postgres))
(setf storage:*db-params* db-params-sqlite)
(setf *db-params-postgres* db-params-postgres)

View file

@ -4,18 +4,28 @@
(defpackage :scopes/test-storage (defpackage :scopes/test-storage
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:tracking :scopes/storage/tracking) (:local-nicknames (:storage :scopes/storage)
(:tracking :scopes/storage/tracking)
(:t :scopes/testing)) (:t :scopes/testing))
(:export #:run)) (:export #:run #:try))
(in-package :scopes/test-storage) (in-package :scopes/test-storage)
(defparameter *db-params-postgres* nil)
(load "test/etc")
(defun try()
(print storage:*db-params*)
(print *db-params-postgres*))
(defun run () (defun run ()
(let ((tst (t:test-suite))) (let ((st (make-instance 'storage:storage))
(test-track tst) (tst (t:test-suite)))
(test-track tst st)
(t:result tst))) (t:result tst)))
(defun test-track (tst) (defun test-track (tst st)
(let ((tr (make-instance 'tracking:track))) (let ((tr (make-instance 'tracking:track)))
(storage:drop-table st :tracks)
(tracking:create-table st :tracks '(trackid username))
;(setf (scs:data tr) nil) ;(setf (scs:data tr) nil)
(t:assert-eq tst (tracking:data tr) nil))) (t:assert-eq tst (tracking:data tr) nil)))