]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaDataTable.py
BaseTools: Refactor python print statements
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / MetaFileWorkspace / MetaDataTable.py
1 ## @file
2 # This file is used to create/update/query/erase table for files
3 #
4 # Copyright (c) 2008 - 2018, 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 from __future__ import print_function
18 import Common.LongFilePathOs as os
19
20 import Common.EdkLogger as EdkLogger
21 from CommonDataClass import DataClass
22 from CommonDataClass.DataClass import FileClass
23
24 ## Convert to SQL required string format
25 def ConvertToSqlString(StringList):
26 return map(lambda s: "'" + s.replace("'", "''") + "'", StringList)
27
28 ## TableFile
29 #
30 # This class defined a common table
31 #
32 # @param object: Inherited from object class
33 #
34 # @param Cursor: Cursor of the database
35 # @param TableName: Name of the table
36 #
37 class Table(object):
38 _COLUMN_ = ''
39 _ID_STEP_ = 1
40 _ID_MAX_ = 0x80000000
41 _DUMMY_ = 0
42
43 def __init__(self, Cursor, Name='', IdBase=0, Temporary=False):
44 self.Cur = Cursor
45 self.Table = Name
46 self.IdBase = int(IdBase)
47 self.ID = int(IdBase)
48 self.Temporary = Temporary
49
50 def __str__(self):
51 return self.Table
52
53 ## Create table
54 #
55 # Create a table
56 #
57 def Create(self, NewTable=True):
58 if NewTable:
59 self.Drop()
60
61 if self.Temporary:
62 SqlCommand = """create temp table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)
63 else:
64 SqlCommand = """create table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)
65 EdkLogger.debug(EdkLogger.DEBUG_8, SqlCommand)
66 self.Cur.execute(SqlCommand)
67 self.ID = self.GetId()
68
69 ## Insert table
70 #
71 # Insert a record into a table
72 #
73 def Insert(self, *Args):
74 self.ID = self.ID + self._ID_STEP_
75 if self.ID >= (self.IdBase + self._ID_MAX_):
76 self.ID = self.IdBase + self._ID_STEP_
77 Values = ", ".join(str(Arg) for Arg in Args)
78 SqlCommand = "insert into %s values(%s, %s)" % (self.Table, self.ID, Values)
79 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)
80 self.Cur.execute(SqlCommand)
81 return self.ID
82
83 ## Query table
84 #
85 # Query all records of the table
86 #
87 def Query(self):
88 SqlCommand = """select * from %s""" % self.Table
89 self.Cur.execute(SqlCommand)
90 for Rs in self.Cur:
91 EdkLogger.verbose(str(Rs))
92 TotalCount = self.GetId()
93
94 ## Drop a table
95 #
96 # Drop the table
97 #
98 def Drop(self):
99 SqlCommand = """drop table IF EXISTS %s""" % self.Table
100 try:
101 self.Cur.execute(SqlCommand)
102 except Exception as e:
103 print("An error occurred when Drop a table:", e.args[0])
104
105 ## Get count
106 #
107 # Get a count of all records of the table
108 #
109 # @retval Count: Total count of all records
110 #
111 def GetCount(self):
112 SqlCommand = """select count(ID) from %s""" % self.Table
113 Record = self.Cur.execute(SqlCommand).fetchall()
114 return Record[0][0]
115
116 def GetId(self):
117 SqlCommand = """select max(ID) from %s""" % self.Table
118 Record = self.Cur.execute(SqlCommand).fetchall()
119 Id = Record[0][0]
120 if Id is None:
121 Id = self.IdBase
122 return Id
123
124 ## Init the ID of the table
125 #
126 # Init the ID of the table
127 #
128 def InitID(self):
129 self.ID = self.GetId()
130
131 ## Exec
132 #
133 # Exec Sql Command, return result
134 #
135 # @param SqlCommand: The SqlCommand to be executed
136 #
137 # @retval RecordSet: The result after executed
138 #
139 def Exec(self, SqlCommand):
140 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)
141 self.Cur.execute(SqlCommand)
142 RecordSet = self.Cur.fetchall()
143 return RecordSet
144
145 def SetEndFlag(self):
146 pass
147
148 def IsIntegral(self):
149 Result = self.Exec("select min(ID) from %s" % (self.Table))
150 if Result[0][0] != -1:
151 return False
152 return True
153
154 def GetAll(self):
155 return self.Exec("select * from %s where ID > 0 order by ID" % (self.Table))
156
157
158 ## TableDataModel
159 #
160 # This class defined a table used for data model
161 #
162 # @param object: Inherited from object class
163 #
164 #
165 class TableDataModel(Table):
166 _COLUMN_ = """
167 ID INTEGER PRIMARY KEY,
168 CrossIndex INTEGER NOT NULL,
169 Name VARCHAR NOT NULL,
170 Description VARCHAR
171 """
172 def __init__(self, Cursor):
173 Table.__init__(self, Cursor, 'DataModel')
174
175 ## Insert table
176 #
177 # Insert a record into table DataModel
178 #
179 # @param ID: ID of a ModelType
180 # @param CrossIndex: CrossIndex of a ModelType
181 # @param Name: Name of a ModelType
182 # @param Description: Description of a ModelType
183 #
184 def Insert(self, CrossIndex, Name, Description):
185 (Name, Description) = ConvertToSqlString((Name, Description))
186 return Table.Insert(self, CrossIndex, Name, Description)
187
188 ## Init table
189 #
190 # Create all default records of table DataModel
191 #
192 def InitTable(self):
193 EdkLogger.verbose("\nInitialize table DataModel started ...")
194 Count = self.GetCount()
195 if Count is not None and Count != 0:
196 return
197 for Item in DataClass.MODEL_LIST:
198 CrossIndex = Item[1]
199 Name = Item[0]
200 Description = Item[0]
201 self.Insert(CrossIndex, Name, Description)
202 EdkLogger.verbose("Initialize table DataModel ... DONE!")
203
204 ## Get CrossIndex
205 #
206 # Get a model's cross index from its name
207 #
208 # @param ModelName: Name of the model
209 # @retval CrossIndex: CrossIndex of the model
210 #
211 def GetCrossIndex(self, ModelName):
212 CrossIndex = -1
213 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
214 self.Cur.execute(SqlCommand)
215 for Item in self.Cur:
216 CrossIndex = Item[0]
217
218 return CrossIndex
219