Browse Source

ecn-robots: enhanced pgdb.py to have proper names, considering object types.

master
Nagy Karoly Gabriel 14 years ago
parent
commit
23779192be
1 changed files with 13 additions and 33 deletions
  1. +13
    -33
      pgdb.py

+ 13
- 33
pgdb.py

@ -39,16 +39,16 @@ class Db(object):
own = self._runsql(sql)
return own
owner = property(_get_owner, _set_owner)
db_owner = property(_get_owner, _set_owner)
@property
def OID(self):
def db_OID(self):
sql = "SELECT oid FROM pg_database WHERE datname = '"+self.sanitize(self._name)+"';"
oid = self._runsql(sql)
return oid
@property
def info(self):
def db_info(self):
information = {'size':'', 'encoding':'', 'collation':'','ctype':''}
information['size'] = self._runsql("SELECT pg_size_pretty(pg_database_size('"+self.sanitize(self._name)+"'));")
information['encoding'], information['collation'], \
@ -56,7 +56,7 @@ class Db(object):
return information
@property
def connections(self):
def db_connections(self):
sql = "SELECT numbackends from pg_stat_database WHERE datname = '"+self.sanitize(self._name)+"';"
cncs = self._runsql(sql)
return cncs
@ -75,21 +75,21 @@ class Db(object):
return False
return True
def delete(self):
def db_delete(self):
if self.db_exists(self._name) == True:
sql = "DROP DATABASE "+self.sanitize(self._name)+";"
drop = self._runsql(sql)
return drop
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:
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)
return create
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)
fl = open(path,"wb")
gz = Popen(['gzip'], stdin = dump.stdout, stdout = fl)
@ -97,7 +97,7 @@ class Db(object):
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:
return "Cannot"
sql = "ALTER DATABASE "+self.sanitize(old)+" RENAME TO "+self.sanitize(new)+";"
@ -107,12 +107,12 @@ class Db(object):
def copy():
pass
def dblist(self):
def db_list(self):
sql = "SELECT datname FROM pg_database WHERE datname NOT IN ('template0', 'template1', 'postgres');"
dbl = self._runsql(sql)
return dbl
def usrlist(self):
def usr_list(self):
sql = "SELECT rolname FROM pg_authid WHERE rolcanlogin=true;"
usrl = self._runsql(sql)
return usrl
@ -123,30 +123,10 @@ class Db(object):
def _test():
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.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__':
_test()

Loading…
Cancel
Save