]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Table/Table.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Table / Table.py
1 ## @file
2 # This file is used to create/update/query/erase a common table
3 #
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 ##
9 # Import Modules
10 #
11 import Common.EdkLogger as EdkLogger
12
13 ## TableFile
14 #
15 # This class defined a common table
16 #
17 # @param object: Inherited from object class
18 #
19 # @param Cursor: Cursor of the database
20 # @param TableName: Name of the table
21 #
22 class Table(object):
23 def __init__(self, Cursor):
24 self.Cur = Cursor
25 self.Table = ''
26 self.ID = 0
27
28 ## Create table
29 #
30 # Create a table
31 #
32 def Create(self, SqlCommand):
33 self.Cur.execute(SqlCommand)
34 self.ID = 0
35 EdkLogger.verbose(SqlCommand + " ... DONE!")
36
37 ## Insert table
38 #
39 # Insert a record into a table
40 #
41 def Insert(self, SqlCommand):
42 self.Exec(SqlCommand)
43
44 ## Query table
45 #
46 # Query all records of the table
47 #
48 def Query(self):
49 EdkLogger.verbose("\nQuery table %s started ..." % self.Table)
50 SqlCommand = """select * from %s""" % self.Table
51 self.Cur.execute(SqlCommand)
52 for Rs in self.Cur:
53 EdkLogger.verbose(str(Rs))
54
55 TotalCount = self.GetCount()
56 EdkLogger.verbose("*** Total %s records in table %s ***" % (TotalCount, self.Table) )
57 EdkLogger.verbose("Query tabel %s DONE!" % self.Table)
58
59 ## Drop a table
60 #
61 # Drop the table
62 #
63 def Drop(self):
64 SqlCommand = """drop table IF EXISTS %s""" % self.Table
65 self.Cur.execute(SqlCommand)
66 EdkLogger.verbose("Drop tabel %s ... DONE!" % self.Table)
67
68 ## Get count
69 #
70 # Get a count of all records of the table
71 #
72 # @retval Count: Total count of all records
73 #
74 def GetCount(self):
75 SqlCommand = """select count(ID) from %s""" % self.Table
76 self.Cur.execute(SqlCommand)
77 for Item in self.Cur:
78 return Item[0]
79
80 ## Generate ID
81 #
82 # Generate an ID if input ID is -1
83 #
84 # @param ID: Input ID
85 #
86 # @retval ID: New generated ID
87 #
88 def GenerateID(self, ID):
89 if ID == -1:
90 self.ID = self.ID + 1
91
92 return self.ID
93
94 ## Init the ID of the table
95 #
96 # Init the ID of the table
97 #
98 def InitID(self):
99 self.ID = self.GetCount()
100
101 ## Exec
102 #
103 # Exec Sql Command, return result
104 #
105 # @param SqlCommand: The SqlCommand to be executed
106 #
107 # @retval RecordSet: The result after executed
108 #
109 def Exec(self, SqlCommand):
110 EdkLogger.debug(4, "SqlCommand: %s" % SqlCommand)
111 self.Cur.execute(SqlCommand)
112 RecordSet = self.Cur.fetchall()
113 EdkLogger.debug(4, "RecordSet: %s" % RecordSet)
114 return RecordSet