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# SPDX-License-Identifier: BSD-2-Clause-Patent
6#
7
8##
9# Import Modules
10#
11from __future__ import absolute_import
12import Common.EdkLogger as EdkLogger
13import CommonDataClass.DataClass as DataClass
14from Table.Table import Table
15from Common.StringUtils import ConvertToSqlString
16
17## TableDataModel
18#
19# This class defined a table used for data model
20#
21# @param object:       Inherited from object class
22#
23#
24class TableDataModel(Table):
25    def __init__(self, Cursor):
26        Table.__init__(self, Cursor)
27        self.Table = 'DataModel'
28
29    ## Create table
30    #
31    # Create table DataModel
32    #
33    # @param ID:           ID of a ModelType
34    # @param CrossIndex:   CrossIndex of a ModelType
35    # @param Name:         Name of a ModelType
36    # @param Description:  Description of a ModelType
37    #
38    def Create(self):
39        SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY,
40                                                       CrossIndex INTEGER NOT NULL,
41                                                       Name VARCHAR NOT NULL,
42                                                       Description VARCHAR
43                                                      )""" % self.Table
44        Table.Create(self, SqlCommand)
45
46    ## Insert table
47    #
48    # Insert a record into table DataModel
49    #
50    # @param ID:           ID of a ModelType
51    # @param CrossIndex:   CrossIndex of a ModelType
52    # @param Name:         Name of a ModelType
53    # @param Description:  Description of a ModelType
54    #
55    def Insert(self, CrossIndex, Name, Description):
56        self.ID = self.ID + 1
57        (Name, Description) = ConvertToSqlString((Name, Description))
58        SqlCommand = """insert into %s values(%s, %s, '%s', '%s')""" % (self.Table, self.ID, CrossIndex, Name, Description)
59        Table.Insert(self, SqlCommand)
60
61        return self.ID
62
63    ## Init table
64    #
65    # Create all default records of table DataModel
66    #
67    def InitTable(self):
68        EdkLogger.verbose("\nInitialize table DataModel started ...")
69        for Item in DataClass.MODEL_LIST:
70            CrossIndex = Item[1]
71            Name = Item[0]
72            Description = Item[0]
73            self.Insert(CrossIndex, Name, Description)
74        EdkLogger.verbose("Initialize table DataModel ... DONE!")
75
76    ## Get CrossIndex
77    #
78    # Get a model's cross index from its name
79    #
80    # @param ModelName:    Name of the model
81    # @retval CrossIndex:  CrossIndex of the model
82    #
83    def GetCrossIndex(self, ModelName):
84        CrossIndex = -1
85        SqlCommand = """select CrossIndex from DataModel where name = '""" + ModelName + """'"""
86        self.Cur.execute(SqlCommand)
87        for Item in self.Cur:
88            CrossIndex = Item[0]
89
90        return CrossIndex
91