]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # This file is used to create a database used by ECC tool\r | |
3 | #\r | |
4 | # Copyright (c) 2007 ~ 2008, Intel Corporation\r | |
5 | # All rights reserved. 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 | |
17 | import sqlite3\r | |
18 | import os, time\r | |
19 | \r | |
20 | import Common.EdkLogger as EdkLogger\r | |
21 | import CommonDataClass.DataClass as DataClass\r | |
22 | \r | |
23 | from Table.TableDataModel import TableDataModel\r | |
24 | from Table.TableFile import TableFile\r | |
25 | from Table.TableFunction import TableFunction\r | |
26 | from Table.TablePcd import TablePcd\r | |
27 | from Table.TableIdentifier import TableIdentifier\r | |
28 | from Table.TableReport import TableReport\r | |
29 | from Table.TableInf import TableInf\r | |
30 | from Table.TableDec import TableDec\r | |
31 | from Table.TableDsc import TableDsc\r | |
32 | from Table.TableFdf import TableFdf\r | |
33 | \r | |
34 | ##\r | |
35 | # Static definitions\r | |
36 | #\r | |
37 | DATABASE_PATH = "Ecc.db"\r | |
38 | \r | |
39 | ## Database\r | |
40 | #\r | |
41 | # This class defined the ECC databse\r | |
42 | # During the phase of initialization, the database will create all tables and\r | |
43 | # insert all records of table DataModel\r | |
44 | # \r | |
45 | # @param object: Inherited from object class\r | |
46 | # @param DbPath: A string for the path of the ECC database\r | |
47 | #\r | |
48 | # @var Conn: Connection of the ECC database\r | |
49 | # @var Cur: Cursor of the connection\r | |
50 | # @var TblDataModel: Local instance for TableDataModel\r | |
51 | #\r | |
52 | class Database(object):\r | |
53 | def __init__(self, DbPath):\r | |
54 | self.DbPath = DbPath\r | |
55 | self.Conn = None\r | |
56 | self.Cur = None\r | |
57 | self.TblDataModel = None\r | |
58 | self.TblFile = None\r | |
59 | self.TblFunction = None\r | |
60 | self.TblIdentifier = None\r | |
61 | self.TblPcd = None\r | |
62 | self.TblReport = None\r | |
63 | self.TblInf = None\r | |
64 | self.TblDec = None\r | |
65 | self.TblDsc = None\r | |
66 | self.TblFdf = None\r | |
67 | \r | |
68 | ## Initialize ECC database\r | |
69 | #\r | |
70 | # 1. Delete all old existing tables\r | |
71 | # 2. Create new tables\r | |
72 | # 3. Initialize table DataModel\r | |
73 | #\r | |
74 | def InitDatabase(self, NewDatabase = True):\r | |
75 | EdkLogger.verbose("\nInitialize ECC database started ...")\r | |
76 | #\r | |
77 | # Drop all old existing tables\r | |
78 | #\r | |
79 | if NewDatabase:\r | |
80 | if os.path.exists(self.DbPath):\r | |
81 | os.remove(self.DbPath)\r | |
82 | self.Conn = sqlite3.connect(self.DbPath, isolation_level = 'DEFERRED')\r | |
83 | self.Conn.execute("PRAGMA page_size=4096")\r | |
84 | self.Conn.execute("PRAGMA synchronous=OFF")\r | |
85 | # to avoid non-ascii charater conversion error\r | |
86 | self.Conn.text_factory = str\r | |
87 | self.Cur = self.Conn.cursor()\r | |
88 | \r | |
89 | self.TblDataModel = TableDataModel(self.Cur)\r | |
90 | self.TblFile = TableFile(self.Cur)\r | |
91 | self.TblFunction = TableFunction(self.Cur)\r | |
92 | self.TblIdentifier = TableIdentifier(self.Cur)\r | |
93 | self.TblPcd = TablePcd(self.Cur)\r | |
94 | self.TblReport = TableReport(self.Cur)\r | |
95 | self.TblInf = TableInf(self.Cur)\r | |
96 | self.TblDec = TableDec(self.Cur)\r | |
97 | self.TblDsc = TableDsc(self.Cur)\r | |
98 | self.TblFdf = TableFdf(self.Cur)\r | |
99 | \r | |
100 | #\r | |
101 | # Create new tables\r | |
102 | #\r | |
103 | if NewDatabase:\r | |
104 | self.TblDataModel.Create()\r | |
105 | self.TblFile.Create()\r | |
106 | self.TblFunction.Create()\r | |
107 | self.TblPcd.Create()\r | |
108 | self.TblReport.Create()\r | |
109 | self.TblInf.Create()\r | |
110 | self.TblDec.Create()\r | |
111 | self.TblDsc.Create()\r | |
112 | self.TblFdf.Create()\r | |
113 | \r | |
114 | #\r | |
115 | # Init each table's ID\r | |
116 | #\r | |
117 | self.TblDataModel.InitID()\r | |
118 | self.TblFile.InitID()\r | |
119 | self.TblFunction.InitID()\r | |
120 | self.TblPcd.InitID()\r | |
121 | self.TblReport.InitID()\r | |
122 | self.TblInf.InitID()\r | |
123 | self.TblDec.InitID()\r | |
124 | self.TblDsc.InitID()\r | |
125 | self.TblFdf.InitID()\r | |
126 | \r | |
127 | #\r | |
128 | # Initialize table DataModel\r | |
129 | #\r | |
130 | if NewDatabase:\r | |
131 | self.TblDataModel.InitTable()\r | |
132 | \r | |
133 | EdkLogger.verbose("Initialize ECC database ... DONE!")\r | |
134 | \r | |
135 | ## Query a table\r | |
136 | #\r | |
137 | # @param Table: The instance of the table to be queried\r | |
138 | #\r | |
139 | def QueryTable(self, Table):\r | |
140 | Table.Query()\r | |
141 | \r | |
142 | ## Close entire database\r | |
143 | #\r | |
144 | # Commit all first\r | |
145 | # Close the connection and cursor\r | |
146 | #\r | |
147 | def Close(self):\r | |
148 | #\r | |
149 | # Commit to file\r | |
150 | # \r | |
151 | self.Conn.commit()\r | |
152 | \r | |
153 | #\r | |
154 | # Close connection and cursor\r | |
155 | #\r | |
156 | self.Cur.close()\r | |
157 | self.Conn.close()\r | |
158 | \r | |
159 | ## Insert one file information\r | |
160 | #\r | |
161 | # Insert one file's information to the database\r | |
162 | # 1. Create a record in TableFile\r | |
163 | # 2. Create functions one by one\r | |
164 | # 2.1 Create variables of function one by one\r | |
165 | # 2.2 Create pcds of function one by one\r | |
166 | # 3. Create variables one by one\r | |
167 | # 4. Create pcds one by one\r | |
168 | #\r | |
169 | def InsertOneFile(self, File):\r | |
170 | #\r | |
171 | # Insert a record for file\r | |
172 | #\r | |
173 | FileID = self.TblFile.Insert(File.Name, File.ExtName, File.Path, File.FullPath, Model = File.Model, TimeStamp = File.TimeStamp)\r | |
174 | IdTable = TableIdentifier(self.Cur)\r | |
175 | IdTable.Table = "Identifier%s" % FileID\r | |
176 | IdTable.Create()\r | |
177 | \r | |
178 | #\r | |
179 | # Insert function of file\r | |
180 | #\r | |
181 | for Function in File.FunctionList:\r | |
182 | FunctionID = self.TblFunction.Insert(Function.Header, Function.Modifier, Function.Name, Function.ReturnStatement, \\r | |
183 | Function.StartLine, Function.StartColumn, Function.EndLine, Function.EndColumn, \\r | |
184 | Function.BodyStartLine, Function.BodyStartColumn, FileID, \\r | |
185 | Function.FunNameStartLine, Function.FunNameStartColumn)\r | |
186 | #\r | |
187 | # Insert Identifier of function\r | |
188 | #\r | |
189 | for Identifier in Function.IdentifierList:\r | |
190 | IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \\r | |
191 | FileID, FunctionID, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)\r | |
192 | #\r | |
193 | # Insert Pcd of function\r | |
194 | #\r | |
195 | for Pcd in Function.PcdList:\r | |
196 | PcdID = self.TblPcd.Insert(Pcd.CName, Pcd.TokenSpaceGuidCName, Pcd.Token, Pcd.DatumType, Pcd.Model, \\r | |
197 | FileID, FunctionID, Pcd.StartLine, Pcd.StartColumn, Pcd.EndLine, Pcd.EndColumn)\r | |
198 | #\r | |
199 | # Insert Identifier of file\r | |
200 | #\r | |
201 | for Identifier in File.IdentifierList:\r | |
202 | IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \\r | |
203 | FileID, -1, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)\r | |
204 | #\r | |
205 | # Insert Pcd of file\r | |
206 | #\r | |
207 | for Pcd in File.PcdList:\r | |
208 | PcdID = self.TblPcd.Insert(Pcd.CName, Pcd.TokenSpaceGuidCName, Pcd.Token, Pcd.DatumType, Pcd.Model, \\r | |
209 | FileID, -1, Pcd.StartLine, Pcd.StartColumn, Pcd.EndLine, Pcd.EndColumn)\r | |
210 | \r | |
211 | EdkLogger.verbose("Insert information from file %s ... DONE!" % File.FullPath)\r | |
212 | \r | |
213 | ## UpdateIdentifierBelongsToFunction\r | |
214 | #\r | |
215 | # Update the field "BelongsToFunction" for each Indentifier\r | |
216 | #\r | |
217 | #\r | |
218 | def UpdateIdentifierBelongsToFunction_disabled(self):\r | |
219 | EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")\r | |
220 | \r | |
221 | SqlCommand = """select ID, BelongsToFile, StartLine, EndLine, Model from Identifier"""\r | |
222 | EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
223 | self.Cur.execute(SqlCommand)\r | |
224 | Records = self.Cur.fetchall()\r | |
225 | for Record in Records:\r | |
226 | IdentifierID = Record[0]\r | |
227 | BelongsToFile = Record[1]\r | |
228 | StartLine = Record[2]\r | |
229 | EndLine = Record[3]\r | |
230 | Model = Record[4]\r | |
231 | \r | |
232 | #\r | |
233 | # Check whether an identifier belongs to a function\r | |
234 | #\r | |
235 | EdkLogger.debug(4, "For common identifiers ... ")\r | |
236 | SqlCommand = """select ID from Function \r | |
237 | where StartLine < %s and EndLine > %s\r | |
238 | and BelongsToFile = %s""" % (StartLine, EndLine, BelongsToFile)\r | |
239 | EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
240 | self.Cur.execute(SqlCommand)\r | |
241 | IDs = self.Cur.fetchall()\r | |
242 | for ID in IDs:\r | |
243 | SqlCommand = """Update Identifier set BelongsToFunction = %s where ID = %s""" % (ID[0], IdentifierID)\r | |
244 | EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
245 | self.Cur.execute(SqlCommand)\r | |
246 | \r | |
247 | #\r | |
248 | # Check whether the identifier is a function header\r | |
249 | #\r | |
250 | EdkLogger.debug(4, "For function headers ... ") \r | |
251 | if Model == DataClass.MODEL_IDENTIFIER_COMMENT:\r | |
252 | SqlCommand = """select ID from Function \r | |
253 | where StartLine = %s + 1\r | |
254 | and BelongsToFile = %s""" % (EndLine, BelongsToFile)\r | |
255 | EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
256 | self.Cur.execute(SqlCommand)\r | |
257 | IDs = self.Cur.fetchall()\r | |
258 | for ID in IDs:\r | |
259 | SqlCommand = """Update Identifier set BelongsToFunction = %s, Model = %s where ID = %s""" % (ID[0], DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, IdentifierID)\r | |
260 | EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
261 | self.Cur.execute(SqlCommand)\r | |
262 | \r | |
263 | EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers ... DONE")\r | |
264 | \r | |
265 | \r | |
266 | ## UpdateIdentifierBelongsToFunction\r | |
267 | #\r | |
268 | # Update the field "BelongsToFunction" for each Indentifier\r | |
269 | #\r | |
270 | #\r | |
271 | def UpdateIdentifierBelongsToFunction(self):\r | |
272 | EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")\r | |
273 | \r | |
274 | SqlCommand = """select ID, BelongsToFile, StartLine, EndLine from Function"""\r | |
275 | Records = self.TblFunction.Exec(SqlCommand)\r | |
276 | Data1 = []\r | |
277 | Data2 = []\r | |
278 | for Record in Records:\r | |
279 | FunctionID = Record[0]\r | |
280 | BelongsToFile = Record[1]\r | |
281 | StartLine = Record[2]\r | |
282 | EndLine = Record[3]\r | |
283 | #Data1.append(("'file%s'" % BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine))\r | |
284 | #Data2.append(("'file%s'" % BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1))\r | |
285 | \r | |
286 | SqlCommand = """Update Identifier%s set BelongsToFunction = %s where BelongsToFile = %s and StartLine > %s and EndLine < %s""" % \\r | |
287 | (BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine)\r | |
288 | self.TblIdentifier.Exec(SqlCommand)\r | |
289 | \r | |
290 | SqlCommand = """Update Identifier%s set BelongsToFunction = %s, Model = %s where BelongsToFile = %s and Model = %s and EndLine = %s""" % \\r | |
291 | (BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1)\r | |
292 | self.TblIdentifier.Exec(SqlCommand)\r | |
293 | # #\r | |
294 | # # Check whether an identifier belongs to a function\r | |
295 | # #\r | |
296 | # print Data1\r | |
297 | # SqlCommand = """Update ? set BelongsToFunction = ? where BelongsToFile = ? and StartLine > ? and EndLine < ?"""\r | |
298 | # print SqlCommand\r | |
299 | # EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
300 | # self.Cur.executemany(SqlCommand, Data1)\r | |
301 | #\r | |
302 | # #\r | |
303 | # # Check whether the identifier is a function header\r | |
304 | # #\r | |
305 | # EdkLogger.debug(4, "For function headers ... ")\r | |
306 | # SqlCommand = """Update ? set BelongsToFunction = ?, Model = ? where BelongsToFile = ? and Model = ? and EndLine = ?"""\r | |
307 | # EdkLogger.debug(4, "SqlCommand: %s" %SqlCommand)\r | |
308 | # self.Cur.executemany(SqlCommand, Data2)\r | |
309 | #\r | |
310 | # EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers ... DONE")\r | |
311 | \r | |
312 | \r | |
313 | ##\r | |
314 | #\r | |
315 | # This acts like the main() function for the script, unless it is 'import'ed into another\r | |
316 | # script.\r | |
317 | #\r | |
318 | if __name__ == '__main__':\r | |
319 | EdkLogger.Initialize()\r | |
320 | #EdkLogger.SetLevel(EdkLogger.VERBOSE)\r | |
321 | EdkLogger.SetLevel(EdkLogger.DEBUG_0)\r | |
322 | EdkLogger.verbose("Start at " + time.strftime('%H:%M:%S', time.localtime()))\r | |
323 | \r | |
324 | Db = Database(DATABASE_PATH)\r | |
325 | Db.InitDatabase()\r | |
326 | Db.QueryTable(Db.TblDataModel)\r | |
327 | \r | |
328 | identifier1 = DataClass.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 32, 43, 54, 43)\r | |
329 | identifier2 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 15, 43, 20, 43)\r | |
330 | identifier3 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 55, 43, 58, 43)\r | |
331 | identifier4 = DataClass.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 77, 43, 88, 43)\r | |
332 | fun1 = DataClass.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], [])\r | |
333 | file = DataClass.FileClass(-1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass.MODEL_FILE_C, '2007-12-28', [fun1], [identifier1, identifier2, identifier3, identifier4], [])\r | |
334 | Db.InsertOneFile(file)\r | |
335 | Db.UpdateIdentifierBelongsToFunction()\r | |
336 | \r | |
337 | Db.QueryTable(Db.TblFile)\r | |
338 | Db.QueryTable(Db.TblFunction)\r | |
339 | Db.QueryTable(Db.TblPcd)\r | |
340 | Db.QueryTable(Db.TblIdentifier)\r | |
341 | \r | |
342 | Db.Close()\r | |
343 | EdkLogger.verbose("End at " + time.strftime('%H:%M:%S', time.localtime()))\r | |
344 | \r |