enhancing the mariadb client service

This commit is contained in:
hyung-hwan 2020-06-21 08:42:36 +00:00
parent dedc6082d3
commit 31d92b9292
4 changed files with 119 additions and 75 deletions

View File

@ -20,7 +20,7 @@ printf ("CONNECTED...\n");
} }
} }
static void mar_on_query_started (mio_dev_mar_t* dev, int mar_ret) static void mar_on_query_started (mio_dev_mar_t* dev, int mar_ret, const mio_bch_t* mar_errmsg)
{ {
if (mar_ret != 0) if (mar_ret != 0)
{ {
@ -31,7 +31,7 @@ printf ("QUERY NOT SENT PROPERLY..%s\n", mysql_error(dev->hnd));
printf ("QUERY SENT..\n"); printf ("QUERY SENT..\n");
if (mio_dev_mar_fetchrows(dev) <= -1) if (mio_dev_mar_fetchrows(dev) <= -1)
{ {
printf ("FETCH ROW FAILURE - %s\n", mysql_error(dev->hnd)); printf ("FETCH ROW FAILURE - %s\n", errmsg);
mio_dev_mar_halt (dev); mio_dev_mar_halt (dev);
} }
} }
@ -118,9 +118,30 @@ oops:
static void on_result (mio_svc_marc_t* svc, mio_oow_t sid, mio_svc_marc_rcode_t rcode, void* data, void* qctx)
{
switch (rcode)
{
case MIO_SVC_MARC_RCODE_ROW:
{
MYSQL_ROW row = (MYSQL_ROW)data;
// if (x == 0)
printf ("[%lu] %s %s\n", sid, row[0], row[1]);
// else if (x == 1)
// printf ("%s %s %s %s %s\n", row[0], row[1], row[2], row[3], row[4]);
//printf ("GOT ROW\n");
break;
}
case MIO_SVC_MARC_RCODE_DONE:
printf ("[%lu] NO DATA..\n", sid);
break;
case MIO_SVC_MARC_RCODE_ERROR:
printf ("QUERY ERROR - %s\n", data); /* TODO: how to get both error code and error message? */
break;
}
}
int main (int argc, char* argv[]) int main (int argc, char* argv[])
{ {
@ -157,7 +178,10 @@ int main (int argc, char* argv[])
goto oops; goto oops;
} }
mio_svc_mar_querywithbchars (marc, 0, "SHOW STATUS", 11, MIO_NULL); mio_svc_mar_querywithbchars (marc, 0, MIO_SVC_MARC_QTYPE_SELECT, "SHOW STATUS", 11, on_result, MIO_NULL);
mio_svc_mar_querywithbchars (marc, 0, MIO_SVC_MARC_QTYPE_ACTION, "DELETE FROM", 11, on_result, MIO_NULL);
// mio_svc_mar_querywithbchars (marc, 0, MIO_SVC_MARC_QTYPE_SELECT, "SHOW STATUS", 11, on_result, MIO_NULL);
mio_svc_mar_querywithbchars (marc, 0, MIO_SVC_MARC_QTYPE_ACTION, "DELETE FROM XXX", 14, on_result, MIO_NULL);
#if 0 #if 0
memset (&mi, 0, MIO_SIZEOF(mi)); memset (&mi, 0, MIO_SIZEOF(mi));

View File

@ -44,8 +44,6 @@ struct mio_svc_marc_t
mio_oow_t capa; mio_oow_t capa;
} sess; } sess;
}; };
struct sess_qry_t struct sess_qry_t
@ -53,8 +51,10 @@ struct sess_qry_t
mio_bch_t* qptr; mio_bch_t* qptr;
mio_oow_t qlen; mio_oow_t qlen;
void* qctx; void* qctx;
int sent; unsigned int sent: 1;
unsigned int need_fetch: 1;
mio_svc_marc_on_result_t on_result;
sess_qry_t* sq_next; sess_qry_t* sq_next;
}; };
@ -111,7 +111,7 @@ void mio_svc_marc_stop (mio_svc_marc_t* marc)
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
static sess_qry_t* make_session_query (mio_t* mio, const mio_bch_t* qptr, mio_oow_t qlen, void* qctx) static sess_qry_t* make_session_query (mio_t* mio, mio_svc_marc_qtype_t qtype, const mio_bch_t* qptr, mio_oow_t qlen, void* qctx, mio_svc_marc_on_result_t on_result)
{ {
sess_qry_t* sq; sess_qry_t* sq;
@ -121,9 +121,11 @@ static sess_qry_t* make_session_query (mio_t* mio, const mio_bch_t* qptr, mio_oo
MIO_MEMCPY (sq + 1, qptr, (MIO_SIZEOF(*qptr) * qlen)); MIO_MEMCPY (sq + 1, qptr, (MIO_SIZEOF(*qptr) * qlen));
sq->sent = 0; sq->sent = 0;
sq->need_fetch = (qtype == MIO_SVC_MARC_QTYPE_SELECT);
sq->qptr = (mio_bch_t*)(sq + 1); sq->qptr = (mio_bch_t*)(sq + 1);
sq->qlen = qlen; sq->qlen = qlen;
sq->qctx = qctx; sq->qctx = qctx;
sq->on_result = on_result;
sq->sq_next = MIO_NULL; sq->sq_next = MIO_NULL;
return sq; return sq;
@ -209,71 +211,63 @@ printf ("connected on sid %d\n", sess->sid);
} }
} }
static void mar_on_query_started (mio_dev_mar_t* dev, int mar_ret) static void mar_on_query_started (mio_dev_mar_t* dev, int mar_ret, const mio_bch_t* mar_errmsg)
{ {
if (mar_ret != 0) dev_xtn_t* xtn = (dev_xtn_t*)mio_dev_mar_getxtn(dev);
sess_t* sess = xtn->sess;
sess_qry_t* sq = get_first_session_query(sess);
if (mar_ret)
{ {
} printf ("QUERY FAILED...%d -> %s\n", mar_ret, mar_errmsg);
else
{
if (mio_dev_mar_fetchrows(dev) <= -1)
{
mio_dev_mar_halt (dev);
}
}
#if 0 #if 0
if (mar_ret != 0) if (mar_ret == CR_SERVER_GONE_ERROR || /* server gone away between queries */
{ mar_ret == CR_SERVER_LOST) /* server gone away during a query */
printf ("QUERY NOT SENT PROPERLY..%s\n", mysql_error(dev->hnd)); #endif
sq->on_result(sess->svc, sess->sid, MIO_SVC_MARC_RCODE_ERROR, mysql_error(dev->hnd), sq->qctx);
dequeue_session_query (sess->svc->mio, sess);
send_pending_query_if_any (sess);
} }
else else
{ {
printf ("QUERY SENT..\n"); printf ("QUERY STARTED\n");
if (sq->need_fetch)
{
if (mio_dev_mar_fetchrows(dev) <= -1) if (mio_dev_mar_fetchrows(dev) <= -1)
{ {
printf ("FETCH ROW FAILURE - %s\n", mysql_error(dev->hnd)); //printf ("FETCH ROW FAILURE - %s\n", mysql_error(dev->hnd));
mio_dev_mar_halt (dev); mio_dev_mar_halt (dev);
} }
} }
#endif else
{
if (sq->on_result)
sq->on_result (sess->svc, sess->sid, MIO_SVC_MARC_RCODE_DONE, MIO_NULL, sq->qctx);
dequeue_session_query (sess->svc->mio, sess);
send_pending_query_if_any (sess);
}
}
} }
static void mar_on_row_fetched (mio_dev_mar_t* dev, void* data) static void mar_on_row_fetched (mio_dev_mar_t* dev, void* data)
{ {
#if 0
MYSQL_ROW row = (MYSQL_ROW)data;
static int x = 0;
if (!row)
{
printf ("NO MORE ROW..\n");
if (x == 0 && mio_dev_mar_querywithbchars(dev, "SELECT * FROM pdns.records", 26) <= -1) mio_dev_mar_halt (dev);
x++;
}
else
{
if (x == 0)
printf ("%s %s\n", row[0], row[1]);
else if (x == 1)
printf ("%s %s %s %s %s\n", row[0], row[1], row[2], row[3], row[4]);
//printf ("GOT ROW\n");
}
#endif
dev_xtn_t* xtn = (dev_xtn_t*)mio_dev_mar_getxtn(dev); dev_xtn_t* xtn = (dev_xtn_t*)mio_dev_mar_getxtn(dev);
sess_t* sess = xtn->sess; sess_t* sess = xtn->sess;
sess_qry_t* sq = get_first_session_query(sess);
if (sq->on_result)
{
sq->on_result (sess->svc, sess->sid, (data? MIO_SVC_MARC_RCODE_ROW: MIO_SVC_MARC_RCODE_DONE), data, sq->qctx);
}
if (!data) if (!data)
{ {
/* no more rows */ dequeue_session_query (sess->svc->mio, sess);
send_pending_query_if_any (sess);
//marc->on_row_fetched (marc, void* data, sess->sid, sess->qctx);
printf ("there is no more row...\n");
} }
else
{
printf ("there is row...\n");
}
} }
static mio_dev_mar_t* alloc_device (mio_svc_marc_t* marc, sess_t* sess) static mio_dev_mar_t* alloc_device (mio_svc_marc_t* marc, sess_t* sess)
@ -340,7 +334,7 @@ static sess_t* get_session (mio_svc_marc_t* marc, mio_oow_t sid)
{ {
sess_qry_t* sq; sess_qry_t* sq;
sq = make_session_query(mio, "", 0, MIO_NULL); /* this is a place holder */ sq = make_session_query(mio, MIO_SVC_MARC_QTYPE_ACTION, "", 0, MIO_NULL, 0); /* this is a place holder */
if (MIO_UNLIKELY(!sq)) return MIO_NULL; if (MIO_UNLIKELY(!sq)) return MIO_NULL;
sess->dev = alloc_device(marc, sess); sess->dev = alloc_device(marc, sess);
@ -358,7 +352,7 @@ static sess_t* get_session (mio_svc_marc_t* marc, mio_oow_t sid)
} }
int mio_svc_mar_querywithbchars (mio_svc_marc_t* marc, mio_oow_t sid, const mio_bch_t* qptr, mio_oow_t qlen, void* qctx) int mio_svc_mar_querywithbchars (mio_svc_marc_t* marc, mio_oow_t sid, mio_svc_marc_qtype_t qtype, const mio_bch_t* qptr, mio_oow_t qlen, mio_svc_marc_on_result_t on_result, void* qctx)
{ {
mio_t* mio = marc->mio; mio_t* mio = marc->mio;
sess_t* sess; sess_t* sess;
@ -367,7 +361,7 @@ int mio_svc_mar_querywithbchars (mio_svc_marc_t* marc, mio_oow_t sid, const mio_
sess = get_session(marc, sid); sess = get_session(marc, sid);
if (MIO_UNLIKELY(!sess)) return -1; if (MIO_UNLIKELY(!sess)) return -1;
sq = make_session_query(mio, qptr, qlen, qctx); sq = make_session_query(mio, qtype, qptr, qlen, qctx, on_result);
if (MIO_UNLIKELY(!sq)) return -1; if (MIO_UNLIKELY(!sq)) return -1;
if (get_first_session_query(sess)) if (get_first_session_query(sess))

View File

@ -217,6 +217,11 @@ static int dev_mar_ioctl (mio_dev_t* dev, int cmd, void* arg)
else else
{ {
/* query sent immediately */ /* query sent immediately */
if (err)
{
if (err == 1) err = mysql_errno(rdev->hnd);
mio_copy_bcstr (rdev->errbuf, MIO_COUNTOF(rdev->errbuf), mysql_error(rdev->hnd));
}
rdev->query_started = 1; rdev->query_started = 1;
rdev->query_ret = err; rdev->query_ret = err;
watch_mysql (rdev, MYSQL_WAIT_READ | MYSQL_WAIT_WRITE); watch_mysql (rdev, MYSQL_WAIT_READ | MYSQL_WAIT_WRITE);
@ -322,7 +327,7 @@ static int dev_evcb_mar_ready (mio_dev_t* dev, int events)
{ {
rdev->query_started = 0; rdev->query_started = 0;
MIO_DEV_MAR_SET_PROGRESS (rdev, MIO_DEV_MAR_QUERY_STARTED); MIO_DEV_MAR_SET_PROGRESS (rdev, MIO_DEV_MAR_QUERY_STARTED);
if (rdev->on_query_started) rdev->on_query_started (rdev, rdev->query_ret); if (rdev->on_query_started) rdev->on_query_started (rdev, rdev->query_ret, (rdev->query_ret? rdev->errbuf: MIO_NULL));
} }
else else
{ {
@ -336,7 +341,8 @@ static int dev_evcb_mar_ready (mio_dev_t* dev, int events)
{ {
/* query sent */ /* query sent */
MIO_DEV_MAR_SET_PROGRESS (rdev, MIO_DEV_MAR_QUERY_STARTED); MIO_DEV_MAR_SET_PROGRESS (rdev, MIO_DEV_MAR_QUERY_STARTED);
if (rdev->on_query_started) rdev->on_query_started (rdev, tmp); if (tmp == 1) tmp = mysql_errno(rdev->hnd); /* tmp is set to 1 by mariadb-connector-c 3.1 as of this writing. let me work around it by fetching the error code */
if (rdev->on_query_started) rdev->on_query_started (rdev, tmp, (tmp? mysql_error(rdev->hnd): MIO_NULL));
} }
} }

View File

@ -88,7 +88,8 @@ typedef void (*mio_dev_mar_on_disconnect_t) (
typedef void (*mio_dev_mar_on_query_started_t) ( typedef void (*mio_dev_mar_on_query_started_t) (
mio_dev_mar_t* dev, mio_dev_mar_t* dev,
int mar_ret int mar_ret,
const mio_bch_t* mar_errmsg
); );
typedef void (*mio_dev_mar_on_row_fetched_t) ( typedef void (*mio_dev_mar_on_row_fetched_t) (
@ -108,6 +109,7 @@ struct mio_dev_mar_t
unsigned int query_started; unsigned int query_started;
unsigned int row_fetched; unsigned int row_fetched;
char errbuf[256];
int query_ret; int query_ret;
int row_wstatus; int row_wstatus;
void* row; void* row;
@ -155,9 +157,25 @@ typedef enum mio_dev_mar_ioctl_cmd_t mio_dev_mar_ioctl_cmd_t;
typedef struct mio_svc_marc_t mio_svc_marc_t; typedef struct mio_svc_marc_t mio_svc_marc_t;
typedef mio_dev_mar_connect_t mio_svc_marc_connect_t; typedef mio_dev_mar_connect_t mio_svc_marc_connect_t;
typedef void (*mio_svc_marc_on_row_fetched) ( enum mio_svc_marc_qtype_t
{
MIO_SVC_MARC_QTYPE_SELECT, /* SELECT, SHOW, ... */
MIO_SVC_MARC_QTYPE_ACTION /* UPDATE, INSERT, DELETE, ALTER ... */
};
typedef enum mio_svc_marc_qtype_t mio_svc_marc_qtype_t;
enum mio_svc_marc_rcode_t
{
MIO_SVC_MARC_RCODE_ROW, /* has row *- data is MYSQL_ROW */
MIO_SVC_MARC_RCODE_DONE, /* completed or no more row - data is NULL */
MIO_SVC_MARC_RCODE_ERROR /* query error - data is a numeric database error code cast to void* */
};
typedef enum mio_svc_marc_rcode_t mio_svc_marc_rcode_t;
typedef void (*mio_svc_marc_on_result_t) (
mio_svc_marc_t* marc, mio_svc_marc_t* marc,
mio_oow_t sid, mio_oow_t sid,
mio_svc_marc_rcode_t rcode,
void* data, void* data,
void* qctx void* qctx
); );
@ -234,8 +252,10 @@ static MIO_INLINE mio_t* mio_svc_marc_getmio(mio_svc_marc_t* svc) { return mio_s
MIO_EXPORT int mio_svc_mar_querywithbchars ( MIO_EXPORT int mio_svc_mar_querywithbchars (
mio_svc_marc_t* marc, mio_svc_marc_t* marc,
mio_oow_t sid, mio_oow_t sid,
mio_svc_marc_qtype_t qtype,
const mio_bch_t* qptr, const mio_bch_t* qptr,
mio_oow_t qlen, mio_oow_t qlen,
mio_svc_marc_on_result_t on_result,
void* qctx void* qctx
); );