Skip to content

Commit

Permalink
load-schema: add GF, implement in SQLite client
Browse files Browse the repository at this point in the history
  • Loading branch information
mak08 committed Dec 17, 2019
1 parent 4190f60 commit f3825ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions sql/sql-api.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description High-level SQL API
;;; Author Michael Kappert 2013
;;; Last Modified <michael 2019-12-16 18:05:34>
;;; Last Modified <michael 2019-12-17 22:53:48>

(in-package :sql)

Expand Down Expand Up @@ -111,7 +111,7 @@
(defun update-schema (schema user-name &key (redeploy nil))
(with-transaction ()
(let* ((old-schema
(load-db-schema (schema-name schema))))
(load-schema *current-connection* (schema-name schema))))

(cond
((null old-schema)
Expand Down
5 changes: 4 additions & 1 deletion sql/sql-if.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description Low-level interface to the backend
;;; Author Michael 2014
;;; Last Modified <michael 2019-12-15 12:49:42>
;;; Last Modified <michael 2019-12-17 22:53:20>

(in-package :sql)

Expand Down Expand Up @@ -48,5 +48,8 @@
(with-output-to-string (s)
(serialize-for-connection conn sql-statement s))))

(defgeneric load-schema (connection name)
(:documentation "Load a schema definition from the DB"))

;;; EOF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 changes: 19 additions & 6 deletions sql/sql-package.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description
;;; Author Michael 2014
;;; Last Modified <michael 2019-12-16 20:37:11>
;;; Last Modified <michael 2019-12-17 23:10:15>

(defpackage "SQL"
(:use "COMMON-LISP"
Expand All @@ -13,7 +13,7 @@
(:export

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Database connection
;; Backend integration

;; The following two macros are implemented and exported from backend packages:
;; with-open-connection ((conn &rest args &key &allow-other-keys) &body forms)
Expand All @@ -25,21 +25,32 @@
;; Select current connection
with-connection

;; SQL - basic interaction
;; SQL - basic interaction.
;; The backends define methods for their own connection class.
sql-exec
sql-query
fetch
serialize-for-connection

;; Retrieving metadata from the DB
;; The backends define methods for their own connection class.
load-schema


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Embedded SQL - DDL Entities

;; SQL Statement
sql-statement

;; The parent struct of all SQL statements. Many DDL methods simply construct
;; an SQL string and pass it it sql-exec%. Instead, an sql-statement should be
;; constructed (a proper subtype may need to be defined first). This way,
;; backends can customize the SQL string.

sql-statement

;; Database
database-create-statement ; paradoxically, this exists because of SQLite
database-create-statement
database-create-statement-name

database-drop-statement
Expand All @@ -49,7 +60,6 @@
defschema
%create-schema
get-schema-by-name
load-db-schema
find-db-schema

schema
Expand Down Expand Up @@ -280,6 +290,9 @@

;; UUIDs
create-uuid

;; Parsing SQL
parse-table-definition

;; Internal use
!{}
Expand Down
24 changes: 23 additions & 1 deletion sqlite-client/sqlite-sql.cl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description SQLite specific DDL statements
;;; Author Michael Kappert 2019
;;; Last Modified <michael 2019-12-16 20:31:35>
;;; Last Modified <michael 2019-12-17 23:29:13>

(in-package :sqlite-client)

Expand Down Expand Up @@ -37,6 +37,28 @@
(format s "ATTACH '~a' AS ~a" schema-db schema-name)
(!{} conn (schema-create-statement-tables statement) s)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Loading metadata
;;;
;;; SQLite stores CREATE TABLE statements in a special table SQLITE_MASTER.

(defmethod load-schema ((connection sqlite-connection) (schema string))
(let ((schema-table
(cond ((string= schema "")
'sqlite_master)
(t
(make-symbol (format nil "~a.sqlite_master" schema))))))
(with-connection (connection)
(multiple-value-bind (columns rows)
(?select 'sql
:from schema-table
:where (?and (?= 'type "table")
(?not (?= 'name "__schema"))))
(make-schema :name schema
:tables (loop
:for (sql) :in rows
:collect (parse-table-definition sql)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; DROP TABLE

Expand Down

0 comments on commit f3825ca

Please sign in to comment.