/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes | Static Public Attributes | Properties | Private Member Functions | Private Attributes | Static Private Attributes
DybPython::dbcas::DBCon Class Reference
Collaboration diagram for DybPython::dbcas::DBCon:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def log
def errlog
def drop
def create
def spawn
def tables
def process
def populate
def fixture
def mysqldump
def dump
def fixture_

Public Attributes

 url_
 origname
 user
 pswd
 fix
 verbosity

Static Public Attributes

string prefix = 'tmp_'

Properties

 url = property( lambda self:os.path.expanduser(os.path.expandvars(self.url_.GetUrl())), lambda self,n:self.url_.SetUrl(n) )
 name = property( lambda self:self.url_.GetFile(), lambda self,n:self.url_.SetFile(n) )
 host = property( lambda self:self.url_.GetHost(), lambda self,n:self.url_.SetHost(n) )
 ascii = property( lambda self:self.url_.GetAnchor(), lambda self,n:self.url_.SetAnchor(n) )
 opts = property( lambda self:self.url_.GetOptions(), lambda self,n:self.url_.SetOptions(n) )
 creds = property( lambda self:dict( name=self.name, host=self.host, user=self.user, pswd=self.pswd ))
 server = property( _get_server, doc=_get_server.__doc__ )
 isconnected = property(lambda self:self.server.IsConnected())

Private Member Functions

def _get_server
def _spawn

Private Attributes

 _server
 using the setter property
 _attempt

Static Private Attributes

string _mysqldump = "mysqldump --no-defaults --host=%(host)s --user=%(user)s --password=%(pswd)s %(opt)s %(name)s "

Detailed Description

Dictionary holding parameters to connect to a DB and 
provides functionality to drop/create databases
and run updates/queries against them.

Definition at line 23 of file dbcas.py.


Constructor & Destructor Documentation

def DybPython::dbcas::DBCon::__init__ (   self,
  url,
  user,
  pswd,
  kwa 
)

Definition at line 33 of file dbcas.py.

00034                                                  :
00035         self.url_  = TUrl(url)
00036         assert self.url_.IsValid()
00037         self.origname = self.name      
00038         self.user = user
00039         self.pswd = pswd
00040         self.fix = kwa.get('fix', None)
00041         self.verbosity = kwa.get('verbosity', 0)
00042         name = kwa.get('name', None)
00043         if name:
00044             self.name = name   ## using the setter property
00045         self._server = None
00046         self._attempt = 0


Member Function Documentation

def DybPython::dbcas::DBCon::__repr__ (   self)

Definition at line 54 of file dbcas.py.

00055                       :
00056         return "DBCon(\"%s\",\"%s\",\"%s\", fix=\"%s\" )" % ( self.url , self.user, "***" , self.fix  ) 

def DybPython::dbcas::DBCon::log (   self)

Definition at line 57 of file dbcas.py.

00057                  : 
00058         return "\n".join( [ dict.__repr__(self) ] )
00059 
def DybPython::dbcas::DBCon::errlog (   self,
  dbn 
)

Definition at line 60 of file dbcas.py.

00061                           :
00062         if self.server.GetErrorCode() == 0:return
00063         if self.get(dbn,None) == None:
00064             self[dbn] = []
00065         self[dbn].append( self.server.GetErrorMsg() )
         
def DybPython::dbcas::DBCon::_get_server (   self) [private]
If the connection attempt fails, try again without specifying the DB name, see :root:`TMySQLServer`

.. todo::
    
     Find way to avoid/capture the error after failure to connect

Definition at line 66 of file dbcas.py.

00067                          :
00068         """
00069         If the connection attempt fails, try again without specifying the DB name, see :root:`TMySQLServer`
00070 
00071         .. todo::
00072             
00073              Find way to avoid/capture the error after failure to connect
00074 
00075         """ 
00076         if not(self._server):
00077             self._server = TSQLServer.Connect( self.url , self.user , self.pswd )
00078             self._attempt += 1
00079             if self._server == None:
00080                 if self.name != "" and self._attempt < 3:
00081                     print "attempt %s failed to connect to %s, try again without specifying the DB " % ( self._attempt, self.url ) 
00082                     self.name = ""
00083                     self._get_server()
00084                 else:
00085                     print "attempt %s failed to connect to %s " % ( self._attempt , self.url ) 
00086             else:
00087                 print "attempt %s succeeded to connect to %s " % ( self._attempt, self.url ) 
        return self._server 
def DybPython::dbcas::DBCon::drop (   self,
  dbn 
)

Definition at line 90 of file dbcas.py.

00091                         :
00092         if dbn == self.origname:
00093             raise DBExc("cannot drop DB of the originating name \"%s\" for safety" % self.origname )
00094         if self.server.DropDataBase( dbn ) == 0:     
00095             if self.verbosity>1:
00096                 print "succeeded to drop db \"%s\" " % dbn
00097         else:
00098             print "failed to drop db \"%s\" " % dbn
00099             self.errlog(dbn)

def DybPython::dbcas::DBCon::create (   self,
  dbn,
  autoclobber = True 
)

Definition at line 100 of file dbcas.py.

00101                                              :
00102         if dbn == self.origname:
00103             raise DBExc("cannot create DB of the originating name \"%s\" for safety" % self.origname )
00104         self.server.EnableErrorOutput(False)
00105         if self.server.CreateDataBase( dbn ) == 0:     
00106             if self.verbosity>1:
00107                 print "succeeded to create db \"%s\" " % dbn
00108         else:
00109             err, msg = self.server.GetErrorCode(), self.server.GetErrorMsg()
00110             if err == 1007 and autoclobber == True and len(self.get(dbn,())) < 5:
00111                 if self.verbosity>2:
00112                     print "failed to create db \"%s\" as it exists already ... " % dbn
00113                 self.errlog(dbn)
00114                 self.drop(dbn)   
00115                 self.create(dbn) 
00116             else:
00117                 print "failed to create db \"%s\" due to %s \"%s\" " % (dbn, err, msg )
00118                
00119         self.server.EnableErrorOutput(True)

def DybPython::dbcas::DBCon::_spawn (   self,
  kwa 
) [private]
     Spawn a DBCon instance ... applying fixture if defined 

Definition at line 120 of file dbcas.py.

00121                             :
00122         """
00123              Spawn a DBCon instance ... applying fixture if defined 
00124         """ 
00125         con = DBCon( self.url , self.user, self.pswd , **kwa )  
00126         con.fixture()
00127         return con

def DybPython::dbcas::DBCon::spawn (   self,
  fixpass = False 
)
Create new DB with prefixed name and spawn a DBCon to talk to it with 

When *fixpass* is True the DB is neither created or dropped, but it is assumed
to exist. This is used when doing DBI double dipping, used for example in 
:dybgaudi:`Database/DBWriter/tests` 

Definition at line 128 of file dbcas.py.

00129                                      :
00130         """
00131         Create new DB with prefixed name and spawn a DBCon to talk to it with 
00132 
00133         When *fixpass* is True the DB is neither created or dropped, but it is assumed
00134         to exist. This is used when doing DBI double dipping, used for example in 
00135         :dybgaudi:`Database/DBWriter/tests` 
00136 
00137         """
00138         name = "%s%s" % (DBCon.prefix, self.origname) 
00139         if not fixpass:
00140             self.create( name )
00141         return self._spawn( name=name, fix=self.fix )

def DybPython::dbcas::DBCon::tables (   self)

Definition at line 142 of file dbcas.py.

00143                     :
00144         return self.server.GetTablesList()

def DybPython::dbcas::DBCon::process (   self,
  sql 
)
Attempts to create prepared statement from sql then processes it  

Definition at line 147 of file dbcas.py.

00148                           :
00149         """
00150         Attempts to create prepared statement from sql then processes it  
00151         """
00152         if sql.strip() == "":
00153             if self.verbosity>2:
00154                 print "skipping blank line"
00155             return True
00156 
00157         if sql.endswith("insert into DbiDemoData3 values( data supplied by DbiDemodata3 - see  DbiValidate::PrepareDatabases() )"):
00158             if self.verbosity>0:
00159                 print "skipping DbiValidate atrocity "
00160             return True
00161 
00162         #print "process [%s] " % sql
00163         if not(self.isconnected):
00164             print "not connected, cannot process \"%s\" " % sql
00165             return
00166         st = self.server.Statement( sql ) 
00167         if not(st):
00168             print "failed to create statement ... probably an error in the sql " 
00169             return
00170         ok = st.Process() 
00171         if False == ok:
00172             print "error during processing of statement for sql \"%s\"  " % sql 
00173         return ok

def DybPython::dbcas::DBCon::populate (   self)

Definition at line 174 of file dbcas.py.

00175                       :
00176         self.process( "create table dummy ( val int )") 

def DybPython::dbcas::DBCon::fixture (   self)

Definition at line 177 of file dbcas.py.

00178                      :
00179         if self.fix == None:
00180             return
00181         self.fixture_( self.fix )         
    
def DybPython::dbcas::DBCon::mysqldump (   self,
  opt 
)

Definition at line 182 of file dbcas.py.

00182                              : 
00183         return self._mysqldump % dict( self.creds, opt=opt )  
00184 
def DybPython::dbcas::DBCon::dump (   self,
  path = None,
  opt = "" 
)

Definition at line 185 of file dbcas.py.

00186                                       :
00187         if not self.ascii:
00188             cmd = path and "%s > %s " % ( self.mysqldump(opt) , path ) or self.mysqldump(opt)
00189             #print "invoking dump with cmd %s " % cmd.replace(self.pswd, "***")
00190             return os.popen( cmd )

def DybPython::dbcas::DBCon::fixture_ (   self,
  path_ 
)

Definition at line 191 of file dbcas.py.

00192                               :
00193         if not(self.name.startswith(DBCon.prefix)):
00194             raise DBExc("as safety measure cannot apply fixture to DB %s, name must start with prefix \"%s\" " % ( self.name, DBCon.prefix) )
00195         path = os.path.expandvars(os.path.expanduser(path_))
00196         print "%s fixture reading path %s " % (self, path_ ) 
00197         for sql in open(path).read().split(";"):
00198             if not(self.process(sql)):
00199                 print "error during processing of \"%s\" " % sql


Member Data Documentation

string DybPython::dbcas::DBCon::prefix = 'tmp_' [static]

Definition at line 29 of file dbcas.py.

string DybPython::dbcas::DBCon::_mysqldump = "mysqldump --no-defaults --host=%(host)s --user=%(user)s --password=%(pswd)s %(opt)s %(name)s " [static, private]

Definition at line 30 of file dbcas.py.

Definition at line 33 of file dbcas.py.

Definition at line 33 of file dbcas.py.

Definition at line 33 of file dbcas.py.

Definition at line 33 of file dbcas.py.

Definition at line 33 of file dbcas.py.

Definition at line 33 of file dbcas.py.

using the setter property

Definition at line 34 of file dbcas.py.

Definition at line 34 of file dbcas.py.


Property Documentation

DybPython::dbcas::DBCon::url = property( lambda self:os.path.expanduser(os.path.expandvars(self.url_.GetUrl())), lambda self,n:self.url_.SetUrl(n) ) [static]

Definition at line 47 of file dbcas.py.

DybPython::dbcas::DBCon::name = property( lambda self:self.url_.GetFile(), lambda self,n:self.url_.SetFile(n) ) [static]

Definition at line 48 of file dbcas.py.

DybPython::dbcas::DBCon::host = property( lambda self:self.url_.GetHost(), lambda self,n:self.url_.SetHost(n) ) [static]

Definition at line 49 of file dbcas.py.

DybPython::dbcas::DBCon::ascii = property( lambda self:self.url_.GetAnchor(), lambda self,n:self.url_.SetAnchor(n) ) [static]

Definition at line 50 of file dbcas.py.

DybPython::dbcas::DBCon::opts = property( lambda self:self.url_.GetOptions(), lambda self,n:self.url_.SetOptions(n) ) [static]

Definition at line 51 of file dbcas.py.

DybPython::dbcas::DBCon::creds = property( lambda self:dict( name=self.name, host=self.host, user=self.user, pswd=self.pswd )) [static]

Definition at line 52 of file dbcas.py.

DybPython::dbcas::DBCon::server = property( _get_server, doc=_get_server.__doc__ ) [static]

Definition at line 88 of file dbcas.py.

DybPython::dbcas::DBCon::isconnected = property(lambda self:self.server.IsConnected()) [static]

Definition at line 145 of file dbcas.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:55:40 for DybPython by doxygen 1.7.4