Fix SQLite usage of sqlite_sequence catalog.

It turns out that this catalog table in SQLite may or may not exists
depending on whether the current schema actually uses the feature. So we
need to learn to query the catalogs about the catalogs before querying the
catalogs to learn about the current schema. Thanks SQLite.
This commit is contained in:
Dimitri Fontaine 2017-06-02 22:34:58 +02:00
parent 57a7353a94
commit 9fb37bf513
2 changed files with 16 additions and 6 deletions

View File

@ -9,16 +9,24 @@
;;;
;;; Integration with the pgloader Source API
;;;
(defclass sqlite-connection (fd-connection) ())
(defclass sqlite-connection (fd-connection)
((has-sequences :accessor has-sequences)))
(defmethod initialize-instance :after ((slconn sqlite-connection) &key)
"Assign the type slot to sqlite."
(setf (slot-value slconn 'type) "sqlite"))
(defmethod open-connection ((slconn sqlite-connection) &key)
(defmethod open-connection ((slconn sqlite-connection) &key check-has-sequences)
(setf (conn-handle slconn)
(sqlite:connect (fd-path slconn)))
(log-message :debug "CONNECTED TO ~a" (fd-path slconn))
(when check-has-sequences
(let ((sql (format nil "SELECT tbl_name
FROM sqlite_master
WHERE tbl_name = 'sqlite_sequence'")))
(log-message :info "SQLite: ~a" sql)
(when (sqlite:execute-single (conn-handle slconn) sql)
(setf (has-sequences slconn) t))))
slconn)
(defmethod close-connection ((slconn sqlite-connection))
@ -66,7 +74,7 @@
(loop for (name) in (sqlite:execute-to-list db sql)
collect name)))
(defun list-columns (table &optional (db *sqlite-db*))
(defun list-columns (table &key db-has-sequences (db *sqlite-db*) )
"Return the list of columns found in TABLE-NAME."
(let* ((table-name (table-source-name table))
(sql (format nil "PRAGMA table_info(~a)" table-name)))
@ -80,7 +88,8 @@
(= 1 nullable)
(unquote default)
pk-id)))
(when (and (not (zerop pk-id))
(when (and db-has-sequences
(not (zerop pk-id))
(string-equal (coldef-ctype field) "integer"))
;; then it might be an auto_increment, which we know by
;; looking at the sqlite_sequence catalog
@ -96,6 +105,7 @@
(defun list-all-columns (schema
&key
db-has-sequences
(db *sqlite-db*)
including
excluding)
@ -104,7 +114,7 @@
:including including
:excluding excluding)
:do (let ((table (add-table schema table-name)))
(list-columns table db))))
(list-columns table :db db :db-has-sequences db-has-sequences))))
;;;

View File

@ -111,7 +111,7 @@
:use-result-as-rows t
:use-result-as-read t
:section :pre)
(with-connection (conn (source-db sqlite))
(with-connection (conn (source-db sqlite) :check-has-sequences t)
(let ((*sqlite-db* (conn-handle conn)))
(list-all-columns schema
:db *sqlite-db*