1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
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
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.
21 ;;;; *************************************************************************
23 (in-package #:clsql-sys)
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?")
39 (defgeneric prepare-sql-parameter (sql-parameter)
40 (:documentation "This method is responsible for formatting parameters
41 as the database expects them (eg: :false is nil, nil is :null, dates are iso8601 strings)")
42 (:method (sql-parameter)
43 (typecase sql-parameter
46 (if (member sql-parameter (list :false :F))
48 (princ-to-string sql-parameter)))
49 (clsql-sys:date (format-date nil sql-parameter :format :iso8601))
50 (clsql-sys:wall-time (format-time nil sql-parameter :format :iso8601))
53 (defmethod initialize-instance :after ((o command-object) &key &allow-other-keys )
54 ;; Inits parameter value coersion
55 (setf (parameters o) (parameters o)))
57 (defmethod (setf parameters) (new (o command-object))
58 " This causes the semantics to match cl-sql instead of cl-postgresql
60 (setf (slot-value o 'parameters)
61 (loop for p in new collecting (prepare-sql-parameter p))))
63 (defun reset-command-object (co)
64 "Resets the command object to have no name and to be unprepared
65 (This is useful if you want to run a command against a second database)"
66 (setf (prepared-name co) ""
67 (has-been-prepared co) nil))
69 (defun command-object (expression &optional parameters (prepared-name ""))
70 (make-instance 'command-object
71 :expression expression
72 :parameters parameters
73 :prepared-name prepared-name))