BaseTools: Use absolute import in Table
[mirror_edk2.git] / BaseTools / Source / Python / Table / TableDataModel.py
1 ## @file
2 # This file is used to create/update/query/erase table for data models
3 #
4 # Copyright (c) 2008 - 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 Common.EdkLogger as EdkLogger
19 import CommonDataClass.DataClass as DataClass
20 from .Table import Table
21 from Common.StringUtils import ConvertToSqlString
22
23 ## TableDataModel
24 #
25 # This class defined a table used for data model
26 #
27 # @param object: Inherited from object class
28 #
29 #
30 class TableDataModel(Table):
31 def __init__(self, Cursor):
32 Table.__init__(self, Cursor)
33 self.Table = 'DataModel'
34
35 ## Create table
36 #
37 # Create table DataModel
38 #
39 # @param ID: ID of a ModelType
40 # @param CrossIndex: CrossIndex of a ModelType
41 # @param Name: Name of a ModelType
42 # @param Description: Description of a ModelType
43 #
44 def Create(self):
45 SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY,
46 CrossIndex INTEGER NOT NULL,
47 Name VARCHAR NOT NULL,
48 Description VARCHAR
49 )""" % self.Table
50 Table.Create(self, SqlCommand)
51
52 ## Insert table
53 #
54 # Insert a record into table DataModel
55 #
56 # @param ID: ID of a ModelType
57 # @param CrossIndex: CrossIndex of a ModelType
58 # @param Name: Name of a ModelType
59 # @param Description: Description of a ModelType
60 #
61 def Insert(self, CrossIndex, Name, Description):
62 self.ID = self.ID + 1
63 (Name, Description) = ConvertToSqlString((Name, Description))
64 SqlCommand = """insert into %s values(%s, %s, '%s', '%s')""" % (self.Table, self.ID, CrossIndex, Name, Description)
65 Table.Insert(self, SqlCommand)
66
67 return self.ID
68
69 ## Init table
70 #
71 # Create all default records of table DataModel
72 #
73 def InitTable(self):
74 EdkLogger.verbose("\nInitialize table DataModel started ...")
75 for Item in DataClass.MODEL_LIST:
76 CrossIndex = Item[1]
77 Name = Item[0]
78 Description = Item[0]
79 self.Insert(CrossIndex, Name, Description)
80 EdkLogger.verbose("Initialize table DataModel ... DONE!")
81
82 ## Get CrossIndex
83 #
84 # Get a model's cross index from its name
85 #
86 # @param ModelName: Name of the model
87 # @retval CrossIndex: CrossIndex of the model
88 #
89 def GetCrossIndex(self, ModelName):
90 CrossIndex = -1
91 SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
92 self.Cur.execute(SqlCommand)
93 for Item in self.Cur:
94 CrossIndex = Item[0]
95
96 return CrossIndex