odb_postgres.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [args]
5 """
6 
7 
8 import os, sys, string, time, getopt
9 from log import *
10 
11 import odb
12 from pyPgSQL import PgSQL
13 
15  def insert_id(self, tablename, colname):
16  self.execute("select last_value from %s_%s_seq" % (tablename, colname))
17  row = self.fetchone()
18  return row[0]
19 
21  def __init__(self, *args, **kwargs):
22  odb.Connection.__init__(self)
23 
24  self._conn = apply(PgSQL.connect, args, kwargs)
25  self.SQLError = PgSQL.OperationalError
26 
27  def getConnType(self): return "postgres"
28 
29  def cursor(self):
30  return Cursor(self._conn.cursor())
31 
32  def escape(self,str):
33  if str is None:
34  return None
35  elif type(str) == type(""):
36  return string.replace(str,"'","''")
37  elif type(str) == type(1):
38  return str
39  else:
40  raise "unknown column data type: %s" % type(str)
41 
42  def listTables(self, cursor):
43  cursor.execute("select tablename from pg_catalog.pg_tables")
44  rows = cursor.fetchall()
45  tables = []
46  for row in rows:
47  tables.append(row[0])
48  return tables
49 
50  def listIndices(self, tableName, cursor):
51  sql = "select indexname from pg_catalog.pg_indexes where tablename='%s'" % tableName
52  cursor.execute(sql)
53  rows = cursor.fetchall()
54  tables = map(lambda row: row[0], rows)
55  return tables
56 
57  def listFieldsDict(self, table_name, cursor):
58  sql = "SELECT c.oid, n.nspname, c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE pg_catalog.pg_table_is_visible(c.oid) AND c.relname = '%s' ORDER BY 2, 3;" % table_name
59  cursor.execute(sql)
60  row = cursor.fetchone()
61  oid = row[0]
62 
63  sql = "SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), a.attnotnull, a.attnum FROM pg_catalog.pg_attribute a WHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum" % oid
64  cursor.execute(sql)
65  rows = cursor.fetchall()
66 
67  columns = {}
68  for row in rows:
69  colname = row[0]
70  columns[colname] = row
71 
72  return columns
73 
74  def alterTableToMatch(self, table, cursor):
75  invalidAppCols, invalidDBCols = table.checkTable()
76  if not invalidAppCols: return
77 
78  defs = []
79  for colname in invalidAppCols.keys():
80  col = table.getColumnDef(colname)
81  colname = col[0]
82  coltype = col[1]
83  options = col[2]
84  defs.append(table._colTypeToSQLType(colname, coltype, options))
85 
86  defs = string.join(defs, ", ")
87 
88  sql = "alter table %s add column " % table.getTableName()
89  sql = sql + "(" + defs + ")"
90 
91  print sql
92 
93  cursor.execute(sql)
94 
95  def auto_increment(self, coltype):
96  return "SERIAL", None
97 
98 
99  def supportsTriggers(self): return False
def __init__(self, args, kwargs)
Definition: odb_postgres.py:21
def listFieldsDict(self, table_name, cursor)
Definition: odb_postgres.py:57
def fetchone(self)
Definition: odb.py:361
def listIndices(self, tableName, cursor)
Definition: odb_postgres.py:50
def alterTableToMatch(self, table, cursor)
Definition: odb_postgres.py:74
def execute(self, sql)
Definition: odb.py:354
def insert_id(self, tablename, colname)
Definition: odb_postgres.py:15


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13