]>
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, Intel Corporation
5 # All rights reserved. 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.
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
, Cursor
, Name
='', IdBase
=0, Temporary
=False):
45 self
.IdBase
= int(IdBase
)
47 self
.Temporary
= Temporary
56 def Create(self
, NewTable
=True):
61 SqlCommand
= """create temp table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
63 SqlCommand
= """create table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
64 EdkLogger
.debug(EdkLogger
.DEBUG_8
, SqlCommand
)
65 self
.Cur
.execute(SqlCommand
)
66 self
.ID
= self
.GetId()
70 # Insert a record into a table
72 def Insert(self
, *Args
):
73 self
.ID
= self
.ID
+ self
._ID
_STEP
_
74 if self
.ID
>= (self
.IdBase
+ self
._ID
_MAX
_):
75 self
.ID
= self
.IdBase
+ self
._ID
_STEP
_
76 Values
= ", ".join([str(Arg
) for Arg
in Args
])
77 SqlCommand
= "insert into %s values(%s, %s)" % (self
.Table
, self
.ID
, Values
)
78 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
79 self
.Cur
.execute(SqlCommand
)
84 # Query all records of the table
87 SqlCommand
= """select * from %s""" % self
.Table
88 self
.Cur
.execute(SqlCommand
)
90 EdkLogger
.verbose(str(Rs
))
91 TotalCount
= self
.GetId()
98 SqlCommand
= """drop table IF EXISTS %s""" % self
.Table
99 self
.Cur
.execute(SqlCommand
)
103 # Get a count of all records of the table
105 # @retval Count: Total count of all records
108 SqlCommand
= """select count(ID) from %s""" % self
.Table
109 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
113 SqlCommand
= """select max(ID) from %s""" % self
.Table
114 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
120 ## Init the ID of the table
122 # Init the ID of the table
125 self
.ID
= self
.GetId()
129 # Exec Sql Command, return result
131 # @param SqlCommand: The SqlCommand to be executed
133 # @retval RecordSet: The result after executed
135 def Exec(self
, SqlCommand
):
136 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
137 self
.Cur
.execute(SqlCommand
)
138 RecordSet
= self
.Cur
.fetchall()
141 def SetEndFlag(self
):
142 self
.Exec("insert into %s values(%s)" % (self
.Table
, self
._DUMMY
_))
144 def IsIntegral(self
):
145 Result
= self
.Exec("select min(ID) from %s" % (self
.Table
))
146 if Result
[0][0] != -1:
152 # This class defined a table used for file
154 # @param object: Inherited from object class
156 class TableFile(Table
):
158 ID INTEGER PRIMARY KEY,
159 Name VARCHAR NOT NULL,
162 FullPath VARCHAR NOT NULL,
163 Model INTEGER DEFAULT 0,
164 TimeStamp SINGLE NOT NULL
166 def __init__(self
, Cursor
):
167 Table
.__init
__(self
, Cursor
, 'File')
171 # Insert a record into table File
173 # @param Name: Name of a File
174 # @param ExtName: ExtName of a File
175 # @param Path: Path of a File
176 # @param FullPath: FullPath of a File
177 # @param Model: Model of a File
178 # @param TimeStamp: TimeStamp of a File
180 def Insert(self
, Name
, ExtName
, Path
, FullPath
, Model
, TimeStamp
):
181 (Name
, ExtName
, Path
, FullPath
) = ConvertToSqlString((Name
, ExtName
, Path
, FullPath
))
194 # Insert one file to table
196 # @param FileFullPath: The full path of the file
197 # @param Model: The model of the file
199 # @retval FileID: The ID after record is inserted
201 def InsertFile(self
, FileFullPath
, Model
):
202 (Filepath
, Name
) = os
.path
.split(FileFullPath
)
203 (Root
, Ext
) = os
.path
.splitext(FileFullPath
)
204 TimeStamp
= os
.stat(FileFullPath
)[8]
205 File
= FileClass(-1, Name
, Ext
, Filepath
, FileFullPath
, Model
, '', [], [], [])
215 ## Get ID of a given file
217 # @param FilePath Path of file
219 # @retval ID ID value of given file in the table
221 def GetFileId(self
, FilePath
):
222 QueryScript
= "select ID from %s where FullPath = '%s'" % (self
.Table
, FilePath
)
223 RecordList
= self
.Exec(QueryScript
)
224 if len(RecordList
) == 0:
226 return RecordList
[0][0]
228 ## Get type of a given file
230 # @param FileId ID of a file
232 # @retval file_type Model value of given file in the table
234 def GetFileType(self
, FileId
):
235 QueryScript
= "select Model from %s where ID = '%s'" % (self
.Table
, FileId
)
236 RecordList
= self
.Exec(QueryScript
)
237 if len(RecordList
) == 0:
239 return RecordList
[0][0]
241 ## Get file timestamp of a given file
243 # @param FileId ID of file
245 # @retval timestamp TimeStamp value of given file in the table
247 def GetFileTimeStamp(self
, FileId
):
248 QueryScript
= "select TimeStamp from %s where ID = '%s'" % (self
.Table
, FileId
)
249 RecordList
= self
.Exec(QueryScript
)
250 if len(RecordList
) == 0:
252 return RecordList
[0][0]
254 ## Update the timestamp of a given file
256 # @param FileId ID of file
257 # @param TimeStamp Time stamp of file
259 def SetFileTimeStamp(self
, FileId
, TimeStamp
):
260 self
.Exec("update %s set TimeStamp=%s where ID='%s'" % (self
.Table
, TimeStamp
, FileId
))
262 ## Get list of file with given type
264 # @param FileType Type value of file
266 # @retval file_list List of files with the given type
268 def GetFileList(self
, FileType
):
269 RecordList
= self
.Exec("select FullPath from %s where Model=%s" % (self
.Table
, FileType
))
270 if len(RecordList
) == 0:
272 return [R
[0] for R
in RecordList
]
276 # This class defined a table used for data model
278 # @param object: Inherited from object class
281 class TableDataModel(Table
):
283 ID INTEGER PRIMARY KEY,
284 CrossIndex INTEGER NOT NULL,
285 Name VARCHAR NOT NULL,
288 def __init__(self
, Cursor
):
289 Table
.__init
__(self
, Cursor
, 'DataModel')
293 # Insert a record into table DataModel
295 # @param ID: ID of a ModelType
296 # @param CrossIndex: CrossIndex of a ModelType
297 # @param Name: Name of a ModelType
298 # @param Description: Description of a ModelType
300 def Insert(self
, CrossIndex
, Name
, Description
):
301 (Name
, Description
) = ConvertToSqlString((Name
, Description
))
302 return Table
.Insert(self
, CrossIndex
, Name
, Description
)
306 # Create all default records of table DataModel
309 EdkLogger
.verbose("\nInitialize table DataModel started ...")
310 Count
= self
.GetCount()
311 if Count
!= None and Count
!= 0:
313 for Item
in DataClass
.MODEL_LIST
:
316 Description
= Item
[0]
317 self
.Insert(CrossIndex
, Name
, Description
)
318 EdkLogger
.verbose("Initialize table DataModel ... DONE!")
322 # Get a model's cross index from its name
324 # @param ModelName: Name of the model
325 # @retval CrossIndex: CrossIndex of the model
327 def GetCrossIndex(self
, ModelName
):
329 SqlCommand
= """select CrossIndex from DataModel where name = '""" + ModelName
+ """'"""
330 self
.Cur
.execute(SqlCommand
)
331 for Item
in self
.Cur
: