int cli_insert_fdb(int statement, cli_oid_t* oid) { int rc = cli_send_columns_fdb(statement, cli_cmd_insert); if (rc == cli_ok) { char buf[sizeof(cli_oid_t) + 8]; statement_desc* s = statements.get(statement); if (!s->session->sock->read(buf, sizeof buf)) { rc = cli_network_error; } else { rc = unpack4(buf); s->prepared = true; s->oid = unpack_oid(buf + 8); if (oid != NULL) { *oid = s->oid; } if (s->autoincrement) { int4 rowid = unpack4(buf + 4); for (column_binding* cb = s->columns; cb != NULL; cb = cb->next) { if (cb->var_type == cli_autoincrement) { *(int4*)cb->var_ptr = rowid; } } } } } return rc; }
int cli_insert(int statement, cli_oid_t* oid) { int rc = cli_send_columns(statement, cli_cmd_insert); if (rc == cli_ok) { char buf[sizeof(cli_oid_t) + 4]; statement_desc* s = statements.get(statement); if (!s->session->sock->read(buf, sizeof buf)) { rc = cli_network_error; } else { rc = unpack4(buf); s->prepared = true; s->oid = unpack_oid(buf + 4); if (oid != NULL) { *oid = s->oid; } } } return rc; }
static int cli_get(int statement, int cmd) { statement_desc* s = statements.get(statement); if (s == NULL) { return cli_bad_descriptor; } if (!s->prepared) { return cli_not_fetched; } cli_request req; req.length = sizeof(cli_request); req.cmd = cmd; req.stmt_id = statement; req.pack(); if (!s->session->sock->write(&req, sizeof req)) { return cli_network_error; } int4 response; if (!s->session->sock->read(&response, sizeof response)) { return cli_network_error; } unpack4(response); if (response <= 0) { return response; } dbSmallBuffer buf(response-4); if (!s->session->sock->read(buf, response-4)) { return cli_network_error; } char* p = buf; s->oid = unpack_oid(p); if (s->oid == 0) { return cli_not_found; } p += sizeof(cli_oid_t); for (column_binding* cb = s->columns; cb != NULL; cb = cb->next) { if (cb->set_fnc != NULL) { int len = unpack4(p); p += 4; char* dst = (char*)cb->set_fnc(cb->var_type, cb->var_ptr, len); if (cb->var_type >= cli_array_of_oid) { switch (sizeof_type[cb->var_type-cli_array_of_oid]) { case 2: while (--len >= 0) { p = unpack2(dst, p); dst += 2; } break; case 4: while (--len >= 0) { p = unpack4(dst, p); dst += 4; } break; case 8: while (--len >= 0) { p = unpack8(dst, p); dst += 8; } break; default: memcpy(dst, p, len); p += len; } } else { memcpy(dst, p, len); p += len; } } else { if (cb->var_type >= cli_asciiz) { int len = unpack4(p); p += 4; char* dst = (char*)cb->var_ptr; char* src = p; int n = len; if (cb->var_len != NULL) { if (n > *cb->var_len) { n = *cb->var_len; } *cb->var_len = n; } if (cb->var_type >= cli_array_of_oid) { switch (sizeof_type[cb->var_type-cli_array_of_oid]) { case 2: while (--n >= 0) { src = unpack2(dst, src); dst += 2; } p += len*2; break; case 4: while (--n >= 0) { src = unpack4(dst, src); dst += 4; } p += len*4; break; case 8: while (--n >= 0) { src = unpack8(dst, src); dst += 8; } p += len*8; break; default: memcpy(dst, p, n); p += len; } } else { if (cb->var_type == cli_pasciiz) { dst = *(char**)dst; } memcpy(dst, p, n); p += len; } } else { switch (sizeof_type[cb->var_type]) { case 2: p = unpack2((char*)cb->var_ptr, p); break; case 4: p = unpack4((char*)cb->var_ptr, p); break; case 8: p = unpack8((char*)cb->var_ptr, p); break; default: *(char*)cb->var_ptr = *p++; } } } } return cli_ok; }
static int cli_get_fdb(int statement, int cmd, cli_oid_t value = 0) { statement_desc* s = statements.get(statement); if (s == NULL) { return cli_bad_descriptor; } if (!s->prepared) { return cli_not_fetched; } struct get_req { cli_request req; cli_oid_t value; } get; int length = sizeof(cli_request); if (cmd == cli_cmd_skip) { length += 4; pack4((char*)(&get.req+1), (int)value); } else if (cmd == cli_cmd_seek) { length += sizeof(cli_oid_t); pack_oid((char*)(&get.req+1), value); } get.req.length = length; get.req.cmd = cmd; get.req.stmt_id = statement; get.req.pack(); if (!s->session->sock->write(&get.req, length)) { return cli_network_error; } int4 response; if (!s->session->sock->read(&response, sizeof response)) { return cli_network_error; } unpack4(response); if (response <= 0) { return response; } if (s->buf_size < (size_t)response-4) { delete[] s->buf; s->buf_size = response-4 < DEFAULT_BUF_SIZE ? DEFAULT_BUF_SIZE : response-4; s->buf = new char[s->buf_size]; } char* buf = s->buf; if (!s->session->sock->read(buf, response-4)) { return cli_network_error; } char* p = buf; int result = cli_ok; if (cmd == cli_cmd_seek) { s->oid = value; result = unpack_oid(p); } else { s->oid = unpack_oid(p); if (s->oid == 0) { return cli_not_found; } } p += sizeof(cli_oid_t); for (column_binding* cb = s->columns; cb != NULL; cb = cb->next) { int type = *p++; if (cb->var_type == cli_any) { cb->var_type = type; } else { assert(cb->var_type == type); } if (cb->set_fnc != NULL) { int len = unpack4(p); p += 4; char* dst = (char*)cb->set_fnc(cb->var_type, cb->var_ptr, len, cb->name, statement, p, cb->user_data); if (dst == NULL) { continue; } if (cb->var_type == cli_array_of_string) { char** s = (char**)dst; while (--len >= 0) { *s++ = p; p += strlen(p) + 1; } } else if (cb->var_type == cli_array_of_wstring) { wchar_t** s = (wchar_t**)dst; while (--len >= 0) { *s++ = (wchar_t*)p; p += (wcslen((wchar_t*)p) + 1)*sizeof(wchar_t); } } else if (cb->var_type >= cli_array_of_oid && cb->var_type < cli_array_of_string) { switch (sizeof_type[cb->var_type-cli_array_of_oid]) { case 2: while (--len >= 0) { p = unpack2(dst, p); dst += 2; } break; case 4: while (--len >= 0) { p = unpack4(dst, p); dst += 4; } break; case 8: while (--len >= 0) { p = unpack8(dst, p); dst += 8; } break; default: memcpy(dst, p, len); p += len; } } else { memcpy(dst, p, len); p += len; } } else { if (cb->var_type >= cli_asciiz && (cb->var_type <= cli_array_of_string || cb->var_type == cli_array_of_wstring)) { int len = unpack4(p); p += 4; char* dst = (char*)cb->var_ptr; char* src = p; int n = len; if (cb->var_len != NULL) { if (n > *cb->var_len) { n = *cb->var_len; } *cb->var_len = n; } if (cb->var_type == cli_wstring || cb->var_type == cli_pwstring) { if (cb->var_type == cli_pwstring) { dst = *(char**)dst; } memcpy(dst, p, n*sizeof(wchar_t)); p += len*sizeof(wchar_t); } else if (cb->var_type >= cli_array_of_oid) { if (cb->var_type == cli_array_of_string) { char** s = (char**)dst; len -= n; while (--n >= 0) { *s++ = p; p += strlen(p) + 1; } while (--len >= 0) { p += strlen(p) + 1; } } else if (cb->var_type == cli_array_of_wstring) { wchar_t** s = (wchar_t**)dst; len -= n; while (--n >= 0) { *s++ = (wchar_t*)p; p += (wcslen((wchar_t*)p) + 1)*sizeof(wchar_t); } while (--len >= 0) { p += (wcslen((wchar_t*)p) + 1)*sizeof(wchar_t); } } else { switch (sizeof_type[cb->var_type-cli_array_of_oid]) { case 2: while (--n >= 0) { src = unpack2(dst, src); dst += 2; } p += len*2; break; case 4: while (--n >= 0) { src = unpack4(dst, src); dst += 4; } p += len*4; break; case 8: while (--n >= 0) { src = unpack8(dst, src); dst += 8; } p += len*8; break; default: memcpy(dst, p, n); p += len; } } } else { if (cb->var_type == cli_pasciiz) { dst = *(char**)dst; } memcpy(dst, p, n); p += len; } } else if (cb->var_type == cli_rectangle) { p = unpack_rectangle((cli_rectangle_t*)cb->var_ptr, p); } else { switch (sizeof_type[cb->var_type]) { case 2: p = unpack2((char*)cb->var_ptr, p); break; case 4: p = unpack4((char*)cb->var_ptr, p); break; case 8: p = unpack8((char*)cb->var_ptr, p); break; default: *(char*)cb->var_ptr = *p++; } } } } s->updated = false; return result; }