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