database_updater::DatabaseUpdater Class Reference

List of all members.

Public Member Functions

def __init__
def ExecuteApplyUpdate
def ExecuteDropTable
def ExecuteSQL
def IsAuthorising
def ParseCommand

Public Attributes

 convert_unsigned
 debug
 command
 args
 dbi

Detailed Description

A database updater that applies ASCII update files.

The constructor also invokes command execution.

Definition at line 417 of file database_updater.py.


Member Function Documentation

def database_updater::DatabaseUpdater::__init__ (   self  ) 

Definition at line 422 of file database_updater.py.

00422                       :
00423 
00424         #  Get command state.
00425         self.convert_unsigned = False    # --convert_unsigned option.
00426         self.debug            = 0        # Set to 1 if --debug
00427         self.command          = ''       # The command supplied
00428         self.args             = ''       # The command args (if any)
00429         self.ParseCommand()
00430 
00431         # Connect the database.
00432         self.dbi         = DatabaseInterface(self)
00433         if not self.dbi.IsOK():
00434             print "\nCannot connect to database, please check values of SK_TSQL_URL/USER/PSWD \nAborting."
00435             usage()
00436             return
00437         
00438         # Process command
00439         if   self.command == "apply_global_update": self.ExecuteApplyUpdate(True)
00440         elif self.command == "apply_local_update":  self.ExecuteApplyUpdate(False)
00441         elif self.command == "drop_table":          self.ExecuteDropTable()
00442         else:
00443             print "\nUnknown command %s.\n" % self.command
00444             usage()
00445             return
00446 
00447 #---------------------------------------------  DatabaseUpdater  --------------------------------------------------
00448 
    def ExecuteApplyUpdate(self,is_global):

def database_updater::DatabaseUpdater::ExecuteApplyUpdate (   self,
  is_global 
)
Apply updates either local or global sequence numbers.

Definition at line 449 of file database_updater.py.

00449                                           :
00450         """Apply updates either local or global sequence numbers."""
00451 
00452         update_file = self.args[0]
00453 
00454         if not os.path.isfile(update_file):
00455             print "\n Cannot '%s'; cannot find update file '%s'" % (self.command,update_file)
00456             return
00457 
00458         if is_global and not self.IsAuthorising():
00459             print "\n Cannot 'apply_global_update'; connected database is not authorising i.e. does not have a GLOBALSEQNO table"
00460             return
00461 
00462         # Read the update file and create a list of TableUpdate
00463         tu = None
00464         file_update = open(update_file)
00465         for line in file_update:
00466             # Ignore blank lines and comments
00467             if re.search(r'^\s*$',line) or re.search(r'^\s*#',line): continue
00468             # Process SQL
00469             mo = re.search(r'^\s*SQL\s+(.*)',line)
00470             if mo:
00471                 if not self.ExecuteSQL(mo.group(1),file_update): return
00472                 continue
00473             if not tu or re.search(r'^\s*BEGIN_TABLE',line):
00474                 # Complete process of last update (if any)
00475                 if tu:
00476                    if not tu.Apply():
00477                        print "\n Aborting '%s'; update file '%s' contains a bad update." % (self.command,update_file)
00478                        return
00479                    tu = None
00480                 tu = TableUpdate(self,line,is_global)
00481             else: tu.AddRow(line)
00482         file_update.close()
00483 
00484         # Apply last update
00485         if not tu:
00486             print "\n Cannot '%s'; update file '%s' is empty." % (self.command,update_file)
00487             return
00488         if not tu.Apply():
00489             print "\n Aborting '%s'; update file '%s' contains a bad update." % (self.command,update_file)
00490             return
00491         
00492 
00493 #---------------------------------------------  DatabaseUpdater  --------------------------------------------------
00494 
    def ExecuteDropTable(self):

def database_updater::DatabaseUpdater::ExecuteDropTable (   self  ) 
Removes table (main and VLD) and any entry in GLOBALSEQNO and LOCALSEQNO tables.

Definition at line 495 of file database_updater.py.

00495                               :
00496         """Removes table (main and VLD) and any entry in GLOBALSEQNO and LOCALSEQNO tables."""
00497 
00498         table_name = self.args[0]
00499         if not self.dbi.TableExists('table_name'):
00500             print 'Database does not have table %s' % table_name
00501         else:
00502 
00503             # Table exists, ask for confirmation 
00504             print '\nAre you sure that you want to remove tables %s and %sVLD Ans:[y or n]' % (table_name,table_name),
00505             ans = raw_input()
00506             if ans[0] != 'y' and ans[0] != 'Y':
00507                 print "\nRequest not confirmed, table not deleted."
00508                 return
00509             if self.IsAuthorising():
00510                 print '\nAre you sure REALLY sure - you are connected to an authorising (Master) database Ans:[y or n]',
00511                 ans = raw_input()
00512                 if ans[0] != 'y' and ans[0] != 'Y':
00513                     print "\nRequest not confirmed, table not deleted."
00514                     return
00515 
00516             # Drop table and it's VLD.
00517             for table in (table_name, table_name + 'VLD'):
00518                 if self.dbi.TableExists(table):
00519                     print "Dropping table " + table
00520                     if not self.dbi.Query('DROP TABLE %s' % table):
00521                         print "\nFailed to drop table %s." % table
00522                         return
00523 
00524         # Drop entries in LOCALSEQNO and GLOBALSEQNO
00525         for seqno_table in ('LOCALSEQNO','GLOBALSEQNO'):
00526             if self.dbi.TableExists(seqno_table):
00527                 print "Removing entry from %s" % seqno_table
00528                 self.dbi.Query("DELETE FROM %s WHERE TABLENAME = '%s'" % (seqno_table,table_name))
00529                             
00530 #---------------------------------------------  DatabaseUpdater  --------------------------------------------------
00531 
    def ExecuteSQL(self,sql_start,file_update):

def database_updater::DatabaseUpdater::ExecuteSQL (   self,
  sql_start,
  file_update 
)
Assemble and execute SQL command.  Return True if no errors when executed.

Definition at line 532 of file database_updater.py.

00532                                               :
00533         """Assemble and execute SQL command.  Return True if no errors when executed."""
00534         sql = sql_start.strip()
00535         if not re.search(r';\s*$',sql):
00536             for line in file_update:
00537                 sql += line.strip()
00538                 if re.search(r';\s*$',sql): break
00539         return self.dbi.Query(sql);
00540                 
00541         
00542 #---------------------------------------------  DatabaseUpdater  --------------------------------------------------
00543 
    def IsAuthorising(self):

def database_updater::DatabaseUpdater::IsAuthorising (   self  ) 
Return True if database is authorising i.e. has a GLOBALSEQNO table.

Definition at line 544 of file database_updater.py.

00544                            :
00545         """Return True if database is authorising i.e. has a GLOBALSEQNO table."""
00546         return  self.dbi.TableExists('GLOBALSEQNO')
00547 
00548 #---------------------------------------------  DatabaseUpdater  --------------------------------------------------
00549 
    def ParseCommand(self):

def database_updater::DatabaseUpdater::ParseCommand (   self  ) 

Definition at line 550 of file database_updater.py.

00550                           :
00551         try:  opts, args = getopt.getopt(sys.argv[1:], "cdh", ["convert_unsigned","debug","help"])
00552         except getopt.GetoptError, err:
00553             print str(err) # will print something like "option -a not recognized"
00554             usage()
00555             sys.exit(2)
00556 
00557         # Process options
00558         for o, a in opts:
00559             #  Deal with --convert_unsigned
00560             if o in ("-c","--convert_unsigned"): self.convert_unsigned = True
00561             #  Deal with debug
00562             elif o in ("-d", "--debug"): self.debug = 1
00563             #  Deal with help
00564             elif o in ("-h", "--help"): 
00565                 usage()
00566                 sys.exit()
00567             else:
00568                 assert False, "unhandled option: " + o
00569 
00570         # Process command and args
00571         if not args:
00572             print "\nNo command supplied.\n"
00573             usage()
00574             sys.exit()
00575         self.command = args.pop(0)
00576         if not args:
00577             print "\nNo command args supplied.\n"
00578             usage()
00579             sys.exit()
00580         self.args    = args
00581 
00582 
00583 #  Driver code.
if __name__ == "__main__":


Member Data Documentation

Definition at line 428 of file database_updater.py.

Definition at line 427 of file database_updater.py.

Definition at line 425 of file database_updater.py.

Definition at line 432 of file database_updater.py.

Definition at line 426 of file database_updater.py.


The documentation for this class was generated from the following file:

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1