1 ## @file 2 # This file is used to create/update/query/erase a common table 3 # 4 # Copyright (c) 2008, Intel Corporation. All rights reserved.<BR> 5 # This program and the accompanying materials 6 # are licensed and made available under the terms and conditions of the BSD License 7 # which accompanies this distribution. The full text of the license may be found at 8 # http://opensource.org/licenses/bsd-license.php 9 # 10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 12 # 13 14 ## 15 # Import Modules 16 # 17 import Common.EdkLogger as EdkLogger 18 19 ## TableFile 20 # 21 # This class defined a common table 22 # 23 # @param object: Inherited from object class 24 # 25 # @param Cursor: Cursor of the database 26 # @param TableName: Name of the table 27 # 28 class Table(object): 29 def __init__(self, Cursor): 30 self.Cur = Cursor 31 self.Table = '' 32 self.ID = 0 33 34 ## Create table 35 # 36 # Create a table 37 # 38 def Create(self, SqlCommand): 39 self.Cur.execute(SqlCommand) 40 self.ID = 0 41 EdkLogger.verbose(SqlCommand + " ... DONE!") 42 43 ## Insert table 44 # 45 # Insert a record into a table 46 # 47 def Insert(self, SqlCommand): 48 self.Exec(SqlCommand) 49 50 ## Query table 51 # 52 # Query all records of the table 53 # 54 def Query(self): 55 EdkLogger.verbose("\nQuery tabel %s started ..." % self.Table) 56 SqlCommand = """select * from %s""" % self.Table 57 self.Cur.execute(SqlCommand) 58 for Rs in self.Cur: 59 EdkLogger.verbose(str(Rs)) 60 61 TotalCount = self.GetCount() 62 EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) ) 63 EdkLogger.verbose("Query tabel %s DONE!" % self.Table) 64 65 ## Drop a table 66 # 67 # Drop the table 68 # 69 def Drop(self): 70 SqlCommand = """drop table IF EXISTS %s""" % self.Table 71 self.Cur.execute(SqlCommand) 72 EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table) 73 74 ## Get count 75 # 76 # Get a count of all records of the table 77 # 78 # @retval Count: Total count of all records 79 # 80 def GetCount(self): 81 SqlCommand = """select count(ID) from %s""" % self.Table 82 self.Cur.execute(SqlCommand) 83 for Item in self.Cur: 84 return Item[0] 85 86 ## Generate ID 87 # 88 # Generate an ID if input ID is -1 89 # 90 # @param ID: Input ID 91 # 92 # @retval ID: New generated ID 93 # 94 def GenerateID(self, ID): 95 if ID == -1: 96 self.ID = self.ID + 1 97 98 return self.ID 99 100 ## Init the ID of the table 101 # 102 # Init the ID of the table 103 # 104 def InitID(self): 105 self.ID = self.GetCount() 106 107 ## Exec 108 # 109 # Exec Sql Command, return result 110 # 111 # @param SqlCommand: The SqlCommand to be executed 112 # 113 # @retval RecordSet: The result after executed 114 # 115 def Exec(self, SqlCommand): 116 EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand) 117 self.Cur.execute(SqlCommand) 118 RecordSet = self.Cur.fetchall() 119 EdkLogger.debug(4, "RecordSet: %s" % RecordSet) 120 return RecordSet 121