--- /dev/null
+## @file\r
+# This file is used to create/update/query/erase a common table\r
+#\r
+# Copyright (c) 2008, Intel Corporation\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+import Common.EdkLogger as EdkLogger\r
+\r
+## TableFile\r
+#\r
+# This class defined a common table\r
+# \r
+# @param object: Inherited from object class\r
+#\r
+# @param Cursor: Cursor of the database\r
+# @param TableName: Name of the table\r
+#\r
+class Table(object):\r
+ def __init__(self, Cursor):\r
+ self.Cur = Cursor\r
+ self.Table = ''\r
+ self.ID = 0\r
+ \r
+ ## Create table\r
+ #\r
+ # Create a table\r
+ #\r
+ def Create(self, SqlCommand):\r
+ self.Cur.execute(SqlCommand)\r
+ self.ID = 0\r
+ EdkLogger.verbose(SqlCommand + " ... DONE!")\r
+\r
+ ## Insert table\r
+ #\r
+ # Insert a record into a table\r
+ #\r
+ def Insert(self, SqlCommand):\r
+ self.Exec(SqlCommand)\r
+ \r
+ ## Query table\r
+ #\r
+ # Query all records of the table\r
+ # \r
+ def Query(self):\r
+ EdkLogger.verbose("\nQuery tabel %s started ..." % self.Table)\r
+ SqlCommand = """select * from %s""" % self.Table\r
+ self.Cur.execute(SqlCommand)\r
+ for Rs in self.Cur:\r
+ EdkLogger.verbose(str(Rs))\r
+ \r
+ TotalCount = self.GetCount()\r
+ EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) )\r
+ EdkLogger.verbose("Query tabel %s DONE!" % self.Table)\r
+\r
+ ## Drop a table\r
+ #\r
+ # Drop the table\r
+ #\r
+ def Drop(self):\r
+ SqlCommand = """drop table IF EXISTS %s""" % self.Table\r
+ self.Cur.execute(SqlCommand)\r
+ EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table)\r
+ \r
+ ## Get count\r
+ #\r
+ # Get a count of all records of the table\r
+ #\r
+ # @retval Count: Total count of all records\r
+ #\r
+ def GetCount(self):\r
+ SqlCommand = """select count(ID) from %s""" % self.Table\r
+ self.Cur.execute(SqlCommand)\r
+ for Item in self.Cur:\r
+ return Item[0]\r
+ \r
+ ## Generate ID\r
+ #\r
+ # Generate an ID if input ID is -1\r
+ #\r
+ # @param ID: Input ID \r
+ #\r
+ # @retval ID: New generated ID\r
+ #\r
+ def GenerateID(self, ID):\r
+ if ID == -1:\r
+ self.ID = self.ID + 1\r
+\r
+ return self.ID\r
+ \r
+ ## Init the ID of the table\r
+ #\r
+ # Init the ID of the table\r
+ #\r
+ def InitID(self):\r
+ self.ID = self.GetCount()\r
+ \r
+ ## Exec\r
+ #\r
+ # Exec Sql Command, return result\r
+ #\r
+ # @param SqlCommand: The SqlCommand to be executed\r
+ #\r
+ # @retval RecordSet: The result after executed\r
+ #\r
+ def Exec(self, SqlCommand):\r
+ EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand)\r
+ self.Cur.execute(SqlCommand)\r
+ RecordSet = self.Cur.fetchall()\r
+ EdkLogger.debug(4, "RecordSet: %s" % RecordSet)\r
+ return RecordSet\r