;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;; *************************************************************************
-(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
-(in-package :cl-user)
+(in-package #:cl-user)
(defpackage :clsql-postgresql-socket
- (:use :common-lisp :clsql-base-sys :postgresql-socket)
+ (:use #:common-lisp #:clsql-base-sys #:postgresql-socket)
(:export #:postgresql-socket-database)
(:documentation "This is the CLSQL socket interface to PostgreSQL."))
-(in-package :clsql-postgresql-socket)
+(in-package #:clsql-postgresql-socket)
;; interface foreign library loading routines
(declare (ignore password options tty))
(concatenate 'string
(etypecase host
+ (null
+ "localhost")
(pathname (namestring host))
(string host))
(when port
(make-instance 'postgresql-socket-database
:name (database-name-from-spec connection-spec
database-type)
+ :database-type :postgresql-socket
:connection-spec connection-spec
:connection connection)))))
(close-postgresql-connection (database-connection database))
t)
-(defmethod database-query (expression (database postgresql-socket-database) types)
+(defmethod database-query (expression (database postgresql-socket-database) result-types)
(let ((connection (database-connection database)))
(with-postgresql-handlers (database expression)
(start-query-execution connection expression)
:expression expression
:errno 'missing-result
:error "Didn't receive result cursor for query."))
- (setq types (canonicalize-types types cursor))
- (loop for row = (read-cursor-row cursor types)
+ (setq result-types (canonicalize-types result-types cursor))
+ (loop for row = (read-cursor-row cursor result-types)
while row
collect row
finally
(defmethod database-query-result-set ((expression string)
(database postgresql-socket-database)
- &key full-set types)
+ &key full-set result-types)
(declare (ignore full-set))
(let ((connection (database-connection database)))
(with-postgresql-handlers (database expression)
(values (make-postgresql-socket-result-set
:done nil
:cursor cursor
- :types (canonicalize-types types cursor))
+ :types (canonicalize-types result-types cursor))
(length (postgresql-cursor-fields cursor)))))))
(defmethod database-dump-result-set (result-set
database nil)))))
-;; Functions depending upon high-level CommonSQL classes/functions
-#|
-(defmethod database-output-sql ((expr clsql-sys::sql-typecast-exp)
- (database postgresql-socket-database))
- (with-slots (clsql-sys::modifier clsql-sys::components)
- expr
- (if clsql-sys::modifier
- (progn
- (clsql-sys::output-sql clsql-sys::components database)
- (write-char #\: clsql-sys::*sql-stream*)
- (write-char #\: clsql-sys::*sql-stream*)
- (write-string (symbol-name clsql-sys::modifier)
- clsql-sys::*sql-stream*)))))
-
-(defmethod database-output-sql-as-type ((type (eql 'integer)) val
- (database postgresql-socket-database))
- (when val ;; typecast it so it uses the indexes
- (make-instance 'clsql-sys::sql-typecast-exp
- :modifier 'int8
- :components val)))
-|#
+(defmethod database-create (connection-spec (type (eql :postgresql-socket)))
+ (destructuring-bind (host name user password) connection-spec
+ (let ((database (database-connect (list host "template1" user password)
+ type)))
+ (unwind-protect
+ (execute-command (format nil "create database ~A" name))
+ (database-disconnect database)))))
+
+(defmethod database-destroy (connection-spec (type (eql :postgresql-socket)))
+ (destructuring-bind (host name user password) connection-spec
+ (let ((database (database-connect (list host "template1" user password)
+ type)))
+ (unwind-protect
+ (execute-command (format nil "drop database ~A" name))
+ (database-disconnect database)))))
+
+(defmethod database-probe (connection-spec (type (eql :postgresql-socket)))
+ (destructuring-bind (host name user password) connection-spec
+ (let ((database (database-connect (list host "template1" user password)
+ type)))
+ (unwind-protect
+ (when
+ (find name (database-query "select datname from pg_database"
+ database :auto)
+ :key #'car :test #'string-equal)
+ t)
+ (database-disconnect database)))))
+
+(defmethod database-describe-table ((database postgresql-socket-database)
+ table)
+ (database-query
+ (format nil "select a.attname, t.typname
+ from pg_class c, pg_attribute a, pg_type t
+ where c.relname = '~a'
+ and a.attnum > 0
+ and a.attrelid = c.oid
+ and a.atttypid = t.oid"
+ (sql-escape (string-downcase
+ (etypecase table
+ (string table)
+ (clsql-base-sys::sql-create-table
+ (symbol-name
+ (slot-value table 'clsql-base-sys::name)))))))
+ database :auto))
(when (clsql-base-sys:database-type-library-loaded :postgresql-socket)
(clsql-base-sys:initialize-database-type :database-type :postgresql-socket))