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