improved and moved command object up to clsql (out of cl-postgres-socket3)
[clsql.git] / sql / command-object.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     postgresql-socket-sql.sql
6 ;;;; Purpose:  High-level PostgreSQL interface using socket
7 ;;;; Authors:  Russ Tyndall (at Acceleration.net) based on original code by
8 ;;;;           Kevin M. Rosenberg based on original code by Pierre R. Mai
9 ;;;; Created:  Sep 2009
10 ;;;;
11 ;;;;
12 ;;;; $Id$
13 ;;;;
14 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2007 by Kevin M. Rosenberg
15 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
16 ;;;;
17 ;;;; CLSQL users are granted the rights to distribute and use this software
18 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
19 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
20 ;;;;
21 ;;;; *************************************************************************
22
23 (in-package #:clsql-sys)
24
25 (defclass command-object ()
26   ((expression :accessor expression :initarg :expression :initform nil)
27    (parameters :accessor parameters :initarg :parameters :initform nil)
28    (prepared-name :accessor prepared-name :initarg :prepared-name :initform ""
29     :documentation "If we want this to be a prepared statement, give it a name
30        to identify it to this session")
31    (has-been-prepared :accessor has-been-prepared :initarg :has-been-prepared :initform nil
32                       :documentation "Have we already prepared this command object")
33    ))
34
35 (defmethod initialize-instance :after ((o command-object) &key &allow-other-keys )
36   ;; Inits parameter nulls
37   (setf (parameters o) (parameters o)))
38
39 (defmethod (setf parameters) (new (o command-object))
40   " This causes the semantics to match cl-sql instead of cl-postgresql
41   "
42   (setf (slot-value o 'parameters)
43         (loop for p in new
44               collecting (cond ((null p) :null)
45                                ((member p (list :false :F)) nil)
46                                (T p)))))
47
48 (defun reset-command-object (co)
49   "Resets the command object to have no name and to be unprepared
50      (This is useful if you want to run a command against a second database)"
51   (setf (prepared-name co) ""
52         (has-been-prepared co) nil))
53
54 (defun command-object (expression &optional parameters (prepared-name ""))
55   (make-instance 'command-object
56                  :expression expression
57                  :parameters parameters
58                  :prepared-name prepared-name))