<commit_before>## @file
# This file is used to create/update/query/erase a meta file table
#
# Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
import uuid

import Common.EdkLogger as EdkLogger

from MetaDataTable import Table, TableFile
from MetaDataTable import ConvertToSqlString
from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \
                                      MODEL_FILE_OTHERS

class MetaFileTable(Table):
    # TRICK: use file ID as the part before '.'
    _ID_STEP_ = 0.00000001
    _ID_MAX_ = 0.99999999

    ## Constructor
    def __init__(self, Cursor, MetaFile, FileType, Temporary):
        self.MetaFile = MetaFile

        self._FileIndexTable = TableFile(Cursor)
        self._FileIndexTable.Create(False)

        FileId = self._FileIndexTable.GetFileId(MetaFile)
        if not FileId:
            FileId = self._FileIndexTable.InsertFile(MetaFile, FileType)

        if Temporary:
            TableName = "_%s_%s_%s" % (FileType, FileId, uuid.uuid4().hex)
        else:
            TableName = "_%s_%s" % (FileType, FileId)

        #Table.__init__(self, Cursor, TableName, FileId, False)
        Table.__init__(self, Cursor, TableName, FileId, Temporary)
        self.Create(not self.IsIntegrity())

    def IsIntegrity(self):
        try:
            Result = self.Cur.execute("select ID from %s where ID<0" % (self.Table)).fetchall()
            if not Result:
                return False

            TimeStamp = self.MetaFile.TimeStamp
            if TimeStamp != self._FileIndexTable.GetFileTimeStamp(self.IdBase):
                # update the timestamp in database
                self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp)
                return False
        except Exception, Exc:
            EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc))
            return False
        return True

## Python class representation of table storing module data
class ModuleTable(MetaFileTable):
    _ID_STEP_ = 0.00000001
    _ID_MAX_  = 0.99999999
    _COLUMN_ = '''
        ID REAL PRIMARY KEY,
        Model INTEGER NOT NULL,
        Value1 TEXT NOT NULL,
        Value2 TEXT,
        Value3 TEXT,
        Scope1 TEXT,
        Scope2 TEXT,
        BelongsToItem REAL NOT NULL,
        StartLine INTEGER NOT NULL,<commit_msg>Correct one case that database is not intergrity but timestamp is not updated.<commit_after>## @file
# This file is used to create/update/query/erase a meta file table
#
# Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
import uuid

import Common.EdkLogger as EdkLogger

from MetaDataTable import Table, TableFile
from MetaDataTable import ConvertToSqlString
from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \
                                      MODEL_FILE_OTHERS

class MetaFileTable(Table):
    # TRICK: use file ID as the part before '.'
    _ID_STEP_ = 0.00000001
    _ID_MAX_ = 0.99999999

    ## Constructor
    def __init__(self, Cursor, MetaFile, FileType, Temporary):
        self.MetaFile = MetaFile

        self._FileIndexTable = TableFile(Cursor)
        self._FileIndexTable.Create(False)

        FileId = self._FileIndexTable.GetFileId(MetaFile)
        if not FileId:
            FileId = self._FileIndexTable.InsertFile(MetaFile, FileType)

        if Temporary:
            TableName = "_%s_%s_%s" % (FileType, FileId, uuid.uuid4().hex)
        else:
            TableName = "_%s_%s" % (FileType, FileId)

        #Table.__init__(self, Cursor, TableName, FileId, False)
        Table.__init__(self, Cursor, TableName, FileId, Temporary)
        self.Create(not self.IsIntegrity())

    def IsIntegrity(self):
        try:
            TimeStamp = self.MetaFile.TimeStamp
            Result = self.Cur.execute("select ID from %s where ID<0" % (self.Table)).fetchall()
            if not Result:
                # update the timestamp in database
                self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp)                
                return False

            if TimeStamp != self._FileIndexTable.GetFileTimeStamp(self.IdBase):
                # update the timestamp in database
                self._FileIndexTable.SetFileTimeStamp(self.IdBase, TimeStamp)
                return False
        except Exception, Exc:
            EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc))
            return False
        return True

## Python class representation of table storing module data
class ModuleTable(MetaFileTable):
    _ID_STEP_ = 0.00000001
    _ID_MAX_  = 0.99999999
    _COLUMN_ = '''
        ID REAL PRIMARY KEY,
        Model INTEGER NOT NULL,
        Value1 TEXT NOT NULL,
        Value2 TEXT,
        Value3 TEXT,
        Scope1 TEXT,
        Scope2 TEXT,
        BelongsToItem REAL NOT NULL,
        StartLine INTEGER NOT NULL,
{'fraction_non_alphanumeric': 0.07536636427076064, 'fraction_numerical': 0.01674808094905792, 'mean_word_length': 2.57196261682243, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612906', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1618, 'n_words': 504}