]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/Database.py
BaseTools: Use absolute import in Common
[mirror_edk2.git] / BaseTools / Source / Python / Common / Database.py
1 ## @file
2 # This file is used to create a database used by ECC tool
3 #
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ##
15 # Import Modules
16 #
17 from __future__ import absolute_import
18 import sqlite3
19 import Common.LongFilePathOs as os
20
21 from . import EdkLogger as EdkLogger
22 from CommonDataClass.DataClass import *
23 from .StringUtils import *
24 from .DataType import *
25
26 from Table.TableDataModel import TableDataModel
27 from Table.TableFile import TableFile
28 from Table.TableInf import TableInf
29 from Table.TableDec import TableDec
30 from Table.TableDsc import TableDsc
31
32 ## Database
33 #
34 # This class defined the build databse
35 # During the phase of initialization, the database will create all tables and
36 # insert all records of table DataModel
37 #
38 # @param object: Inherited from object class
39 # @param DbPath: A string for the path of the ECC database
40 #
41 # @var Conn: Connection of the ECC database
42 # @var Cur: Cursor of the connection
43 # @var TblDataModel: Local instance for TableDataModel
44 #
45 class Database(object):
46 def __init__(self, DbPath):
47 if os.path.exists(DbPath):
48 os.remove(DbPath)
49 self.Conn = sqlite3.connect(DbPath, isolation_level = 'DEFERRED')
50 self.Conn.execute("PRAGMA page_size=8192")
51 self.Conn.execute("PRAGMA synchronous=OFF")
52 self.Cur = self.Conn.cursor()
53 self.TblDataModel = TableDataModel(self.Cur)
54 self.TblFile = TableFile(self.Cur)
55 self.TblInf = TableInf(self.Cur)
56 self.TblDec = TableDec(self.Cur)
57 self.TblDsc = TableDsc(self.Cur)
58
59 ## Initialize build database
60 #
61 # 1. Delete all old existing tables
62 # 2. Create new tables
63 # 3. Initialize table DataModel
64 #
65 def InitDatabase(self):
66 EdkLogger.verbose("\nInitialize ECC database started ...")
67 #
68 # Drop all old existing tables
69 #
70 # self.TblDataModel.Drop()
71 # self.TblDsc.Drop()
72 # self.TblFile.Drop()
73
74 #
75 # Create new tables
76 #
77 self.TblDataModel.Create()
78 self.TblFile.Create()
79 self.TblInf.Create()
80 self.TblDec.Create()
81 self.TblDsc.Create()
82
83 #
84 # Initialize table DataModel
85 #
86 self.TblDataModel.InitTable()
87 EdkLogger.verbose("Initialize ECC database ... DONE!")
88
89 ## Query a table
90 #
91 # @param Table: The instance of the table to be queried
92 #
93 def QueryTable(self, Table):
94 Table.Query()
95
96 ## Close entire database
97 #
98 # Commit all first
99 # Close the connection and cursor
100 #
101 def Close(self):
102 self.Conn.commit()
103 self.Cur.close()
104 self.Conn.close()
105
106 ##
107 #
108 # This acts like the main() function for the script, unless it is 'import'ed into another
109 # script.
110 #
111 if __name__ == '__main__':
112 EdkLogger.Initialize()
113 EdkLogger.SetLevel(EdkLogger.DEBUG_0)
114
115 Db = Database(DATABASE_PATH)
116 Db.InitDatabase()
117 Db.QueryTable(Db.TblDataModel)
118 Db.QueryTable(Db.TblFile)
119 Db.QueryTable(Db.TblDsc)
120 Db.Close()