Skip to content

Commit e330c14

Browse files
author
лаврин
committed
Addition to statement and PS getWarnings tests to co,pliment patch in the rev#916.1.2("...This patch makes statements read their warning count and return warnings only if the count >0)
1 parent 8dbb2a3 commit e330c14

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

test/unit/classes/preparedstatement.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
33
44
The MySQL Connector/C++ is licensed under the terms of the GPLv2
55
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -1292,10 +1292,15 @@ void preparedstatement::getWarnings()
12921292
stmt->execute("DROP TABLE IF EXISTS test");
12931293
stmt->execute("CREATE TABLE test(id INT UNSIGNED)");
12941294

1295+
// Generating 2 warnings to make sure we get only the last 1 - won't hurt
12951296
// Lets hope that this will always cause a 1264 or similar warning
1296-
pstmt.reset(con->prepareStatement("INSERT INTO test(id) VALUES (-1)"));
1297+
pstmt.reset(con->prepareStatement("INSERT INTO test(id) VALUES (?)"));
1298+
pstmt->setInt(1, -2);
12971299
pstmt->executeUpdate();
1300+
pstmt->setInt(1, -1);
1301+
pstmt->executeUpdate();
12981302

1303+
int count= 0;
12991304
for (const sql::SQLWarning* warn=pstmt->getWarnings(); warn; warn=warn->getNextWarning())
13001305
{
13011306
msg.str("");
@@ -1314,8 +1319,12 @@ void preparedstatement::getWarnings()
13141319
ASSERT(("" != warn->getSQLState()));
13151320
}
13161321
ASSERT(("" != warn->getMessage()));
1322+
1323+
++count;
13171324
}
13181325

1326+
ASSERT_EQUALS(1, count);
1327+
13191328
for (const sql::SQLWarning* warn=pstmt->getWarnings(); warn; warn=warn->getNextWarning())
13201329
{
13211330
msg.str("");
@@ -1342,6 +1351,15 @@ void preparedstatement::getWarnings()
13421351
FAIL("There should be no more warnings!");
13431352
}
13441353

1354+
pstmt->setInt(1, -3);
1355+
pstmt->executeUpdate();
1356+
ASSERT(pstmt->getWarnings() != NULL);
1357+
// Statement without tables access does not reset warnings.
1358+
pstmt.reset(con->prepareStatement("SELECT 1"));
1359+
res.reset(pstmt->executeQuery());
1360+
ASSERT(pstmt->getWarnings() == NULL);
1361+
ASSERT(res->next());
1362+
13451363
// TODO - how to use getNextWarning() ?
13461364
stmt->execute("DROP TABLE IF EXISTS test");
13471365
}

test/unit/classes/statement.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
33
44
The MySQL Connector/C++ is licensed under the terms of the GPLv2
55
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -63,18 +63,22 @@ void statement::getWarnings()
6363
{
6464
logMsg("statement::getWarnings() - MySQL_Statement::get|clearWarnings()");
6565
std::stringstream msg;
66+
unsigned int count= 0;
6667

6768
stmt.reset(con->createStatement());
6869
try
6970
{
7071
stmt->execute("DROP TABLE IF EXISTS test");
7172
stmt->execute("CREATE TABLE test(id INT UNSIGNED)");
7273

74+
// Generating 2 warnings to make sure we get only the last 1 - won't hurt
75+
stmt->execute("INSERT INTO test(id) VALUES (-2)");
7376
// Lets hope that this will always cause a 1264 or similar warning
7477
stmt->execute("INSERT INTO test(id) VALUES (-1)");
7578

7679
for (const sql::SQLWarning* warn=stmt->getWarnings(); warn; warn=warn->getNextWarning())
7780
{
81+
++count;
7882
msg.str("");
7983
msg << "... ErrorCode = '" << warn->getErrorCode() << "', ";
8084
msg << "SQLState = '" << warn->getSQLState() << "', ";
@@ -93,6 +97,8 @@ void statement::getWarnings()
9397
ASSERT(("" != warn->getMessage()));
9498
}
9599

100+
ASSERT_EQUALS(1, count);
101+
96102
for (const sql::SQLWarning* warn=stmt->getWarnings(); warn; warn=warn->getNextWarning())
97103
{
98104
msg.str("");
@@ -119,6 +125,17 @@ void statement::getWarnings()
119125
FAIL("There should be no more warnings!");
120126
}
121127

128+
// New warning
129+
stmt->execute("INSERT INTO test(id) VALUES (-3)");
130+
// Verifying we have warnings now
131+
ASSERT(stmt->getWarnings() != NULL);
132+
133+
// Statement without tables access does not reset warnings.
134+
stmt->execute("SELECT 1");
135+
ASSERT(stmt->getWarnings() == NULL);
136+
res.reset(stmt->getResultSet());
137+
res->next();
138+
122139
// TODO - how to use getNextWarning() ?
123140
stmt->execute("DROP TABLE IF EXISTS test");
124141
}

0 commit comments

Comments
 (0)