]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaDataTable.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / MetaFileWorkspace / MetaDataTable.py
1 ## @file
2 # This file is used to create/update/query/erase table for files
3 #
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 ##
9 # Import Modules
10 #
11 from __future__ import print_function
12 import Common.LongFilePathOs as os
13
14 import Common.EdkLogger as EdkLogger
15 from CommonDataClass import DataClass
16 from CommonDataClass.DataClass import FileClass
17
18 ## Convert to SQL required string format
19 def ConvertToSqlString(StringList):
20 return map(lambda s: "'" + s.replace("'", "''") + "'", StringList)
21
22 ## TableFile
23 #
24 # This class defined a common table
25 #
26 # @param object: Inherited from object class
27 #
28 # @param Cursor: Cursor of the database
29 # @param TableName: Name of the table
30 #
31 class Table(object):
32 _COLUMN_ = ''
33 _ID_STEP_ = 1
34 _ID_MAX_ = 0x80000000
35 _DUMMY_ = 0
36
37 def __init__(self, Cursor, Name='', IdBase=0, Temporary=False):
38 self.Cur = Cursor
39 self.Table = Name
40 self.IdBase = int(IdBase)
41 self.ID = int(IdBase)
42 self.Temporary = Temporary
43
44 def __str__(self):
45 return self.Table
46
47 ## Create table
48 #
49 # Create a table
50 #
51 def Create(self, NewTable=True):
52 if NewTable:
53 self.Drop()
54
55 if self.Temporary:
56 SqlCommand = """create temp table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)
57 else:
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()
62
63 ## Insert table
64 #
65 # Insert a record into a table
66 #
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)
75 return self.ID
76
77 ## Query table
78 #
79 # Query all records of the table
80 #
81 def Query(self):
82 SqlCommand = """select * from %s""" % self.Table
83 self.Cur.execute(SqlCommand)
84 for Rs in self.Cur:
85 EdkLogger.verbose(str(Rs))
86 TotalCount = self.GetId()
87
88 ## Drop a table
89 #
90 # Drop the table
91 #
92 def Drop(self):
93 SqlCommand = """drop table IF EXISTS %s""" % self.Table
94 try:
95 self.Cur.execute(SqlCommand)
96 except Exception as e:
97 print("An error occurred when Drop a table:", e.args[0])
98
99 ## Get count
100 #
101 # Get a count of all records of the table
102 #
103 # @retval Count: Total count of all records
104 #
105 def GetCount(self):
106 SqlCommand = """select count(ID) from %s""" % self.Table
107 Record = self.Cur.execute(SqlCommand).fetchall()
108 return Record[0][0]
109
110 def GetId(self):
111 SqlCommand = """select max(ID) from %s""" % self.Table
112 Record = self.Cur.execute(SqlCommand).fetchall()
113 Id = Record[0][0]
114 if Id is None:
115 Id = self.IdBase
116 return Id
117
118 ## Init the ID of the table
119 #
120 # Init the ID of the table
121 #
122 def InitID(self):
123 self.ID = self.GetId()
124
125 ## Exec
126 #
127 # Exec Sql Command, return result
128 #
129 # @param SqlCommand: The SqlCommand to be executed
130 #
131 # @retval RecordSet: The result after executed
132 #
133 def Exec(self, SqlCommand):
134 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)
135 self.Cur.execute(SqlCommand)
136 RecordSet = self.Cur.fetchall()
137 return RecordSet
138
139 def SetEndFlag(self):
140 pass
141
142 def IsIntegral(self):
143 Result = self.Exec("select min(ID) from %s" % (self.Table))
144 if Result[0][0] != -1:
145 return False
146 return True
147
148 def GetAll(self):
149 return self.Exec("select * from %s where ID > 0 order by ID" % (self.Table))
150
151
152 ## TableDataModel
153 #
154 # This class defined a table used for data model
155 #
156 # @param object: Inherited from object class
157 #
158 #
159 class TableDataModel(Table):
160 _COLUMN_ = """
161 ID INTEGER PRIMARY KEY,
162 CrossIndex INTEGER NOT NULL,
163 Name VARCHAR NOT NULL,
164 Description VARCHAR
165 """
166 def __init__(self, Cursor):
167 Table.__init__(self, Cursor, 'DataModel')
168
169 ## Insert table
170 #
171 # Insert a record into table DataModel
172 #
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
177 #
178 def Insert(self, CrossIndex, Name, Description):
179 (Name, Description) = ConvertToSqlString((Name, Description))
180 return Table.Insert(self, CrossIndex, Name, Description)
181
182 ## Init table
183 #
184 # Create all default records of table DataModel
185 #
186 def InitTable(self):
187 EdkLogger.verbose("\nInitialize table DataModel started ...")
188 Count = self.GetCount()
189 if Count is not None and Count != 0:
190 return
191 for Item in DataClass.MODEL_LIST:
192 CrossIndex = Item[1]
193 Name = Item[0]
194 Description = Item[0]
195 self.Insert(CrossIndex, Name, Description)
196 EdkLogger.verbose("Initialize table DataModel ... DONE!")
197
198 ## Get CrossIndex
199 #
200 # Get a model's cross index from its name
201 #
202 # @param ModelName: Name of the model
203 # @retval CrossIndex: CrossIndex of the model
204 #
205 def GetCrossIndex(self, ModelName):
206 CrossIndex = -1
207 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
208 self.Cur.execute(SqlCommand)
209 for Item in self.Cur:
210 CrossIndex = Item[0]
211
212 return CrossIndex
213