Commit b01c4261 authored by Alexey Botchkov's avatar Alexey Botchkov

MDEV-19275 Provide SQL service to plugins.

Protocol_local fixed so it can be used now.
Some Protocol:: methods made virtual so they can adapt.
as well as net_ok and net_send_error functions.
execute_sql_string function is exported to the plugins.
To be changed with the mysql_use_result.
parent 68cba091
...@@ -491,8 +491,8 @@ int emb_load_querycache_result(THD *thd, Querycache_stream *src) ...@@ -491,8 +491,8 @@ int emb_load_querycache_result(THD *thd, Querycache_stream *src)
*prev_row= NULL; *prev_row= NULL;
data->embedded_info->prev_ptr= prev_row; data->embedded_info->prev_ptr= prev_row;
return_ok: return_ok:
net_send_eof(thd, thd->server_status, thd->protocol->net_send_eof(thd, thd->server_status,
thd->get_stmt_da()->current_statement_warn_count()); thd->get_stmt_da()->current_statement_warn_count());
DBUG_RETURN(0); DBUG_RETURN(0);
err: err:
DBUG_RETURN(1); DBUG_RETURN(1);
......
...@@ -81,4 +81,3 @@ class Querycache_stream ...@@ -81,4 +81,3 @@ class Querycache_stream
uint emb_count_querycache_size(THD *thd); uint emb_count_querycache_size(THD *thd);
int emb_load_querycache_result(THD *thd, Querycache_stream *src); int emb_load_querycache_result(THD *thd, Querycache_stream *src);
void emb_store_querycache_result(Querycache_stream *dst, THD* thd); void emb_store_querycache_result(Querycache_stream *dst, THD* thd);
bool net_send_eof(THD *thd, uint server_status, uint total_warn_count);
...@@ -1139,7 +1139,7 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags) ...@@ -1139,7 +1139,7 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags)
for (uint pos= 0 ; (item= it++); pos++) for (uint pos= 0 ; (item= it++); pos++)
{ {
if (prot.store_field_metadata(thd, item, pos)) if (prot.store_item_metadata(thd, item, pos))
goto err; goto err;
} }
...@@ -1254,8 +1254,7 @@ bool Protocol_binary::write() ...@@ -1254,8 +1254,7 @@ bool Protocol_binary::write()
@retval FALSE Success @retval FALSE Success
*/ */
bool bool Protocol::net_send_ok(THD *thd,
net_send_ok(THD *thd,
uint server_status, uint statement_warn_count, uint server_status, uint statement_warn_count,
ulonglong affected_rows, ulonglong id, const char *message, ulonglong affected_rows, ulonglong id, const char *message,
bool, bool) bool, bool)
...@@ -1290,7 +1289,7 @@ net_send_ok(THD *thd, ...@@ -1290,7 +1289,7 @@ net_send_ok(THD *thd,
*/ */
bool bool
net_send_eof(THD *thd, uint server_status, uint statement_warn_count) Protocol::net_send_eof(THD *thd, uint server_status, uint statement_warn_count)
{ {
bool error= write_eof_packet(thd, server_status, statement_warn_count); bool error= write_eof_packet(thd, server_status, statement_warn_count);
thd->cur_data= 0; thd->cur_data= 0;
...@@ -1298,8 +1297,8 @@ net_send_eof(THD *thd, uint server_status, uint statement_warn_count) ...@@ -1298,8 +1297,8 @@ net_send_eof(THD *thd, uint server_status, uint statement_warn_count)
} }
bool net_send_error_packet(THD *thd, uint sql_errno, const char *err, bool Protocol::net_send_error_packet(THD *thd, uint sql_errno, const char *err,
const char *sqlstate) const char *sqlstate)
{ {
uint error; uint error;
char converted_err[MYSQL_ERRMSG_SIZE]; char converted_err[MYSQL_ERRMSG_SIZE];
......
...@@ -1087,11 +1087,6 @@ unsigned int STDCALL mysql_field_count(MYSQL *mysql) ...@@ -1087,11 +1087,6 @@ unsigned int STDCALL mysql_field_count(MYSQL *mysql)
return mysql->field_count; return mysql->field_count;
} }
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql)
{
return mysql->affected_rows;
}
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql) my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
{ {
return mysql->insert_id; return mysql->insert_id;
......
...@@ -3735,6 +3735,12 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql) ...@@ -3735,6 +3735,12 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql)
} }
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql)
{
return mysql->affected_rows;
}
/************************************************************************** /**************************************************************************
Return next row of the query results Return next row of the query results
**************************************************************************/ **************************************************************************/
......
...@@ -2553,7 +2553,7 @@ void close_connection(THD *thd, uint sql_errno) ...@@ -2553,7 +2553,7 @@ void close_connection(THD *thd, uint sql_errno)
if (sql_errno) if (sql_errno)
{ {
net_send_error(thd, sql_errno, ER_DEFAULT(sql_errno), NULL); thd->protocol->net_send_error(thd, sql_errno, ER_DEFAULT(sql_errno), NULL);
thd->print_aborted_warning(lvl, ER_DEFAULT(sql_errno)); thd->print_aborted_warning(lvl, ER_DEFAULT(sql_errno));
} }
else else
......
...@@ -32,13 +32,6 @@ ...@@ -32,13 +32,6 @@
#include <stdarg.h> #include <stdarg.h>
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024; static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
/* Declared non-static only because of the embedded library. */
bool net_send_error_packet(THD *, uint, const char *, const char *);
/* Declared non-static only because of the embedded library. */
bool net_send_ok(THD *, uint, uint, ulonglong, ulonglong, const char *,
bool, bool);
/* Declared non-static only because of the embedded library. */
bool net_send_eof(THD *thd, uint server_status, uint statement_warn_count);
#ifndef EMBEDDED_LIBRARY #ifndef EMBEDDED_LIBRARY
static bool write_eof_packet(THD *, NET *, uint, uint); static bool write_eof_packet(THD *, NET *, uint, uint);
#endif #endif
...@@ -152,11 +145,11 @@ bool Protocol_binary::net_store_data_cs(const uchar *from, size_t length, ...@@ -152,11 +145,11 @@ bool Protocol_binary::net_store_data_cs(const uchar *from, size_t length,
@retval TRUE An error occurred and the message wasn't sent properly @retval TRUE An error occurred and the message wasn't sent properly
*/ */
bool net_send_error(THD *thd, uint sql_errno, const char *err, bool Protocol::net_send_error(THD *thd, uint sql_errno, const char *err,
const char* sqlstate) const char* sqlstate)
{ {
bool error; bool error;
DBUG_ENTER("net_send_error"); DBUG_ENTER("Protocol::net_send_error");
DBUG_ASSERT(!thd->spcont); DBUG_ASSERT(!thd->spcont);
DBUG_ASSERT(sql_errno); DBUG_ASSERT(sql_errno);
...@@ -214,11 +207,11 @@ bool net_send_error(THD *thd, uint sql_errno, const char *err, ...@@ -214,11 +207,11 @@ bool net_send_error(THD *thd, uint sql_errno, const char *err,
#ifndef EMBEDDED_LIBRARY #ifndef EMBEDDED_LIBRARY
bool bool
net_send_ok(THD *thd, Protocol::net_send_ok(THD *thd,
uint server_status, uint statement_warn_count, uint server_status, uint statement_warn_count,
ulonglong affected_rows, ulonglong id, const char *message, ulonglong affected_rows, ulonglong id,
bool is_eof, const char *message, bool is_eof,
bool skip_flush) bool skip_flush)
{ {
NET *net= &thd->net; NET *net= &thd->net;
StringBuffer<MYSQL_ERRMSG_SIZE + 10> store; StringBuffer<MYSQL_ERRMSG_SIZE + 10> store;
...@@ -226,7 +219,7 @@ net_send_ok(THD *thd, ...@@ -226,7 +219,7 @@ net_send_ok(THD *thd,
bool state_changed= false; bool state_changed= false;
bool error= FALSE; bool error= FALSE;
DBUG_ENTER("net_send_ok"); DBUG_ENTER("Protocol::net_send_ok");
if (! net->vio) // hack for re-parsing queries if (! net->vio) // hack for re-parsing queries
{ {
...@@ -329,11 +322,11 @@ static uchar eof_buff[1]= { (uchar) 254 }; /* Marker for end of fields */ ...@@ -329,11 +322,11 @@ static uchar eof_buff[1]= { (uchar) 254 }; /* Marker for end of fields */
*/ */
bool bool
net_send_eof(THD *thd, uint server_status, uint statement_warn_count) Protocol::net_send_eof(THD *thd, uint server_status, uint statement_warn_count)
{ {
NET *net= &thd->net; NET *net= &thd->net;
bool error= FALSE; bool error= FALSE;
DBUG_ENTER("net_send_eof"); DBUG_ENTER("Protocol::net_send_eof");
/* /*
Check if client understand new format packets (OK instead of EOF) Check if client understand new format packets (OK instead of EOF)
...@@ -420,8 +413,8 @@ static bool write_eof_packet(THD *thd, NET *net, ...@@ -420,8 +413,8 @@ static bool write_eof_packet(THD *thd, NET *net,
@retval TRUE An error occurred and the messages wasn't sent properly @retval TRUE An error occurred and the messages wasn't sent properly
*/ */
bool net_send_error_packet(THD *thd, uint sql_errno, const char *err, bool Protocol::net_send_error_packet(THD *thd, uint sql_errno, const char *err,
const char* sqlstate) const char* sqlstate)
{ {
NET *net= &thd->net; NET *net= &thd->net;
...@@ -434,7 +427,7 @@ bool net_send_error_packet(THD *thd, uint sql_errno, const char *err, ...@@ -434,7 +427,7 @@ bool net_send_error_packet(THD *thd, uint sql_errno, const char *err,
char buff[2+1+SQLSTATE_LENGTH+MYSQL_ERRMSG_SIZE], *pos; char buff[2+1+SQLSTATE_LENGTH+MYSQL_ERRMSG_SIZE], *pos;
my_bool ret; my_bool ret;
uint8 save_compress; uint8 save_compress;
DBUG_ENTER("send_error_packet"); DBUG_ENTER("Protocol::send_error_packet");
if (net->vio == 0) if (net->vio == 0)
{ {
...@@ -963,7 +956,7 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags) ...@@ -963,7 +956,7 @@ bool Protocol::send_result_set_metadata(List<Item> *list, uint flags)
for (uint pos= 0; (item=it++); pos++) for (uint pos= 0; (item=it++); pos++)
{ {
prot.prepare_for_resend(); prot.prepare_for_resend();
if (prot.store_field_metadata(thd, item, pos)) if (prot.store_item_metadata(thd, item, pos))
goto err; goto err;
if (prot.write()) if (prot.write())
DBUG_RETURN(1); DBUG_RETURN(1);
...@@ -1043,7 +1036,7 @@ bool Protocol::write() ...@@ -1043,7 +1036,7 @@ bool Protocol::write()
#endif /* EMBEDDED_LIBRARY */ #endif /* EMBEDDED_LIBRARY */
bool Protocol_text::store_field_metadata(THD *thd, Item *item, uint pos) bool Protocol_text::store_item_metadata(THD *thd, Item *item, uint pos)
{ {
Send_field field(thd, item); Send_field field(thd, item);
return store_field_metadata(thd, field, item->charset_for_protocol(), pos); return store_field_metadata(thd, field, item->charset_for_protocol(), pos);
......
...@@ -50,14 +50,13 @@ class Protocol ...@@ -50,14 +50,13 @@ class Protocol
} }
#endif #endif
uint field_count; uint field_count;
#ifndef EMBEDDED_LIBRARY
bool net_store_data(const uchar *from, size_t length);
bool net_store_data_cs(const uchar *from, size_t length,
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
#else
virtual bool net_store_data(const uchar *from, size_t length); virtual bool net_store_data(const uchar *from, size_t length);
virtual bool net_store_data_cs(const uchar *from, size_t length, virtual bool net_store_data_cs(const uchar *from, size_t length,
CHARSET_INFO *fromcs, CHARSET_INFO *tocs); CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
virtual bool net_send_ok(THD *, uint, uint, ulonglong, ulonglong, const char *,
bool, bool);
virtual bool net_send_error_packet(THD *, uint, const char *, const char *);
#ifdef EMBEDDED_LIBRARY
char **next_field; char **next_field;
MYSQL_FIELD *next_mysql_field; MYSQL_FIELD *next_mysql_field;
MEM_ROOT *alloc; MEM_ROOT *alloc;
...@@ -181,6 +180,9 @@ class Protocol ...@@ -181,6 +180,9 @@ class Protocol
}; };
virtual enum enum_protocol_type type()= 0; virtual enum enum_protocol_type type()= 0;
virtual bool net_send_eof(THD *thd, uint server_status, uint statement_warn_count);
bool net_send_error(THD *thd, uint sql_errno, const char *err,
const char* sqlstate);
void end_statement(); void end_statement();
friend int send_answer_1(Protocol *protocol, String *s1, String *s2, friend int send_answer_1(Protocol *protocol, String *s1, String *s2,
...@@ -191,7 +193,7 @@ class Protocol ...@@ -191,7 +193,7 @@ class Protocol
/** Class used for the old (MySQL 4.0 protocol). */ /** Class used for the old (MySQL 4.0 protocol). */
class Protocol_text final :public Protocol class Protocol_text :public Protocol
{ {
StringBuffer<FLOATING_POINT_BUFFER> buffer; StringBuffer<FLOATING_POINT_BUFFER> buffer;
bool store_numeric_string_aux(const char *from, size_t length); bool store_numeric_string_aux(const char *from, size_t length);
...@@ -226,10 +228,10 @@ class Protocol_text final :public Protocol ...@@ -226,10 +228,10 @@ class Protocol_text final :public Protocol
#ifdef EMBEDDED_LIBRARY #ifdef EMBEDDED_LIBRARY
void remove_last_row() override; void remove_last_row() override;
#endif #endif
bool store_field_metadata(const THD *thd, const Send_field &field, virtual bool store_field_metadata(const THD *thd, const Send_field &field,
CHARSET_INFO *charset_for_protocol, CHARSET_INFO *charset_for_protocol,
uint pos); uint pos);
bool store_field_metadata(THD *thd, Item *item, uint pos); bool store_item_metadata(THD *thd, Item *item, uint pos);
bool store_field_metadata_for_list_fields(const THD *thd, Field *field, bool store_field_metadata_for_list_fields(const THD *thd, Field *field,
const TABLE_LIST *table_list, const TABLE_LIST *table_list,
uint pos); uint pos);
...@@ -321,8 +323,6 @@ class Protocol_discard final : public Protocol ...@@ -321,8 +323,6 @@ class Protocol_discard final : public Protocol
void send_warning(THD *thd, uint sql_errno, const char *err=0); void send_warning(THD *thd, uint sql_errno, const char *err=0);
bool net_send_error(THD *thd, uint sql_errno, const char *err,
const char* sqlstate);
void net_send_progress_packet(THD *thd); void net_send_progress_packet(THD *thd);
uchar *net_store_data(uchar *to,const uchar *from, size_t length); uchar *net_store_data(uchar *to,const uchar *from, size_t length);
uchar *net_store_data(uchar *to,int32 from); uchar *net_store_data(uchar *to,int32 from);
......
...@@ -254,35 +254,36 @@ void mysql_audit_notify_connection_disconnect(THD *thd, int errcode) ...@@ -254,35 +254,36 @@ void mysql_audit_notify_connection_disconnect(THD *thd, int errcode)
} }
static inline static inline
void mysql_audit_notify_connection_change_user(THD *thd) void mysql_audit_notify_connection_change_user(THD *thd,
const Security_context *old_ctx)
{ {
if (mysql_audit_connection_enabled()) if (mysql_audit_connection_enabled())
{ {
const Security_context *sctx= thd->security_ctx;
mysql_event_connection event; mysql_event_connection event;
event.event_subclass= MYSQL_AUDIT_CONNECTION_CHANGE_USER; event.event_subclass= MYSQL_AUDIT_CONNECTION_CHANGE_USER;
event.status= thd->get_stmt_da()->is_error() ? event.status= thd->get_stmt_da()->is_error() ?
thd->get_stmt_da()->sql_errno() : 0; thd->get_stmt_da()->sql_errno() : 0;
event.thread_id= (unsigned long)thd->thread_id; event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user; event.user= old_ctx->user;
event.user_length= safe_strlen_uint(sctx->user); event.user_length= safe_strlen_uint(old_ctx->user);
event.priv_user= sctx->priv_user; event.priv_user= old_ctx->priv_user;
event.priv_user_length= strlen_uint(sctx->priv_user); event.priv_user_length= strlen_uint(old_ctx->priv_user);
event.external_user= sctx->external_user; event.external_user= old_ctx->external_user;
event.external_user_length= safe_strlen_uint(sctx->external_user); event.external_user_length= safe_strlen_uint(old_ctx->external_user);
event.proxy_user= sctx->proxy_user; event.proxy_user= old_ctx->proxy_user;
event.proxy_user_length= strlen_uint(sctx->proxy_user); event.proxy_user_length= strlen_uint(old_ctx->proxy_user);
event.host= sctx->host; event.host= old_ctx->host;
event.host_length= safe_strlen_uint(sctx->host); event.host_length= safe_strlen_uint(old_ctx->host);
event.ip= sctx->ip; event.ip= old_ctx->ip;
event.ip_length= safe_strlen_uint(sctx->ip); event.ip_length= safe_strlen_uint(old_ctx->ip);
event.database= thd->db; event.database= thd->db;
mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS, &event); mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS, &event);
} }
} }
static inline static inline
void mysql_audit_external_lock_ex(THD *thd, my_thread_id thread_id, void mysql_audit_external_lock_ex(THD *thd, my_thread_id thread_id,
const char *user, const char *host, const char *ip, query_id_t query_id, const char *user, const char *host, const char *ip, query_id_t query_id,
......
...@@ -348,6 +348,12 @@ void thd_clear_errors(THD *thd) ...@@ -348,6 +348,12 @@ void thd_clear_errors(THD *thd)
} }
extern "C" unsigned long long thd_query_id(const MYSQL_THD thd)
{
return((unsigned long long)thd->query_id);
}
/** /**
Get thread attributes for connection threads Get thread attributes for connection threads
...@@ -4983,6 +4989,55 @@ extern "C" size_t thd_query_safe(MYSQL_THD thd, char *buf, size_t buflen) ...@@ -4983,6 +4989,55 @@ extern "C" size_t thd_query_safe(MYSQL_THD thd, char *buf, size_t buflen)
} }
extern "C" const char *thd_user_name(MYSQL_THD thd)
{
if (!thd->security_ctx)
return 0;
return thd->security_ctx->user;
}
extern "C" const char *thd_client_host(MYSQL_THD thd)
{
if (!thd->security_ctx)
return 0;
return thd->security_ctx->host;
}
extern "C" const char *thd_client_ip(MYSQL_THD thd)
{
if (!thd->security_ctx)
return 0;
return thd->security_ctx->ip;
}
extern "C" LEX_CSTRING *thd_current_db(MYSQL_THD thd)
{
return &thd->db;
}
extern "C" int thd_current_status(MYSQL_THD thd)
{
Diagnostics_area *da= thd->get_stmt_da();
if (!da)
return 0;
return da->is_error() ? da->sql_errno() : 0;
}
extern "C" enum enum_server_command thd_current_command(MYSQL_THD thd)
{
return thd->get_command();
}
extern "C" int thd_slave_thread(const MYSQL_THD thd) extern "C" int thd_slave_thread(const MYSQL_THD thd)
{ {
return(thd->slave_thread); return(thd->slave_thread);
......
...@@ -195,7 +195,14 @@ extern char empty_c_string[1]; ...@@ -195,7 +195,14 @@ extern char empty_c_string[1];
extern MYSQL_PLUGIN_IMPORT const char **errmesg; extern MYSQL_PLUGIN_IMPORT const char **errmesg;
extern "C" LEX_STRING * thd_query_string (MYSQL_THD thd); extern "C" LEX_STRING * thd_query_string (MYSQL_THD thd);
extern "C" unsigned long long thd_query_id(const MYSQL_THD thd);
extern "C" size_t thd_query_safe(MYSQL_THD thd, char *buf, size_t buflen); extern "C" size_t thd_query_safe(MYSQL_THD thd, char *buf, size_t buflen);
extern "C" const char *thd_user_name(MYSQL_THD thd);
extern "C" const char *thd_client_host(MYSQL_THD thd);
extern "C" const char *thd_client_ip(MYSQL_THD thd);
extern "C" LEX_CSTRING *thd_current_db(MYSQL_THD thd);
extern "C" int thd_current_status(MYSQL_THD thd);
extern "C" enum enum_server_command thd_current_command(MYSQL_THD thd);
/** /**
@class CSET_STRING @class CSET_STRING
......
...@@ -1494,7 +1494,7 @@ void CONNECT::close_with_error(uint sql_errno, ...@@ -1494,7 +1494,7 @@ void CONNECT::close_with_error(uint sql_errno,
if (thd) if (thd)
{ {
if (sql_errno) if (sql_errno)
net_send_error(thd, sql_errno, message, NULL); thd->protocol->net_send_error(thd, sql_errno, message, NULL);
close_connection(thd, close_error); close_connection(thd, close_error);
delete thd; delete thd;
set_current_thd(0); set_current_thd(0);
......
...@@ -1748,7 +1748,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, ...@@ -1748,7 +1748,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
else else
auth_rc= acl_authenticate(thd, packet_length); auth_rc= acl_authenticate(thd, packet_length);
mysql_audit_notify_connection_change_user(thd); mysql_audit_notify_connection_change_user(thd, &save_security_ctx);
if (auth_rc) if (auth_rc)
{ {
/* Free user if allocated by acl_authenticate */ /* Free user if allocated by acl_authenticate */
......
This diff is collapsed.
...@@ -200,7 +200,7 @@ class Ed_connection ...@@ -200,7 +200,7 @@ class Ed_connection
@retval TRUE error, use get_last_error() @retval TRUE error, use get_last_error()
to see the error number. to see the error number.
*/ */
bool execute_direct(LEX_STRING sql_text); bool execute_direct(Protocol *p, LEX_STRING sql_text);
/** /**
Same as the previous, but takes an instance of Server_runnable Same as the previous, but takes an instance of Server_runnable
...@@ -213,7 +213,7 @@ class Ed_connection ...@@ -213,7 +213,7 @@ class Ed_connection
return a result set return a result set
@retval TRUE failure @retval TRUE failure
*/ */
bool execute_direct(Server_runnable *server_runnable); bool execute_direct(Protocol *p, Server_runnable *server_runnable);
/** /**
Get the number of affected (deleted, updated) Get the number of affected (deleted, updated)
...@@ -309,7 +309,6 @@ class Ed_connection ...@@ -309,7 +309,6 @@ class Ed_connection
THD *m_thd; THD *m_thd;
Ed_result_set *m_rsets; Ed_result_set *m_rsets;
Ed_result_set *m_current_rset; Ed_result_set *m_current_rset;
friend class Protocol_local;
private: private:
void free_old_result(); void free_old_result();
void add_result_set(Ed_result_set *ed_result_set); void add_result_set(Ed_result_set *ed_result_set);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment