File: meta-queries.lisp

package info (click to toggle)
cl-pg 20040921
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 236 kB
  • ctags: 219
  • sloc: lisp: 2,881; makefile: 53; sh: 26
file content (40 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;;; meta-queries.lisp -- DBMS metainformation
;;;
;;; Author: Eric Marsden <emarsden@laas.fr>
;;; Time-stamp: <2004-03-05 emarsden>
;;
;;
;; Metainformation such as the list of databases present in the
;; database management system, list of tables, attributes per table.
;; This information is not available directly, but can be deduced by
;; querying the system tables.
;;
;; Based on the queries issued by psql in response to user commands
;; `\d' and `\d tablename'; see file pgsql/src/bin/psql/psql.c


(in-package :postgresql)

(defun pg-databases (conn)
  "Return a list of the databases available at this site."
  (let ((res (pg-exec conn "SELECT datname FROM pg_database")))
    (reduce #'append (pg-result res :tuples))))

(defun pg-tables (conn)
  "Return a list of the tables present in this database."
  (let ((res (pg-exec conn "SELECT relname FROM pg_class, pg_user WHERE "
                      "(relkind = 'r') AND relname !~ '^pg_' AND usesysid = relowner ORDER BY relname")))
    (reduce #'append (pg-result res :tuples))))

(defun pg-columns (conn table)
  "Return a list of the columns present in TABLE."
  (let ((res (pg-exec conn (format nil "SELECT * FROM ~s WHERE 0 = 1" table))))
    (mapcar #'first (pg-result res :attributes))))

(defun pg-backend-version (conn)
  "Return a string identifying the version and operating environment of the backend."
  (let ((res (pg-exec conn "SELECT version()")))
    (first (pg-result res :tuple 0))))


;; EOF