Mercurial > hg > amuse
comparison base/database/database-connect.lisp @ 279:c591a5034da6
New base module for general database functions
Ignore-this: 403f22ca6285cba4a23eeddb1d0a806f
Currently just provides a slightly more secure way of connecting to a database.
darcs-hash:20090519135617-16a00-ce026a38abfb53e5728cd0cbb6c976e403691dde.gz
author | j.forth <j.forth@gold.ac.uk> |
---|---|
date | Tue, 19 May 2009 14:56:17 +0100 |
parents | |
children | e1842efb1dd4 |
comparison
equal
deleted
inserted
replaced
278:8ec5cc5466fb | 279:c591a5034da6 |
---|---|
1 (cl:in-package #:amuse-database-admin) | |
2 | |
3 (defparameter *amuse-database* nil) | |
4 | |
5 (defun connect-to-database (&key (database-name "amuse") username | |
6 use-tunnel (make-default t)) | |
7 "Well, slightly more secure I guess. Requires that an option file | |
8 exists containing connection-spec s-expressions. If no such file | |
9 exists a skeleton file can be created by calling | |
10 make-db-option-file (the password still needs to be added | |
11 manually). The path is ~/.amuse.lisp. | |
12 | |
13 A better approach might be to use something like: | |
14 http://www.cliki.net/trivial-configuration-parser. Or for slime to | |
15 prompt the user for the password something | |
16 like (swank::eval-in-emacs `(read-passwd \"Password: \")). However, | |
17 this presents other security issues. | |
18 | |
19 If use-tunnel is t, then regardless of the host specified in the | |
20 connection-spec, it will be set to localhost. It is assumed that the | |
21 tunnel is already set up. | |
22 | |
23 When make-default is t, the newly created connection is assigned to | |
24 *amuse-database*. If make-default is nil, then the connection is | |
25 returned and is expected to be handled by the caller. This is useful | |
26 for connecting to different databases within individual | |
27 implementations (mainly for testing purposes)." | |
28 (if (and make-default | |
29 *amuse-database* | |
30 (clsql-mysql::is-database-open *amuse-database*)) | |
31 (error "A default AMuSE database connection already exists: | |
32 ~S" (clsql:database-name *amuse-database*)) | |
33 (let ((connection-spec (%get-connection-spec database-name | |
34 username))) | |
35 (when use-tunnel | |
36 (setf (car connection-spec) "127.0.0.1")) | |
37 (let ((connection (%get-database-connection connection-spec))) | |
38 (when make-default | |
39 (setf *amuse-database* connection)) | |
40 connection)))) | |
41 | |
42 (defun disconnect-from-database (&optional (database-connection | |
43 *amuse-database*)) | |
44 (clsql:disconnect :database database-connection)) | |
45 | |
46 (defun make-db-option-file (&key database-name host username (port "3306")) | |
47 (with-open-file (stream (%make-db-option-file-pathname) :direction :output) | |
48 (sb-ext:run-program "chmod" | |
49 (list "600" (namestring | |
50 (%make-db-option-file-pathname))) | |
51 :search t) | |
52 (princ " | |
53 ;;; Option file containing connection-spec s-expressions for connecting | |
54 ;;; to a database from within AMuSE. You can have multiple specifications, | |
55 ;;; but they must be uniquely identifiable by database name or a combination | |
56 ;;; of database and username. | |
57 | |
58 ;;; You need to fill in the missing <details>." stream) | |
59 (format stream "~2%(~S ~S ~S \"<pwd>\" ~S)" | |
60 (if host host "<host>") | |
61 (if database-name database-name "<db-name>") | |
62 (if username username "<username>") | |
63 port)) | |
64 (warn "You now need to manually edit ~A." | |
65 (namestring (%make-db-option-file-pathname)))) | |
66 | |
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
68 ;;; Helper functions | |
69 | |
70 (defun %get-database-connection (connection-spec) | |
71 (clsql:connect connection-spec | |
72 :if-exists :old | |
73 :database-type :mysql)) | |
74 | |
75 (defun %get-connection-spec (db-name username) | |
76 (let ((option-file (probe-file (%make-db-option-file-pathname)))) | |
77 (if option-file | |
78 (with-open-file (stream option-file) | |
79 (loop for connection-spec = (read stream nil) | |
80 while connection-spec | |
81 do (destructuring-bind (host db usr pwd prt) | |
82 connection-spec | |
83 (declare (ignore host pwd prt)) | |
84 (when (equal db-name db) | |
85 (when (or (null username) | |
86 (equal username usr)) | |
87 (return connection-spec)))) | |
88 finally (error "No connection-spec exists matching | |
89 database: ~A and username: ~A" db-name username))) | |
90 ;; If file doesn't exist, create it. | |
91 (error "~A option file does not exist. | |
92 You need to run: amuse-database-admin:make-db-option-file." | |
93 (namestring (%make-db-option-file-pathname)))))) | |
94 | |
95 (defun %make-db-option-file-pathname () | |
96 (merge-pathnames (user-homedir-pathname) ".amuse.lisp")) |