130fdf1140b8d1ce93f3821d986fa165552023440lgao## @file
230fdf1140b8d1ce93f3821d986fa165552023440lgao# This file is used to create/update/query/erase table for files
330fdf1140b8d1ce93f3821d986fa165552023440lgao#
41be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chen# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>
540d841f6a8f84e75409178e19e69b95e01bada0flgao# This program and the accompanying materials
630fdf1140b8d1ce93f3821d986fa165552023440lgao# are licensed and made available under the terms and conditions of the BSD License
730fdf1140b8d1ce93f3821d986fa165552023440lgao# which accompanies this distribution.  The full text of the license may be found at
830fdf1140b8d1ce93f3821d986fa165552023440lgao# http://opensource.org/licenses/bsd-license.php
930fdf1140b8d1ce93f3821d986fa165552023440lgao#
1030fdf1140b8d1ce93f3821d986fa165552023440lgao# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
1130fdf1140b8d1ce93f3821d986fa165552023440lgao# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
1230fdf1140b8d1ce93f3821d986fa165552023440lgao#
1330fdf1140b8d1ce93f3821d986fa165552023440lgao
1430fdf1140b8d1ce93f3821d986fa165552023440lgao##
1530fdf1140b8d1ce93f3821d986fa165552023440lgao# Import Modules
1630fdf1140b8d1ce93f3821d986fa165552023440lgao#
1730fdf1140b8d1ce93f3821d986fa165552023440lgaoimport Common.EdkLogger as EdkLogger
1830fdf1140b8d1ce93f3821d986fa165552023440lgaofrom Table import Table
1930fdf1140b8d1ce93f3821d986fa165552023440lgaofrom Common.String import ConvertToSqlString
201be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chenimport Common.LongFilePathOs as os
2130fdf1140b8d1ce93f3821d986fa165552023440lgaofrom CommonDataClass.DataClass import FileClass
2230fdf1140b8d1ce93f3821d986fa165552023440lgao
2330fdf1140b8d1ce93f3821d986fa165552023440lgao## TableFile
2430fdf1140b8d1ce93f3821d986fa165552023440lgao#
2530fdf1140b8d1ce93f3821d986fa165552023440lgao# This class defined a table used for file
2630fdf1140b8d1ce93f3821d986fa165552023440lgao#
2730fdf1140b8d1ce93f3821d986fa165552023440lgao# @param object:       Inherited from object class
2830fdf1140b8d1ce93f3821d986fa165552023440lgao#
2930fdf1140b8d1ce93f3821d986fa165552023440lgaoclass TableFile(Table):
3030fdf1140b8d1ce93f3821d986fa165552023440lgao    def __init__(self, Cursor):
3130fdf1140b8d1ce93f3821d986fa165552023440lgao        Table.__init__(self, Cursor)
3230fdf1140b8d1ce93f3821d986fa165552023440lgao        self.Table = 'File'
3330fdf1140b8d1ce93f3821d986fa165552023440lgao
3430fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Create table
3530fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3630fdf1140b8d1ce93f3821d986fa165552023440lgao    # Create table File
3730fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3830fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param ID:        ID of a File
3930fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Name:      Name of a File
4030fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param ExtName:   ExtName of a File
4130fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Path:      Path of a File
4230fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param FullPath:  FullPath of a File
4330fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Model:     Model of a File
4430fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param TimeStamp: TimeStamp of a File
4530fdf1140b8d1ce93f3821d986fa165552023440lgao    #
4630fdf1140b8d1ce93f3821d986fa165552023440lgao    def Create(self):
4730fdf1140b8d1ce93f3821d986fa165552023440lgao        SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY,
4830fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       Name VARCHAR NOT NULL,
4930fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       ExtName VARCHAR,
5030fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       Path VARCHAR,
5130fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       FullPath VARCHAR NOT NULL,
5230fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       Model INTEGER DEFAULT 0,
5330fdf1140b8d1ce93f3821d986fa165552023440lgao                                                       TimeStamp VARCHAR NOT NULL
5430fdf1140b8d1ce93f3821d986fa165552023440lgao                                                      )""" % self.Table
5530fdf1140b8d1ce93f3821d986fa165552023440lgao        Table.Create(self, SqlCommand)
5630fdf1140b8d1ce93f3821d986fa165552023440lgao
5730fdf1140b8d1ce93f3821d986fa165552023440lgao    ## Insert table
5830fdf1140b8d1ce93f3821d986fa165552023440lgao    #
5930fdf1140b8d1ce93f3821d986fa165552023440lgao    # Insert a record into table File
6030fdf1140b8d1ce93f3821d986fa165552023440lgao    #
6130fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param ID:        ID of a File
6230fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Name:      Name of a File
6330fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param ExtName:   ExtName of a File
6430fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Path:      Path of a File
6530fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param FullPath:  FullPath of a File
6630fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Model:     Model of a File
6730fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param TimeStamp: TimeStamp of a File
6830fdf1140b8d1ce93f3821d986fa165552023440lgao    #
6930fdf1140b8d1ce93f3821d986fa165552023440lgao    def Insert(self, Name, ExtName, Path, FullPath, Model, TimeStamp):
7030fdf1140b8d1ce93f3821d986fa165552023440lgao        self.ID = self.ID + 1
7130fdf1140b8d1ce93f3821d986fa165552023440lgao        (Name, ExtName, Path, FullPath) = ConvertToSqlString((Name, ExtName, Path, FullPath))
7230fdf1140b8d1ce93f3821d986fa165552023440lgao        SqlCommand = """insert into %s values(%s, '%s', '%s', '%s', '%s', %s, '%s')""" \
7330fdf1140b8d1ce93f3821d986fa165552023440lgao                                           % (self.Table, self.ID, Name, ExtName, Path, FullPath, Model, TimeStamp)
7430fdf1140b8d1ce93f3821d986fa165552023440lgao        Table.Insert(self, SqlCommand)
7530fdf1140b8d1ce93f3821d986fa165552023440lgao
7630fdf1140b8d1ce93f3821d986fa165552023440lgao        return self.ID
7730fdf1140b8d1ce93f3821d986fa165552023440lgao    ## InsertFile
7830fdf1140b8d1ce93f3821d986fa165552023440lgao    #
7930fdf1140b8d1ce93f3821d986fa165552023440lgao    # Insert one file to table
8030fdf1140b8d1ce93f3821d986fa165552023440lgao    #
8130fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param FileFullPath:  The full path of the file
8230fdf1140b8d1ce93f3821d986fa165552023440lgao    # @param Model:         The model of the file
8330fdf1140b8d1ce93f3821d986fa165552023440lgao    #
8430fdf1140b8d1ce93f3821d986fa165552023440lgao    # @retval FileID:       The ID after record is inserted
8530fdf1140b8d1ce93f3821d986fa165552023440lgao    #
8630fdf1140b8d1ce93f3821d986fa165552023440lgao    def InsertFile(self, FileFullPath, Model):
8730fdf1140b8d1ce93f3821d986fa165552023440lgao        (Filepath, Name) = os.path.split(FileFullPath)
8830fdf1140b8d1ce93f3821d986fa165552023440lgao        (Root, Ext) = os.path.splitext(FileFullPath)
8930fdf1140b8d1ce93f3821d986fa165552023440lgao        TimeStamp = os.stat(FileFullPath)[8]
9030fdf1140b8d1ce93f3821d986fa165552023440lgao        File = FileClass(-1, Name, Ext, Filepath, FileFullPath, Model, '', [], [], [])
9130fdf1140b8d1ce93f3821d986fa165552023440lgao        return self.Insert(File.Name, File.ExtName, File.Path, File.FullPath, File.Model, TimeStamp)
92d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao
93d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    ## Get ID of a given file
94d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    #
95d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    #   @param  FilePath    Path of file
96d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    #
97d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    #   @retval ID          ID value of given file in the table
98d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    #
99d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao    def GetFileId(self, File):
100d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao        QueryScript = "select ID from %s where FullPath = '%s'" % (self.Table, str(File))
101d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao        RecordList = self.Exec(QueryScript)
102d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao        if len(RecordList) == 0:
103d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao            return None
104d0acc87a41d9aa25fe87eb096efa62afacd1f865lgao        return RecordList[0][0]
105