|
@ -5,7 +5,7 @@ import psycopg2 |
|
|
import psycopg2.extensions |
|
|
import psycopg2.extensions |
|
|
|
|
|
|
|
|
class Db(object): |
|
|
class Db(object): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self,name): |
|
|
def __init__(self,name): |
|
|
self._name = name |
|
|
self._name = name |
|
|
self.conn = psycopg2.connect("dbname='postgres' user='postgres' host= \ |
|
|
self.conn = psycopg2.connect("dbname='postgres' user='postgres' host= \ |
|
@ -27,10 +27,10 @@ class Db(object): |
|
|
self.cursor.execute(given_sql) |
|
|
self.cursor.execute(given_sql) |
|
|
try: |
|
|
try: |
|
|
out = self.cursor.fetchall() |
|
|
out = self.cursor.fetchall() |
|
|
except Exception,e: |
|
|
|
|
|
|
|
|
except Exception,e: |
|
|
out = "Exception " +str(e)+" ocured" |
|
|
out = "Exception " +str(e)+" ocured" |
|
|
return out |
|
|
return out |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_owner(self): |
|
|
def _get_owner(self): |
|
|
sql = "SELECT pg_get_userbyid(datdba) FROM pg_database WHERE datname = \ |
|
|
sql = "SELECT pg_get_userbyid(datdba) FROM pg_database WHERE datname = \ |
|
|
'"+self.sanitize(self._name)+"';" |
|
|
'"+self.sanitize(self._name)+"';" |
|
@ -41,7 +41,7 @@ class Db(object): |
|
|
sql = "ALTER DATABASE "+self._name+" OWNER TO "+self.sanitize(owner)+";" |
|
|
sql = "ALTER DATABASE "+self._name+" OWNER TO "+self.sanitize(owner)+";" |
|
|
own = self._runsql(sql) |
|
|
own = self._runsql(sql) |
|
|
return own |
|
|
return own |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db_owner = property(_get_owner, _set_owner) |
|
|
db_owner = property(_get_owner, _set_owner) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
@ -57,7 +57,7 @@ class Db(object): |
|
|
information['size'] = self._runsql("SELECT pg_size_pretty( \ |
|
|
information['size'] = self._runsql("SELECT pg_size_pretty( \ |
|
|
pg_database_size('"+self.sanitize( |
|
|
pg_database_size('"+self.sanitize( |
|
|
self._name)+"'));") |
|
|
self._name)+"'));") |
|
|
inf = self._runsql("""SELECT pg_encoding_to_char(encoding), |
|
|
|
|
|
|
|
|
inf = self._runsql("""SELECT pg_encoding_to_char(encoding), |
|
|
datcollate, datctype FROM pg_database WHERE datname='""" |
|
|
datcollate, datctype FROM pg_database WHERE datname='""" |
|
|
+self._name+"';") |
|
|
+self._name+"';") |
|
|
information['encoding'] = inf[0][0] |
|
|
information['encoding'] = inf[0][0] |
|
@ -71,7 +71,7 @@ class Db(object): |
|
|
'"+self.sanitize(self._name)+"';" |
|
|
'"+self.sanitize(self._name)+"';" |
|
|
cncs = self._runsql(sql) |
|
|
cncs = self._runsql(sql) |
|
|
return cncs |
|
|
return cncs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def user_exists(self, user): |
|
|
def user_exists(self, user): |
|
|
sql = "SELECT rolname FROM pg_authid WHERE rolname = \ |
|
|
sql = "SELECT rolname FROM pg_authid WHERE rolname = \ |
|
|
'"+self.sanitize(user)+"';" |
|
|
'"+self.sanitize(user)+"';" |
|
@ -147,7 +147,7 @@ class Db(object): |
|
|
sql = "DROP ROLE IF EXISTS "+self.sanitize(us)+";" |
|
|
sql = "DROP ROLE IF EXISTS "+self.sanitize(us)+";" |
|
|
usrd = self._runsql(sql) |
|
|
usrd = self._runsql(sql) |
|
|
return usrd |
|
|
return usrd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize(self,s): |
|
|
def sanitize(self,s): |
|
|
mset = '0123456789ABCDEFGHIJKLMNOPRSTUVXYZabcdefghijklmnoprstuvxyz-_.' |
|
|
mset = '0123456789ABCDEFGHIJKLMNOPRSTUVXYZabcdefghijklmnoprstuvxyz-_.' |
|
|
return ''.join([c for c in s if c in mset]) |
|
|
return ''.join([c for c in s if c in mset]) |
|
|