]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaDataTable.py
MdeModulePkg/PerformanceMeasurement.h: Correct the license
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / 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 import Common.LongFilePathOs as os
18
19 import Common.EdkLogger as EdkLogger
20 from CommonDataClass import DataClass
21 from CommonDataClass.DataClass import FileClass
22
23 ## Convert to SQL required string format
24 def ConvertToSqlString(StringList):
25 return map(lambda s: "'" + s.replace("'", "''") + "'", StringList)
26
27 ## TableFile
28 #
29 # This class defined a common table
30 #
31 # @param object: Inherited from object class
32 #
33 # @param Cursor: Cursor of the database
34 # @param TableName: Name of the table
35 #
36 class Table(object):
37 _COLUMN_ = ''
38 _ID_STEP_ = 1
39 _ID_MAX_ = 0x80000000
40 _DUMMY_ = 0
41
42 def __init__(self, Cursor, Name='', IdBase=0, Temporary=False):
43 self.Cur = Cursor
44 self.Table = Name
45 self.IdBase = int(IdBase)
46 self.ID = int(IdBase)
47 self.Temporary = Temporary
48
49 def __str__(self):
50 return self.Table
51
52 ## Create table
53 #
54 # Create a table
55 #
56 def Create(self, NewTable=True):
57 if NewTable:
58 self.Drop()
59
60 if self.Temporary:
61 SqlCommand = """create temp table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)
62 else:
63 SqlCommand = """create table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)
64 EdkLogger.debug(EdkLogger.DEBUG_8, SqlCommand)
65 self.Cur.execute(SqlCommand)
66 self.ID = self.GetId()
67
68 ## Insert table
69 #
70 # Insert a record into a table
71 #
72 def Insert(self, *Args):
73 self.ID = self.ID + self._ID_STEP_
74 if self.ID >= (self.IdBase + self._ID_MAX_):
75 self.ID = self.IdBase + self._ID_STEP_
76 Values = ", ".join(str(Arg) for Arg in Args)
77 SqlCommand = "insert into %s values(%s, %s)" % (self.Table, self.ID, Values)
78 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)
79 self.Cur.execute(SqlCommand)
80 return self.ID
81
82 ## Query table
83 #
84 # Query all records of the table
85 #
86 def Query(self):
87 SqlCommand = """select * from %s""" % self.Table
88 self.Cur.execute(SqlCommand)
89 for Rs in self.Cur:
90 EdkLogger.verbose(str(Rs))
91 TotalCount = self.GetId()
92
93 ## Drop a table
94 #
95 # Drop the table
96 #
97 def Drop(self):
98 SqlCommand = """drop table IF EXISTS %s""" % self.Table
99 self.Cur.execute(SqlCommand)
100
101 ## Get count
102 #
103 # Get a count of all records of the table
104 #
105 # @retval Count: Total count of all records
106 #
107 def GetCount(self):
108 SqlCommand = """select count(ID) from %s""" % self.Table
109 Record = self.Cur.execute(SqlCommand).fetchall()
110 return Record[0][0]
111
112 def GetId(self):
113 SqlCommand = """select max(ID) from %s""" % self.Table
114 Record = self.Cur.execute(SqlCommand).fetchall()
115 Id = Record[0][0]
116 if Id is None:
117 Id = self.IdBase
118 return Id
119
120 ## Init the ID of the table
121 #
122 # Init the ID of the table
123 #
124 def InitID(self):
125 self.ID = self.GetId()
126
127 ## Exec
128 #
129 # Exec Sql Command, return result
130 #
131 # @param SqlCommand: The SqlCommand to be executed
132 #
133 # @retval RecordSet: The result after executed
134 #
135 def Exec(self, SqlCommand):
136 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)
137 self.Cur.execute(SqlCommand)
138 RecordSet = self.Cur.fetchall()
139 return RecordSet
140
141 def SetEndFlag(self):
142 self.Exec("insert into %s values(%s)" % (self.Table, self._DUMMY_))
143 #
144 # Need to execution commit for table data changed.
145 #
146 self.Cur.connection.commit()
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 ## TableFile
158 #
159 # This class defined a table used for file
160 #
161 # @param object: Inherited from object class
162 #
163 class TableFile(Table):
164 _COLUMN_ = '''
165 ID INTEGER PRIMARY KEY,
166 Name VARCHAR NOT NULL,
167 ExtName VARCHAR,
168 Path VARCHAR,
169 FullPath VARCHAR NOT NULL,
170 Model INTEGER DEFAULT 0,
171 TimeStamp SINGLE NOT NULL,
172 FromItem REAL NOT NULL
173 '''
174 def __init__(self, Cursor):
175 Table.__init__(self, Cursor, 'File')
176
177 ## Insert table
178 #
179 # Insert a record into table File
180 #
181 # @param Name: Name of a File
182 # @param ExtName: ExtName of a File
183 # @param Path: Path of a File
184 # @param FullPath: FullPath of a File
185 # @param Model: Model of a File
186 # @param TimeStamp: TimeStamp of a File
187 #
188 def Insert(self, Name, ExtName, Path, FullPath, Model, TimeStamp, FromItem=0):
189 (Name, ExtName, Path, FullPath) = ConvertToSqlString((Name, ExtName, Path, FullPath))
190 return Table.Insert(
191 self,
192 Name,
193 ExtName,
194 Path,
195 FullPath,
196 Model,
197 TimeStamp,
198 FromItem
199 )
200
201 ## InsertFile
202 #
203 # Insert one file to table
204 #
205 # @param FileFullPath: The full path of the file
206 # @param Model: The model of the file
207 #
208 # @retval FileID: The ID after record is inserted
209 #
210 def InsertFile(self, File, Model, FromItem=''):
211 if FromItem:
212 return self.Insert(
213 File.Name,
214 File.Ext,
215 File.Dir,
216 File.Path,
217 Model,
218 File.TimeStamp,
219 FromItem
220 )
221 return self.Insert(
222 File.Name,
223 File.Ext,
224 File.Dir,
225 File.Path,
226 Model,
227 File.TimeStamp
228 )
229
230 ## Get ID of a given file
231 #
232 # @param FilePath Path of file
233 #
234 # @retval ID ID value of given file in the table
235 #
236 def GetFileId(self, File, FromItem=None):
237 if FromItem:
238 QueryScript = "select ID from %s where FullPath = '%s' and FromItem = %s" % (self.Table, str(File), str(FromItem))
239 else:
240 QueryScript = "select ID from %s where FullPath = '%s'" % (self.Table, str(File))
241 RecordList = self.Exec(QueryScript)
242 if len(RecordList) == 0:
243 return None
244 return RecordList[0][0]
245
246 ## Get type of a given file
247 #
248 # @param FileId ID of a file
249 #
250 # @retval file_type Model value of given file in the table
251 #
252 def GetFileType(self, FileId):
253 QueryScript = "select Model from %s where ID = '%s'" % (self.Table, FileId)
254 RecordList = self.Exec(QueryScript)
255 if len(RecordList) == 0:
256 return None
257 return RecordList[0][0]
258
259 ## Get file timestamp of a given file
260 #
261 # @param FileId ID of file
262 #
263 # @retval timestamp TimeStamp value of given file in the table
264 #
265 def GetFileTimeStamp(self, FileId):
266 QueryScript = "select TimeStamp from %s where ID = '%s'" % (self.Table, FileId)
267 RecordList = self.Exec(QueryScript)
268 if len(RecordList) == 0:
269 return None
270 return RecordList[0][0]
271
272 ## Update the timestamp of a given file
273 #
274 # @param FileId ID of file
275 # @param TimeStamp Time stamp of file
276 #
277 def SetFileTimeStamp(self, FileId, TimeStamp):
278 self.Exec("update %s set TimeStamp=%s where ID='%s'" % (self.Table, TimeStamp, FileId))
279
280 ## Get list of file with given type
281 #
282 # @param FileType Type value of file
283 #
284 # @retval file_list List of files with the given type
285 #
286 def GetFileList(self, FileType):
287 RecordList = self.Exec("select FullPath from %s where Model=%s" % (self.Table, FileType))
288 if len(RecordList) == 0:
289 return []
290 return [R[0] for R in RecordList]
291
292 ## TableDataModel
293 #
294 # This class defined a table used for data model
295 #
296 # @param object: Inherited from object class
297 #
298 #
299 class TableDataModel(Table):
300 _COLUMN_ = """
301 ID INTEGER PRIMARY KEY,
302 CrossIndex INTEGER NOT NULL,
303 Name VARCHAR NOT NULL,
304 Description VARCHAR
305 """
306 def __init__(self, Cursor):
307 Table.__init__(self, Cursor, 'DataModel')
308
309 ## Insert table
310 #
311 # Insert a record into table DataModel
312 #
313 # @param ID: ID of a ModelType
314 # @param CrossIndex: CrossIndex of a ModelType
315 # @param Name: Name of a ModelType
316 # @param Description: Description of a ModelType
317 #
318 def Insert(self, CrossIndex, Name, Description):
319 (Name, Description) = ConvertToSqlString((Name, Description))
320 return Table.Insert(self, CrossIndex, Name, Description)
321
322 ## Init table
323 #
324 # Create all default records of table DataModel
325 #
326 def InitTable(self):
327 EdkLogger.verbose("\nInitialize table DataModel started ...")
328 Count = self.GetCount()
329 if Count is not None and Count != 0:
330 return
331 for Item in DataClass.MODEL_LIST:
332 CrossIndex = Item[1]
333 Name = Item[0]
334 Description = Item[0]
335 self.Insert(CrossIndex, Name, Description)
336 EdkLogger.verbose("Initialize table DataModel ... DONE!")
337
338 ## Get CrossIndex
339 #
340 # Get a model's cross index from its name
341 #
342 # @param ModelName: Name of the model
343 # @retval CrossIndex: CrossIndex of the model
344 #
345 def GetCrossIndex(self, ModelName):
346 CrossIndex = -1
347 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
348 self.Cur.execute(SqlCommand)
349 for Item in self.Cur:
350 CrossIndex = Item[0]
351
352 return CrossIndex
353