aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/python/pgext.py
blob: 97c7e342bfcd7008208935d9eedee9d76bba5f84 (plain)
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
41
42
43
from pg import *

# This library file contains some common functions not directly provided by the
# PostGres C library. It offers too a keyword interface for pgmodule connect
# function.

# encapsulate pg connect function for keywords enabling
def doconnect(dbname = None, host = None, port = None, opt = None, tty = None):
	return connect(dbname, host, port, opt, tty)

# list all databases on the server 
def ListDB(pgcnx):
	result = pgcnx.query("select datname from pg_database")
	list = []
	for node in result:
		list.append(result[i][0])
	return list

# list all tables (classes) in the selected database
def ListTables(pgcnx):
	result = pgcnx.query("select relname from pg_class "	\
		"where relkind = 'r' "				\
		"  and relname !~ '^Inv' "			\
		"  and relname !~ '^pg_'")
	list = []
	for node in result:
		list.append(node[0])
	return list

# list table fields (attribute) in given table
def ListAllFields(pgcnx, table):
	result = pgcnx.query("select c.relname, a.attname, t.typname " \
		"from pg_class c, pg_attribute a, pg_type t "	\
		"where c.relname = '%s' "			\
		"  and a.attnum > 0"				\
		"  and a.attrelid = c.oid"			\
		"  and a.atttypid = t.oid "			\
		"order by relname, attname" % table)
	# personnal preference ... so I leave the original query
	list = []
	for node in result:
		list.append(node[1], node[2])
	return list