]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/MetaFileWorkspace/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 from __future__
import print_function
12 import Common
.LongFilePathOs
as os
14 import Common
.EdkLogger
as EdkLogger
15 from CommonDataClass
import DataClass
16 from CommonDataClass
.DataClass
import FileClass
18 ## Convert to SQL required string format
19 def ConvertToSqlString(StringList
):
20 return map(lambda s
: "'" + s
.replace("'", "''") + "'", StringList
)
24 # This class defined a common table
26 # @param object: Inherited from object class
28 # @param Cursor: Cursor of the database
29 # @param TableName: Name of the table
37 def __init__(self
, Cursor
, Name
='', IdBase
=0, Temporary
=False):
40 self
.IdBase
= int(IdBase
)
42 self
.Temporary
= Temporary
51 def Create(self
, NewTable
=True):
56 SqlCommand
= """create temp table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
58 SqlCommand
= """create table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
59 EdkLogger
.debug(EdkLogger
.DEBUG_8
, SqlCommand
)
60 self
.Cur
.execute(SqlCommand
)
61 self
.ID
= self
.GetId()
65 # Insert a record into a table
67 def Insert(self
, *Args
):
68 self
.ID
= self
.ID
+ self
._ID
_STEP
_
69 if self
.ID
>= (self
.IdBase
+ self
._ID
_MAX
_):
70 self
.ID
= self
.IdBase
+ self
._ID
_STEP
_
71 Values
= ", ".join(str(Arg
) for Arg
in Args
)
72 SqlCommand
= "insert into %s values(%s, %s)" % (self
.Table
, self
.ID
, Values
)
73 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
74 self
.Cur
.execute(SqlCommand
)
79 # Query all records of the table
82 SqlCommand
= """select * from %s""" % self
.Table
83 self
.Cur
.execute(SqlCommand
)
85 EdkLogger
.verbose(str(Rs
))
86 TotalCount
= self
.GetId()
93 SqlCommand
= """drop table IF EXISTS %s""" % self
.Table
95 self
.Cur
.execute(SqlCommand
)
96 except Exception as e
:
97 print("An error occurred when Drop a table:", e
.args
[0])
101 # Get a count of all records of the table
103 # @retval Count: Total count of all records
106 SqlCommand
= """select count(ID) from %s""" % self
.Table
107 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
111 SqlCommand
= """select max(ID) from %s""" % self
.Table
112 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
118 ## Init the ID of the table
120 # Init the ID of the table
123 self
.ID
= self
.GetId()
127 # Exec Sql Command, return result
129 # @param SqlCommand: The SqlCommand to be executed
131 # @retval RecordSet: The result after executed
133 def Exec(self
, SqlCommand
):
134 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
135 self
.Cur
.execute(SqlCommand
)
136 RecordSet
= self
.Cur
.fetchall()
139 def SetEndFlag(self
):
142 def IsIntegral(self
):
143 Result
= self
.Exec("select min(ID) from %s" % (self
.Table
))
144 if Result
[0][0] != -1:
149 return self
.Exec("select * from %s where ID > 0 order by ID" % (self
.Table
))
154 # This class defined a table used for data model
156 # @param object: Inherited from object class
159 class TableDataModel(Table
):
161 ID INTEGER PRIMARY KEY,
162 CrossIndex INTEGER NOT NULL,
163 Name VARCHAR NOT NULL,
166 def __init__(self
, Cursor
):
167 Table
.__init
__(self
, Cursor
, 'DataModel')
171 # Insert a record into table DataModel
173 # @param ID: ID of a ModelType
174 # @param CrossIndex: CrossIndex of a ModelType
175 # @param Name: Name of a ModelType
176 # @param Description: Description of a ModelType
178 def Insert(self
, CrossIndex
, Name
, Description
):
179 (Name
, Description
) = ConvertToSqlString((Name
, Description
))
180 return Table
.Insert(self
, CrossIndex
, Name
, Description
)
184 # Create all default records of table DataModel
187 EdkLogger
.verbose("\nInitialize table DataModel started ...")
188 Count
= self
.GetCount()
189 if Count
is not None and Count
!= 0:
191 for Item
in DataClass
.MODEL_LIST
:
194 Description
= Item
[0]
195 self
.Insert(CrossIndex
, Name
, Description
)
196 EdkLogger
.verbose("Initialize table DataModel ... DONE!")
200 # Get a model's cross index from its name
202 # @param ModelName: Name of the model
203 # @retval CrossIndex: CrossIndex of the model
205 def GetCrossIndex(self
, ModelName
):
207 SqlCommand
= """select CrossIndex from DataModel where name = '""" + ModelName
+ """'"""
208 self
.Cur
.execute(SqlCommand
)
209 for Item
in self
.Cur
: