1 ## @file 2 # This file is used to create/update/query/erase table for files 3 # 4 # Copyright (c) 2008 - 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 Common.EdkLogger as EdkLogger 18 from Table import Table 19 from Common.String import ConvertToSqlString 20 import Common.LongFilePathOs as os 21 from CommonDataClass.DataClass import FileClass 22 23 ## TableFile 24 # 25 # This class defined a table used for file 26 # 27 # @param object: Inherited from object class 28 # 29 class TableFile(Table): 30 def __init__(self, Cursor): 31 Table.__init__(self, Cursor) 32 self.Table = 'File' 33 34 ## Create table 35 # 36 # Create table File 37 # 38 # @param ID: ID of a File 39 # @param Name: Name of a File 40 # @param ExtName: ExtName of a File 41 # @param Path: Path of a File 42 # @param FullPath: FullPath of a File 43 # @param Model: Model of a File 44 # @param TimeStamp: TimeStamp of a File 45 # 46 def Create(self): 47 SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY, 48 Name VARCHAR NOT NULL, 49 ExtName VARCHAR, 50 Path VARCHAR, 51 FullPath VARCHAR NOT NULL, 52 Model INTEGER DEFAULT 0, 53 TimeStamp VARCHAR NOT NULL 54 )""" % self.Table 55 Table.Create(self, SqlCommand) 56 57 ## Insert table 58 # 59 # Insert a record into table File 60 # 61 # @param ID: ID of a File 62 # @param Name: Name of a File 63 # @param ExtName: ExtName of a File 64 # @param Path: Path of a File 65 # @param FullPath: FullPath of a File 66 # @param Model: Model of a File 67 # @param TimeStamp: TimeStamp of a File 68 # 69 def Insert(self, Name, ExtName, Path, FullPath, Model, TimeStamp): 70 self.ID = self.ID + 1 71 (Name, ExtName, Path, FullPath) = ConvertToSqlString((Name, ExtName, Path, FullPath)) 72 SqlCommand = """insert into %s values(%s, '%s', '%s', '%s', '%s', %s, '%s')""" \ 73 % (self.Table, self.ID, Name, ExtName, Path, FullPath, Model, TimeStamp) 74 Table.Insert(self, SqlCommand) 75 76 return self.ID 77 ## InsertFile 78 # 79 # Insert one file to table 80 # 81 # @param FileFullPath: The full path of the file 82 # @param Model: The model of the file 83 # 84 # @retval FileID: The ID after record is inserted 85 # 86 def InsertFile(self, FileFullPath, Model): 87 (Filepath, Name) = os.path.split(FileFullPath) 88 (Root, Ext) = os.path.splitext(FileFullPath) 89 TimeStamp = os.stat(FileFullPath)[8] 90 File = FileClass(-1, Name, Ext, Filepath, FileFullPath, Model, '', [], [], []) 91 return self.Insert(File.Name, File.ExtName, File.Path, File.FullPath, File.Model, TimeStamp) 92 93 ## Get ID of a given file 94 # 95 # @param FilePath Path of file 96 # 97 # @retval ID ID value of given file in the table 98 # 99 def GetFileId(self, File): 100 QueryScript = "select ID from %s where FullPath = '%s'" % (self.Table, str(File)) 101 RecordList = self.Exec(QueryScript) 102 if len(RecordList) == 0: 103 return None 104 return RecordList[0][0] 105