<commit_before>/*
 * $Id: query.cc,v 2.13 2005/10/20 11:24:12 martin Exp $
 * Copyright (c) 2004, 2005, Voidsoft AB
 */

#include "query.hh"

Query::Query(TDSPP* tds)
    : TDS(tds), endoffile(false) {
    rows = new Rows();
}

Query::~Query() {
    delete rows;
}

void Query::init(void) {
}


void Query::execute(void) {
    if (ct_command(TDS->cmd, CS_LANG_CMD, 
                   (CS_CHAR*)command.c_str(), CS_NULLTERM, 
                   CS_UNUSED) != CS_SUCCEED)
        throw TDSPP::Exception("Query::execute: ct_command() failed");
    if (ct_send(TDS->cmd) != CS_SUCCEED) throw TDSPP::Exception("Query::execute: ct_send() failed");
    
    getresults();
    first();
}


void Query::getrc(void) {
    if (ct_res_info(TDS->cmd, CS_ROW_COUNT, 
                    &rowcount, CS_UNUSED, NULL) != CS_SUCCEED)
        throw TDSPP::Exception("Query::getrc: ct_res_info() failed");
}

void Query::addrow(void) {
    Rows::FieldList fl;
    rows->currentrow = rows->rows.size();
    rows->rows.push_back(fl);
    for (int i=0; i < fieldcount; i ++) {
        if (!addfield()) {
            rows->rows.pop_back();
            break;
        }
    }
}

bool Query::addfield(void) {
    CS_DATAFMT datafmt;
    

    if (ct_describe(TDS->cmd, 
                    rows->rows[rows->currentrow].size()+1, &datafmt) 
        != CS_SUCCEED) {
        throw TDSPP::Exception("Query::addfield: ct_describe() failed");
    }
    
    /** DATETYPE is reported as 8 bytes from server, 
     *  but becomes 32 when converted. Adjusting for that.
     */
    if (datafmt.maxlength < 32) datafmt.maxlength = 32;
    
    //if (datafmt.format == CS_FMT_UNUSED) return false;
    datafmt.format    = CS_FMT_NULLTERM;
    datafmt.datatype  = CS_CHAR_TYPE;
    if (datafmt.maxlength>1024) datafmt.maxlength = 1024;
    
    Field* f = new Field(datafmt.name, datafmt.maxlength);
    CS_INT datalength;
    CS_SMALLINT ind;

    if (ct_bind(TDS->cmd, 
                rows->rows[rows->currentrow].size()+1, 
                &datafmt, 
                f->data, 
                &datalength, 
                &ind) != CS_SUCCEED) {
        delete f;
        throw TDSPP::Exception("Query::addfield: ct_bind() failed");
    }
    rows->rows[rows->currentrow].push_back(f);
    return true;   
}
   
void Query::getresults(void) {
    
    CS_RETCODE ret;
    CS_RETCODE results_ret;
    CS_INT result_type;
    CS_INT count;
    while ((results_ret = ct_results(TDS->cmd, &result_type)) == CS_SUCCEED) {
        switch ((int) result_type) {
        case CS_CMD_SUCCEED:
            getrc();
            break;
        case CS_CMD_DONE:
            getrc();
            break;
        case CS_CMD_FAIL:
            throw TDSPP::Exception("Query::getresults: ct_results() failed.");
        case CS_ROW_RESULT:
            if (ct_res_info(TDS->cmd, 
                            CS_NUMDATA, 
                            &fieldcount, 
                            CS_UNUSED, 
                            NULL) != CS_SUCCEED)
                throw TDSPP::Exception("Query::getresults: ct_res_info() failed");
            rows->clear();
            do {
                addrow();
                ret = ct_fetch(TDS->cmd, 
                               CS_UNUSED, 
                               CS_UNUSED, 
                               CS_UNUSED, 
                               &count);
            } while (ret == CS_SUCCEED);

            switch ((int) ret) {
            case CS_END_DATA: {
                // Delete unused fields in extra row.
                Rows::FIte i;
                for (i = rows->rows.back().begin(); i != rows->rows.back().end(); ++i) {
                    delete (*i);
                }
                rows->rows.pop_back();
                break;
	    }
            case CS_ROW_FAIL:
                throw TDSPP::Exception("Query::getresults: ct_fetch() returned CS_ROW_FAIL.");
            case CS_FAIL:
                throw TDSPP::Exception("Query::getresults: ct_fetch() returned CS_FAIL.");
            default:
                throw TDSPP::Exception("Query::getresults: ct_fetch() unexpected return.");
            } // switch
            
            rows->currentrow = 0;
            rowcount = rows->rows.size();
            return;
        case CS_COMPUTE_RESULT:
            throw TDSPP::Exception("Query::getresults: ct_results() unexpected CS_COMPUTE_RESULT.");
        default:
            throw TDSPP::Exception("Query::getresults: ct_results() unexpected result_type.");
        } // switch
    } // while
    
    switch ((int) results_ret) {
    case CS_END_RESULTS:
        break;
    case CS_FAIL:
        throw TDSPP::Exception("Query::getresults: ct_results() failed.");
    default:<commit_msg>Not return while ct_results == CS_SUCCESS<commit_after>/*
 * $Id: query.cc,v 2.13 2005/10/20 11:24:12 martin Exp $
 * Copyright (c) 2004, 2005, Voidsoft AB
 */

#include "query.hh"

Query::Query(TDSPP* tds)
    : TDS(tds), endoffile(false) {
    rows = new Rows();
}

Query::~Query() {
    delete rows;
}

void Query::init(void) {
}


void Query::execute(void) {
    if (ct_command(TDS->cmd, CS_LANG_CMD, 
                   (CS_CHAR*)command.c_str(), CS_NULLTERM, 
                   CS_UNUSED) != CS_SUCCEED)
        throw TDSPP::Exception("Query::execute: ct_command() failed");
    if (ct_send(TDS->cmd) != CS_SUCCEED) throw TDSPP::Exception("Query::execute: ct_send() failed");
    
    getresults();
    first();
}


void Query::getrc(void) {
    if (ct_res_info(TDS->cmd, CS_ROW_COUNT, 
                    &rowcount, CS_UNUSED, NULL) != CS_SUCCEED)
        throw TDSPP::Exception("Query::getrc: ct_res_info() failed");
}

void Query::addrow(void) {
    Rows::FieldList fl;
    rows->currentrow = rows->rows.size();
    rows->rows.push_back(fl);
    for (int i=0; i < fieldcount; i ++) {
        if (!addfield()) {
            rows->rows.pop_back();
            break;
        }
    }
}

bool Query::addfield(void) {
    CS_DATAFMT datafmt;
    

    if (ct_describe(TDS->cmd, 
                    rows->rows[rows->currentrow].size()+1, &datafmt) 
        != CS_SUCCEED) {
        throw TDSPP::Exception("Query::addfield: ct_describe() failed");
    }
    
    /** DATETYPE is reported as 8 bytes from server, 
     *  but becomes 32 when converted. Adjusting for that.
     */
    if (datafmt.maxlength < 32) datafmt.maxlength = 32;
    
    //if (datafmt.format == CS_FMT_UNUSED) return false;
    datafmt.format    = CS_FMT_NULLTERM;
    datafmt.datatype  = CS_CHAR_TYPE;
    if (datafmt.maxlength>1024) datafmt.maxlength = 1024;
    
    Field* f = new Field(datafmt.name, datafmt.maxlength);
    CS_INT datalength;
    CS_SMALLINT ind;

    if (ct_bind(TDS->cmd, 
                rows->rows[rows->currentrow].size()+1, 
                &datafmt, 
                f->data, 
                &datalength, 
                &ind) != CS_SUCCEED) {
        delete f;
        throw TDSPP::Exception("Query::addfield: ct_bind() failed");
    }
    rows->rows[rows->currentrow].push_back(f);
    return true;   
}
   
void Query::getresults(void) {
    
    CS_RETCODE ret;
    CS_RETCODE results_ret;
    CS_INT result_type;
    CS_INT count;
    while ((results_ret = ct_results(TDS->cmd, &result_type)) == CS_SUCCEED) {
        switch ((int) result_type) {
        case CS_CMD_SUCCEED:
            getrc();
            break;
        case CS_CMD_DONE:
            getrc();
            break;
        case CS_CMD_FAIL:
            throw TDSPP::Exception("Query::getresults: ct_results() failed.");
        case CS_ROW_RESULT:
            if (ct_res_info(TDS->cmd, 
                            CS_NUMDATA, 
                            &fieldcount, 
                            CS_UNUSED, 
                            NULL) != CS_SUCCEED)
                throw TDSPP::Exception("Query::getresults: ct_res_info() failed");
            rows->clear();
            do {
                addrow();
                ret = ct_fetch(TDS->cmd, 
                               CS_UNUSED, 
                               CS_UNUSED, 
                               CS_UNUSED, 
                               &count);
            } while (ret == CS_SUCCEED);

            switch ((int) ret) {
            case CS_END_DATA: {
                // Delete unused fields in extra row.
                Rows::FIte i;
                for (i = rows->rows.back().begin(); i != rows->rows.back().end(); ++i) {
                    delete (*i);
                }
                rows->rows.pop_back();
                break;
	    }
            case CS_ROW_FAIL:
                throw TDSPP::Exception("Query::getresults: ct_fetch() returned CS_ROW_FAIL.");
            case CS_FAIL:
                throw TDSPP::Exception("Query::getresults: ct_fetch() returned CS_FAIL.");
            default:
                throw TDSPP::Exception("Query::getresults: ct_fetch() unexpected return.");
            } // switch
            
            rows->currentrow = 0;
            rowcount = rows->rows.size();
            break;
        case CS_COMPUTE_RESULT:
            throw TDSPP::Exception("Query::getresults: ct_results() unexpected CS_COMPUTE_RESULT.");
        default:
            throw TDSPP::Exception("Query::getresults: ct_results() unexpected result_type.");
        } // switch
    } // while
    
    switch ((int) results_ret) {
    case CS_END_RESULTS:
        break;
    case CS_FAIL:
        throw TDSPP::Exception("Query::getresults: ct_results() failed.");
    default:
{'fraction_non_alphanumeric': 0.12907307813982916, 'fraction_numerical': 0.009279763787830856, 'mean_word_length': 1.760186263096624, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '1473724', 'n_tokens_mistral': 3111, 'n_tokens_neox': 2925, 'n_words': 577}