<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * 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.
 *****************************************************************************/

#pragma once

#include "medialibrary/IQuery.h"
#include "SqliteConnection.h"
#include "SqliteTools.h"

#include <vector>
#include <functional>
#include <string>

namespace medialibrary
{

template <typename Impl, typename Intf, typename... RequestParams>
class SqliteQuery : public IQuery<Intf>
{
public:
    using Result = typename IQuery<Intf>::Result;

    template <typename... Params>
    SqliteQuery( MediaLibraryPtr ml, std::string field, std::string base,
                     Params&&... params )
        : m_ml( ml )
        , m_field( field )
        , m_base( base )
        , m_params( std::forward<Params>( params )... )
        , m_count( 0 )
        , m_hasCount( false )
    {
    }

    virtual size_t count() override
    {
        if ( m_hasCount == true )
            return m_count;

        std::string req = "SELECT COUNT() " + m_base;
        auto dbConn = m_ml->getConn();
        auto ctx = dbConn->acquireReadContext();
        sqlite::Statement stmt( dbConn->handle(), req );
        stmt.execute( m_params );
        auto row = stmt.row();
        row >> m_count;
        assert(stmt.row() == nullptr );
        m_hasCount = true;
        return m_count;
    }

    virtual Result items( uint32_t nbItems, uint32_t offset ) override
    {
        if ( nbItems == 0 && offset == 0 )
            return all();
        const std::string req = "SELECT " + m_field + " " + m_base + " LIMIT ? OFFSET ?";
        return Impl::template fetchAll<Intf>( m_ml, req, m_params, nbItems, offset );
    }

    virtual Result all() override
    {
        const std::string req = "SELECT " + m_field + " " + m_base;
        return Impl::template fetchAll<Intf>( m_ml, req, m_params );
    }

private:
    MediaLibraryPtr m_ml;
    std::string m_field;
    std::string m_base;
    std::tuple<typename std::decay<RequestParams>::type...> m_params;<commit_msg>SqliteQuery: Log COUNT request & request duration<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 <NAME>, Videolabs
 *
 * 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.
 *****************************************************************************/

#pragma once

#include "medialibrary/IQuery.h"
#include "SqliteConnection.h"
#include "SqliteTools.h"

#include <vector>
#include <functional>
#include <string>

namespace medialibrary
{

template <typename Impl, typename Intf, typename... RequestParams>
class SqliteQuery : public IQuery<Intf>
{
public:
    using Result = typename IQuery<Intf>::Result;

    template <typename... Params>
    SqliteQuery( MediaLibraryPtr ml, std::string field, std::string base,
                     Params&&... params )
        : m_ml( ml )
        , m_field( field )
        , m_base( base )
        , m_params( std::forward<Params>( params )... )
        , m_count( 0 )
        , m_hasCount( false )
    {
    }

    virtual size_t count() override
    {
        if ( m_hasCount == true )
            return m_count;

        std::string req = "SELECT COUNT() " + m_base;
        auto dbConn = m_ml->getConn();
        auto ctx = dbConn->acquireReadContext();
        auto chrono = std::chrono::steady_clock::now();
        sqlite::Statement stmt( dbConn->handle(), req );
        stmt.execute( m_params );
        auto duration = std::chrono::steady_clock::now() - chrono;
        LOG_DEBUG("Executed ", req, " in ",
                 std::chrono::duration_cast<std::chrono::microseconds>( duration ).count(), "µs" );
        auto row = stmt.row();
        row >> m_count;
        assert(stmt.row() == nullptr );
        m_hasCount = true;
        return m_count;
    }

    virtual Result items( uint32_t nbItems, uint32_t offset ) override
    {
        if ( nbItems == 0 && offset == 0 )
            return all();
        const std::string req = "SELECT " + m_field + " " + m_base + " LIMIT ? OFFSET ?";
        return Impl::template fetchAll<Intf>( m_ml, req, m_params, nbItems, offset );
    }

    virtual Result all() override
    {
        const std::string req = "SELECT " + m_field + " " + m_base;
        return Impl::template fetchAll<Intf>( m_ml, req, m_params );
    }

private:
    MediaLibraryPtr m_ml;
    std::string m_field;
    std::string m_base;
    std::tuple<typename std::decay<RequestParams>::type...> m_params;
{'fraction_non_alphanumeric': 0.17887618740943487, 'fraction_numerical': 0.0077282241184994365, 'mean_word_length': 3.1003300330033, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1176989', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1695, 'n_words': 572}