⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localcli.cpp

📁 用于嵌入式环境的数据库
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    }
    if (!stmt->prepared) { 
        sql_scanner scanner(stmt->sql.base());
        if (scanner.get() != tkn_insert
            || scanner.get() != tkn_into
            || scanner.get() != tkn_ident)
        {
            return cli_bad_statement;
        }
        int rc = match_columns(scanner.identifier(), stmt);
        if (rc != cli_ok) {
            return rc;
        }
        stmt->prepared = true;
    }
    dbSmallBuffer buf(stmt->table->appSize);    
    char* obj = buf.base();
    memset(obj, 0, stmt->table->appSize);  
    dbFieldDescriptor *first = stmt->table->columns, *fd = first;
    do { 
        if (fd->appType == dbField::tpString) { 
            *(char**)(obj + fd->appOffs) = "";
        }
    } while ((fd = fd->next) != first);
    
    int rc = store_columns(buf.base(), stmt);
    if (rc != cli_ok) { 
        return rc;
    }

    dbAnyReference ref;
    stmt->session->db->insertRecord(stmt->table, &ref, buf.base());
    stmt->oid = ref.getOid();
    if (oid != NULL) { 
        *oid = ref.getOid();
    }
    if (stmt->n_autoincremented_columns > 0) { 
        for (column_binding* cb = stmt->columns; cb != NULL; cb = cb->next) {
            if (cb->var_type == cli_autoincrement) { 
                *(cli_int4_t*)cb->var_ptr = ref.getOid();
            }
        }
    }
    return cli_ok;
}

int cli_update(int statement)
{
    return dbCLI::instance.update(statement);
}

int dbCLI::update(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if (!stmt->for_update) {
        return cli_not_update_mode;
    }
    if (stmt->updated) { 
        return cli_already_updated;
    }
    if (stmt->cursor.isEmpty()) { 
        return cli_not_found;
    }
    if (stmt->record_struct == NULL) { 
        dbSmallBuffer buf(stmt->table->appSize); 
        char* record = buf.base();
        memset(record, 0, stmt->table->appSize);
        stmt->cursor.setRecord((byte*)record);
        stmt->cursor.fetch();
        
        int rc = store_columns(buf.base(), stmt);
        if (rc != cli_ok) { 
            return rc;
        }
        stmt->cursor.update();
    } else { 
        stmt->cursor.update();
    }
    stmt->updated = true;
    return cli_ok;
}

int cli_freeze(int statement)
{
    return dbCLI::instance.freeze(statement);
}

int dbCLI::freeze(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    stmt->cursor.freeze();
    return cli_ok;
}

int cli_unfreeze(int statement)
{
    return dbCLI::instance.unfreeze(statement);
}

int dbCLI::unfreeze(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    stmt->cursor.unfreeze();
    return cli_ok;
}

int cli_get_first(int statement)
{
    return dbCLI::instance.get_first(statement);
}

int dbCLI::get_first(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if (!stmt->cursor.gotoFirst()) { 
        return cli_not_found;
    }
    return fetch_columns(stmt);
}

int cli_get_last(int statement)
{
    return dbCLI::instance.get_last(statement);
}

int dbCLI::get_last(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if (!stmt->cursor.gotoLast()) { 
        return cli_not_found;
    }
    return fetch_columns(stmt);
}

int cli_get_next(int statement)
{
    return dbCLI::instance.get_next(statement);
}

int dbCLI::get_next(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if (!((stmt->first_fetch && stmt->cursor.gotoFirst()) ||
          (!stmt->first_fetch && stmt->cursor.gotoNext())))
    {
        return cli_not_found;
    }
    return fetch_columns(stmt);
}

int cli_get_prev(int statement)
{
    return dbCLI::instance.get_prev(statement);
}

int dbCLI::get_prev(int statement)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if (!((stmt->first_fetch && stmt->cursor.gotoLast()) ||
          (!stmt->first_fetch && stmt->cursor.gotoPrev())))
    {
        return cli_not_found;
    }
    return fetch_columns(stmt);
}

int cli_skip(int statement, int n)
{
    return dbCLI::instance.skip(statement, n);
}

int dbCLI::skip(int statement, int n)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    if ((n > 0 && !((stmt->first_fetch && stmt->cursor.gotoFirst() && stmt->cursor.skip(n-1)
                     || (!stmt->first_fetch && stmt->cursor.skip(n)))))
        || (n < 0 && !((stmt->first_fetch && stmt->cursor.gotoLast() && stmt->cursor.skip(n+1)
                        || (!stmt->first_fetch && stmt->cursor.skip(n))))))
    {
        return cli_not_found;
    } 
    return fetch_columns(stmt);
}


int cli_seek(int statement, cli_oid_t oid)
{
    return dbCLI::instance.seek(statement, oid);
}

int dbCLI::seek(int statement, cli_oid_t oid)
{
    statement_desc* stmt = statements.get(statement);    
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    if (!stmt->prepared) { 
        return cli_not_fetched;
    }
    int pos = stmt->cursor.seek(oid);
    if (pos < 0) { 
        return cli_not_found;
    } 
    int rc = fetch_columns(stmt);
    if (rc == cli_ok) { 
        return pos;
    } else { 
        return rc;
    }
}


cli_oid_t cli_get_oid(int statement)
{
    return dbCLI::instance.get_current_oid(statement);
}

cli_oid_t dbCLI::get_current_oid(int statement)
{
    statement_desc* s = statements.get(statement);
    if (s == NULL) {
        return (cli_oid_t)cli_bad_descriptor;
    }
    return s->cursor.currId;
}


int cli_free(int statement)
{
    return dbCLI::instance.free_statement(statement);
}

int dbCLI::free_statement(int statement)
{
    statement_desc* stmt = statements.get(statement);
    if (stmt == NULL) {
        return cli_bad_descriptor;
    }
    return free_statement(stmt);
}

int dbCLI::free_statement(statement_desc* stmt)
{
    {
        dbCriticalSection cs(stmt->session->mutex);
        statement_desc *sp, **spp = &stmt->session->stmts;
        while ((sp = *spp) != stmt) {
            if (sp == NULL) {
                return cli_bad_descriptor;
            }
            spp = &sp->next;
        }
        *spp = stmt->next;
    }
    column_binding *cb, *next_cb;
    for (cb = stmt->columns; cb != NULL; cb = next_cb) {
        next_cb = cb->next;
        delete[] cb->name;
        column_allocator.free(cb);
    }
    parameter_binding *pb, *next_pb;
    for (pb = stmt->params; pb != NULL; pb = next_pb) {
        next_pb = pb->next;
        delete[] pb->name;
        parameter_allocator.free(pb);
    }
    statements.free(stmt);
    return cli_ok;
}


int cli_commit(int session)
{
    return dbCLI::instance.commit(session);
}

int dbCLI::commit(int session)
{
    session_desc* s = sessions.get(session);
    if (s == NULL) {
        return cli_bad_descriptor;
    }
    while (s->dropped_tables != NULL) {
        dbTableDescriptor* next = s->dropped_tables->nextDbTable;
        delete s->dropped_tables;
        s->dropped_tables = next;
    }
    s->db->commit();
    s->existed_tables = s->db->tables;
    return cli_ok;
}

int cli_precommit(int session)
{
    return dbCLI::instance.precommit(session);
}

int dbCLI::precommit(int session)
{
    session_desc* s = sessions.get(session);
    if (s == NULL) {
        return cli_bad_descriptor;
    }
    s->db->precommit();
    return cli_ok;
}

int cli_abort(int session)
{
    return dbCLI::instance.abort(session);
}

int dbCLI::abort(int session)
{
    session_desc* s = sessions.get(session);
    if (s == NULL) {
        return cli_bad_descriptor;
    }
    dbDatabase* db = s->db;
    while (s->dropped_tables != NULL) {
        dbTableDescriptor* next = s->dropped_tables->nextDbTable;
        db->linkTable(s->dropped_tables, s->dropped_tables->tableId);
        s->dropped_tables = next;
    }
    s->db->rollback();
    while (db->tables != s->existed_tables) { 
        dbTableDescriptor* table = db->tables;
        db->unlinkTable(table);
        delete table;
    }
    return cli_ok;
}


int cli_remove(int statement)
{
    return dbCLI::instance.remove(statement);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -