]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaDataTable.py
BaseTools: Replace the sqlite database with list
[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, Db, Name='', IdBase=0, Temporary=False):
43 self.Db = Db
44 self.Table = Name
45 self.IdBase = int(IdBase)
46 self.ID = int(IdBase)
47 self.Temporary = Temporary
48 self.Contents = []
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 self.Db.CreateEmptyTable(self.Table)
59 self.ID = self.GetId()
60
61 ## Insert table
62 #
63 # Insert a record into a table
64 #
65 def Insert(self, *Args):
66 self.ID = self.ID + self._ID_STEP_
67 if self.ID >= (self.IdBase + self._ID_MAX_):
68 self.ID = self.IdBase + self._ID_STEP_
69 row = [self.ID]
70 row.extend(Args)
71 self.Contents.append(row)
72
73 return self.ID
74
75
76 ## Get count
77 #
78 # Get a count of all records of the table
79 #
80 # @retval Count: Total count of all records
81 #
82 def GetCount(self):
83 tab = self.Db.GetTable(self.Table)
84 return len(tab)
85
86
87 def GetId(self):
88 tab = self.Db.GetTable(self.Table)
89 Id = max([int(item[0]) for item in tab])
90 if Id is None:
91 Id = self.IdBase
92 return 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.GetId()
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(EdkLogger.DEBUG_5, SqlCommand)
111 self.Db.execute(SqlCommand)
112 RecordSet = self.Db.fetchall()
113 return RecordSet
114
115 def SetEndFlag(self):
116 Tab = self.Db.GetTable(self.Table)
117 Tab.append(self._DUMMY_)
118
119
120 def IsIntegral(self):
121 tab = self.Db.GetTable(self.Table)
122 Id = min([int(item[0]) for item in tab])
123 if Id != -1:
124 return False
125 return True
126
127 def GetAll(self):
128 tab = self.Db.GetTable(self.Table)
129 return tab
130
131
132 ## TableFile
133 #
134 # This class defined a table used for file
135 #
136 # @param object: Inherited from object class
137 #
138 class TableFile(Table):
139 _COLUMN_ = '''
140 ID INTEGER PRIMARY KEY,
141 Name VARCHAR NOT NULL,
142 ExtName VARCHAR,
143 Path VARCHAR,
144 FullPath VARCHAR NOT NULL,
145 Model INTEGER DEFAULT 0,
146 TimeStamp SINGLE NOT NULL,
147 FromItem REAL NOT NULL
148 '''
149 def __init__(self, Cursor):
150 Table.__init__(self, Cursor, 'File')
151
152 ## Insert table
153 #
154 # Insert a record into table File
155 #
156 # @param Name: Name of a File
157 # @param ExtName: ExtName of a File
158 # @param Path: Path of a File
159 # @param FullPath: FullPath of a File
160 # @param Model: Model of a File
161 # @param TimeStamp: TimeStamp of a File
162 #
163 def Insert(self, Name, ExtName, Path, FullPath, Model, TimeStamp, FromItem=0):
164 (Name, ExtName, Path, FullPath) = ConvertToSqlString((Name, ExtName, Path, FullPath))
165 return Table.Insert(
166 self,
167 Name,
168 ExtName,
169 Path,
170 FullPath,
171 Model,
172 TimeStamp,
173 FromItem
174 )
175
176 ## InsertFile
177 #
178 # Insert one file to table
179 #
180 # @param FileFullPath: The full path of the file
181 # @param Model: The model of the file
182 #
183 # @retval FileID: The ID after record is inserted
184 #
185 def InsertFile(self, File, Model, FromItem=''):
186 if FromItem:
187 return self.Insert(
188 File.Name,
189 File.Ext,
190 File.Dir,
191 File.Path,
192 Model,
193 File.TimeStamp,
194 FromItem
195 )
196 return self.Insert(
197 File.Name,
198 File.Ext,
199 File.Dir,
200 File.Path,
201 Model,
202 File.TimeStamp
203 )
204
205 ## Get type of a given file
206 #
207 # @param FileId ID of a file
208 #
209 # @retval file_type Model value of given file in the table
210 #
211 def GetFileType(self, FileId):
212 QueryScript = "select Model from %s where ID = '%s'" % (self.Table, FileId)
213 RecordList = self.Exec(QueryScript)
214 if len(RecordList) == 0:
215 return None
216 return RecordList[0][0]
217
218 ## Get file timestamp of a given file
219 #
220 # @param FileId ID of file
221 #
222 # @retval timestamp TimeStamp value of given file in the table
223 #
224 def GetFileTimeStamp(self, FileId):
225 QueryScript = "select TimeStamp from %s where ID = '%s'" % (self.Table, FileId)
226 RecordList = self.Exec(QueryScript)
227 if len(RecordList) == 0:
228 return None
229 return RecordList[0][0]
230
231 ## Update the timestamp of a given file
232 #
233 # @param FileId ID of file
234 # @param TimeStamp Time stamp of file
235 #
236 def SetFileTimeStamp(self, FileId, TimeStamp):
237 self.Exec("update %s set TimeStamp=%s where ID='%s'" % (self.Table, TimeStamp, FileId))
238
239 ## Get list of file with given type
240 #
241 # @param FileType Type value of file
242 #
243 # @retval file_list List of files with the given type
244 #
245 def GetFileList(self, FileType):
246 RecordList = self.Exec("select FullPath from %s where Model=%s" % (self.Table, FileType))
247 if len(RecordList) == 0:
248 return []
249 return [R[0] for R in RecordList]
250
251 ## TableDataModel
252 #
253 # This class defined a table used for data model
254 #
255 # @param object: Inherited from object class
256 #
257 #
258 class TableDataModel(Table):
259 _COLUMN_ = """
260 ID INTEGER PRIMARY KEY,
261 CrossIndex INTEGER NOT NULL,
262 Name VARCHAR NOT NULL,
263 Description VARCHAR
264 """
265 def __init__(self, Cursor):
266 Table.__init__(self, Cursor, 'DataModel')
267
268 ## Insert table
269 #
270 # Insert a record into table DataModel
271 #
272 # @param ID: ID of a ModelType
273 # @param CrossIndex: CrossIndex of a ModelType
274 # @param Name: Name of a ModelType
275 # @param Description: Description of a ModelType
276 #
277 def Insert(self, CrossIndex, Name, Description):
278 (Name, Description) = ConvertToSqlString((Name, Description))
279 return Table.Insert(self, CrossIndex, Name, Description)
280
281 ## Init table
282 #
283 # Create all default records of table DataModel
284 #
285 def InitTable(self):
286 EdkLogger.verbose("\nInitialize table DataModel started ...")
287 Count = self.GetCount()
288 if Count is not None and Count != 0:
289 return
290 for Item in DataClass.MODEL_LIST:
291 CrossIndex = Item[1]
292 Name = Item[0]
293 Description = Item[0]
294 self.Insert(CrossIndex, Name, Description)
295 EdkLogger.verbose("Initialize table DataModel ... DONE!")
296
297 ## Get CrossIndex
298 #
299 # Get a model's cross index from its name
300 #
301 # @param ModelName: Name of the model
302 # @retval CrossIndex: CrossIndex of the model
303 #
304 def GetCrossIndex(self, ModelName):
305 CrossIndex = -1
306 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
307 self.Db.execute(SqlCommand)
308 for Item in self.Db:
309 CrossIndex = Item[0]
310
311 return CrossIndex
312