]>
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 # 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 from __future__
import print_function
18 import Common
.LongFilePathOs
as os
20 import Common
.EdkLogger
as EdkLogger
21 from CommonDataClass
import DataClass
22 from CommonDataClass
.DataClass
import FileClass
24 ## Convert to SQL required string format
25 def ConvertToSqlString(StringList
):
26 return map(lambda s
: "'" + s
.replace("'", "''") + "'", StringList
)
30 # This class defined a common table
32 # @param object: Inherited from object class
34 # @param Cursor: Cursor of the database
35 # @param TableName: Name of the table
43 def __init__(self
, Cursor
, Name
='', IdBase
=0, Temporary
=False):
46 self
.IdBase
= int(IdBase
)
48 self
.Temporary
= Temporary
57 def Create(self
, NewTable
=True):
62 SqlCommand
= """create temp table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
64 SqlCommand
= """create table IF NOT EXISTS %s (%s)""" % (self
.Table
, self
._COLUMN
_)
65 EdkLogger
.debug(EdkLogger
.DEBUG_8
, SqlCommand
)
66 self
.Cur
.execute(SqlCommand
)
67 self
.ID
= self
.GetId()
71 # Insert a record into a table
73 def Insert(self
, *Args
):
74 self
.ID
= self
.ID
+ self
._ID
_STEP
_
75 if self
.ID
>= (self
.IdBase
+ self
._ID
_MAX
_):
76 self
.ID
= self
.IdBase
+ self
._ID
_STEP
_
77 Values
= ", ".join(str(Arg
) for Arg
in Args
)
78 SqlCommand
= "insert into %s values(%s, %s)" % (self
.Table
, self
.ID
, Values
)
79 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
80 self
.Cur
.execute(SqlCommand
)
85 # Query all records of the table
88 SqlCommand
= """select * from %s""" % self
.Table
89 self
.Cur
.execute(SqlCommand
)
91 EdkLogger
.verbose(str(Rs
))
92 TotalCount
= self
.GetId()
99 SqlCommand
= """drop table IF EXISTS %s""" % self
.Table
101 self
.Cur
.execute(SqlCommand
)
102 except Exception as e
:
103 print("An error occurred when Drop a table:", e
.args
[0])
107 # Get a count of all records of the table
109 # @retval Count: Total count of all records
112 SqlCommand
= """select count(ID) from %s""" % self
.Table
113 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
117 SqlCommand
= """select max(ID) from %s""" % self
.Table
118 Record
= self
.Cur
.execute(SqlCommand
).fetchall()
124 ## Init the ID of the table
126 # Init the ID of the table
129 self
.ID
= self
.GetId()
133 # Exec Sql Command, return result
135 # @param SqlCommand: The SqlCommand to be executed
137 # @retval RecordSet: The result after executed
139 def Exec(self
, SqlCommand
):
140 EdkLogger
.debug(EdkLogger
.DEBUG_5
, SqlCommand
)
141 self
.Cur
.execute(SqlCommand
)
142 RecordSet
= self
.Cur
.fetchall()
145 def SetEndFlag(self
):
148 def IsIntegral(self
):
149 Result
= self
.Exec("select min(ID) from %s" % (self
.Table
))
150 if Result
[0][0] != -1:
155 return self
.Exec("select * from %s where ID > 0 order by ID" % (self
.Table
))
160 # This class defined a table used for data model
162 # @param object: Inherited from object class
165 class TableDataModel(Table
):
167 ID INTEGER PRIMARY KEY,
168 CrossIndex INTEGER NOT NULL,
169 Name VARCHAR NOT NULL,
172 def __init__(self
, Cursor
):
173 Table
.__init
__(self
, Cursor
, 'DataModel')
177 # Insert a record into table DataModel
179 # @param ID: ID of a ModelType
180 # @param CrossIndex: CrossIndex of a ModelType
181 # @param Name: Name of a ModelType
182 # @param Description: Description of a ModelType
184 def Insert(self
, CrossIndex
, Name
, Description
):
185 (Name
, Description
) = ConvertToSqlString((Name
, Description
))
186 return Table
.Insert(self
, CrossIndex
, Name
, Description
)
190 # Create all default records of table DataModel
193 EdkLogger
.verbose("\nInitialize table DataModel started ...")
194 Count
= self
.GetCount()
195 if Count
is not None and Count
!= 0:
197 for Item
in DataClass
.MODEL_LIST
:
200 Description
= Item
[0]
201 self
.Insert(CrossIndex
, Name
, Description
)
202 EdkLogger
.verbose("Initialize table DataModel ... DONE!")
206 # Get a model's cross index from its name
208 # @param ModelName: Name of the model
209 # @retval CrossIndex: CrossIndex of the model
211 def GetCrossIndex(self
, ModelName
):
213 SqlCommand
= """select CrossIndex from DataModel where name = '""" + ModelName
+ """'"""
214 self
.Cur
.execute(SqlCommand
)
215 for Item
in self
.Cur
: