[POS-commit] r5307 - in stoqlib/trunk: external/sqlobject/postgres

Johan Dahlin jdahlin at async.com.br
Wed Nov 1 12:06:46 BRT 2006


Author: jdahlin
Date: Wed Nov  1 12:06:45 2006
New Revision: 5307

Modified:
   stoqlib/trunk/external/sqlobject/postgres/pgconnection.py
   stoqlib/trunk/stoqlib/database/database.py

Log:
#2904: Move database manpulation functions to connection

Modified: stoqlib/trunk/external/sqlobject/postgres/pgconnection.py
==============================================================================
--- stoqlib/trunk/external/sqlobject/postgres/pgconnection.py	(original)
+++ stoqlib/trunk/external/sqlobject/postgres/pgconnection.py	Wed Nov  1 12:06:45 2006
@@ -181,6 +181,41 @@
         self.query("ALTER SEQUENCE %s START %d MINVALUE %d MAXVALUE %d" % (
             sequence, start, minvalue, maxvalue))
 
+    # Johan 2006-10-27: Add Database methods
+    def createDatabase(self, name, ifNotExists=False):
+        if ifNotExists and self.databaseExists(name):
+            return False
+
+        # We must close the transaction with a commit so that
+        # the CREATE DATABASE can work (which can't be in a transaction):
+        conn = self.getConnection()
+        cur = conn.cursor()
+        cur.execute('COMMIT')
+        cur.execute('CREATE DATABASE %s' % name)
+        cur.close()
+
+        return True
+
+    def dropDatabase(self, name, ifExists=False):
+        if ifExists and not self.databaseExists(name):
+            return False
+
+        # We must close the transaction with a commit so that
+        # the CREATE DATABASE can work (which can't be in a transaction):
+        conn = self.getConnection()
+        cur = conn.cursor()
+        cur.execute('COMMIT')
+        cur.execute('DROP DATABASE %s' % name)
+        cur.close()
+
+        return False
+
+    def databaseExists(self, name):
+        res = self.queryOne(
+            "SELECT COUNT(*) FROM pg_database WHERE datname='%s'" %
+            name)
+        return res[0] == 1
+
     def addColumn(self, tableName, column):
         self.query('ALTER TABLE %s ADD COLUMN %s' %
                    (tableName,
@@ -330,12 +365,7 @@
             if self.host:
                 dsn += ' host=%s' % self.host
         conn = self.module.connect(dsn)
-        cur = conn.cursor()
-        # We must close the transaction with a commit so that
-        # the CREATE DATABASE can work (which can't be in a transaction):
-        cur.execute('COMMIT')
-        cur.execute('CREATE DATABASE %s' % self.db)
-        cur.close()
+        conn.createDatabase(self.db)
         conn.close()
 
 

Modified: stoqlib/trunk/stoqlib/database/database.py
==============================================================================
--- stoqlib/trunk/stoqlib/database/database.py	(original)
+++ stoqlib/trunk/stoqlib/database/database.py	Wed Nov  1 12:06:45 2006
@@ -47,9 +47,7 @@
     @param dbname: name of the database
     @returns: if the database exists
     """
-    results = conn.queryOne(
-        "SELECT COUNT(*) FROM pg_database WHERE datname='%s'" % dbname)
-    return results[0] == 1
+    return conn.databaseExists(dbname)
 
 def drop_database(conn, dbname):
     """
@@ -57,12 +55,7 @@
     @param conn: a database connection
     @param dbname: name of the database
     """
-    pgconn = conn.getConnection()
-    curs = pgconn.cursor()
-    curs.execute('commit')
-
-    log.info('Dropping SQL database: %s' % dbname)
-    curs.execute('DROP DATABASE %s' % dbname)
+    return conn.dropDatabase(dbname)
 
 def create_database(conn, dbname):
     """
@@ -70,16 +63,7 @@
     @param conn: a database connection
     @param dbname: name of the database
     """
-
-    # We need to close the current transaction, which is probably created
-    # by SQLObject somehow, the only way to do that is to fetch the psycopg
-    # connection, get the cursor and run the 'commit' statement
-    pgconn = conn.getConnection()
-    curs = pgconn.cursor()
-    curs.execute('commit')
-
-    log.info('Creating SQL database: %s' % dbname)
-    curs.execute('CREATE DATABASE %s' % dbname)
+    return conn.createDatabase(dbname)
 
 def create_database_if_missing(conn, dbname):
     """
@@ -88,25 +72,17 @@
     @param dbname: the name of the database
     @returns: True if a database was created, False otherwise
     """
-    if database_exists(conn, dbname):
-        return False
-
-    create_database(conn, dbname)
-
-    return True
+    return conn.createDatabase(dbname, ifNotExists=True)
 
 def clean_database(dbname):
     """
     Cleans a database
     @param dbname: name of the database
     """
-
     settings = get_utility(IDatabaseSettings)
     conn = settings.get_default_connection()
-    if database_exists(conn, dbname):
-        drop_database(conn, dbname)
-
-    create_database(conn, dbname)
+    conn.dropDatabase(dbname, ifExists=True)
+    conn.createDatabase(dbname)
     conn.close()
 
 #


More information about the POS-commit mailing list