130fdf1140b8d1ce93f3821d986fa165552023440lgao## @file
230fdf1140b8d1ce93f3821d986fa165552023440lgao# This file is used to create/update/query/erase a common table
330fdf1140b8d1ce93f3821d986fa165552023440lgao#
440d841f6a8f84e75409178e19e69b95e01bada0flgao# Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
540d841f6a8f84e75409178e19e69b95e01bada0flgao# This program and the accompanying materials
630fdf1140b8d1ce93f3821d986fa165552023440lgao# are licensed and made available under the terms and conditions of the BSD License
730fdf1140b8d1ce93f3821d986fa165552023440lgao# which accompanies this distribution.  The full text of the license may be found at
830fdf1140b8d1ce93f3821d986fa165552023440lgao# http://opensource.org/licenses/bsd-license.php
930fdf1140b8d1ce93f3821d986fa165552023440lgao#
1030fdf1140b8d1ce93f3821d986fa165552023440lgao# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
1130fdf1140b8d1ce93f3821d986fa165552023440lgao# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
1230fdf1140b8d1ce93f3821d986fa165552023440lgao#
1330fdf1140b8d1ce93f3821d986fa165552023440lgao
1430fdf1140b8d1ce93f3821d986fa165552023440lgao##
1530fdf1140b8d1ce93f3821d986fa165552023440lgao# Import Modules
1630fdf1140b8d1ce93f3821d986fa165552023440lgao#
1730fdf1140b8d1ce93f3821d986fa165552023440lgaoimport Common.EdkLogger as EdkLogger
1830fdf1140b8d1ce93f3821d986fa165552023440lgao
1930fdf1140b8d1ce93f3821d986fa165552023440lgao## TableFile
2030fdf1140b8d1ce93f3821d986fa165552023440lgao#
2130fdf1140b8d1ce93f3821d986fa165552023440lgao# This class defined a common table
2230fdf1140b8d1ce93f3821d986fa165552023440lgao#
2330fdf1140b8d1ce93f3821d986fa165552023440lgao# @param object:     Inherited from object class
2430fdf1140b8d1ce93f3821d986fa165552023440lgao#
2530fdf1140b8d1ce93f3821d986fa165552023440lgao# @param Cursor:     Cursor of the database
2630fdf1140b8d1ce93f3821d986fa165552023440lgao# @param TableName:  Name of the table
2730fdf1140b8d1ce93f3821d986fa165552023440lgao#
2830fdf1140b8d1ce93f3821d986fa165552023440lgaoclass Table(object):
2930fdf1140b8d1ce93f3821d986fa165552023440lgao    def __init__(self, Cursor):
3030fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur = Cursor
3130fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Table = ''
3230fdf1140b8d1ce93f3821d986fa165552023440lgao        self.ID = 0
3330fdf1140b8d1ce93f3821d986fa165552023440lgao
3430fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Create table
3530fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3630fdf1140b8d1ce93f3821d986fa165552023440lgao    # Create a table
3730fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3830fdf1140b8d1ce93f3821d986fa165552023440lgao    def Create(self, SqlCommand):
3930fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur.execute(SqlCommand)
4030fdf1140b8d1ce93f3821d986fa165552023440lgao        self.ID = 0
4130fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.verbose(SqlCommand + " ... DONE!")
4230fdf1140b8d1ce93f3821d986fa165552023440lgao
4330fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Insert table
4430fdf1140b8d1ce93f3821d986fa165552023440lgao    #
4530fdf1140b8d1ce93f3821d986fa165552023440lgao    # Insert a record into a table
4630fdf1140b8d1ce93f3821d986fa165552023440lgao    #
4730fdf1140b8d1ce93f3821d986fa165552023440lgao    def Insert(self, SqlCommand):
4830fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Exec(SqlCommand)
4930fdf1140b8d1ce93f3821d986fa165552023440lgao
5030fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Query table
5130fdf1140b8d1ce93f3821d986fa165552023440lgao    #
5230fdf1140b8d1ce93f3821d986fa165552023440lgao    # Query all records of the table
5330fdf1140b8d1ce93f3821d986fa165552023440lgao    #
5430fdf1140b8d1ce93f3821d986fa165552023440lgao    def Query(self):
5530fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.verbose("\nQuery tabel %s started ..." % self.Table)
5630fdf1140b8d1ce93f3821d986fa165552023440lgao        SqlCommand = """select * from %s""" % self.Table
5730fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur.execute(SqlCommand)
5830fdf1140b8d1ce93f3821d986fa165552023440lgao        for Rs in self.Cur:
5930fdf1140b8d1ce93f3821d986fa165552023440lgao            EdkLogger.verbose(str(Rs))
6030fdf1140b8d1ce93f3821d986fa165552023440lgao
6130fdf1140b8d1ce93f3821d986fa165552023440lgao        TotalCount = self.GetCount()
6230fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) )
6330fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.verbose("Query tabel %s DONE!" % self.Table)
6430fdf1140b8d1ce93f3821d986fa165552023440lgao
6530fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Drop a table
6630fdf1140b8d1ce93f3821d986fa165552023440lgao    #
6730fdf1140b8d1ce93f3821d986fa165552023440lgao    # Drop the table
6830fdf1140b8d1ce93f3821d986fa165552023440lgao    #
6930fdf1140b8d1ce93f3821d986fa165552023440lgao    def Drop(self):
7030fdf1140b8d1ce93f3821d986fa165552023440lgao        SqlCommand = """drop table IF EXISTS %s""" % self.Table
7130fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur.execute(SqlCommand)
7230fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table)
7330fdf1140b8d1ce93f3821d986fa165552023440lgao
7430fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Get count
7530fdf1140b8d1ce93f3821d986fa165552023440lgao    #
7630fdf1140b8d1ce93f3821d986fa165552023440lgao    # Get a count of all records of the table
7730fdf1140b8d1ce93f3821d986fa165552023440lgao    #
7830fdf1140b8d1ce93f3821d986fa165552023440lgao    # @retval Count:  Total count of all records
7930fdf1140b8d1ce93f3821d986fa165552023440lgao    #
8030fdf1140b8d1ce93f3821d986fa165552023440lgao    def GetCount(self):
8130fdf1140b8d1ce93f3821d986fa165552023440lgao        SqlCommand = """select count(ID) from %s""" % self.Table
8230fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur.execute(SqlCommand)
8330fdf1140b8d1ce93f3821d986fa165552023440lgao        for Item in self.Cur:
8430fdf1140b8d1ce93f3821d986fa165552023440lgao            return Item[0]
8530fdf1140b8d1ce93f3821d986fa165552023440lgao
8630fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Generate ID
8730fdf1140b8d1ce93f3821d986fa165552023440lgao    #
8830fdf1140b8d1ce93f3821d986fa165552023440lgao    # Generate an ID if input ID is -1
8930fdf1140b8d1ce93f3821d986fa165552023440lgao    #
9030fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param ID:   Input ID
9130fdf1140b8d1ce93f3821d986fa165552023440lgao    #
9230fdf1140b8d1ce93f3821d986fa165552023440lgao    # @retval ID:  New generated ID
9330fdf1140b8d1ce93f3821d986fa165552023440lgao    #
9430fdf1140b8d1ce93f3821d986fa165552023440lgao    def GenerateID(self, ID):
9530fdf1140b8d1ce93f3821d986fa165552023440lgao        if ID == -1:
9630fdf1140b8d1ce93f3821d986fa165552023440lgao            self.ID = self.ID + 1
9730fdf1140b8d1ce93f3821d986fa165552023440lgao
9830fdf1140b8d1ce93f3821d986fa165552023440lgao        return self.ID
9930fdf1140b8d1ce93f3821d986fa165552023440lgao
10030fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Init the ID of the table
10130fdf1140b8d1ce93f3821d986fa165552023440lgao    #
10230fdf1140b8d1ce93f3821d986fa165552023440lgao    # Init the ID of the table
10330fdf1140b8d1ce93f3821d986fa165552023440lgao    #
10430fdf1140b8d1ce93f3821d986fa165552023440lgao    def InitID(self):
10530fdf1140b8d1ce93f3821d986fa165552023440lgao        self.ID = self.GetCount()
10630fdf1140b8d1ce93f3821d986fa165552023440lgao
10730fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Exec
10830fdf1140b8d1ce93f3821d986fa165552023440lgao    #
10930fdf1140b8d1ce93f3821d986fa165552023440lgao    # Exec Sql Command, return result
11030fdf1140b8d1ce93f3821d986fa165552023440lgao    #
11130fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param SqlCommand:  The SqlCommand to be executed
11230fdf1140b8d1ce93f3821d986fa165552023440lgao    #
11330fdf1140b8d1ce93f3821d986fa165552023440lgao    # @retval RecordSet:  The result after executed
11430fdf1140b8d1ce93f3821d986fa165552023440lgao    #
11530fdf1140b8d1ce93f3821d986fa165552023440lgao    def Exec(self, SqlCommand):
11630fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand)
11730fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Cur.execute(SqlCommand)
11830fdf1140b8d1ce93f3821d986fa165552023440lgao        RecordSet = self.Cur.fetchall()
11930fdf1140b8d1ce93f3821d986fa165552023440lgao        EdkLogger.debug(4, "RecordSet: %s" % RecordSet)
12030fdf1140b8d1ce93f3821d986fa165552023440lgao        return RecordSet
121