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