void print_int_col( /*==========*/ FILE* stream, const ib_tpl_t tpl, int i, ib_col_meta_t* col_meta) { ib_err_t err = DB_SUCCESS; switch (col_meta->type_len) { case 1: { if (col_meta->attr & IB_COL_UNSIGNED) { ib_u8_t u8; err = ib_tuple_read_u8(tpl, i, &u8); fprintf(stream, "%u", u8); } else { ib_i8_t i8; err = ib_tuple_read_i8(tpl, i, &i8); fprintf(stream, "%d", i8); } break; } case 2: { if (col_meta->attr & IB_COL_UNSIGNED) { ib_u16_t u16; err = ib_tuple_read_u16(tpl, i, &u16); fprintf(stream, "%u", u16); } else { ib_i16_t i16; err = ib_tuple_read_i16(tpl, i, &i16); fprintf(stream, "%d", i16); } break; } case 4: { if (col_meta->attr & IB_COL_UNSIGNED) { ib_u32_t u32; err = ib_tuple_read_u32(tpl, i, &u32); fprintf(stream, "%u", u32); } else { ib_i32_t i32; err = ib_tuple_read_i32(tpl, i, &i32); fprintf(stream, "%d", i32); } break; } case 8: { if (col_meta->attr & IB_COL_UNSIGNED) { ib_u64_t u64; err = ib_tuple_read_u64(tpl, i, &u64); fprintf(stream, "%lu", u64); } else { ib_i64_t i64; err = ib_tuple_read_i64(tpl, i, &i64); fprintf(stream, "%ld", i64); } break; } default: assert(0); break; } assert(err == DB_SUCCESS); }
/********************************************************************* UPDATE T SET c1 = RANDOM(string), c3 = MOD(c3 + 1, 10) WHERE c3 = MOD(RANDOM(INT), 10); */ static ib_err_t update_random_rows( /*===============*/ ib_crsr_t crsr) { ib_i32_t c3; ib_err_t err; ib_i32_t key; int res = ~0; ib_crsr_t index_crsr; ib_tpl_t sec_key_tpl; /* Open the secondary index. */ err = ib_cursor_open_index_using_name(crsr, "c3", &index_crsr); assert(err == DB_SUCCESS); /* Set the record lock mode */ err = ib_cursor_set_lock_mode(index_crsr, IB_LOCK_X); assert(err == DB_SUCCESS); /* Since we will be updating the clustered index record, set the need to access clustered index flag in the cursor. */ ib_cursor_set_cluster_access(index_crsr); /* Create a tuple for searching the secondary index. */ sec_key_tpl = ib_sec_search_tuple_create(index_crsr); assert(sec_key_tpl != NULL); /* Set the value to look for. */ #ifdef __WIN__ key = rand() % 10; #else key = random() % 10; #endif err = ib_tuple_write_i32(sec_key_tpl, 0, key); assert(err == DB_SUCCESS); /* Search for the key using the cluster index (PK) */ err = ib_cursor_moveto(index_crsr, sec_key_tpl, IB_CUR_GE, &res); assert(err == DB_SUCCESS || err == DB_END_OF_INDEX || err == DB_RECORD_NOT_FOUND); ib_tuple_delete(sec_key_tpl); /* Match found */ if (res == 0) { int l; char* ptr; const char* first; ib_ulint_t data_len; ib_col_meta_t col_meta; ib_ulint_t first_len; ib_tpl_t old_tpl = NULL; ib_tpl_t new_tpl = NULL; /* Create the tuple instance that we will use to update the table. old_tpl is used for reading the existing row and new_tpl will contain the update row data. */ old_tpl = ib_clust_read_tuple_create(crsr); assert(old_tpl != NULL); new_tpl = ib_clust_read_tuple_create(crsr); assert(new_tpl != NULL); err = ib_cursor_read_row(index_crsr, old_tpl); assert(err == DB_SUCCESS); /* Get the first column value. */ first = ib_col_get_value(old_tpl, 0); first_len = ib_col_get_meta(old_tpl, 0, &col_meta); /* There are no SQL_NULL values in our test data. */ assert(first != NULL); /* Copy the old contents to the new tuple. */ err = ib_tuple_copy(new_tpl, old_tpl); /* Update the c3 column in the new tuple. */ data_len = ib_col_get_meta(old_tpl, 2, &col_meta); assert(data_len != IB_SQL_NULL); err = ib_tuple_read_i32(old_tpl, 2, &c3); assert(err == DB_SUCCESS); assert(c3 == key); c3 = (c3 + 1) % 10; ptr = (char*) malloc(8192); l = gen_rand_text(ptr, 128); /* Get the new text to insert. */ l = gen_rand_text(ptr, 8192); /* Set the new key value in the new tuple. */ err = ib_col_set_value(new_tpl, 0, ptr, l); assert(err == DB_SUCCESS); /* Get the new text to insert. */ l = gen_rand_text(ptr, 8192); /* Set the c2 value in the new tuple. */ err = ib_col_set_value(new_tpl, 1, ptr, l); assert(err == DB_SUCCESS); /* Set the updated c3 value in the new tuple. */ err = ib_tuple_write_i32(new_tpl, 2, c3); assert(err == DB_SUCCESS); /* NOTE: We are using the secondary index cursor to update the record and not the cluster index cursor. */ err = ib_cursor_update_row(index_crsr, old_tpl, new_tpl); assert(err == DB_SUCCESS || err == DB_DUPLICATE_KEY); /* Reset the old and new tuple instances. */ old_tpl = ib_tuple_clear(old_tpl); assert(old_tpl != NULL); new_tpl = ib_tuple_clear(new_tpl); assert(new_tpl != NULL); free(ptr); ib_tuple_delete(old_tpl); ib_tuple_delete(new_tpl); } err = ib_cursor_close(index_crsr); assert(err == DB_SUCCESS); return(err); }