1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
\r
2 ;;;; *************************************************************************
\r
3 ;;;; FILE IDENTIFICATION
\r
5 ;;;; Name: mysql-usql.cl
\r
6 ;;;; Purpose: MySQL interface functions to support UncommonSQL
\r
7 ;;;; Programmers: Kevin M. Rosenberg and onShore Development Inc
\r
8 ;;;; Date Started: Mar 2002
\r
10 ;;;; $Id: mysql-usql.cl,v 1.6 2002/05/27 17:19:30 kevin Exp $
\r
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
\r
13 ;;;; and by onShore Development Inc.
\r
15 ;;;; CLSQL users are granted the rights to distribute and use this software
\r
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
\r
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
\r
18 ;;;; *************************************************************************
\r
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
\r
21 (in-package :clsql-mysql)
\r
23 ;; Table and attribute introspection
\r
25 (defmethod database-list-tables ((database mysql-database)
\r
26 &key (system-tables nil))
\r
27 (declare (ignore system-tables))
\r
28 (mapcar #'car (database-query "show tables" database :auto)))
\r
31 (defmethod database-list-attributes ((table string) (database mysql-database))
\r
34 (format nil "SHOW COLUMNS FROM ~A" table)
\r
37 (defmethod database-attribute-type (attribute (table string)
\r
38 (database mysql-database))
\r
43 "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute)
\r
45 (let* ((str (car result))
\r
46 (end-str (position #\( str))
\r
47 (substr (subseq str 0 end-str)))
\r
49 (intern (string-upcase substr) :keyword) nil))))
\r
51 ;;; Sequence functions
\r
53 (defun %sequence-name-to-table (sequence-name)
\r
54 (concatenate 'string "_usql_seq_" (sql-escape sequence-name)))
\r
56 (defmethod database-create-sequence (sequence-name
\r
57 (database mysql-database))
\r
58 (let ((table-name (%sequence-name-to-table sequence-name)))
\r
59 (database-execute-command
\r
60 (concatenate 'string "CREATE TABLE " table-name
\r
61 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
\r
63 (database-execute-command
\r
64 (concatenate 'string "INSERT INTO " table-name
\r
68 (defmethod database-drop-sequence (sequence-name
\r
69 (database mysql-database))
\r
70 (database-execute-command
\r
71 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
\r
74 (defmethod database-sequence-next (sequence-name (database mysql-database))
\r
75 (database-execute-command
\r
76 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
\r
77 " SET id=LAST_INSERT_ID(id+1)")
\r
79 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
\r
81 ;; Misc USQL functions
\r
85 (defmethod database-output-sql ((expr clsql-sys::sql-typecast-exp)
\r
86 (database mysql-database))
\r
87 (with-slots (clsql-sys::modifier clsql-sys::components)
\r
89 (if clsql-sys::modifier
\r
91 (clsql-sys::output-sql clsql-sys::components database)
\r
92 (write-char #\: sql-sys::*sql-stream*)
\r
93 (write-char #\: sql-sys::*sql-stream*)
\r
94 (write-string (symbol-name clsql-sys::modifier)
\r
95 clsql-sys::*sql-stream*)))))
\r
98 (defmethod database-output-sql-as-type ((type (eql 'integer)) val
\r
99 (database mysql-database))
\r
100 ;; typecast it so it uses the indexes
\r
102 (make-instance 'clsql-sys::sql-typecast-exp
\r