<commit_before>// vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
/*
The MIT License

Copyright (c) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
                                       http://flusspferd.org/contributors.txt)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "sqlite_cursor.hpp"
#include "sqlite.hpp"
#include <sstream>
#include <new>

namespace sqlite3_plugin {

using namespace flusspferd;

///////////////////////////
// 'Private' constructor that is called from sqlite3::cursor
sqlite3_cursor::sqlite3_cursor(object const &obj, sqlite3_stmt *_sth)
: base_type(obj)
, sth(_sth)
, state(CursorState_Init)
{
}


///////////////////////////
sqlite3_cursor::~sqlite3_cursor()
{
    close();
}

///////////////////////////
void sqlite3_cursor::close() {
    if (sth) {
        sqlite3_finalize(sth);
        sth = NULL;
    }
}

///////////////////////////
void sqlite3_cursor::reset() {
    sqlite3_reset(sth);
    state = CursorState_Init;
}

///////////////////////////
object sqlite3_cursor::next() {
    local_root_scope scope;

    if (!sth){
        throw exception("SQLite3.Cursor.next called on closed cursor");
    }

    switch (state) {
        case CursorState_Finished:
            // We've seen the last row, remember it and return the 
            // EOF indicator
            return object();
        case CursorState_Errored:
            throw exception("SQLite3.Cursor: This cursor has seen an error and"
                            " needs to be reset");
        default:
            break;    
    }

    int code = sqlite3_step(sth);

    if (code == SQLITE_DONE) {
        state = CursorState_Finished;
        return object();
    } else if (code != SQLITE_ROW) {
        if (sqlite3_errcode( sqlite3_db_handle(sth) ) != SQLITE_OK) {
            state = CursorState_Errored;
            raise_sqlite_error();
        } else {
            throw exception("SQLite3.Cursor: database reported misuse error. "
                            "Please try again");
        }
    }

    state = CursorState_InProgress;

    // Build up the row object.
    array row = create_array();
    int cols = sqlite3_column_count(sth);

    for (int i=0; i < cols; i++)
    {
        int type = sqlite3_column_type(sth, i);
        value col;
    
        switch (type) {
            case SQLITE_INTEGER:
                col = value(sqlite3_column_int(sth, i));
                break;
            case SQLITE_FLOAT:
                col = value(sqlite3_column_double(sth, i));
                break;
            case SQLITE_NULL:
                col = object();
                break;
            case SQLITE_BLOB:
            {
                // TODO: Implement blog - issue #104
                throw exception("Blob data type not supported yet");
            }
            case SQLITE_TEXT:
            {
                char16_t *text  = (char16_t*)sqlite3_column_text16(sth, i);
                
                if (!text) {
                    // Checking if it was an allocation error if so we throw
                    raise_sqlite_error();
                }
                // Its actually num of *bytes* not chars
                size_t length = sqlite3_column_bytes16(sth, i) / 2;
                col = string(text, length);
            }
            break;
            default:
            {
                std::stringstream ss;
                ss << "SQLite3.Cursor.next: Unknown column type " << type;
                throw exception(ss.str().c_str());
            }
            break;
        }
        row.set_element(i, col);
    }
    return row;<commit_msg>Added support for blob data types - fixes #104<commit_after>// vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
/*
The MIT License

Copyright (c) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
                                       http://flusspferd.org/contributors.txt)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "sqlite_cursor.hpp"
#include "sqlite.hpp"
#include <sstream>
#include <new>

namespace sqlite3_plugin {

using namespace flusspferd;

///////////////////////////
// 'Private' constructor that is called from sqlite3::cursor
sqlite3_cursor::sqlite3_cursor(object const &obj, sqlite3_stmt *_sth)
: base_type(obj)
, sth(_sth)
, state(CursorState_Init)
{
}


///////////////////////////
sqlite3_cursor::~sqlite3_cursor()
{
    close();
}

///////////////////////////
void sqlite3_cursor::close() {
    if (sth) {
        sqlite3_finalize(sth);
        sth = NULL;
    }
}

///////////////////////////
void sqlite3_cursor::reset() {
    sqlite3_reset(sth);
    state = CursorState_Init;
}

///////////////////////////
object sqlite3_cursor::next() {
    local_root_scope scope;

    if (!sth){
        throw exception("SQLite3.Cursor.next called on closed cursor");
    }

    switch (state) {
        case CursorState_Finished:
            // We've seen the last row, remember it and return the 
            // EOF indicator
            return object();
        case CursorState_Errored:
            throw exception("SQLite3.Cursor: This cursor has seen an error and"
                            " needs to be reset");
        default:
            break;    
    }

    int code = sqlite3_step(sth);

    if (code == SQLITE_DONE) {
        state = CursorState_Finished;
        return object();
    } else if (code != SQLITE_ROW) {
        if (sqlite3_errcode( sqlite3_db_handle(sth) ) != SQLITE_OK) {
            state = CursorState_Errored;
            raise_sqlite_error();
        } else {
            throw exception("SQLite3.Cursor: database reported misuse error. "
                            "Please try again");
        }
    }

    state = CursorState_InProgress;

    // Build up the row object.
    array row = create_array();
    int cols = sqlite3_column_count(sth);

    for (int i=0; i < cols; i++)
    {
        int type = sqlite3_column_type(sth, i);
        value col;
    
        switch (type) {
            case SQLITE_INTEGER:
                col = value(sqlite3_column_int(sth, i));
                break;
            case SQLITE_FLOAT:
                col = value(sqlite3_column_double(sth, i));
                break;
            case SQLITE_NULL:
                col = object();
                break;
            case SQLITE_BLOB:
            {
                unsigned char * bytes = sqlite3_column_binary(sth, i);
                if (!bytes) {
                    raise_sqlite_error();
                }

                size_t length = sqlite3_column_bytes(sth);
                col = byte_string(object(), bytes, length);
            }
            break;
            case SQLITE_TEXT:
            {
                char16_t *text  = (char16_t*)sqlite3_column_text16(sth, i);
                
                if (!text) {
                    // Checking if it was an allocation error if so we throw
                    raise_sqlite_error();
                }
                // Its actually num of *bytes* not chars
                size_t length = sqlite3_column_bytes16(sth, i) / 2;
                col = string(text, length);
            }
            break;
            default:
            {
                std::stringstream ss;
                ss << "SQLite3.Cursor.next: Unknown column type " << type;
                throw exception(ss.str().c_str());
            }
            break;
        }
        row.set_element(i, col);
    }
    return row;
{'fraction_non_alphanumeric': 0.10992870064914334, 'fraction_numerical': 0.01042886027455571, 'mean_word_length': 1.8565349544072949, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3614323', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2539, 'n_words': 824}