]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaDataTable.py
BaseTools: Refactor python print statements
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / MetaFileWorkspace / MetaDataTable.py
CommitLineData
d0acc87a
LG
1## @file\r
2# This file is used to create/update/query/erase table for files\r
3#\r
8252e6bf 4# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
d0acc87a
LG
5# This program and the accompanying materials\r
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14##\r
15# Import Modules\r
16#\r
72443dd2 17from __future__ import print_function\r
1be2ed90 18import Common.LongFilePathOs as os\r
d0acc87a
LG
19\r
20import Common.EdkLogger as EdkLogger\r
21from CommonDataClass import DataClass\r
22from CommonDataClass.DataClass import FileClass\r
23\r
24## Convert to SQL required string format\r
25def ConvertToSqlString(StringList):\r
26 return map(lambda s: "'" + s.replace("'", "''") + "'", StringList)\r
27\r
28## TableFile\r
29#\r
30# This class defined a common table\r
31#\r
32# @param object: Inherited from object class\r
33#\r
34# @param Cursor: Cursor of the database\r
35# @param TableName: Name of the table\r
36#\r
37class Table(object):\r
38 _COLUMN_ = ''\r
39 _ID_STEP_ = 1\r
40 _ID_MAX_ = 0x80000000\r
41 _DUMMY_ = 0\r
42\r
43 def __init__(self, Cursor, Name='', IdBase=0, Temporary=False):\r
44 self.Cur = Cursor\r
45 self.Table = Name\r
46 self.IdBase = int(IdBase)\r
47 self.ID = int(IdBase)\r
48 self.Temporary = Temporary\r
49\r
50 def __str__(self):\r
51 return self.Table\r
52\r
53 ## Create table\r
54 #\r
55 # Create a table\r
56 #\r
57 def Create(self, NewTable=True):\r
58 if NewTable:\r
59 self.Drop()\r
60\r
61 if self.Temporary:\r
62 SqlCommand = """create temp table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)\r
63 else:\r
64 SqlCommand = """create table IF NOT EXISTS %s (%s)""" % (self.Table, self._COLUMN_)\r
65 EdkLogger.debug(EdkLogger.DEBUG_8, SqlCommand)\r
66 self.Cur.execute(SqlCommand)\r
67 self.ID = self.GetId()\r
68\r
69 ## Insert table\r
70 #\r
71 # Insert a record into a table\r
72 #\r
73 def Insert(self, *Args):\r
74 self.ID = self.ID + self._ID_STEP_\r
75 if self.ID >= (self.IdBase + self._ID_MAX_):\r
76 self.ID = self.IdBase + self._ID_STEP_\r
8252e6bf 77 Values = ", ".join(str(Arg) for Arg in Args)\r
d0acc87a
LG
78 SqlCommand = "insert into %s values(%s, %s)" % (self.Table, self.ID, Values)\r
79 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)\r
80 self.Cur.execute(SqlCommand)\r
81 return self.ID\r
82\r
83 ## Query table\r
84 #\r
85 # Query all records of the table\r
86 #\r
87 def Query(self):\r
88 SqlCommand = """select * from %s""" % self.Table\r
89 self.Cur.execute(SqlCommand)\r
90 for Rs in self.Cur:\r
91 EdkLogger.verbose(str(Rs))\r
92 TotalCount = self.GetId()\r
93\r
94 ## Drop a table\r
95 #\r
96 # Drop the table\r
97 #\r
98 def Drop(self):\r
99 SqlCommand = """drop table IF EXISTS %s""" % self.Table\r
64b2609f
LG
100 try:\r
101 self.Cur.execute(SqlCommand)\r
5b0671c1 102 except Exception as e:\r
72443dd2 103 print("An error occurred when Drop a table:", e.args[0])\r
d0acc87a
LG
104\r
105 ## Get count\r
106 #\r
107 # Get a count of all records of the table\r
108 #\r
109 # @retval Count: Total count of all records\r
110 #\r
111 def GetCount(self):\r
112 SqlCommand = """select count(ID) from %s""" % self.Table\r
113 Record = self.Cur.execute(SqlCommand).fetchall()\r
114 return Record[0][0]\r
115\r
116 def GetId(self):\r
117 SqlCommand = """select max(ID) from %s""" % self.Table\r
118 Record = self.Cur.execute(SqlCommand).fetchall()\r
119 Id = Record[0][0]\r
4231a819 120 if Id is None:\r
d0acc87a
LG
121 Id = self.IdBase\r
122 return Id\r
123\r
124 ## Init the ID of the table\r
125 #\r
126 # Init the ID of the table\r
127 #\r
128 def InitID(self):\r
129 self.ID = self.GetId()\r
130\r
131 ## Exec\r
132 #\r
133 # Exec Sql Command, return result\r
134 #\r
135 # @param SqlCommand: The SqlCommand to be executed\r
136 #\r
137 # @retval RecordSet: The result after executed\r
138 #\r
139 def Exec(self, SqlCommand):\r
140 EdkLogger.debug(EdkLogger.DEBUG_5, SqlCommand)\r
141 self.Cur.execute(SqlCommand)\r
142 RecordSet = self.Cur.fetchall()\r
143 return RecordSet\r
144\r
145 def SetEndFlag(self):\r
146 pass\r
147\r
148 def IsIntegral(self):\r
149 Result = self.Exec("select min(ID) from %s" % (self.Table))\r
150 if Result[0][0] != -1:\r
151 return False\r
152 return True\r
153\r
154 def GetAll(self):\r
155 return self.Exec("select * from %s where ID > 0 order by ID" % (self.Table))\r
156\r
157\r
158## TableDataModel\r
159#\r
160# This class defined a table used for data model\r
161#\r
162# @param object: Inherited from object class\r
163#\r
164#\r
165class TableDataModel(Table):\r
166 _COLUMN_ = """\r
167 ID INTEGER PRIMARY KEY,\r
168 CrossIndex INTEGER NOT NULL,\r
169 Name VARCHAR NOT NULL,\r
170 Description VARCHAR\r
171 """\r
172 def __init__(self, Cursor):\r
173 Table.__init__(self, Cursor, 'DataModel')\r
174\r
175 ## Insert table\r
176 #\r
177 # Insert a record into table DataModel\r
178 #\r
179 # @param ID: ID of a ModelType\r
180 # @param CrossIndex: CrossIndex of a ModelType\r
181 # @param Name: Name of a ModelType\r
182 # @param Description: Description of a ModelType\r
183 #\r
184 def Insert(self, CrossIndex, Name, Description):\r
185 (Name, Description) = ConvertToSqlString((Name, Description))\r
186 return Table.Insert(self, CrossIndex, Name, Description)\r
187\r
188 ## Init table\r
189 #\r
190 # Create all default records of table DataModel\r
191 #\r
192 def InitTable(self):\r
193 EdkLogger.verbose("\nInitialize table DataModel started ...")\r
194 Count = self.GetCount()\r
4231a819 195 if Count is not None and Count != 0:\r
d0acc87a
LG
196 return\r
197 for Item in DataClass.MODEL_LIST:\r
198 CrossIndex = Item[1]\r
199 Name = Item[0]\r
200 Description = Item[0]\r
201 self.Insert(CrossIndex, Name, Description)\r
202 EdkLogger.verbose("Initialize table DataModel ... DONE!")\r
203\r
204 ## Get CrossIndex\r
205 #\r
206 # Get a model's cross index from its name\r
207 #\r
208 # @param ModelName: Name of the model\r
209 # @retval CrossIndex: CrossIndex of the model\r
210 #\r
211 def GetCrossIndex(self, ModelName):\r
212 CrossIndex = -1\r
213 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""\r
214 self.Cur.execute(SqlCommand)\r
215 for Item in self.Cur:\r
216 CrossIndex = Item[0]\r
217\r
218 return CrossIndex\r
219\r