int my_stmt_result(const char *buff) { MYSQL_STMT *stmt; int row_count; int rc; if (!opt_silent) fprintf(stdout, "\n\n %s", buff); stmt= mysql_simple_prepare(mysql, buff); check_stmt(stmt); rc= mysql_stmt_execute(stmt); check_execute(stmt, rc); row_count= my_process_stmt_result(stmt); mysql_stmt_close(stmt); return row_count; }
static int zcond(i_ctx_t *i_ctx_p) { os_ptr op = osp; es_ptr ep = esp; /* Push the array on the e-stack and call the continuation. */ if (!r_is_array(op)) return_op_typecheck(op); check_execute(*op); if ((r_size(op) & 1) != 0) return_error(e_rangecheck); if (r_size(op) == 0) return zpop(i_ctx_p); check_estack(3); esp = ep += 3; ref_assign(ep - 2, op); /* the cond body */ make_op_estack(ep - 1, cond_continue); array_get(imemory, op, 0L, ep); esfile_check_cache(); pop(1); return o_push_estack; }
void stmt_fetch_init(Stmt_fetch *fetch, unsigned stmt_no_arg, const char *query_arg) { unsigned long type= CURSOR_TYPE_READ_ONLY; int rc; unsigned i; MYSQL_RES *metadata; DBUG_ENTER("stmt_fetch_init"); /* Save query and statement number for error messages */ fetch->stmt_no= stmt_no_arg; fetch->query= query_arg; fetch->handle= mysql_stmt_init(mysql); rc= mysql_stmt_prepare(fetch->handle, fetch->query, (ulong)strlen(fetch->query)); check_execute(fetch->handle, rc); /* The attribute is sent to server on execute and asks to open read-only for result set */ mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE, (const void*) &type); rc= mysql_stmt_execute(fetch->handle); check_execute(fetch->handle, rc); /* Find out total number of columns in result set */ metadata= mysql_stmt_result_metadata(fetch->handle); fetch->column_count= mysql_num_fields(metadata); mysql_free_result(metadata); /* Now allocate bind handles and buffers for output data: calloc memory to reduce number of MYSQL_BIND members we need to set up. */ fetch->bind_array= (MYSQL_BIND *) calloc(1, sizeof(MYSQL_BIND) * fetch->column_count); fetch->out_data= (char**) calloc(1, sizeof(char*) * fetch->column_count); fetch->out_data_length= (ulong*) calloc(1, sizeof(ulong) * fetch->column_count); for (i= 0; i < fetch->column_count; ++i) { fetch->out_data[i]= (char*) calloc(1, MAX_COLUMN_LENGTH); fetch->bind_array[i].buffer_type= MYSQL_TYPE_STRING; fetch->bind_array[i].buffer= fetch->out_data[i]; fetch->bind_array[i].buffer_length= MAX_COLUMN_LENGTH; fetch->bind_array[i].length= fetch->out_data_length + i; } mysql_stmt_bind_result(fetch->handle, fetch->bind_array); fetch->row_count= 0; fetch->is_open= TRUE; /* Ready for reading rows */ DBUG_VOID_RETURN; }
static int my_process_stmt_result(MYSQL_STMT *stmt) { int field_count; int row_count= 0; MYSQL_BIND buffer[MAX_RES_FIELDS]; MYSQL_FIELD *field; MYSQL_RES *result; char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE]; ulong length[MAX_RES_FIELDS]; my_bool is_null[MAX_RES_FIELDS]; int rc, i; if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */ { while (!mysql_stmt_fetch(stmt)) row_count++; return row_count; } field_count= MY_MIN(mysql_num_fields(result), MAX_RES_FIELDS); memset(buffer, 0, sizeof(buffer)); memset(length, 0, sizeof(length)); memset(is_null, 0, sizeof(is_null)); for(i= 0; i < field_count; i++) { buffer[i].buffer_type= MYSQL_TYPE_STRING; buffer[i].buffer_length= MAX_FIELD_DATA_SIZE; buffer[i].length= &length[i]; buffer[i].buffer= (void *) data[i]; buffer[i].is_null= &is_null[i]; } rc= mysql_stmt_bind_result(stmt, buffer); check_execute(stmt, rc); rc= 1; mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc); rc= mysql_stmt_store_result(stmt); check_execute(stmt, rc); my_print_result_metadata(result); mysql_field_seek(result, 0); while ((rc= mysql_stmt_fetch(stmt)) == 0) { if (!opt_silent) { fputc('\t', stdout); fputc('|', stdout); } mysql_field_seek(result, 0); for (i= 0; i < field_count; i++) { field= mysql_fetch_field(result); if (!opt_silent) { if (is_null[i]) fprintf(stdout, " %-*s |", (int) field->max_length, "NULL"); else if (length[i] == 0) { data[i][0]= '\0'; /* unmodified buffer */ fprintf(stdout, " %*s |", (int) field->max_length, data[i]); } else if (IS_NUM(field->type)) fprintf(stdout, " %*s |", (int) field->max_length, data[i]); else fprintf(stdout, " %-*s |", (int) field->max_length, data[i]); } } if (!opt_silent) { fputc('\t', stdout); fputc('\n', stdout); } row_count++; } DIE_UNLESS(rc == MYSQL_NO_DATA); if (!opt_silent) { if (row_count) my_print_dashes(result); fprintf(stdout, "\n\t%d %s returned\n", row_count, row_count == 1 ? "row" : "rows"); } mysql_free_result(result); return row_count; }
my_bool fetch_n(const char **query_list, unsigned query_count, enum fetch_type fetch_type) { unsigned open_statements= query_count; int rc, error_count= 0; Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) * query_count); Stmt_fetch *fetch; DBUG_ENTER("fetch_n"); for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { /* Init will exit(1) in case of error */ stmt_fetch_init(fetch, fetch - fetch_array, query_list[fetch - fetch_array]); } if (fetch_type == USE_STORE_RESULT) { for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { rc= mysql_stmt_store_result(fetch->handle); check_execute(fetch->handle, rc); } } while (open_statements) { for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch))) { open_statements--; /* We try to fetch from the rest of the statements in case of error */ if (rc != MYSQL_NO_DATA) { fprintf(stderr, "Got error reading rows from statement %d,\n" "query is: %s,\n" "error message: %s", (int) (fetch - fetch_array), fetch->query, mysql_stmt_error(fetch->handle)); error_count++; } } } } if (error_count) fprintf(stderr, "Fetch FAILED"); else { unsigned total_row_count= 0; for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) total_row_count+= fetch->row_count; if (!opt_silent) printf("Success, total rows fetched: %d\n", total_row_count); } for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) stmt_fetch_close(fetch); free(fetch_array); DBUG_RETURN(error_count != 0); }