+(defmethod database-create (connection-spec (type (eql :postgresql)))
+ (destructuring-bind (host name user password) connection-spec
+ (declare (ignore user password))
+ (multiple-value-bind (output status)
+ (clsql-sys:command-output "createdb -h~A ~A"
+ (if host host "localhost")
+ name)
+ (if (or (not (zerop status))
+ (search "database creation failed: ERROR:" output))
+ (error 'clsql-access-error
+ :connection-spec connection-spec
+ :database-type type
+ :error
+ (format nil "database-create failed: ~A"
+ output))
+ t))))
+
+(defmethod database-destroy (connection-spec (type (eql :postgresql)))
+ (destructuring-bind (host name user password) connection-spec
+ (declare (ignore user password))
+ (multiple-value-bind (output status)
+ (clsql-sys:command-output "dropdb -h~A ~A"
+ (if host host "localhost")
+ name)
+ (if (or (not (zerop status))
+ (search "database removal failed: ERROR:" output))
+ (error 'clsql-access-error
+ :connection-spec connection-spec
+ :database-type type
+ :error
+ (format nil "database-destory failed: ~A"
+ output))
+ t))))
+
+
+(defmethod database-probe (connection-spec (type (eql :postgresql)))
+ (when (find (second connection-spec) (database-list connection-spec type)
+ :key #'car :test #'string-equal)
+ t))
+
+(defmethod database-list (connection-spec (type (eql :postgresql)))
+ (destructuring-bind (host name user password) connection-spec
+ (declare (ignore name))
+ (let ((database (database-connect (list host "template1" user password)
+ type)))
+ (unwind-protect
+ (progn
+ (setf (slot-value database 'clsql-sys::state) :open)
+ (mapcar #'car (database-query "select datname from pg_database"
+ database nil nil)))
+ (progn
+ (database-disconnect database)
+ (setf (slot-value database 'clsql-sys::state) :closed))))))
+
+(defmethod database-describe-table ((database postgresql-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 table)))
+ database :auto nil))
+
+(defun %pg-database-connection (connection-spec)
+ (check-connection-spec connection-spec :postgresql
+ (host db user password &optional port options tty))
+ (macrolet ((coerce-string (var)
+ `(unless (typep ,var 'simple-base-string)
+ (setf ,var (coerce ,var 'simple-base-string)))))
+ (destructuring-bind (host db user password &optional port options tty)
+ connection-spec
+ (coerce-string db)
+ (coerce-string user)
+ (let ((connection (PQsetdbLogin host port options tty db user password)))
+ (declare (type postgresql::pgsql-conn-ptr connection))
+ (unless (eq (PQstatus connection) :connection-ok)
+ ;; Connect failed
+ (error 'clsql-connect-error
+ :database-type :postgresql
+ :connection-spec connection-spec
+ :errno (PQstatus connection)
+ :error (PQerrorMessage connection)))
+ connection))))
+
+(defmethod database-reconnect ((database postgresql-database))
+ (let ((lock (database-lock database)))
+ (with-process-lock (lock "Reconnecting")
+ (with-slots (connection-spec conn-ptr)
+ database
+ (setf conn-ptr (%pg-database-connection connection-spec))
+ database))))
+
+;;; Database capabilities
+
+(defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
+ t)
+
+(defmethod db-type-default-case ((db-type (eql :postgresql)))
+ :lower)