Commit | Line | Data |
---|---|---|
52302d4d LG |
1 | ## @file\r |
2 | # This file is used to create a database used by EOT tool\r | |
3 | #\r | |
1be2ed90 | 4 | # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r |
40d841f6 | 5 | # This program and the accompanying materials\r |
52302d4d LG |
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 | |
1be2ed90 | 18 | import Common.LongFilePathOs as os, time\r |
52302d4d LG |
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.TableIdentifier import TableIdentifier\r | |
27 | from Table.TableEotReport import TableEotReport\r | |
28 | from Table.TableInf import TableInf\r | |
29 | from Table.TableDec import TableDec\r | |
30 | from Table.TableDsc import TableDsc\r | |
31 | from Table.TableFdf import TableFdf\r | |
32 | from Table.TableQuery import TableQuery\r | |
33 | \r | |
34 | ##\r | |
35 | # Static definitions\r | |
36 | #\r | |
37 | DATABASE_PATH = "Eot.db"\r | |
38 | \r | |
39 | ## Database class\r | |
40 | #\r | |
41 | # This class defined the EOT 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 | class Database(object):\r | |
46 | ## The constructor\r | |
47 | #\r | |
48 | # @param self: The object pointer\r | |
49 | # @param DbPath: The file path of the database\r | |
50 | #\r | |
51 | def __init__(self, DbPath):\r | |
52 | self.DbPath = DbPath\r | |
53 | self.Conn = None\r | |
54 | self.Cur = None\r | |
55 | self.TblDataModel = None\r | |
56 | self.TblFile = None\r | |
57 | self.TblFunction = None\r | |
58 | self.TblIdentifier = None\r | |
59 | self.TblReport = None\r | |
60 | self.TblInf = None\r | |
61 | self.TblDec = None\r | |
62 | self.TblDsc = None\r | |
63 | self.TblFdf = None\r | |
64 | self.TblQuery = None\r | |
65 | self.TblQuery2 = None\r | |
66 | \r | |
67 | ## InitDatabase() method\r | |
68 | # 1. Delete all old existing tables\r | |
69 | # 2. Create new tables\r | |
70 | # 3. Initialize table DataModel\r | |
71 | #\r | |
72 | # @param self: The object pointer\r | |
73 | # @param NewDatabase: Check if it needs to create a new database\r | |
74 | #\r | |
75 | def InitDatabase(self, NewDatabase = True):\r | |
76 | EdkLogger.verbose("\nInitialize EOT database started ...")\r | |
77 | #\r | |
78 | # Drop all old existing tables\r | |
79 | #\r | |
80 | if NewDatabase:\r | |
81 | if os.path.exists(self.DbPath):\r | |
82 | os.remove(self.DbPath)\r | |
83 | self.Conn = sqlite3.connect(self.DbPath, isolation_level = 'DEFERRED')\r | |
84 | self.Conn.execute("PRAGMA page_size=8192")\r | |
85 | self.Conn.execute("PRAGMA synchronous=OFF")\r | |
86 | # to avoid non-ascii charater conversion error\r | |
87 | self.Conn.text_factory = str\r | |
88 | self.Cur = self.Conn.cursor()\r | |
89 | \r | |
90 | self.TblDataModel = TableDataModel(self.Cur)\r | |
91 | self.TblFile = TableFile(self.Cur)\r | |
92 | self.TblFunction = TableFunction(self.Cur)\r | |
93 | self.TblIdentifier = TableIdentifier(self.Cur)\r | |
94 | self.TblReport = TableEotReport(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 | self.TblQuery = TableQuery(self.Cur)\r | |
100 | self.TblQuery2 = TableQuery(self.Cur)\r | |
101 | self.TblQuery2.Table = 'Query2'\r | |
102 | \r | |
103 | # Create new tables\r | |
104 | if NewDatabase:\r | |
105 | self.TblDataModel.Create()\r | |
106 | self.TblFile.Create()\r | |
107 | self.TblFunction.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 | self.TblQuery.Create()\r | |
114 | self.TblQuery2.Create()\r | |
115 | \r | |
116 | # Init each table's ID\r | |
117 | self.TblDataModel.InitID()\r | |
118 | self.TblFile.InitID()\r | |
119 | self.TblFunction.InitID()\r | |
120 | self.TblReport.InitID()\r | |
121 | self.TblInf.InitID()\r | |
122 | self.TblDec.InitID()\r | |
123 | self.TblDsc.InitID()\r | |
124 | self.TblFdf.InitID()\r | |
125 | self.TblQuery.Drop()\r | |
126 | self.TblQuery.Create()\r | |
127 | self.TblQuery.InitID()\r | |
128 | self.TblQuery2.Drop()\r | |
129 | self.TblQuery2.Create()\r | |
130 | self.TblQuery2.InitID()\r | |
131 | \r | |
132 | # Initialize table DataModel\r | |
133 | if NewDatabase:\r | |
134 | self.TblDataModel.InitTable()\r | |
135 | \r | |
136 | EdkLogger.verbose("Initialize EOT database ... DONE!")\r | |
137 | \r | |
138 | ## QueryTable() method\r | |
139 | #\r | |
140 | # Query a table\r | |
141 | #\r | |
142 | # @param self: The object pointer\r | |
143 | # @param Table: The instance of the table to be queried\r | |
144 | #\r | |
145 | def QueryTable(self, Table):\r | |
146 | Table.Query()\r | |
147 | \r | |
148 | ## Close() method\r | |
149 | #\r | |
150 | # Commit all first\r | |
151 | # Close the connection and cursor\r | |
152 | #\r | |
153 | def Close(self):\r | |
154 | # Commit to file\r | |
155 | self.Conn.commit()\r | |
156 | \r | |
157 | # Close connection and cursor\r | |
158 | self.Cur.close()\r | |
159 | self.Conn.close()\r | |
160 | \r | |
161 | ## InsertOneFile() method\r | |
162 | #\r | |
163 | # Insert one file's information to the database\r | |
164 | # 1. Create a record in TableFile\r | |
165 | # 2. Create functions one by one\r | |
166 | # 2.1 Create variables of function one by one\r | |
167 | # 2.2 Create pcds of function one by one\r | |
168 | # 3. Create variables one by one\r | |
169 | # 4. Create pcds one by one\r | |
170 | #\r | |
171 | # @param self: The object pointer\r | |
172 | # @param File: The object of the file to be inserted\r | |
173 | #\r | |
174 | def InsertOneFile(self, File):\r | |
175 | # Insert a record for file\r | |
176 | FileID = self.TblFile.Insert(File.Name, File.ExtName, File.Path, File.FullPath, Model = File.Model, TimeStamp = File.TimeStamp)\r | |
177 | IdTable = TableIdentifier(self.Cur)\r | |
178 | IdTable.Table = "Identifier%s" % FileID\r | |
179 | IdTable.Create()\r | |
180 | \r | |
181 | # Insert function of file\r | |
182 | for Function in File.FunctionList:\r | |
183 | FunctionID = self.TblFunction.Insert(Function.Header, Function.Modifier, Function.Name, Function.ReturnStatement, \\r | |
184 | Function.StartLine, Function.StartColumn, Function.EndLine, Function.EndColumn, \\r | |
185 | Function.BodyStartLine, Function.BodyStartColumn, FileID, \\r | |
186 | Function.FunNameStartLine, Function.FunNameStartColumn)\r | |
187 | \r | |
188 | # Insert Identifier of function\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 | # Insert Identifier of file\r | |
193 | for Identifier in File.IdentifierList:\r | |
194 | IdentifierID = IdTable.Insert(Identifier.Modifier, Identifier.Type, Identifier.Name, Identifier.Value, Identifier.Model, \\r | |
195 | FileID, -1, Identifier.StartLine, Identifier.StartColumn, Identifier.EndLine, Identifier.EndColumn)\r | |
196 | \r | |
197 | EdkLogger.verbose("Insert information from file %s ... DONE!" % File.FullPath)\r | |
198 | \r | |
199 | ## UpdateIdentifierBelongsToFunction() method\r | |
200 | #\r | |
201 | # Update the field "BelongsToFunction" for each Indentifier\r | |
202 | #\r | |
203 | # @param self: The object pointer\r | |
204 | #\r | |
205 | def UpdateIdentifierBelongsToFunction(self):\r | |
206 | EdkLogger.verbose("Update 'BelongsToFunction' for Identifiers started ...")\r | |
207 | \r | |
208 | SqlCommand = """select ID, BelongsToFile, StartLine, EndLine from Function"""\r | |
209 | Records = self.TblFunction.Exec(SqlCommand)\r | |
210 | Data1 = []\r | |
211 | Data2 = []\r | |
212 | for Record in Records:\r | |
213 | FunctionID = Record[0]\r | |
214 | BelongsToFile = Record[1]\r | |
215 | StartLine = Record[2]\r | |
216 | EndLine = Record[3]\r | |
217 | \r | |
218 | SqlCommand = """Update Identifier%s set BelongsToFunction = %s where BelongsToFile = %s and StartLine > %s and EndLine < %s""" % \\r | |
219 | (BelongsToFile, FunctionID, BelongsToFile, StartLine, EndLine)\r | |
220 | self.TblIdentifier.Exec(SqlCommand)\r | |
221 | \r | |
222 | SqlCommand = """Update Identifier%s set BelongsToFunction = %s, Model = %s where BelongsToFile = %s and Model = %s and EndLine = %s""" % \\r | |
223 | (BelongsToFile, FunctionID, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER, BelongsToFile, DataClass.MODEL_IDENTIFIER_COMMENT, StartLine - 1)\r | |
224 | self.TblIdentifier.Exec(SqlCommand)\r | |
225 | \r | |
226 | \r | |
227 | ##\r | |
228 | #\r | |
229 | # This acts like the main() function for the script, unless it is 'import'ed into another\r | |
230 | # script.\r | |
231 | #\r | |
232 | if __name__ == '__main__':\r | |
233 | EdkLogger.Initialize()\r | |
234 | EdkLogger.SetLevel(EdkLogger.DEBUG_0)\r | |
235 | EdkLogger.verbose("Start at " + time.strftime('%H:%M:%S', time.localtime()))\r | |
236 | \r | |
237 | Db = Database(DATABASE_PATH)\r | |
238 | Db.InitDatabase()\r | |
239 | Db.QueryTable(Db.TblDataModel)\r | |
240 | \r | |
241 | identifier1 = DataClass.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 32, 43, 54, 43)\r | |
242 | identifier2 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 15, 43, 20, 43)\r | |
243 | identifier3 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 55, 43, 58, 43)\r | |
244 | identifier4 = DataClass.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 77, 43, 88, 43)\r | |
245 | fun1 = DataClass.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], [])\r | |
246 | file = DataClass.FileClass(-1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass.MODEL_FILE_C, '2007-12-28', [fun1], [identifier1, identifier2, identifier3, identifier4], [])\r | |
247 | Db.InsertOneFile(file)\r | |
248 | \r | |
249 | Db.QueryTable(Db.TblFile)\r | |
250 | Db.QueryTable(Db.TblFunction)\r | |
251 | Db.QueryTable(Db.TblIdentifier)\r | |
252 | \r | |
253 | Db.Close()\r | |
254 | EdkLogger.verbose("End at " + time.strftime('%H:%M:%S', time.localtime()))\r | |
255 | \r |