odb_postgres.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 """
00004 usage: %(progname)s [args]
00005 """
00006 
00007 
00008 import os, sys, string, time, getopt
00009 from log import *
00010 
00011 import odb
00012 from pyPgSQL import PgSQL
00013 
00014 class Cursor(odb.Cursor):
00015   def insert_id(self, tablename, colname):
00016     self.execute("select last_value from %s_%s_seq" % (tablename, colname))
00017     row = self.fetchone()
00018     return row[0]
00019 
00020 class Connection(odb.Connection):
00021   def __init__(self, *args, **kwargs):
00022     odb.Connection.__init__(self)
00023 
00024     self._conn = apply(PgSQL.connect, args, kwargs)
00025     self.SQLError = PgSQL.OperationalError
00026     
00027   def getConnType(self): return "postgres"
00028 
00029   def cursor(self):
00030     return Cursor(self._conn.cursor())
00031 
00032   def escape(self,str):
00033     if str is None:
00034       return None
00035     elif type(str) == type(""):
00036       return string.replace(str,"'","''")
00037     elif type(str) == type(1):
00038       return str
00039     else:
00040       raise "unknown column data type: %s" % type(str)
00041 
00042   def listTables(self, cursor):
00043     cursor.execute("select tablename from pg_catalog.pg_tables")
00044     rows = cursor.fetchall()
00045     tables = []
00046     for row in rows:
00047       tables.append(row[0])
00048     return tables
00049 
00050   def listIndices(self, tableName, cursor):
00051     sql = "select indexname from pg_catalog.pg_indexes where tablename='%s'" % tableName
00052     cursor.execute(sql)
00053     rows = cursor.fetchall()
00054     tables = map(lambda row: row[0], rows)
00055     return tables
00056 
00057   def listFieldsDict(self, table_name, cursor):
00058     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
00059     cursor.execute(sql)
00060     row = cursor.fetchone()
00061     oid = row[0]
00062     
00063     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
00064     cursor.execute(sql)
00065     rows = cursor.fetchall()
00066 
00067     columns = {}
00068     for row in rows:
00069       colname = row[0]
00070       columns[colname] = row
00071 
00072     return columns
00073 
00074   def alterTableToMatch(self, table, cursor):
00075     invalidAppCols, invalidDBCols = table.checkTable()
00076     if not invalidAppCols: return
00077 
00078     defs = []
00079     for colname in invalidAppCols.keys():
00080       col = table.getColumnDef(colname)
00081       colname = col[0]
00082       coltype = col[1]
00083       options = col[2]
00084       defs.append(table._colTypeToSQLType(colname, coltype, options))
00085 
00086     defs = string.join(defs, ", ")
00087 
00088     sql = "alter table %s add column " % table.getTableName()
00089     sql = sql + "(" + defs + ")"
00090 
00091     print sql
00092 
00093     cursor.execute(sql)
00094       
00095   def auto_increment(self, coltype):
00096     return "SERIAL", None
00097 
00098     
00099   def supportsTriggers(self): return False


pyclearsilver
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:35:42