[rhmessaging-commits] rhmessaging commits: r1696 - mgmt/mint/python/mint.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Thu Feb 14 15:11:57 EST 2008


Author: justi9
Date: 2008-02-14 15:11:56 -0500 (Thu, 14 Feb 2008)
New Revision: 1696

Modified:
   mgmt/mint/python/mint/__init__.py
Log:
In support of a command for creating and dropping the schema, adds a
MintDatabase class with methods for common database operations.



Modified: mgmt/mint/python/mint/__init__.py
===================================================================
--- mgmt/mint/python/mint/__init__.py	2008-02-14 20:10:14 UTC (rev 1695)
+++ mgmt/mint/python/mint/__init__.py	2008-02-14 20:11:56 UTC (rev 1696)
@@ -262,3 +262,73 @@
     methodId = self.currentMethodId
     self.outstandingMethodCalls[methodId] = callback
     return methodId
+
+class MintDatabase(object):
+  def __init__(self, uri):
+    self.uri = uri
+
+  def getConnection(self):
+    return connectionForURI(self.uri).getConnection()
+
+  def checkConnection(self):
+    conn = self.getConnection()
+
+    try:
+      cursor = conn.cursor()
+      cursor.execute("select now()")
+    finally:
+      conn.close()
+
+  def checkSchema(self):
+    pass
+
+  def dropSchema(self):
+    conn = self.getConnection()
+    try:
+        cursor = conn.cursor()
+        cursor.execute("drop schema public cascade")
+
+        conn.commit()
+    finally:
+        conn.close()
+
+  def createSchema(self, file_paths):
+    conn = self.getConnection()
+
+    scripts = list()
+
+    for path in file_paths:
+      file = open(path, "r")
+      try:
+        scripts.append((path, file.read()))
+      finally:
+        file.close()
+
+    try:
+      cursor = conn.cursor()
+
+      cursor.execute("create schema public")
+
+      for path, text in scripts:
+        stmts = text.split(";")
+        count = 0
+
+        for stmt in stmts:
+          stmt = stmt.strip()
+
+          if stmt:
+            try:
+              cursor.execute(stmt)
+            except Exception, e:
+              print "Failed executing statement:"
+              print stmt
+
+              raise e
+
+            count += 1
+
+        print "Executed %i statements from file '%s'" % (count, path)
+
+      conn.commit()
+    finally:
+        conn.close()




More information about the rhmessaging-commits mailing list