Skip to content

Commit af97ea3

Browse files
committed
ODBC-454 Error in SQLForeignKeys in keys of FK parameters only and no FK
in the given table(or it does not exist). The driver formed wrong query in this case. The fix and the testcase. Also cmake min version increased to 3.12.0 as C/C has done the same and there is no sense to keep smaller version.
1 parent ad40a6a commit af97ea3

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# 51 Franklin St., Fifth Floor, Boston, MA 02110, USA
1818
# *************************************************************************************/
1919

20-
cmake_minimum_required(VERSION 2.8.12)
20+
CMAKE_MINIMUM_REQUIRED(VERSION 3.12.0)
2121

2222
IF(NOT ${CMAKE_VERSION} VERSION_LESS "3.20.0")
2323
CMAKE_POLICY(SET CMP0115 NEW)
@@ -26,9 +26,6 @@ CMAKE_POLICY(SET CMP0048 NEW)
2626
CMAKE_POLICY(SET CMP0040 NEW)
2727
CMAKE_POLICY(SET CMP0057 NEW)
2828
CMAKE_POLICY(SET CMP0054 NEW)
29-
IF(NOT ${CMAKE_VERSION} VERSION_LESS "3.20.0")
30-
CMAKE_POLICY(SET CMP0115 NEW)
31-
ENDIF()
3229

3330
PROJECT(mariadb_connector_odbc
3431
VERSION 3.2.5

driver/ma_catalog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,9 @@ SQLRETURN MADB_StmtForeignKeys(MADB_Stmt *Stmt, char *PKCatalogName, SQLSMALLINT
11081108
Stmt->Connection->guard->safeRealQuery(aux.str());
11091109
auto result= mysql_store_result(Stmt->Connection->mariadb);
11101110

1111-
if (result == nullptr)
1111+
if (result == nullptr || mysql_num_rows(result) == 0)
11121112
{
1113+
mysql_free_result(result);
11131114
//Does not matter what are they, but we need to put there something
11141115
if (refSchemaCond)
11151116
schemaCond= refSchemaCond;

driver/ma_desc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ MADB_SetIrdRecord(MADB_Stmt *Stmt, MADB_DescRecord *Record, const MYSQL_FIELD *F
264264
}
265265

266266

267-
if (0)//Stmt->Connection->IsAnsi == '\0')
267+
if (false)/*!Stmt->Connection->IsAnsi)*/
268268
{
269269
switch (Record->ConciseType) {
270270
case SQL_CHAR: Record->ConciseType= SQL_WCHAR; break;

driver/ma_helper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ const char *MADB_GetTypeName(const MYSQL_FIELD *Field)
246246
}
247247
/* }}} */
248248

249-
249+
/* {{{ MADB_GetDefaultColumnValues */
250250
MYSQL_RES *MADB_GetDefaultColumnValues(MADB_Stmt *Stmt, const MYSQL_FIELD *fields)
251251
{
252252
SQLString DynStr("SELECT COLUMN_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='");
@@ -271,7 +271,7 @@ MYSQL_RES *MADB_GetDefaultColumnValues(MADB_Stmt *Stmt, const MYSQL_FIELD *field
271271
Stmt->Connection->guard->safeRealQuery(DynStr);
272272
return mysql_store_result(Stmt->Connection->mariadb);
273273
}
274-
274+
/* }}} */
275275

276276
char *MADB_GetDefaultColumnValue(MYSQL_RES *res, const char *Column)
277277
{

driver/ma_statement.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,14 +2628,14 @@ SQLRETURN MADB_GetBookmark(MADB_Stmt *Stmt,
26282628
}
26292629

26302630
/* {{{ MADB_StmtGetData */
2631-
SQLRETURN MADB_StmtGetData(SQLHSTMT StatementHandle,
2631+
SQLRETURN MADB_StmtGetData(SQLHSTMT StatementHandle,
26322632
SQLUSMALLINT Col_or_Param_Num,
2633-
SQLSMALLINT TargetType,
2634-
SQLPOINTER TargetValuePtr,
2635-
SQLLEN BufferLength,
2636-
SQLLEN * StrLen_or_IndPtr,
2637-
bool InternalUse /* Currently this is respected for SQL_CHAR type only,
2638-
since all "internal" calls of the function need string representation of data */)
2633+
SQLSMALLINT TargetType,
2634+
SQLPOINTER TargetValuePtr,
2635+
SQLLEN BufferLength,
2636+
SQLLEN* StrLen_or_IndPtr,
2637+
bool InternalUse /* Currently this is respected for SQL_CHAR type only,
2638+
since all "internal" calls of the function need string representation of data */)
26392639
{
26402640
MADB_Stmt *Stmt= (MADB_Stmt *)StatementHandle;
26412641
SQLUSMALLINT Offset= Col_or_Param_Num - 1;

test/keys.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3-
2013, 2022 MariaDB Corporation AB
3+
2013, 2025 MariaDB Corporation plc
44
55
The MySQL Connector/ODBC is licensed under the terms of the GPLv2
66
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -117,7 +117,7 @@ ODBC_TEST(my_foreign_keys)
117117

118118
OK_SIMPLE_STMT(Stmt,"DROP DATABASE IF EXISTS test_odbc_fk");
119119
OK_SIMPLE_STMT(Stmt,"CREATE DATABASE test_odbc_fk");
120-
OK_SIMPLE_STMT(Stmt,"use test_odbc_fk");
120+
OK_SIMPLE_STMT(Stmt,"USE test_odbc_fk");
121121

122122
OK_SIMPLE_STMT(Stmt,"DROP TABLE IF EXISTS test_fkey_c1");
123123
OK_SIMPLE_STMT(Stmt,"DROP TABLE IF EXISTS test_fkey_3");
@@ -127,10 +127,9 @@ ODBC_TEST(my_foreign_keys)
127127
OK_SIMPLE_STMT(Stmt,"DROP TABLE IF EXISTS test_fkey_comment_f");
128128
OK_SIMPLE_STMT(Stmt,"DROP TABLE IF EXISTS test_fkey_comment_p");
129129

130-
OK_SIMPLE_STMT(Stmt,
131-
"CREATE TABLE test_fkey1(\
132-
A INTEGER NOT NULL,B INTEGER NOT NULL,C INTEGER NOT NULL,\
133-
D INTEGER,PRIMARY KEY (C,B,A)) ENGINE=InnoDB;");
130+
OK_SIMPLE_STMT(Stmt, "CREATE TABLE test_fkey1("
131+
"A INTEGER NOT NULL,B INTEGER NOT NULL,C INTEGER NOT NULL,"
132+
"D INTEGER,PRIMARY KEY (C,B,A)) ENGINE=InnoDB;");
134133

135134
OK_SIMPLE_STMT(Stmt,
136135
"CREATE TABLE test_fkey_p1(\
@@ -468,12 +467,45 @@ void t_strstr()
468467
}
469468
}
470469

470+
/* ODBC-454 */
471+
ODBC_TEST(odbc454)
472+
{
473+
/* First running the way it should be */
474+
CHECK_STMT_RC(Stmt, SQLForeignKeys(Stmt,
475+
NULL, SQL_NTS,
476+
NULL, SQL_NTS,
477+
NULL, SQL_NTS,
478+
(SQLCHAR *)"test_odbc_fk", SQL_NTS, /* possibly non-existent catalog aka schema suffices to repeat the issue */
479+
NULL, SQL_NTS,
480+
(SQLCHAR *)"odbc454", SQL_NTS)); /* We need here non-existent or table w/out FK's*/
481+
is_num(0, myrowcount(Stmt));
482+
CHECK_STMT_RC(Stmt, SQLFreeStmt(Stmt, SQL_CLOSE));
483+
484+
/* This should normally cause error. But if the error suppressed using connstring option - everything should be fine */
485+
if (SQL_SUCCEEDED(SQLForeignKeys(Stmt,
486+
NULL, SQL_NTS,
487+
NULL, SQL_NTS,
488+
NULL, SQL_NTS,
489+
NULL, SQL_NTS,
490+
(SQLCHAR *)"test_odbc_fk", SQL_NTS,
491+
(SQLCHAR *)"", SQL_NTS)))
492+
{
493+
is_num(0, myrowcount(Stmt));
494+
}
495+
else
496+
{
497+
odbc_print_error(SQL_HANDLE_STMT, Stmt);
498+
}
499+
CHECK_STMT_RC(Stmt, SQLFreeStmt(Stmt, SQL_CLOSE));
500+
return OK;
501+
}
471502

472503

473504
MA_ODBC_TESTS my_tests[]=
474505
{
475506
{my_no_keys, "my_no_keys"},
476507
{my_foreign_keys, "my_foreign_keys"},
508+
{odbc454, "ODBC-454-nofks"},
477509
{NULL, NULL}
478510
};
479511

0 commit comments

Comments
 (0)