📄 libmysql.c
字号:
DBUG_RETURN(0);}/**************************************************************************** Alloc result struct for buffered results. All rows are read to buffer.** mysql_data_seek may be used.**************************************************************************/MYSQL_RES * STDCALLmysql_store_result(MYSQL *mysql){ MYSQL_RES *result; DBUG_ENTER("mysql_store_result"); if (!mysql->fields) DBUG_RETURN(0); if (mysql->status != MYSQL_STATUS_GET_RESULT) { strmov(mysql->net.last_error, ER(mysql->net.last_errno=CR_COMMANDS_OUT_OF_SYNC)); DBUG_RETURN(0); } mysql->status=MYSQL_STATUS_READY; /* server is ready */ if (!(result=(MYSQL_RES*) my_malloc(sizeof(MYSQL_RES)+ sizeof(ulong)*mysql->field_count, MYF(MY_WME | MY_ZEROFILL)))) { mysql->net.last_errno=CR_OUT_OF_MEMORY; strmov(mysql->net.last_error, ER(mysql->net.last_errno)); DBUG_RETURN(0); } result->eof=1; /* Marker for buffered */ result->lengths=(ulong*) (result+1); if (!(result->data=read_rows(mysql,mysql->fields,mysql->field_count))) { my_free((gptr) result,MYF(0)); DBUG_RETURN(0); } mysql->affected_rows= result->row_count= result->data->rows; result->data_cursor= result->data->data; result->fields= mysql->fields; result->field_alloc= mysql->field_alloc; result->field_count= mysql->field_count; result->current_field=0; result->current_row=0; /* Must do a fetch first */ mysql->fields=0; /* fields is now in result */ DBUG_RETURN(result); /* Data fetched */}/**************************************************************************** Alloc struct for use with unbuffered reads. Data is fetched by domand** when calling to mysql_fetch_row.** mysql_data_seek is a noop.**** No other queries may be specified with the same MYSQL handle.** There shouldn't be much processing per row because mysql server shouldn't** have to wait for the client (and will not wait more than 30 sec/packet).**************************************************************************/MYSQL_RES * STDCALLmysql_use_result(MYSQL *mysql){ MYSQL_RES *result; DBUG_ENTER("mysql_use_result"); if (!mysql->fields) DBUG_RETURN(0); if (mysql->status != MYSQL_STATUS_GET_RESULT) { strmov(mysql->net.last_error, ER(mysql->net.last_errno=CR_COMMANDS_OUT_OF_SYNC)); DBUG_RETURN(0); } if (!(result=(MYSQL_RES*) my_malloc(sizeof(*result)+ sizeof(ulong)*mysql->field_count, MYF(MY_WME | MY_ZEROFILL)))) DBUG_RETURN(0); result->lengths=(ulong*) (result+1); if (!(result->row=(MYSQL_ROW) my_malloc(sizeof(result->row[0])*(mysql->field_count+1), MYF(MY_WME)))) { /* Ptrs: to one row */ my_free((gptr) result,MYF(0)); DBUG_RETURN(0); } result->fields= mysql->fields; result->field_alloc= mysql->field_alloc; result->field_count= mysql->field_count; result->current_field=0; result->handle= mysql; result->current_row= 0; mysql->fields=0; /* fields is now in result */ mysql->status=MYSQL_STATUS_USE_RESULT; DBUG_RETURN(result); /* Data is read to be fetched */}/**************************************************************************** Return next field of the query results**************************************************************************/MYSQL_FIELD * STDCALLmysql_fetch_field(MYSQL_RES *result){ if (result->current_field >= result->field_count) return(NULL); return &result->fields[result->current_field++];}/**************************************************************************** Return next row of the query results**************************************************************************/MYSQL_ROW STDCALLmysql_fetch_row(MYSQL_RES *res){ DBUG_ENTER("mysql_fetch_row"); if (!res->data) { /* Unbufferred fetch */ if (!res->eof) { if (!(read_one_row(res->handle,res->field_count,res->row, res->lengths))) { res->row_count++; DBUG_RETURN(res->current_row=res->row); } else { DBUG_PRINT("info",("end of data")); res->eof=1; res->handle->status=MYSQL_STATUS_READY; /* Don't clear handle in mysql_free_results */ res->handle=0; } } DBUG_RETURN((MYSQL_ROW) NULL); } { MYSQL_ROW tmp; if (!res->data_cursor) { DBUG_PRINT("info",("end of data")); DBUG_RETURN(res->current_row=(MYSQL_ROW) NULL); } tmp = res->data_cursor->data; res->data_cursor = res->data_cursor->next; DBUG_RETURN(res->current_row=tmp); }}/**************************************************************************** Get column lengths of the current row** If one uses mysql_use_result, res->lengths contains the length information,** else the lengths are calculated from the offset between pointers.**************************************************************************/ulong * STDCALLmysql_fetch_lengths(MYSQL_RES *res){ ulong *lengths,*prev_length; byte *start; MYSQL_ROW column,end; if (!(column=res->current_row)) return 0; /* Something is wrong */ if (res->data) { start=0; prev_length=0; /* Keep gcc happy */ lengths=res->lengths; for (end=column+res->field_count+1 ; column != end ; column++,lengths++) { if (!*column) { *lengths=0; /* Null */ continue; } if (start) /* Found end of prev string */ *prev_length= (uint) (*column-start-1); start= *column; prev_length=lengths; } } return res->lengths;}/**************************************************************************** Move to a specific row and column**************************************************************************/void STDCALLmysql_data_seek(MYSQL_RES *result, my_ulonglong row){ MYSQL_ROWS *tmp=0; DBUG_PRINT("info",("mysql_data_seek(%ld)",(long) row)); if (result->data) for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ; result->current_row=0; result->data_cursor = tmp;}/*************************************************************************** put the row or field cursor one a position one got from mysql_row_tell()** This doesn't restore any data. The next mysql_fetch_row or** mysql_fetch_field will return the next row or field after the last used*************************************************************************/MYSQL_ROW_OFFSET STDCALLmysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET row){ MYSQL_ROW_OFFSET return_value=result->data_cursor; result->current_row= 0; result->data_cursor= row; return return_value;}MYSQL_FIELD_OFFSET STDCALLmysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET field_offset){ MYSQL_FIELD_OFFSET return_value=result->current_field; result->current_field=field_offset; return return_value;}/******************************************************************************* List all databases*****************************************************************************/MYSQL_RES * STDCALLmysql_list_dbs(MYSQL *mysql, const char *wild){ char buff[255]; DBUG_ENTER("mysql_list_dbs"); append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild); if (mysql_query(mysql,buff)) DBUG_RETURN(0); DBUG_RETURN (mysql_store_result(mysql));}/******************************************************************************* List all tables in a database** If wild is given then only the tables matching wild is returned*****************************************************************************/MYSQL_RES * STDCALLmysql_list_tables(MYSQL *mysql, const char *wild){ char buff[255]; DBUG_ENTER("mysql_list_tables"); append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild); if (mysql_query(mysql,buff)) DBUG_RETURN(0); DBUG_RETURN (mysql_store_result(mysql));}/**************************************************************************** List all fields in a table** If wild is given then only the fields matching wild is returned** Instead of this use query:** show fields in 'table' like "wild"**************************************************************************/MYSQL_RES * STDCALLmysql_list_fields(MYSQL *mysql, const char *table, const char *wild){ MYSQL_RES *result; MYSQL_DATA *query; char buff[257],*end; DBUG_ENTER("mysql_list_fields"); DBUG_PRINT("enter",("table: '%s' wild: '%s'",table,wild ? wild : "")); LINT_INIT(query); end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128); if (simple_command(mysql,COM_FIELD_LIST,buff,(uint) (end-buff),1) || !(query = read_rows(mysql,(MYSQL_FIELD*) 0,6))) DBUG_RETURN(NULL); free_old_query(mysql); if (!(result = (MYSQL_RES *) my_malloc(sizeof(MYSQL_RES), MYF(MY_WME | MY_ZEROFILL)))) { free_rows(query); DBUG_RETURN(NULL); } result->field_alloc=mysql->field_alloc; mysql->fields=0; result->field_count = (uint) query->rows; result->fields= unpack_fields(query,&result->field_alloc, result->field_count,1, (my_bool) test(mysql->server_capabilities & CLIENT_LONG_FLAG)); result->eof=1; DBUG_RETURN(result);}/* List all running processes (threads) in server */MYSQL_RES * STDCALLmysql_list_processes(MYSQL *mysql){ MYSQL_DATA *fields; uint field_count; uchar *pos; DBUG_ENTER("mysql_list_processes"); LINT_INIT(fields); if (simple_command(mysql,COM_PROCESS_INFO,0,0,0)) DBUG_RETURN(0); free_old_query(mysql); pos=(uchar*) mysql->net.read_pos; field_count=(uint) net_field_length(&pos); if (!(fields = read_rows(mysql,(MYSQL_FIELD*) 0,5))) DBUG_RETURN(NULL); if (!(mysql->fields=unpack_fields(fields,&mysql->field_alloc,field_count,0, (my_bool) test(mysql->server_capabilities & CLIENT_LONG_FLAG)))) DBUG_RETURN(0); mysql->status=MYSQL_STATUS_GET_RESULT; mysql->field_count=field_count; DBUG_RETURN(mysql_store_result(mysql));}int STDCALLmysql_create_db(MYSQL *mysql, const char *db){ DBUG_ENTER("mysql_createdb"); DBUG_PRINT("enter",("db: %s",db)); DBUG_RETURN(simple_command(mysql,COM_CREATE_DB,db, (uint) strlen(db),0));}int STDCALLmysql_drop_db(MYSQL *mysql, const char *db){ DBUG_ENTER("mysql_drop_db"); DBUG_PRINT("enter",("db: %s",db)); DBUG_RETURN(simple_command(mysql,COM_DROP_DB,db,(uint) strlen(db),0));}int STDCALLmysql_shutdown(MYSQL *mysql){ DBUG_ENTER("mysql_shutdown"); DBUG_RETURN(simple_command(mysql,COM_SHUTDOWN,0,0,0));}int STDCALLmysql_refresh(MYSQL *mysql,uint options){ uchar bits[1]; DBUG_ENTER("mysql_refresh"); bits[0]= (uchar) options; DBUG_RETURN(simple_command(mysql,COM_REFRESH,(char*) bits,1,0));}int STDCALLmysql_kill(MYSQL *mysql,ulong pid){ char buff[12]; DBUG_ENTER("mysql_kill"); int4store(buff,pid); DBUG_RETURN(simple_command(mysql,COM_PROCESS_KILL,buff,4,0));}int STDCALLmysql_dump_debug_info(MYSQL *mysql){ DBUG_ENTER("mysql_dump_debug_info"); DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0));}char * STDCALLmysql_stat(MYSQL *mysql){ DBUG_ENTER("mysql_stat"); if (simple_command(mysql,COM_STATISTICS,0,0,0)) return mysql->net.last_error; mysql->net.read_pos[mysql->packet_length]=0; /* End of stat string */ if (!mysql->net.read_pos[0]) { mysql->net.last_errno=CR_WRONG_HOST_INFO; strmov(mysql->net.last_error, ER(mysql->net.last_errno)); return mysql->net.last_error; } DBUG_RETURN((char*) mysql->net.read_pos);}int STDCALLmysql_ping(MYSQL *mysql){ DBUG_ENTER("mysql_ping"); DBUG_RETURN(simple_command(mysql,COM_PING,0,0,0));}char * STDCALLmysql_get_server_info(MYSQL *mysql){ return((char*) mysql->server_version);}char * STDCALLmysql_get_host_info(MYSQL *mysql){ return(mysql->host_info);}uint STDCALLmysql_get_proto_info(MYSQL *mysql){ return (mysql->protocol_version);}char * STDCALLmysql_get_client_info(void){ return (char*) MYSQL_SERVER_VERSION;}int STDCALLmysql_options(MYSQL *mysql,enum mysql_option option, const char *arg){ DBUG_ENTER("mysql_option"); DBUG_PRINT("enter",("option: %d",(int) option)); switch (option) { case MYSQL_OPT_CONNECT_TIMEOUT: mysql->options.connect_timeout= *(uint*) arg; break; case MYSQL_OPT_COMPRESS: mysql->options.compress= 1; /* Remember for connect */ break; case MYSQL_OPT_NAMED_PIPE: mysql->options.named_pipe=1; /* Force named pipe */ break; case MYSQL_OPT_LOCAL_INFILE: /* Allow LOAD DATA LOCAL ?*/ if (!arg || test(*(uint*) arg)) mysql->options.client_flag|= CLIENT_LOCAL_FILES; else mysql->options.client_flag&= ~CLIENT_LOCAL_FILES; break; case MYSQL_INIT_COMMAND: my_free(mysql->options.init_command,MYF(MY_ALLOW_ZERO_PTR)); mysql->options.init_command=my_strdup(arg,MY
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -