]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileTable.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / MetaFileWorkspace / MetaFileTable.py
1 ## @file
2 # This file is used to create/update/query/erase a meta file 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 from __future__ import absolute_import
12 import uuid
13
14 import Common.EdkLogger as EdkLogger
15 import Ecc.EccGlobalData as EccGlobalData
16
17 from Ecc.MetaFileWorkspace.MetaDataTable import Table
18 from Ecc.MetaFileWorkspace.MetaDataTable import ConvertToSqlString
19 from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \
20 MODEL_FILE_OTHERS
21
22 class MetaFileTable(Table):
23 ## Constructor
24 def __init__(self, Cursor, MetaFile, FileType, TableName, Temporary = False):
25 self.MetaFile = MetaFile
26 self.TblFile = EccGlobalData.gDb.TblFile
27 if (FileType == MODEL_FILE_INF):
28 TableName = "Inf"
29 if (FileType == MODEL_FILE_DSC):
30 if Temporary:
31 TableName = "_%s_%s" % ("Dsc", uuid.uuid4().hex)
32 else:
33 TableName = "Dsc"
34 if (FileType == MODEL_FILE_DEC):
35 TableName = "Dec"
36
37 Table.__init__(self, Cursor, TableName, 0, Temporary)
38 self.Create(False)
39
40
41 ## Python class representation of table storing module data
42 class ModuleTable(MetaFileTable):
43 _COLUMN_ = '''
44 ID REAL PRIMARY KEY,
45 Model INTEGER NOT NULL,
46 Value1 TEXT NOT NULL,
47 Value2 TEXT,
48 Value3 TEXT,
49 Usage TEXT,
50 Scope1 TEXT,
51 Scope2 TEXT,
52 BelongsToItem REAL NOT NULL,
53 BelongsToFile SINGLE NOT NULL,
54 StartLine INTEGER NOT NULL,
55 StartColumn INTEGER NOT NULL,
56 EndLine INTEGER NOT NULL,
57 EndColumn INTEGER NOT NULL,
58 Enabled INTEGER DEFAULT 0
59 '''
60 # used as table end flag, in case the changes to database is not committed to db file
61 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1"
62
63 ## Constructor
64 def __init__(self, Cursor):
65 MetaFileTable.__init__(self, Cursor, '', MODEL_FILE_INF, "Inf", False)
66
67 ## Insert a record into table Inf
68 #
69 # @param Model: Model of a Inf item
70 # @param Value1: Value1 of a Inf item
71 # @param Value2: Value2 of a Inf item
72 # @param Value3: Value3 of a Inf item
73 # @param Scope1: Arch of a Inf item
74 # @param Scope2 Platform os a Inf item
75 # @param BelongsToItem: The item belongs to which another item
76 # @param StartLine: StartLine of a Inf item
77 # @param StartColumn: StartColumn of a Inf item
78 # @param EndLine: EndLine of a Inf item
79 # @param EndColumn: EndColumn of a Inf item
80 # @param Enabled: If this item enabled
81 #
82 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',
83 BelongsToItem=-1, BelongsToFile = -1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0, Usage=''):
84 (Value1, Value2, Value3, Usage, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Usage, Scope1, Scope2))
85 return Table.Insert(
86 self,
87 Model,
88 Value1,
89 Value2,
90 Value3,
91 Usage,
92 Scope1,
93 Scope2,
94 BelongsToItem,
95 BelongsToFile,
96 StartLine,
97 StartColumn,
98 EndLine,
99 EndColumn,
100 Enabled
101 )
102
103 ## Query table
104 #
105 # @param Model: The Model of Record
106 # @param Arch: The Arch attribute of Record
107 # @param Platform The Platform attribute of Record
108 #
109 # @retval: A recordSet of all found records
110 #
111 def Query(self, Model, Arch=None, Platform=None):
112 ConditionString = "Model=%s AND Enabled>=0" % Model
113 ValueString = "Value1,Value2,Value3,Usage,Scope1,Scope2,ID,StartLine"
114
115 if Arch is not None and Arch != 'COMMON':
116 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch
117 if Platform is not None and Platform != 'COMMON':
118 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Platform
119
120 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
121 return self.Exec(SqlCommand)
122
123 ## Python class representation of table storing package data
124 class PackageTable(MetaFileTable):
125 _COLUMN_ = '''
126 ID REAL PRIMARY KEY,
127 Model INTEGER NOT NULL,
128 Value1 TEXT NOT NULL,
129 Value2 TEXT,
130 Value3 TEXT,
131 Scope1 TEXT,
132 Scope2 TEXT,
133 BelongsToItem REAL NOT NULL,
134 BelongsToFile SINGLE NOT NULL,
135 StartLine INTEGER NOT NULL,
136 StartColumn INTEGER NOT NULL,
137 EndLine INTEGER NOT NULL,
138 EndColumn INTEGER NOT NULL,
139 Enabled INTEGER DEFAULT 0
140 '''
141 # used as table end flag, in case the changes to database is not committed to db file
142 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1"
143
144 ## Constructor
145 def __init__(self, Cursor):
146 MetaFileTable.__init__(self, Cursor, '', MODEL_FILE_DEC, "Dec", False)
147
148 ## Insert table
149 #
150 # Insert a record into table Dec
151 #
152 # @param Model: Model of a Dec item
153 # @param Value1: Value1 of a Dec item
154 # @param Value2: Value2 of a Dec item
155 # @param Value3: Value3 of a Dec item
156 # @param Scope1: Arch of a Dec item
157 # @param Scope2: Module type of a Dec item
158 # @param BelongsToItem: The item belongs to which another item
159 # @param StartLine: StartLine of a Dec item
160 # @param StartColumn: StartColumn of a Dec item
161 # @param EndLine: EndLine of a Dec item
162 # @param EndColumn: EndColumn of a Dec item
163 # @param Enabled: If this item enabled
164 #
165 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON',
166 BelongsToItem=-1, BelongsToFile = -1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):
167 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))
168 return Table.Insert(
169 self,
170 Model,
171 Value1,
172 Value2,
173 Value3,
174 Scope1,
175 Scope2,
176 BelongsToItem,
177 BelongsToFile,
178 StartLine,
179 StartColumn,
180 EndLine,
181 EndColumn,
182 Enabled
183 )
184
185 ## Query table
186 #
187 # @param Model: The Model of Record
188 # @param Arch: The Arch attribute of Record
189 #
190 # @retval: A recordSet of all found records
191 #
192 def Query(self, Model, Arch=None):
193 ConditionString = "Model=%s AND Enabled>=0" % Model
194 ValueString = "Value1,Value2,Value3,Scope1,ID,StartLine"
195
196 if Arch is not None and Arch != 'COMMON':
197 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Arch
198
199 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
200 return self.Exec(SqlCommand)
201
202 ## Python class representation of table storing platform data
203 class PlatformTable(MetaFileTable):
204 _COLUMN_ = '''
205 ID REAL PRIMARY KEY,
206 Model INTEGER NOT NULL,
207 Value1 TEXT NOT NULL,
208 Value2 TEXT,
209 Value3 TEXT,
210 Scope1 TEXT,
211 Scope2 TEXT,
212 BelongsToItem REAL NOT NULL,
213 BelongsToFile SINGLE NOT NULL,
214 FromItem REAL NOT NULL,
215 StartLine INTEGER NOT NULL,
216 StartColumn INTEGER NOT NULL,
217 EndLine INTEGER NOT NULL,
218 EndColumn INTEGER NOT NULL,
219 Enabled INTEGER DEFAULT 0
220 '''
221 # used as table end flag, in case the changes to database is not committed to db file
222 _DUMMY_ = "-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1, -1, -1"
223
224 ## Constructor
225 def __init__(self, Cursor, MetaFile = '', FileType = MODEL_FILE_DSC, Temporary = False):
226 MetaFileTable.__init__(self, Cursor, MetaFile, FileType, "Dsc", Temporary)
227
228 ## Insert table
229 #
230 # Insert a record into table Dsc
231 #
232 # @param Model: Model of a Dsc item
233 # @param Value1: Value1 of a Dsc item
234 # @param Value2: Value2 of a Dsc item
235 # @param Value3: Value3 of a Dsc item
236 # @param Scope1: Arch of a Dsc item
237 # @param Scope2: Module type of a Dsc item
238 # @param BelongsToItem: The item belongs to which another item
239 # @param FromItem: The item belongs to which dsc file
240 # @param StartLine: StartLine of a Dsc item
241 # @param StartColumn: StartColumn of a Dsc item
242 # @param EndLine: EndLine of a Dsc item
243 # @param EndColumn: EndColumn of a Dsc item
244 # @param Enabled: If this item enabled
245 #
246 def Insert(self, Model, Value1, Value2, Value3, Scope1='COMMON', Scope2='COMMON', BelongsToItem=-1, BelongsToFile = -1,
247 FromItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=1):
248 (Value1, Value2, Value3, Scope1, Scope2) = ConvertToSqlString((Value1, Value2, Value3, Scope1, Scope2))
249 return Table.Insert(
250 self,
251 Model,
252 Value1,
253 Value2,
254 Value3,
255 Scope1,
256 Scope2,
257 BelongsToItem,
258 BelongsToFile,
259 FromItem,
260 StartLine,
261 StartColumn,
262 EndLine,
263 EndColumn,
264 Enabled
265 )
266
267 ## Query table
268 #
269 # @param Model: The Model of Record
270 # @param Scope1: Arch of a Dsc item
271 # @param Scope2: Module type of a Dsc item
272 # @param BelongsToItem: The item belongs to which another item
273 # @param FromItem: The item belongs to which dsc file
274 #
275 # @retval: A recordSet of all found records
276 #
277 def Query(self, Model, Scope1=None, Scope2=None, BelongsToItem=None, FromItem=None):
278 ConditionString = "Model=%s AND Enabled>0" % Model
279 ValueString = "Value1,Value2,Value3,Scope1,Scope2,ID,StartLine"
280
281 if Scope1 is not None and Scope1 != 'COMMON':
282 ConditionString += " AND (Scope1='%s' OR Scope1='COMMON')" % Scope1
283 if Scope2 is not None and Scope2 != 'COMMON':
284 ConditionString += " AND (Scope2='%s' OR Scope2='COMMON' OR Scope2='DEFAULT')" % Scope2
285
286 if BelongsToItem is not None:
287 ConditionString += " AND BelongsToItem=%s" % BelongsToItem
288 else:
289 ConditionString += " AND BelongsToItem<0"
290
291 if FromItem is not None:
292 ConditionString += " AND FromItem=%s" % FromItem
293
294 SqlCommand = "SELECT %s FROM %s WHERE %s" % (ValueString, self.Table, ConditionString)
295 return self.Exec(SqlCommand)
296
297 ## Factory class to produce different storage for different type of meta-file
298 class MetaFileStorage(object):
299 _FILE_TABLE_ = {
300 MODEL_FILE_INF : ModuleTable,
301 MODEL_FILE_DEC : PackageTable,
302 MODEL_FILE_DSC : PlatformTable,
303 MODEL_FILE_OTHERS : MetaFileTable,
304 }
305
306 _FILE_TYPE_ = {
307 ".inf" : MODEL_FILE_INF,
308 ".dec" : MODEL_FILE_DEC,
309 ".dsc" : MODEL_FILE_DSC,
310 }
311
312 ## Constructor
313 def __new__(Class, Cursor, MetaFile, FileType=None, Temporary=False):
314 # no type given, try to find one
315 if not FileType:
316 if MetaFile.Type in self._FILE_TYPE_:
317 FileType = Class._FILE_TYPE_[MetaFile.Type]
318 else:
319 FileType = MODEL_FILE_OTHERS
320
321 # don't pass the type around if it's well known
322 if FileType == MODEL_FILE_OTHERS:
323 Args = (Cursor, MetaFile, FileType, Temporary)
324 else:
325 Args = (Cursor, MetaFile, FileType, Temporary)
326
327 # create the storage object and return it to caller
328 return Class._FILE_TABLE_[FileType](*Args)
329