Home | History | Annotate | Download | only in Common
      1 ## @file

      2 # This file is used to create a database used by ECC tool

      3 #

      4 # Copyright (c) 2007 - 2014, 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 import sqlite3
     18 import Common.LongFilePathOs as os
     19 
     20 import EdkLogger as EdkLogger
     21 from CommonDataClass.DataClass import *
     22 from String import *
     23 from DataType import *
     24 
     25 from Table.TableDataModel import TableDataModel
     26 from Table.TableFile import TableFile
     27 from Table.TableInf import TableInf
     28 from Table.TableDec import TableDec
     29 from Table.TableDsc import TableDsc
     30 
     31 ## Database

     32 #

     33 # This class defined the build databse

     34 # During the phase of initialization, the database will create all tables and

     35 # insert all records of table DataModel

     36 # 

     37 # @param object:      Inherited from object class

     38 # @param DbPath:      A string for the path of the ECC database

     39 #

     40 # @var Conn:          Connection of the ECC database

     41 # @var Cur:           Cursor of the connection

     42 # @var TblDataModel:  Local instance for TableDataModel

     43 #

     44 class Database(object):
     45     def __init__(self, DbPath):
     46         if os.path.exists(DbPath):
     47             os.remove(DbPath)
     48         self.Conn = sqlite3.connect(DbPath, isolation_level = 'DEFERRED')
     49         self.Conn.execute("PRAGMA page_size=8192")
     50         self.Conn.execute("PRAGMA synchronous=OFF")
     51         self.Cur = self.Conn.cursor()
     52         self.TblDataModel = TableDataModel(self.Cur)
     53         self.TblFile = TableFile(self.Cur)
     54         self.TblInf = TableInf(self.Cur)
     55         self.TblDec = TableDec(self.Cur)
     56         self.TblDsc = TableDsc(self.Cur)
     57     
     58     ## Initialize build database

     59     #

     60     # 1. Delete all old existing tables

     61     # 2. Create new tables

     62     # 3. Initialize table DataModel

     63     #

     64     def InitDatabase(self):
     65         EdkLogger.verbose("\nInitialize ECC database started ...")
     66         #

     67         # Drop all old existing tables

     68         #

     69 #        self.TblDataModel.Drop()

     70 #        self.TblDsc.Drop()

     71 #        self.TblFile.Drop()

     72         
     73         #

     74         # Create new tables

     75         #

     76         self.TblDataModel.Create()
     77         self.TblFile.Create()
     78         self.TblInf.Create()
     79         self.TblDec.Create()
     80         self.TblDsc.Create()
     81         
     82         #

     83         # Initialize table DataModel

     84         #

     85         self.TblDataModel.InitTable()
     86         EdkLogger.verbose("Initialize ECC database ... DONE!")
     87 
     88     ## Query a table

     89     #

     90     # @param Table:  The instance of the table to be queried

     91     #

     92     def QueryTable(self, Table):
     93         Table.Query()
     94     
     95     ## Close entire database

     96     #

     97     # Commit all first 

     98     # Close the connection and cursor

     99     #

    100     def Close(self):
    101         self.Conn.commit()
    102         self.Cur.close()
    103         self.Conn.close()
    104 
    105 ##

    106 #

    107 # This acts like the main() function for the script, unless it is 'import'ed into another

    108 # script.

    109 #

    110 if __name__ == '__main__':
    111     EdkLogger.Initialize()
    112     EdkLogger.SetLevel(EdkLogger.DEBUG_0)
    113     
    114     Db = Database(DATABASE_PATH)
    115     Db.InitDatabase()
    116     Db.QueryTable(Db.TblDataModel)   
    117     Db.QueryTable(Db.TblFile)
    118     Db.QueryTable(Db.TblDsc)
    119     Db.Close()
    120