1## @file
2# This file is used to create/update/query/erase table for ECC reports
3#
4# Copyright (c) 2008 - 2015, 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 Common.LongFilePathOs as os, time
19from Table import Table
20from Common.String import ConvertToSqlString2
21import EccToolError as EccToolError
22import EccGlobalData as EccGlobalData
23from Common.LongFilePathSupport import OpenLongFilePath as open
24
25## TableReport
26#
27# This class defined a table used for data model
28#
29# @param object:       Inherited from object class
30#
31#
32class TableReport(Table):
33    def __init__(self, Cursor):
34        Table.__init__(self, Cursor)
35        self.Table = 'Report'
36
37    ## Create table
38    #
39    # Create table report
40    #
41    # @param ID:             ID of an Error
42    # @param ErrorID:        ID of an Error TypeModel of a Report item
43    # @param OtherMsg:       Other error message besides the standard error message
44    # @param BelongsToItem:  The error belongs to which item
45    # @param Enabled:        If this error enabled
46    # @param Corrected:      if this error corrected
47    #
48    def Create(self):
49        SqlCommand = """create table IF NOT EXISTS %s (ID INTEGER PRIMARY KEY,
50                                                       ErrorID INTEGER NOT NULL,
51                                                       OtherMsg TEXT,
52                                                       BelongsToTable TEXT NOT NULL,
53                                                       BelongsToItem SINGLE NOT NULL,
54                                                       Enabled INTEGER DEFAULT 0,
55                                                       Corrected INTEGER DEFAULT -1
56                                                      )""" % self.Table
57        Table.Create(self, SqlCommand)
58
59    ## Insert table
60    #
61    # Insert a record into table report
62    #
63    # @param ID:             ID of an Error
64    # @param ErrorID:        ID of an Error TypeModel of a report item
65    # @param OtherMsg:       Other error message besides the standard error message
66    # @param BelongsToTable: The error item belongs to which table
67    # @param BelongsToItem:  The error belongs to which item
68    # @param Enabled:        If this error enabled
69    # @param Corrected:      if this error corrected
70    #
71    def Insert(self, ErrorID, OtherMsg='', BelongsToTable='', BelongsToItem= -1, Enabled=0, Corrected= -1):
72        self.ID = self.ID + 1
73        SqlCommand = """insert into %s values(%s, %s, '%s', '%s', %s, %s, %s)""" \
74                     % (self.Table, self.ID, ErrorID, ConvertToSqlString2(OtherMsg), BelongsToTable, BelongsToItem, Enabled, Corrected)
75        Table.Insert(self, SqlCommand)
76
77        return self.ID
78
79    ## Query table
80    #
81    # @retval:       A recordSet of all found records
82    #
83    def Query(self):
84        SqlCommand = """select ID, ErrorID, OtherMsg, BelongsToTable, BelongsToItem, Corrected from %s
85                        where Enabled > -1 order by ErrorID, BelongsToItem""" % (self.Table)
86        return self.Exec(SqlCommand)
87
88    ## Update table
89    #
90    def UpdateBelongsToItemByFile(self, ItemID=-1, File=""):
91        SqlCommand = """update Report set BelongsToItem=%s where BelongsToTable='File' and BelongsToItem=-2
92                        and OtherMsg like '%%%s%%'""" % (ItemID, File)
93        return self.Exec(SqlCommand)
94
95    ## Convert to CSV
96    #
97    # Get all enabled records from table report and save them to a .csv file
98    #
99    # @param Filename:  To filename to save the report content
100    #
101    def ToCSV(self, Filename='Report.csv'):
102        try:
103            File = open(Filename, 'w+')
104            File.write("""No, Error Code, Error Message, File, LineNo, Other Error Message\n""")
105            RecordSet = self.Query()
106            Index = 0
107            for Record in RecordSet:
108                Index = Index + 1
109                ErrorID = Record[1]
110                OtherMsg = Record[2]
111                BelongsToTable = Record[3]
112                BelongsToItem = Record[4]
113                IsCorrected = Record[5]
114                SqlCommand = ''
115                if BelongsToTable == 'File':
116                    SqlCommand = """select 1, FullPath from %s where ID = %s
117                             """ % (BelongsToTable, BelongsToItem)
118                else:
119                    SqlCommand = """select A.StartLine, B.FullPath from %s as A, File as B
120                                    where A.ID = %s and B.ID = A.BelongsToFile
121                                 """ % (BelongsToTable, BelongsToItem)
122                NewRecord = self.Exec(SqlCommand)
123                if NewRecord != []:
124                    File.write("""%s,%s,"%s",%s,%s,"%s"\n""" % (Index, ErrorID, EccToolError.gEccErrorMessage[ErrorID], NewRecord[0][1], NewRecord[0][0], OtherMsg))
125                    EdkLogger.quiet("%s(%s): [%s]%s %s" % (NewRecord[0][1], NewRecord[0][0], ErrorID, EccToolError.gEccErrorMessage[ErrorID], OtherMsg))
126
127            File.close()
128        except IOError:
129            NewFilename = 'Report_' + time.strftime("%Y%m%d_%H%M%S.csv", time.localtime())
130            EdkLogger.warn("ECC", "The report file %s is locked by other progress, use %s instead!" % (Filename, NewFilename))
131            self.ToCSV(NewFilename)
132
133