Author: test
Date: 2005-09-07 21:06:29 +0000 (Wed, 07 Sep 2005)
New Revision: 973
Modified:
SQLObject/trunk/sqlobject/dbconnection.py
SQLObject/trunk/sqlobject/postgres/pgconnection.py
Log:
Add conn.createEmptyDatabase() method, plus an implementation for Postgres
Modified: SQLObject/trunk/sqlobject/dbconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/dbconnection.py 2005-09-07 21:05:48 UTC (rev 972)
+++ SQLObject/trunk/sqlobject/dbconnection.py 2005-09-07 21:06:29 UTC (rev 973)
@@ -667,6 +667,12 @@
finally:
self._poolLock.release()
+ def createEmptyDatabase(self):
+ """
+ Create an empty database.
+ """
+ raise NotImplementedError
+
class Iteration(object):
def __init__(self, dbconn, rawconn, select, keepConnection=False):
Modified: SQLObject/trunk/sqlobject/postgres/pgconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/postgres/pgconnection.py 2005-09-07 21:05:48 UTC (rev 972)
+++ SQLObject/trunk/sqlobject/postgres/pgconnection.py 2005-09-07 21:06:29 UTC (rev 973)
@@ -16,6 +16,7 @@
user=None, passwd=None, usePygresql=False,
**kw):
global psycopg, pgdb
+ self.usePygresql = usePygresql
if usePygresql:
if pgdb is None:
import pgdb
@@ -278,7 +279,35 @@
return self._server_version
server_version = property(server_version)
+ def createEmptyDatabase(self):
+ # We have to connect to *some* database, so we'll connect to
+ # template1, which is a common open database.
+ # @@: This doesn't use self.use_dsn or self.dsn_dict
+ if self.usePygresql:
+ dsn = '%s:template1:%s:%s' % (
+ self.host or '', self.user or '', self.password or '')
+ else:
+ dsn = 'dbname=template1'
+ if self.user:
+ dsn += ' user=%s' % self.user
+ if self.password:
+ dsn += ' passwd=%s' % self.passwd
+ 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')
+ # And we can't use template1 since we're connected to template1,
+ # so we use template0. @@: What's the difference between
+ # these two templates?
+ cur.execute('CREATE DATABASE %s TEMPLATE=template0' % self.db)
+ cur.close()
+ conn.close()
+
+
# Converter for psycopg Binary type.
def PsycoBinaryConverter(value, db):
assert db == 'postgres'
|