d931bac46ed4fd63240c5ada0b0003844b4804a0
[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                :documentation "query that refers to parameters using \"$1\", \"$2\", \"$n\".
28        These match positions in the parameters list.")
29    (parameters :accessor parameters :initarg :parameters :initform nil
30                :documentation "list of parameters")
31    (prepared-name :accessor prepared-name :initarg :prepared-name :initform ""
32     :documentation "If we want this to be a prepared statement, give it a name
33        to identify it to this session")
34    (has-been-prepared :accessor has-been-prepared :initarg :has-been-prepared :initform nil
35                       :documentation "Have we already prepared this command object?")
36    ))
37
38 (defmethod initialize-instance :after ((o command-object) &key &allow-other-keys )
39   ;; Inits parameter nulls
40   (setf (parameters o) (parameters o)))
41
42 (defmethod (setf parameters) (new (o command-object))
43   " This causes the semantics to match cl-sql instead of cl-postgresql
44   "
45   (setf (slot-value o 'parameters)
46         (loop for p in new
47               collecting (cond ((null p) :null)
48                                ((member p (list :false :F)) nil)
49                                (T p)))))
50
51 (defun reset-command-object (co)
52   "Resets the command object to have no name and to be unprepared
53      (This is useful if you want to run a command against a second database)"
54   (setf (prepared-name co) ""
55         (has-been-prepared co) nil))
56
57 (defun command-object (expression &optional parameters (prepared-name ""))
58   (make-instance 'command-object
59                  :expression expression
60                  :parameters parameters
61                  :prepared-name prepared-name))