<commit_before>/* sqlite3.hpp
 *
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _SQLITE3_HPP
#define _SQLITE3_HPP

#include <sqlite3.h>

#include <map>
#include <stdexcept>
#include <string>
#include <vector>

class SQLite3
{
public:
    class Exception : public std::runtime_error
    {
    public:
        Exception(const std::string & msg) : runtime_error(msg) {}
        Exception(const char * msg) : runtime_error(msg) {}
    };

    class LibException : public Exception
    {
    public:
        LibException(int code, const std::string & msg) : Exception(msg), ecode{code} {}
        LibException(int code, const char * msg) : Exception(msg), ecode{code} {}
        int code() const noexcept { return ecode; }
        const char * codeStr() const noexcept { return sqlite3_errstr(ecode); }
    protected:
        int ecode;
    };

    struct Blob {
        size_t size;
        const void * data;
    };

    class Statement {
    public:
        enum class StepResult {DONE, ROW, BUSY};

        Statement(const Statement &) = delete;
        Statement & operator=(const Statement &) = delete;

        Statement(SQLite3 & db, const char * sql)
        : db{db}
        {
            auto result = sqlite3_prepare_v2(db.db, sql, -1, &stmt, nullptr);
            if (result != SQLITE_OK)
                throw LibException(result, "Statement");
        };

        Statement(SQLite3 & db, const std::string & sql)
        : db{db}
        {
            auto result = sqlite3_prepare_v2(db.db, sql.c_str(), sql.length() + 1, &stmt, nullptr);
            if (result != SQLITE_OK)
                throw LibException(result, "Statement");
        };

        void bind(int pos, int val)
        {
            auto result = sqlite3_bind_int(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Integer bind failed");
        }

        void bind(int pos, std::int64_t val)
        {
            auto result = sqlite3_bind_int64(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Integer64 bind failed");
        }

        void bind(int pos, double val)
        {
            auto result = sqlite3_bind_double(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Double bind failed");
        }

        void bind(int pos, bool val)
        {
            auto result = sqlite3_bind_int(stmt, pos, val ? 1 : 0);
            if (result != SQLITE_OK)
                throw LibException(result, "Bool bind failed");
        }

        void bind(int pos, const char * val)
        {
            auto result = sqlite3_bind_text(stmt, pos, val, -1, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Text bind failed");
        }

        void bind(int pos, const std::string & val)
        {
            auto result = sqlite3_bind_text(stmt, pos, val.c_str(), -1, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Text bind failed");
        }

        void bind(int pos, const Blob & val)
        {
            auto result = sqlite3_bind_blob(stmt, pos, val.data, val.size, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Blob bind failed");
        }

        void bind(int pos, const std::vector<unsigned char> & val)
        {
            auto result = sqlite3_bind_blob(stmt, pos, val.data(), val.size(), SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Blob bind failed");
        }

        template<typename... Args>
        Statement & bindv(Args && ... args)
        {
            size_t pos{1};
            Pass{(bind(pos++, args), 0)...};
            return *this;
        }

        StepResult step()
        {
            auto result = sqlite3_step(stmt);
            switch (result) {<commit_msg>[sqlite3] Mute sequence-point warnings.<commit_after>/* sqlite3.hpp
 *
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _SQLITE3_HPP
#define _SQLITE3_HPP

#include <sqlite3.h>

#include <map>
#include <stdexcept>
#include <string>
#include <vector>

class SQLite3
{
public:
    class Exception : public std::runtime_error
    {
    public:
        Exception(const std::string & msg) : runtime_error(msg) {}
        Exception(const char * msg) : runtime_error(msg) {}
    };

    class LibException : public Exception
    {
    public:
        LibException(int code, const std::string & msg) : Exception(msg), ecode{code} {}
        LibException(int code, const char * msg) : Exception(msg), ecode{code} {}
        int code() const noexcept { return ecode; }
        const char * codeStr() const noexcept { return sqlite3_errstr(ecode); }
    protected:
        int ecode;
    };

    struct Blob {
        size_t size;
        const void * data;
    };

    class Statement {
    public:
        enum class StepResult {DONE, ROW, BUSY};

        Statement(const Statement &) = delete;
        Statement & operator=(const Statement &) = delete;

        Statement(SQLite3 & db, const char * sql)
        : db{db}
        {
            auto result = sqlite3_prepare_v2(db.db, sql, -1, &stmt, nullptr);
            if (result != SQLITE_OK)
                throw LibException(result, "Statement");
        };

        Statement(SQLite3 & db, const std::string & sql)
        : db{db}
        {
            auto result = sqlite3_prepare_v2(db.db, sql.c_str(), sql.length() + 1, &stmt, nullptr);
            if (result != SQLITE_OK)
                throw LibException(result, "Statement");
        };

        void bind(int pos, int val)
        {
            auto result = sqlite3_bind_int(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Integer bind failed");
        }

        void bind(int pos, std::int64_t val)
        {
            auto result = sqlite3_bind_int64(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Integer64 bind failed");
        }

        void bind(int pos, double val)
        {
            auto result = sqlite3_bind_double(stmt, pos, val);
            if (result != SQLITE_OK)
                throw LibException(result, "Double bind failed");
        }

        void bind(int pos, bool val)
        {
            auto result = sqlite3_bind_int(stmt, pos, val ? 1 : 0);
            if (result != SQLITE_OK)
                throw LibException(result, "Bool bind failed");
        }

        void bind(int pos, const char * val)
        {
            auto result = sqlite3_bind_text(stmt, pos, val, -1, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Text bind failed");
        }

        void bind(int pos, const std::string & val)
        {
            auto result = sqlite3_bind_text(stmt, pos, val.c_str(), -1, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Text bind failed");
        }

        void bind(int pos, const Blob & val)
        {
            auto result = sqlite3_bind_blob(stmt, pos, val.data, val.size, SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Blob bind failed");
        }

        void bind(int pos, const std::vector<unsigned char> & val)
        {
            auto result = sqlite3_bind_blob(stmt, pos, val.data(), val.size(), SQLITE_TRANSIENT);
            if (result != SQLITE_OK)
                throw LibException(result, "Blob bind failed");
        }

        template<typename... Args>
        Statement & bindv(Args && ... args)
        {
            #pragma GCC diagnostic push
            #pragma GCC diagnostic ignored "-Wsequence-point"
            size_t pos{1};
            Pass{(bind(pos++, args), 0)...};
            #pragma GCC diagnostic pop
            return *this;
        }

        StepResult step()
        {
            auto result = sqlite3_step(stmt);
            switch (result) {
{'fraction_non_alphanumeric': 0.10164069755443195, 'fraction_numerical': 0.011247549272520896, 'mean_word_length': 2.0974752317034198, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4018477', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2680, 'n_words': 894}