created a command object and implemented the query and database-query functions for it
authorRuss Tyndall <russ@acceleration.net>
Thu, 8 Oct 2009 20:18:05 +0000 (16:18 -0400)
committerRuss Tyndall <russ@acceleration.net>
Thu, 8 Oct 2009 20:18:05 +0000 (16:18 -0400)
clsql-postgresql-socket3.asd
db-postgresql-socket3/command-object.lisp [new file with mode: 0644]
db-postgresql-socket3/sql.lisp

index eed756048138314abb94828d92d5f04167e8a6ba..e3d2c9be7f57741f533d61f496f22182a883d31b 100644 (file)
@@ -3,9 +3,9 @@
 ;;;; FILE IDENTIFICATION
 ;;;;
 ;;;; Name:          clsql-postgresql-socket.asd
-;;;; Purpose:       ASDF file for CLSQL PostgresSQL socket backend
-;;;; Programmer:    Kevin M. Rosenberg
-;;;; Date Started:  Aug 2002
+;;;; Purpose:       ASDF file for CLSQL PostgresSQL socket (protocol vs 3) backend
+;;;; Programmer:    Russ Tyndall
+;;;; Date Started:  Sept 2009
 ;;;;
 ;;;; $Id$
 ;;;;
@@ -33,6 +33,7 @@
   :components
   ((:module :db-postgresql-socket3
            :serial T
-           :components ((:file "package")
+           :components ((:file "command-object")
+                        (:file "package")
                         (:file "api")
                         (:file "sql")))))
diff --git a/db-postgresql-socket3/command-object.lisp b/db-postgresql-socket3/command-object.lisp
new file mode 100644 (file)
index 0000000..47dad33
--- /dev/null
@@ -0,0 +1,37 @@
+;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name:     postgresql-socket-sql.sql
+;;;; Purpose:  High-level PostgreSQL interface using socket
+;;;; Authors:  Russ Tyndall (at Acceleration.net) based on original code by
+;;;;           Kevin M. Rosenberg based on original code by Pierre R. Mai
+;;;; Created:  Sep 2009
+;;;;
+;;;;
+;;;; $Id$
+;;;;
+;;;; This file, part of CLSQL, is Copyright (c) 2002-2007 by Kevin M. Rosenberg
+;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
+;;;;
+;;;; CLSQL users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the Lisp Lesser GNU Public License
+;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
+;;;;
+;;;; *************************************************************************
+
+(in-package #:clsql-sys)
+
+(defclass command-object ()
+  ((expression :accessor expression :initarg :expression :initform nil)
+   (parameters :accessor parameters :initarg :parameters :initform nil)
+   (prepared-name :accessor prepared-name :initarg :prepared-name :initform ""
+    :documentation "If we want this to be a prepared statement, give it a name
+       to identify it to this session")
+   (has-been-prepared :accessor has-been-prepared :initarg :has-been-prepared :initform nil
+                     :documentation "Have we already prepared this command object")
+   ))
+
+(export '(expression parameters prepared-name has-been-prepared command-object))
+
+
index 11261624640a71830ea6484f640d10811647bc3d..8428d481f08a3d0570d652d9b17d00a1089c7c57 100644 (file)
        (apply #'values (cl-postgres:exec-query connection expression #'clsql-default-row-reader)))
       )))
 
+(defmethod query ((obj command-object) &key (database *default-database*)
+                  (result-types :auto) (flatp nil) (field-names t))
+  (clsql-sys::record-sql-command (expression obj) database)
+  (multiple-value-bind (rows names)
+      (database-query obj database result-types field-names)
+    (let ((result (if (and flatp (= 1 (length (car rows))))
+                      (mapcar #'car rows)
+                     rows)))
+      (clsql-sys::record-sql-result result database)
+      (if field-names
+          (values result names)
+         result))))
+
+(defmethod database-query ((obj command-object) (database postgresql-socket3-database) result-types field-names)
+  (let ((connection (database-connection database))
+       (cl-postgres:*sql-readtable* *sqlreader*))
+    (with-postgresql-handlers (database obj)
+      (let ((*include-field-names* field-names))
+       (unless (has-been-prepared obj)
+         (cl-postgres:prepare-query connection (prepared-name obj) (expression obj))
+         (setf (has-been-prepared obj) T))
+       (apply #'values (cl-postgres:exec-prepared
+                        connection
+                        (prepared-name obj)
+                        (parameters obj)
+                        #'clsql-default-row-reader))))))
+
 (defmethod database-execute-command
     ((expression string) (database postgresql-socket3-database))
   (let ((connection (database-connection database)))