|
@ -39,16 +39,16 @@ class Db(object): |
|
|
own = self._runsql(sql) |
|
|
own = self._runsql(sql) |
|
|
return own |
|
|
return own |
|
|
|
|
|
|
|
|
owner = property(_get_owner, _set_owner) |
|
|
|
|
|
|
|
|
db_owner = property(_get_owner, _set_owner) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def OID(self): |
|
|
|
|
|
|
|
|
def db_OID(self): |
|
|
sql = "SELECT oid FROM pg_database WHERE datname = '"+self.sanitize(self._name)+"';" |
|
|
sql = "SELECT oid FROM pg_database WHERE datname = '"+self.sanitize(self._name)+"';" |
|
|
oid = self._runsql(sql) |
|
|
oid = self._runsql(sql) |
|
|
return oid |
|
|
return oid |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def info(self): |
|
|
|
|
|
|
|
|
def db_info(self): |
|
|
information = {'size':'', 'encoding':'', 'collation':'','ctype':''} |
|
|
information = {'size':'', 'encoding':'', 'collation':'','ctype':''} |
|
|
information['size'] = self._runsql("SELECT pg_size_pretty(pg_database_size('"+self.sanitize(self._name)+"'));") |
|
|
information['size'] = self._runsql("SELECT pg_size_pretty(pg_database_size('"+self.sanitize(self._name)+"'));") |
|
|
information['encoding'], information['collation'], \ |
|
|
information['encoding'], information['collation'], \ |
|
@ -56,7 +56,7 @@ class Db(object): |
|
|
return information |
|
|
return information |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def connections(self): |
|
|
|
|
|
|
|
|
def db_connections(self): |
|
|
sql = "SELECT numbackends from pg_stat_database WHERE datname = '"+self.sanitize(self._name)+"';" |
|
|
sql = "SELECT numbackends from pg_stat_database WHERE datname = '"+self.sanitize(self._name)+"';" |
|
|
cncs = self._runsql(sql) |
|
|
cncs = self._runsql(sql) |
|
|
return cncs |
|
|
return cncs |
|
@ -75,21 +75,21 @@ class Db(object): |
|
|
return False |
|
|
return False |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
def delete(self): |
|
|
|
|
|
|
|
|
def db_delete(self): |
|
|
if self.db_exists(self._name) == True: |
|
|
if self.db_exists(self._name) == True: |
|
|
sql = "DROP DATABASE "+self.sanitize(self._name)+";" |
|
|
sql = "DROP DATABASE "+self.sanitize(self._name)+";" |
|
|
drop = self._runsql(sql) |
|
|
drop = self._runsql(sql) |
|
|
return drop |
|
|
return drop |
|
|
return "Failed" |
|
|
return "Failed" |
|
|
|
|
|
|
|
|
def create(self, own, coll, ctyp, enc=u'UTF8'): |
|
|
|
|
|
|
|
|
def db_create(self, own, coll, ctyp, enc=u'UTF8'): |
|
|
if self.db_exists(self._name) == False: |
|
|
if self.db_exists(self._name) == False: |
|
|
sql = "CREATE DATABASE "+self.sanitize(self._name)+" WITH OWNER = "+self.sanitize(own)+" ENCODING = '"+self.sanitize(enc)+"' LC_COLLATE = '"+self.sanitize(coll)+"' LC_CTYPE = '"+self.sanitize(ctyp)+"';" |
|
|
sql = "CREATE DATABASE "+self.sanitize(self._name)+" WITH OWNER = "+self.sanitize(own)+" ENCODING = '"+self.sanitize(enc)+"' LC_COLLATE = '"+self.sanitize(coll)+"' LC_CTYPE = '"+self.sanitize(ctyp)+"';" |
|
|
create = self._runsql(sql) |
|
|
create = self._runsql(sql) |
|
|
return create |
|
|
return create |
|
|
return "Failed" |
|
|
return "Failed" |
|
|
|
|
|
|
|
|
def dump(self, path, method): |
|
|
|
|
|
|
|
|
def db_dump(self, path, method): |
|
|
dump = Popen(['/usr/bin/pg_dump', '-U','postgres','-F'+ method, self._name], stdout=PIPE) |
|
|
dump = Popen(['/usr/bin/pg_dump', '-U','postgres','-F'+ method, self._name], stdout=PIPE) |
|
|
fl = open(path,"wb") |
|
|
fl = open(path,"wb") |
|
|
gz = Popen(['gzip'], stdin = dump.stdout, stdout = fl) |
|
|
gz = Popen(['gzip'], stdin = dump.stdout, stdout = fl) |
|
@ -97,7 +97,7 @@ class Db(object): |
|
|
return "Finished dumping "+self._name |
|
|
return "Finished dumping "+self._name |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rename(self,old, new): |
|
|
|
|
|
|
|
|
def db_rename(self,old, new): |
|
|
if self.db_exists(new) == True or self.db_exists(old) == False: |
|
|
if self.db_exists(new) == True or self.db_exists(old) == False: |
|
|
return "Cannot" |
|
|
return "Cannot" |
|
|
sql = "ALTER DATABASE "+self.sanitize(old)+" RENAME TO "+self.sanitize(new)+";" |
|
|
sql = "ALTER DATABASE "+self.sanitize(old)+" RENAME TO "+self.sanitize(new)+";" |
|
@ -107,12 +107,12 @@ class Db(object): |
|
|
def copy(): |
|
|
def copy(): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
def dblist(self): |
|
|
|
|
|
|
|
|
def db_list(self): |
|
|
sql = "SELECT datname FROM pg_database WHERE datname NOT IN ('template0', 'template1', 'postgres');" |
|
|
sql = "SELECT datname FROM pg_database WHERE datname NOT IN ('template0', 'template1', 'postgres');" |
|
|
dbl = self._runsql(sql) |
|
|
dbl = self._runsql(sql) |
|
|
return dbl |
|
|
return dbl |
|
|
|
|
|
|
|
|
def usrlist(self): |
|
|
|
|
|
|
|
|
def usr_list(self): |
|
|
sql = "SELECT rolname FROM pg_authid WHERE rolcanlogin=true;" |
|
|
sql = "SELECT rolname FROM pg_authid WHERE rolcanlogin=true;" |
|
|
usrl = self._runsql(sql) |
|
|
usrl = self._runsql(sql) |
|
|
return usrl |
|
|
return usrl |
|
@ -123,30 +123,10 @@ class Db(object): |
|
|
|
|
|
|
|
|
def _test(): |
|
|
def _test(): |
|
|
test = Db(u'postgres') |
|
|
test = Db(u'postgres') |
|
|
print test.dblist() |
|
|
|
|
|
print test.usrlist() |
|
|
|
|
|
print test.info['encoding'], test.info['collation'], test.info['ctype'] |
|
|
|
|
|
#print test.owner |
|
|
|
|
|
|
|
|
print test.db_list() |
|
|
|
|
|
print test.usr_list() |
|
|
|
|
|
print test.db_info['encoding'], test.info['collation'], test.info['ctype'] |
|
|
print test.sanitize("aaaa-fgdg?sd/!_fb*gs'h;s'hdghj.dn ") |
|
|
print test.sanitize("aaaa-fgdg?sd/!_fb*gs'h;s'hdghj.dn ") |
|
|
#print test.connections |
|
|
|
|
|
#print "User aaa is ",test.user_exists("aaa") |
|
|
|
|
|
#print "User postgres is ",test.user_exists("postgres") |
|
|
|
|
|
#print "database xxxaaa is ", test.db_exists("xxxaaa") |
|
|
|
|
|
#print "database postgres is ", test.db_exists("postgres") |
|
|
|
|
|
#print test.dblist() |
|
|
|
|
|
#test2 = Db(u'aaa') |
|
|
|
|
|
#print test2.create(u'postgres', u'en_US.UTF-8', u'en_US.UTF-8') |
|
|
|
|
|
#print test2.dblist() |
|
|
|
|
|
#test2.rename(u'aaa',u'bbb') |
|
|
|
|
|
#print test2.dblist() |
|
|
|
|
|
#print test2.usrlist() |
|
|
|
|
|
#print test2.owner |
|
|
|
|
|
#test2.owner = u'karasz' |
|
|
|
|
|
#print test2.owner |
|
|
|
|
|
#test = Db(u'aaa') |
|
|
|
|
|
#test.create(u'postgres', u'en_US.UTF-8', u'en_US.UTF-8') |
|
|
|
|
|
#print test.dump('/tmp/aaa.gz',u'p') |
|
|
|
|
|
#test.delete() |
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
_test() |
|
|
_test() |
|
|
|
|
|
|