]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaDataTable.py
2 # This file is used to create/update/query/erase table for files
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 import Common
.LongFilePathOs
as os
13 import Common
.EdkLogger
as EdkLogger
14 from CommonDataClass
import DataClass
15 from CommonDataClass
.DataClass
import FileClass
17 ## Convert to SQL required string format
18 def ConvertToSqlString(StringList
):
19 return list(map(lambda s
: "'" + s
.replace("'", "''") + "'", StringList
))
23 # This class defined a common table
25 # @param object: Inherited from object class
27 # @param Cursor: Cursor of the database
28 # @param TableName: Name of the table
36 def __init__(self
, Db
, Name
='', IdBase
=0, Temporary
=False):
39 self
.IdBase
= int(IdBase
)
41 self
.Temporary
= Temporary
51 def Create(self
, NewTable
=True):
52 self
.Db
.CreateEmptyTable(self
.Table
)
53 self
.ID
= self
.GetId()
57 # Insert a record into a table
59 def Insert(self
, *Args
):
60 self
.ID
= self
.ID
+ self
._ID
_STEP
_
61 if self
.ID
>= (self
.IdBase
+ self
._ID
_MAX
_):
62 self
.ID
= self
.IdBase
+ self
._ID
_STEP
_
65 self
.Contents
.append(row
)
72 # Get a count of all records of the table
74 # @retval Count: Total count of all records
77 tab
= self
.Db
.GetTable(self
.Table
)
82 tab
= self
.Db
.GetTable(self
.Table
)
83 Id
= max([int(item
[0]) for item
in tab
])
88 ## Init the ID of the table
90 # Init the ID of the table
93 self
.ID
= self
.GetId()
97 # Exec Sql Command, return result
99 # @param SqlCommand: The SqlCommand to be executed
101 # @retval RecordSet: The result after executed
103 def Exec(self
, SqlCommand
):
104 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
105 self
.Db
.execute(SqlCommand
)
106 RecordSet
= self
.Db
.fetchall()
109 def SetEndFlag(self
):
110 Tab
= self
.Db
.GetTable(self
.Table
)
111 Tab
.append(self
._DUMMY
_)
114 def IsIntegral(self
):
115 tab
= self
.Db
.GetTable(self
.Table
)
116 Id
= min([int(item
[0]) for item
in tab
])
122 tab
= self
.Db
.GetTable(self
.Table
)
128 # This class defined a table used for file
130 # @param object: Inherited from object class
132 class TableFile(Table
):
134 ID INTEGER PRIMARY KEY,
135 Name VARCHAR NOT NULL,
138 FullPath VARCHAR NOT NULL,
139 Model INTEGER DEFAULT 0,
140 TimeStamp SINGLE NOT NULL,
141 FromItem REAL NOT NULL
143 def __init__(self
, Cursor
):
144 Table
.__init
__(self
, Cursor
, 'File')
148 # Insert a record into table File
150 # @param Name: Name of a File
151 # @param ExtName: ExtName of a File
152 # @param Path: Path of a File
153 # @param FullPath: FullPath of a File
154 # @param Model: Model of a File
155 # @param TimeStamp: TimeStamp of a File
157 def Insert(self
, Name
, ExtName
, Path
, FullPath
, Model
, TimeStamp
, FromItem
=0):
158 (Name
, ExtName
, Path
, FullPath
) = ConvertToSqlString((Name
, ExtName
, Path
, FullPath
))
172 # Insert one file to table
174 # @param FileFullPath: The full path of the file
175 # @param Model: The model of the file
177 # @retval FileID: The ID after record is inserted
179 def InsertFile(self
, File
, Model
, FromItem
=''):
199 ## Get type of a given file
201 # @param FileId ID of a file
203 # @retval file_type Model value of given file in the table
205 def GetFileType(self
, FileId
):
206 QueryScript
= "select Model from %s where ID = '%s'" % (self
.Table
, FileId
)
207 RecordList
= self
.Exec(QueryScript
)
208 if len(RecordList
) == 0:
210 return RecordList
[0][0]
212 ## Get file timestamp of a given file
214 # @param FileId ID of file
216 # @retval timestamp TimeStamp value of given file in the table
218 def GetFileTimeStamp(self
, FileId
):
219 QueryScript
= "select TimeStamp from %s where ID = '%s'" % (self
.Table
, FileId
)
220 RecordList
= self
.Exec(QueryScript
)
221 if len(RecordList
) == 0:
223 return RecordList
[0][0]
225 ## Update the timestamp of a given file
227 # @param FileId ID of file
228 # @param TimeStamp Time stamp of file
230 def SetFileTimeStamp(self
, FileId
, TimeStamp
):
231 self
.Exec("update %s set TimeStamp=%s where ID='%s'" % (self
.Table
, TimeStamp
, FileId
))
233 ## Get list of file with given type
235 # @param FileType Type value of file
237 # @retval file_list List of files with the given type
239 def GetFileList(self
, FileType
):
240 RecordList
= self
.Exec("select FullPath from %s where Model=%s" % (self
.Table
, FileType
))
241 if len(RecordList
) == 0:
243 return [R
[0] for R
in RecordList
]
247 # This class defined a table used for data model
249 # @param object: Inherited from object class
252 class TableDataModel(Table
):
254 ID INTEGER PRIMARY KEY,
255 CrossIndex INTEGER NOT NULL,
256 Name VARCHAR NOT NULL,
259 def __init__(self
, Cursor
):
260 Table
.__init
__(self
, Cursor
, 'DataModel')
264 # Insert a record into table DataModel
266 # @param ID: ID of a ModelType
267 # @param CrossIndex: CrossIndex of a ModelType
268 # @param Name: Name of a ModelType
269 # @param Description: Description of a ModelType
271 def Insert(self
, CrossIndex
, Name
, Description
):
272 (Name
, Description
) = ConvertToSqlString((Name
, Description
))
273 return Table
.Insert(self
, CrossIndex
, Name
, Description
)
277 # Create all default records of table DataModel
280 EdkLogger
.verbose("\nInitialize table DataModel started ...")
281 Count
= self
.GetCount()
282 if Count
is not None and Count
!= 0:
284 for Item
in DataClass
.MODEL_LIST
:
287 Description
= Item
[0]
288 self
.Insert(CrossIndex
, Name
, Description
)
289 EdkLogger
.verbose("Initialize table DataModel ... DONE!")
293 # Get a model's cross index from its name
295 # @param ModelName: Name of the model
296 # @retval CrossIndex: CrossIndex of the model
298 def GetCrossIndex(self
, ModelName
):
300 SqlCommand
= """select CrossIndex from DataModel where name = '""" + ModelName
+ """'"""
301 self
.Db
.execute(SqlCommand
)