From 09a2bc41215abb9dd9c92db6cb70e04fa845f938 Mon Sep 17 00:00:00 2001 From: Russ Tyndall Date: Thu, 8 Oct 2009 16:18:05 -0400 Subject: [PATCH] created a command object and implemented the query and database-query functions for it --- clsql-postgresql-socket3.asd | 9 +++--- db-postgresql-socket3/command-object.lisp | 37 +++++++++++++++++++++++ db-postgresql-socket3/sql.lisp | 27 +++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 db-postgresql-socket3/command-object.lisp diff --git a/clsql-postgresql-socket3.asd b/clsql-postgresql-socket3.asd index eed7560..e3d2c9b 100644 --- a/clsql-postgresql-socket3.asd +++ b/clsql-postgresql-socket3.asd @@ -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 index 0000000..47dad33 --- /dev/null +++ b/db-postgresql-socket3/command-object.lisp @@ -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)) + + diff --git a/db-postgresql-socket3/sql.lisp b/db-postgresql-socket3/sql.lisp index 1126162..8428d48 100644 --- a/db-postgresql-socket3/sql.lisp +++ b/db-postgresql-socket3/sql.lisp @@ -201,6 +201,33 @@ (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))) -- 2.34.1