Skip to content

Commit 96ba1f1

Browse files
committed
- Handle the use of date/time values when making queries for MYSQL or
ODBC. Was raised by 7549. modified: storage/connect/ha_connect.cc storage/connect/odbconn.cpp storage/connect/tabodbc.cpp
1 parent 35548d5 commit 96ba1f1

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

storage/connect/ha_connect.cc

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,8 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
25192519
char *body= filp->Body;
25202520
unsigned int i;
25212521
bool ismul= false, x= (tty == TYPE_AM_MYX || tty == TYPE_AM_XDBC);
2522+
bool nonul= (tty == TYPE_AM_ODBC && (tdbp->GetMode() == MODE_INSERT ||
2523+
tdbp->GetMode() == MODE_DELETE));
25222524
OPVAL vop= OP_XX;
25232525

25242526
if (!cond)
@@ -2536,7 +2538,7 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
25362538

25372539
if (trace)
25382540
htrc("Cond: Ftype=%d name=%s\n", cond_item->functype(),
2539-
cond_item->func_name());
2541+
cond_item->func_name());
25402542

25412543
switch (cond_item->functype()) {
25422544
case Item_func::COND_AND_FUNC: vop= OP_AND; break;
@@ -2555,7 +2557,7 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
25552557
for (i= 0; i < arglist->elements; i++)
25562558
if ((subitem= li++)) {
25572559
if (!CheckCond(g, filp, tty, subitem)) {
2558-
if (vop == OP_OR)
2560+
if (vop == OP_OR || nonul)
25592561
return NULL;
25602562
else
25612563
*p2= 0;
@@ -2651,6 +2653,8 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
26512653
if (trace) {
26522654
htrc("Field index=%d\n", pField->field->field_index);
26532655
htrc("Field name=%s\n", pField->field->field_name);
2656+
htrc("Field type=%d\n", pField->field->type());
2657+
htrc("Field_type=%d\n", args[i]->field_type());
26542658
} // endif trace
26552659

26562660
// IN and BETWEEN clauses should be col VOP list
@@ -2670,8 +2674,9 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
26702674
char buff[256];
26712675
String *res, tmp(buff, sizeof(buff), &my_charset_bin);
26722676
Item_basic_constant *pval= (Item_basic_constant *)args[i];
2677+
Item::Type type= args[i]->real_type();
26732678

2674-
switch (args[i]->real_type()) {
2679+
switch (type) {
26752680
case COND::STRING_ITEM:
26762681
case COND::INT_ITEM:
26772682
case COND::REAL_ITEM:
@@ -2696,10 +2701,64 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
26962701

26972702
if (!x) {
26982703
// Append the value to the filter
2699-
if (args[i]->field_type() == MYSQL_TYPE_VARCHAR)
2700-
strcat(strncat(strcat(body, "'"), res->ptr(), res->length()), "'");
2701-
else
2702-
strncat(body, res->ptr(), res->length());
2704+
switch (args[i]->field_type()) {
2705+
case MYSQL_TYPE_TIMESTAMP:
2706+
case MYSQL_TYPE_DATETIME:
2707+
if (tty == TYPE_AM_ODBC) {
2708+
strcat(body, "{ts '");
2709+
strcat(strncat(body, res->ptr(), res->length()), "'}");
2710+
break;
2711+
} // endif ODBC
2712+
2713+
case MYSQL_TYPE_DATE:
2714+
if (tty == TYPE_AM_ODBC) {
2715+
strcat(body, "{d '");
2716+
strcat(strncat(body, res->ptr(), res->length()), "'}");
2717+
break;
2718+
} // endif ODBC
2719+
2720+
case MYSQL_TYPE_TIME:
2721+
if (tty == TYPE_AM_ODBC) {
2722+
strcat(body, "{t '");
2723+
strcat(strncat(body, res->ptr(), res->length()), "'}");
2724+
break;
2725+
} // endif ODBC
2726+
2727+
case MYSQL_TYPE_VARCHAR:
2728+
if (tty == TYPE_AM_ODBC && i) {
2729+
switch (args[0]->field_type()) {
2730+
case MYSQL_TYPE_TIMESTAMP:
2731+
case MYSQL_TYPE_DATETIME:
2732+
strcat(body, "{ts '");
2733+
strncat(body, res->ptr(), res->length());
2734+
strcat(body, "'}");
2735+
break;
2736+
case MYSQL_TYPE_DATE:
2737+
strcat(body, "{d '");
2738+
strncat(body, res->ptr(), res->length());
2739+
strcat(body, "'}");
2740+
break;
2741+
case MYSQL_TYPE_TIME:
2742+
strcat(body, "{t '");
2743+
strncat(body, res->ptr(), res->length());
2744+
strcat(body, "'}");
2745+
break;
2746+
default:
2747+
strcat(body, "'");
2748+
strncat(body, res->ptr(), res->length());
2749+
strcat(body, "'");
2750+
} // endswitch field type
2751+
2752+
} else {
2753+
strcat(body, "'");
2754+
strncat(body, res->ptr(), res->length());
2755+
strcat(body, "'");
2756+
} // endif tty
2757+
2758+
break;
2759+
default:
2760+
strncat(body, res->ptr(), res->length());
2761+
} // endswitch field type
27032762

27042763
} else {
27052764
if (args[i]->field_type() == MYSQL_TYPE_VARCHAR) {

storage/connect/odbconn.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,8 @@ bool ODBConn::BindParam(ODBCCOL *colp)
17401740
strcpy(m_G->Message, x->GetErrorMessage(0));
17411741
colsize = colp->GetPrecision();
17421742
sqlt = GetSQLType(buftype);
1743+
dec = IsTypeChar(buftype) ? 0 : colp->GetScale();
1744+
nul = SQL_NULLABLE_UNKNOWN;
17431745
} // end try/catch
17441746

17451747
buf = colp->GetBuffer(0);

storage/connect/tabodbc.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,7 @@ bool TDBODBC::BindParameters(PGLOBAL g)
573573
/***********************************************************************/
574574
char *TDBODBC::MakeCommand(PGLOBAL g)
575575
{
576-
char *p, name[68], *qc = Ocp->GetQuoteChar();
577-
char *stmt = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
576+
char *p, *stmt, name[68], *body = NULL, *qc = Ocp->GetQuoteChar();
578577
char *qrystr = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 1);
579578
bool qtd = Quoted > 0;
580579
int i = 0, k = 0;
@@ -585,6 +584,15 @@ char *TDBODBC::MakeCommand(PGLOBAL g)
585584
qrystr[i] = (Qrystr[i] == '`') ? *qc : tolower(Qrystr[i]);
586585
} while (Qrystr[i++]);
587586

587+
if (To_CondFil && (p = strstr(qrystr, " where "))) {
588+
p[7] = 0; // Remove where clause
589+
Qrystr[(p - qrystr) + 7] = 0;
590+
body = To_CondFil->Body;
591+
stmt = (char*)PlugSubAlloc(g, NULL, strlen(qrystr)
592+
+ strlen(body) + 64);
593+
} else
594+
stmt = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
595+
588596
// Check whether the table name is equal to a keyword
589597
// If so, it must be quoted in the original query
590598
strlwr(strcat(strcat(strcpy(name, " "), Name), " "));
@@ -612,6 +620,9 @@ char *TDBODBC::MakeCommand(PGLOBAL g)
612620
stmt[i++] = (Qrystr[k] == '`') ? *qc : Qrystr[k];
613621
} while (Qrystr[k++]);
614622

623+
if (body)
624+
strcat(stmt, body);
625+
615626
} else {
616627
sprintf(g->Message, "Cannot use this %s command",
617628
(Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
@@ -774,7 +785,7 @@ int TDBODBC::GetProgMax(PGLOBAL g)
774785
/***********************************************************************/
775786
bool TDBODBC::OpenDB(PGLOBAL g)
776787
{
777-
bool rc = false;
788+
bool rc = true;
778789

779790
if (g->Trace)
780791
htrc("ODBC OpenDB: tdbp=%p tdb=R%d use=%dmode=%d\n",
@@ -849,12 +860,12 @@ bool TDBODBC::OpenDB(PGLOBAL g)
849860

850861
} // endif Query
851862

852-
} else if (Mode == MODE_UPDATE || Mode == MODE_DELETE)
853-
Query = MakeCommand(g);
854-
else
863+
} else if (Mode == MODE_UPDATE || Mode == MODE_DELETE) {
864+
rc = false; // wait for CheckCond before calling MakeCommand(g);
865+
} else
855866
sprintf(g->Message, "Invalid mode %d", Mode);
856867

857-
if (!Query || rc) {
868+
if (rc) {
858869
Ocp->Close();
859870
return true;
860871
} // endif rc
@@ -886,6 +897,9 @@ int TDBODBC::ReadDB(PGLOBAL g)
886897
GetTdb_No(), Mode, To_Key_Col, To_Link, To_Kindex);
887898

888899
if (Mode == MODE_UPDATE || Mode == MODE_DELETE) {
900+
if (!Query && !(Query = MakeCommand(g)))
901+
return RC_FX;
902+
889903
// Send the UPDATE/DELETE command to the remote table
890904
if (!Ocp->ExecSQLcommand(Query)) {
891905
sprintf(g->Message, "%s: %d affected rows", TableName, AftRows);
@@ -955,6 +969,9 @@ int TDBODBC::WriteDB(PGLOBAL g)
955969
int TDBODBC::DeleteDB(PGLOBAL g, int irc)
956970
{
957971
if (irc == RC_FX) {
972+
if (!Query && !(Query = MakeCommand(g)))
973+
return RC_FX;
974+
958975
// Send the DELETE (all) command to the remote table
959976
if (!Ocp->ExecSQLcommand(Query)) {
960977
sprintf(g->Message, "%s: %d affected rows", TableName, AftRows);

0 commit comments

Comments
 (0)