<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 <NAME>, Videolabs, VideoLAN
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <cassert>

#include "Label.h"
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"

namespace medialibrary
{

const std::string Label::Table::Name = "Label";
const std::string Label::Table::PrimaryKeyColumn = "id_label";
int64_t Label::* const Label::Table::PrimaryKey = &Label::m_id;
const std::string Label::FileRelationTable::Name = "LabelFileRelation";

Label::Label(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
    , m_id( row.extract<decltype(m_id)>() )
    , m_name( row.extract<decltype (m_name)>() )
{
    assert( row.hasRemainingColumns() == false );
}

Label::Label( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
    , m_name( name )
{
}

int64_t Label::id() const
{
    return m_id;
}

const std::string& Label::name() const
{
    return m_name;
}

Query<IMedia> Label::media()
{
    static const std::string req = "FROM " + Media::Table::Name + " f "
            "INNER JOIN " + FileRelationTable::Name + " lfr ON lfr.media_id = f.id_media "
            "WHERE lfr.label_id = ?";
    return make_query<Media, IMedia>( m_ml, "f.*", req, "", m_id );
}

LabelPtr Label::create( MediaLibraryPtr ml, const std::string& name )
{
    auto self = std::make_shared<Label>( ml, name );
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
    if ( insert( ml, self, req, self->m_name ) == false )
        return nullptr;
    return self;
}

std::string Label::schema( const std::string& tableName, uint32_t )
{
    if ( tableName == FileRelationTable::Name )
    {
        return "CREATE TABLE " + FileRelationTable::Name +
        "("
            "label_id INTEGER,"
            "media_id INTEGER,"
            "PRIMARY KEY(label_id,media_id),"
            "FOREIGN KEY(label_id) "
                "REFERENCES " + Table::Name + "(id_label) ON DELETE CASCADE,"
            "FOREIGN KEY(media_id) "
                "REFERENCES " + Media::Table::Name + "(id_media) ON DELETE CASCADE"
        ")";
    }
    assert( tableName == Table::Name );
    return "CREATE TABLE " + Table::Name +
    "("
        "id_label INTEGER PRIMARY KEY AUTOINCREMENT,"
        "name TEXT UNIQUE ON CONFLICT FAIL"
    ")";
}

std::string Label::trigger( Triggers trigger, uint32_t dbModel )
{
    assert( trigger == Triggers::DeleteFts );
    return "CREATE TRIGGER " + triggerName( trigger, dbModel ) +
           " BEFORE DELETE ON " + Table::Name +
           " BEGIN"
           " UPDATE " + Media::FtsTable::Name +
                " SET labels = TRIM(REPLACE(labels, old.name, ''))"
                " WHERE labels MATCH old.name;"
                " END";
}

std::string Label::triggerName( Triggers trigger, uint32_t )
{
    UNUSED_IN_RELEASE( trigger );

    assert( trigger == Triggers::DeleteFts );
    return "delete_label_fts";
}

std::string Label::index(Label::Indexes index, uint32_t dbModel)
{
    switch ( index )
    {
        case Indexes::MediaId:
            assert( dbModel >= 34 );
            return "CREATE INDEX " + indexName( index, dbModel ) +
                    " ON " + FileRelationTable::Name + "(media_id)";
    }
    return "<invalid request>";
}

std::string Label::indexName(Label::Indexes index, uint32_t dbModel)
{
    switch ( index )
    {
        case Indexes::MediaId:
            assert( dbModel >= 34 );
            return "label_rel_media_id_idx";
    }
    return "<invalid request>";
}

bool Label::checkDbModel( MediaLibraryPtr ml )
{
    OPEN_READ_CONTEXT( ctx, ml->getConn() );

    return sqlite::Tools::checkTableSchema(
                                       schema( Table::Name, Settings::DbModelVersion ),
                                       Table::Name ) &&
           sqlite::Tools::checkTableSchema(
                                       schema( FileRelationTable::Name, Settings::DbModelVersion ),
                                       FileRelationTable::Name ) &&
           sqlite::Tools::checkTriggerStatement(
                 trigger( Triggers::DeleteFts, Settings::DbModelVersion ),
                 triggerName( Triggers::DeleteFts, Settings::DbModelVersion ) ) &&
           sqlite::Tools::checkIndexStatement(
                 index( Indexes::MediaId, Settings::DbModelVersion ),
                 indexName( Indexes::MediaId, Settings::DbModelVersion ) );<commit_msg>Label: Cosmetics<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 <NAME>, Videolabs, VideoLAN
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <cassert>

#include "Label.h"
#include "Media.h"
#include "database/SqliteTools.h"
#include "database/SqliteQuery.h"

namespace medialibrary
{

const std::string Label::Table::Name = "Label";
const std::string Label::Table::PrimaryKeyColumn = "id_label";
int64_t Label::* const Label::Table::PrimaryKey = &Label::m_id;
const std::string Label::FileRelationTable::Name = "LabelFileRelation";

Label::Label(MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
    , m_id( row.extract<decltype(m_id)>() )
    , m_name( row.extract<decltype (m_name)>() )
{
    assert( row.hasRemainingColumns() == false );
}

Label::Label( MediaLibraryPtr ml, const std::string& name )
    : m_ml( ml )
    , m_id( 0 )
    , m_name( name )
{
}

int64_t Label::id() const
{
    return m_id;
}

const std::string& Label::name() const
{
    return m_name;
}

Query<IMedia> Label::media()
{
    static const std::string req = "FROM " + Media::Table::Name + " f "
            "INNER JOIN " + FileRelationTable::Name + " lfr ON lfr.media_id = f.id_media "
            "WHERE lfr.label_id = ?";
    return make_query<Media, IMedia>( m_ml, "f.*", req, "", m_id );
}

LabelPtr Label::create( MediaLibraryPtr ml, const std::string& name )
{
    auto self = std::make_shared<Label>( ml, name );
    const char* req = "INSERT INTO Label VALUES(NULL, ?)";
    if ( insert( ml, self, req, self->m_name ) == false )
        return nullptr;
    return self;
}

std::string Label::schema( const std::string& tableName, uint32_t )
{
    if ( tableName == FileRelationTable::Name )
    {
        return "CREATE TABLE " + FileRelationTable::Name +
        "("
            "label_id INTEGER,"
            "media_id INTEGER,"
            "PRIMARY KEY(label_id,media_id),"
            "FOREIGN KEY(label_id) "
                "REFERENCES " + Table::Name + "(id_label) ON DELETE CASCADE,"
            "FOREIGN KEY(media_id) "
                "REFERENCES " + Media::Table::Name + "(id_media) ON DELETE CASCADE"
        ")";
    }
    assert( tableName == Table::Name );
    return "CREATE TABLE " + Table::Name +
    "("
        "id_label INTEGER PRIMARY KEY AUTOINCREMENT,"
        "name TEXT UNIQUE ON CONFLICT FAIL"
    ")";
}

std::string Label::trigger( Triggers trigger, uint32_t dbModel )
{
    assert( trigger == Triggers::DeleteFts );
    return "CREATE TRIGGER " + triggerName( trigger, dbModel ) +
           " BEFORE DELETE ON " + Table::Name +
           " BEGIN"
           " UPDATE " + Media::FtsTable::Name +
                " SET labels = TRIM(REPLACE(labels, old.name, ''))"
                " WHERE labels MATCH old.name;"
                " END";
}

std::string Label::triggerName( Triggers trigger, uint32_t )
{
    UNUSED_IN_RELEASE( trigger );

    assert( trigger == Triggers::DeleteFts );
    return "delete_label_fts";
}

std::string Label::index( Label::Indexes index, uint32_t dbModel )
{
    switch ( index )
    {
        case Indexes::MediaId:
            assert( dbModel >= 34 );
            return "CREATE INDEX " + indexName( index, dbModel ) +
                    " ON " + FileRelationTable::Name + "(media_id)";
    }
    return "<invalid request>";
}

std::string Label::indexName( Label::Indexes index, uint32_t dbModel )
{
    switch ( index )
    {
        case Indexes::MediaId:
            assert( dbModel >= 34 );
            return "label_rel_media_id_idx";
    }
    return "<invalid request>";
}

bool Label::checkDbModel( MediaLibraryPtr ml )
{
    OPEN_READ_CONTEXT( ctx, ml->getConn() );

    return sqlite::Tools::checkTableSchema(
                                       schema( Table::Name, Settings::DbModelVersion ),
                                       Table::Name ) &&
           sqlite::Tools::checkTableSchema(
                                       schema( FileRelationTable::Name, Settings::DbModelVersion ),
                                       FileRelationTable::Name ) &&
           sqlite::Tools::checkTriggerStatement(
                 trigger( Triggers::DeleteFts, Settings::DbModelVersion ),
                 triggerName( Triggers::DeleteFts, Settings::DbModelVersion ) ) &&
           sqlite::Tools::checkIndexStatement(
                 index( Indexes::MediaId, Settings::DbModelVersion ),
                 indexName( Indexes::MediaId, Settings::DbModelVersion ) );
{'fraction_non_alphanumeric': 0.1619815035253182, 'fraction_numerical': 0.00732533650764582, 'mean_word_length': 2.8923734853884535, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655629', 'n_tokens_mistral': 3265, 'n_tokens_neox': 3123, 'n_words': 849}