2 # This file is used to create a database used by EOT tool
4 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 import Common
.LongFilePathOs
as os
, time
14 import Common
.EdkLogger
as EdkLogger
15 import CommonDataClass
.DataClass
as DataClass
17 from Table
.TableDataModel
import TableDataModel
18 from Table
.TableFile
import TableFile
19 from Table
.TableFunction
import TableFunction
20 from Table
.TableIdentifier
import TableIdentifier
21 from Table
.TableEotReport
import TableEotReport
22 from Table
.TableInf
import TableInf
23 from Table
.TableDec
import TableDec
24 from Table
.TableDsc
import TableDsc
25 from Table
.TableFdf
import TableFdf
26 from Table
.TableQuery
import TableQuery
31 DATABASE_PATH
= "Eot.db"
35 # This class defined the EOT database
36 # During the phase of initialization, the database will create all tables and
37 # insert all records of table DataModel
39 class Database(object):
42 # @param self: The object pointer
43 # @param DbPath: The file path of the database
45 def __init__(self
, DbPath
):
49 self
.TblDataModel
= None
51 self
.TblFunction
= None
52 self
.TblIdentifier
= None
61 ## InitDatabase() method
62 # 1. Delete all old existing tables
63 # 2. Create new tables
64 # 3. Initialize table DataModel
66 # @param self: The object pointer
67 # @param NewDatabase: Check if it needs to create a new database
69 def InitDatabase(self
, NewDatabase
= True):
70 EdkLogger
.verbose("\nInitialize EOT database started ...")
72 # Drop all old existing tables
75 if os
.path
.exists(self
.DbPath
):
76 os
.remove(self
.DbPath
)
77 self
.Conn
= sqlite3
.connect(self
.DbPath
, isolation_level
= 'DEFERRED')
78 self
.Conn
.execute("PRAGMA page_size=8192")
79 self
.Conn
.execute("PRAGMA synchronous=OFF")
80 # to avoid non-ascii character conversion error
81 self
.Conn
.text_factory
= str
82 self
.Cur
= self
.Conn
.cursor()
84 self
.TblDataModel
= TableDataModel(self
.Cur
)
85 self
.TblFile
= TableFile(self
.Cur
)
86 self
.TblFunction
= TableFunction(self
.Cur
)
87 self
.TblIdentifier
= TableIdentifier(self
.Cur
)
88 self
.TblReport
= TableEotReport(self
.Cur
)
89 self
.TblInf
= TableInf(self
.Cur
)
90 self
.TblDec
= TableDec(self
.Cur
)
91 self
.TblDsc
= TableDsc(self
.Cur
)
92 self
.TblFdf
= TableFdf(self
.Cur
)
93 self
.TblQuery
= TableQuery(self
.Cur
)
94 self
.TblQuery2
= TableQuery(self
.Cur
)
95 self
.TblQuery2
.Table
= 'Query2'
99 self
.TblDataModel
.Create()
100 self
.TblFile
.Create()
101 self
.TblFunction
.Create()
102 self
.TblReport
.Create()
107 self
.TblQuery
.Create()
108 self
.TblQuery2
.Create()
110 # Init each table's ID
111 self
.TblDataModel
.InitID()
112 self
.TblFile
.InitID()
113 self
.TblFunction
.InitID()
114 self
.TblReport
.InitID()
120 self
.TblQuery
.Create()
121 self
.TblQuery
.InitID()
122 self
.TblQuery2
.Drop()
123 self
.TblQuery2
.Create()
124 self
.TblQuery2
.InitID()
126 # Initialize table DataModel
128 self
.TblDataModel
.InitTable()
130 EdkLogger
.verbose("Initialize EOT database ... DONE!")
132 ## QueryTable() method
136 # @param self: The object pointer
137 # @param Table: The instance of the table to be queried
139 def QueryTable(self
, Table
):
145 # Close the connection and cursor
151 # Close connection and cursor
155 ## InsertOneFile() method
157 # Insert one file's information to the database
158 # 1. Create a record in TableFile
159 # 2. Create functions one by one
160 # 2.1 Create variables of function one by one
161 # 2.2 Create pcds of function one by one
162 # 3. Create variables one by one
163 # 4. Create pcds one by one
165 # @param self: The object pointer
166 # @param File: The object of the file to be inserted
168 def InsertOneFile(self
, File
):
169 # Insert a record for file
170 FileID
= self
.TblFile
.Insert(File
.Name
, File
.ExtName
, File
.Path
, File
.FullPath
, Model
= File
.Model
, TimeStamp
= File
.TimeStamp
)
171 IdTable
= TableIdentifier(self
.Cur
)
172 IdTable
.Table
= "Identifier%s" % FileID
175 # Insert function of file
176 for Function
in File
.FunctionList
:
177 FunctionID
= self
.TblFunction
.Insert(Function
.Header
, Function
.Modifier
, Function
.Name
, Function
.ReturnStatement
, \
178 Function
.StartLine
, Function
.StartColumn
, Function
.EndLine
, Function
.EndColumn
, \
179 Function
.BodyStartLine
, Function
.BodyStartColumn
, FileID
, \
180 Function
.FunNameStartLine
, Function
.FunNameStartColumn
)
182 # Insert Identifier of function
183 for Identifier
in Function
.IdentifierList
:
184 IdentifierID
= IdTable
.Insert(Identifier
.Modifier
, Identifier
.Type
, Identifier
.Name
, Identifier
.Value
, Identifier
.Model
, \
185 FileID
, FunctionID
, Identifier
.StartLine
, Identifier
.StartColumn
, Identifier
.EndLine
, Identifier
.EndColumn
)
186 # Insert Identifier of file
187 for Identifier
in File
.IdentifierList
:
188 IdentifierID
= IdTable
.Insert(Identifier
.Modifier
, Identifier
.Type
, Identifier
.Name
, Identifier
.Value
, Identifier
.Model
, \
189 FileID
, -1, Identifier
.StartLine
, Identifier
.StartColumn
, Identifier
.EndLine
, Identifier
.EndColumn
)
191 EdkLogger
.verbose("Insert information from file %s ... DONE!" % File
.FullPath
)
193 ## UpdateIdentifierBelongsToFunction() method
195 # Update the field "BelongsToFunction" for each Identifier
197 # @param self: The object pointer
199 def UpdateIdentifierBelongsToFunction(self
):
200 EdkLogger
.verbose("Update 'BelongsToFunction' for Identifiers started ...")
202 SqlCommand
= """select ID, BelongsToFile, StartLine, EndLine from Function"""
203 Records
= self
.TblFunction
.Exec(SqlCommand
)
206 for Record
in Records
:
207 FunctionID
= Record
[0]
208 BelongsToFile
= Record
[1]
209 StartLine
= Record
[2]
212 SqlCommand
= """Update Identifier%s set BelongsToFunction = %s where BelongsToFile = %s and StartLine > %s and EndLine < %s""" % \
213 (BelongsToFile
, FunctionID
, BelongsToFile
, StartLine
, EndLine
)
214 self
.TblIdentifier
.Exec(SqlCommand
)
216 SqlCommand
= """Update Identifier%s set BelongsToFunction = %s, Model = %s where BelongsToFile = %s and Model = %s and EndLine = %s""" % \
217 (BelongsToFile
, FunctionID
, DataClass
.MODEL_IDENTIFIER_FUNCTION_HEADER
, BelongsToFile
, DataClass
.MODEL_IDENTIFIER_COMMENT
, StartLine
- 1)
218 self
.TblIdentifier
.Exec(SqlCommand
)
223 # This acts like the main() function for the script, unless it is 'import'ed into another
226 if __name__
== '__main__':
227 EdkLogger
.Initialize()
228 EdkLogger
.SetLevel(EdkLogger
.DEBUG_0
)
229 EdkLogger
.verbose("Start at " + time
.strftime('%H:%M:%S', time
.localtime()))
231 Db
= Database(DATABASE_PATH
)
233 Db
.QueryTable(Db
.TblDataModel
)
235 identifier1
= DataClass
.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass
.MODEL_IDENTIFIER_COMMENT
, 1, -1, 32, 43, 54, 43)
236 identifier2
= DataClass
.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass
.MODEL_IDENTIFIER_COMMENT
, 1, -1, 15, 43, 20, 43)
237 identifier3
= DataClass
.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass
.MODEL_IDENTIFIER_COMMENT
, 1, -1, 55, 43, 58, 43)
238 identifier4
= DataClass
.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass
.MODEL_IDENTIFIER_COMMENT
, 1, -1, 77, 43, 88, 43)
239 fun1
= DataClass
.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], [])
240 file = DataClass
.FileClass(-1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass
.MODEL_FILE_C
, '2007-12-28', [fun1
], [identifier1
, identifier2
, identifier3
, identifier4
], [])
241 Db
.InsertOneFile(file)
243 Db
.QueryTable(Db
.TblFile
)
244 Db
.QueryTable(Db
.TblFunction
)
245 Db
.QueryTable(Db
.TblIdentifier
)
248 EdkLogger
.verbose("End at " + time
.strftime('%H:%M:%S', time
.localtime()))