]>
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 # 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
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.
17 import Common
.LongFilePathOs
as os
19 import Common
.EdkLogger
as EdkLogger
20 from CommonDataClass
import DataClass
21 from CommonDataClass
.DataClass
import FileClass
23 ## Convert to SQL required string format
24 def ConvertToSqlString(StringList
):
25 return map(lambda s
: "'" + s
.replace("'", "''") + "'", StringList
)
29 # This class defined a common table
31 # @param object: Inherited from object class
33 # @param Cursor: Cursor of the database
34 # @param TableName: Name of the table
42 def __init__(self
, Db
, Name
='', IdBase
=0, Temporary
=False):
45 self
.IdBase
= int(IdBase
)
47 self
.Temporary
= Temporary
57 def Create(self
, NewTable
=True):
58 self
.Db
.CreateEmptyTable(self
.Table
)
59 self
.ID
= self
.GetId()
63 # Insert a record into a table
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
_
71 self
.Contents
.append(row
)
78 # Get a count of all records of the table
80 # @retval Count: Total count of all records
83 tab
= self
.Db
.GetTable(self
.Table
)
88 tab
= self
.Db
.GetTable(self
.Table
)
89 Id
= max([int(item
[0]) for item
in tab
])
94 ## Init the ID of the table
96 # Init the ID of the table
99 self
.ID
= self
.GetId()
103 # Exec Sql Command, return result
105 # @param SqlCommand: The SqlCommand to be executed
107 # @retval RecordSet: The result after executed
109 def Exec(self
, SqlCommand
):
110 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
111 self
.Db
.execute(SqlCommand
)
112 RecordSet
= self
.Db
.fetchall()
115 def SetEndFlag(self
):
116 Tab
= self
.Db
.GetTable(self
.Table
)
117 Tab
.append(self
._DUMMY
_)
120 def IsIntegral(self
):
121 tab
= self
.Db
.GetTable(self
.Table
)
122 Id
= min([int(item
[0]) for item
in tab
])
128 tab
= self
.Db
.GetTable(self
.Table
)
134 # This class defined a table used for file
136 # @param object: Inherited from object class
138 class TableFile(Table
):
140 ID INTEGER PRIMARY KEY,
141 Name VARCHAR NOT NULL,
144 FullPath VARCHAR NOT NULL,
145 Model INTEGER DEFAULT 0,
146 TimeStamp SINGLE NOT NULL,
147 FromItem REAL NOT NULL
149 def __init__(self
, Cursor
):
150 Table
.__init
__(self
, Cursor
, 'File')
154 # Insert a record into table File
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
163 def Insert(self
, Name
, ExtName
, Path
, FullPath
, Model
, TimeStamp
, FromItem
=0):
164 (Name
, ExtName
, Path
, FullPath
) = ConvertToSqlString((Name
, ExtName
, Path
, FullPath
))
178 # Insert one file to table
180 # @param FileFullPath: The full path of the file
181 # @param Model: The model of the file
183 # @retval FileID: The ID after record is inserted
185 def InsertFile(self
, File
, Model
, FromItem
=''):
205 ## Get type of a given file
207 # @param FileId ID of a file
209 # @retval file_type Model value of given file in the table
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:
216 return RecordList
[0][0]
218 ## Get file timestamp of a given file
220 # @param FileId ID of file
222 # @retval timestamp TimeStamp value of given file in the table
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:
229 return RecordList
[0][0]
231 ## Update the timestamp of a given file
233 # @param FileId ID of file
234 # @param TimeStamp Time stamp of file
236 def SetFileTimeStamp(self
, FileId
, TimeStamp
):
237 self
.Exec("update %s set TimeStamp=%s where ID='%s'" % (self
.Table
, TimeStamp
, FileId
))
239 ## Get list of file with given type
241 # @param FileType Type value of file
243 # @retval file_list List of files with the given type
245 def GetFileList(self
, FileType
):
246 RecordList
= self
.Exec("select FullPath from %s where Model=%s" % (self
.Table
, FileType
))
247 if len(RecordList
) == 0:
249 return [R
[0] for R
in RecordList
]
253 # This class defined a table used for data model
255 # @param object: Inherited from object class
258 class TableDataModel(Table
):
260 ID INTEGER PRIMARY KEY,
261 CrossIndex INTEGER NOT NULL,
262 Name VARCHAR NOT NULL,
265 def __init__(self
, Cursor
):
266 Table
.__init
__(self
, Cursor
, 'DataModel')
270 # Insert a record into table DataModel
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
277 def Insert(self
, CrossIndex
, Name
, Description
):
278 (Name
, Description
) = ConvertToSqlString((Name
, Description
))
279 return Table
.Insert(self
, CrossIndex
, Name
, Description
)
283 # Create all default records of table DataModel
286 EdkLogger
.verbose("\nInitialize table DataModel started ...")
287 Count
= self
.GetCount()
288 if Count
is not None and Count
!= 0:
290 for Item
in DataClass
.MODEL_LIST
:
293 Description
= Item
[0]
294 self
.Insert(CrossIndex
, Name
, Description
)
295 EdkLogger
.verbose("Initialize table DataModel ... DONE!")
299 # Get a model's cross index from its name
301 # @param ModelName: Name of the model
302 # @retval CrossIndex: CrossIndex of the model
304 def GetCrossIndex(self
, ModelName
):
306 SqlCommand
= """select CrossIndex from DataModel where name = '""" + ModelName
+ """'"""
307 self
.Db
.execute(SqlCommand
)