Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Handle null values better #12

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Plugins/SQL/NWScript/nwnx_sql_t.nss
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ void main()
report("Negative prepare query", NWNX_SQL_PrepareQuery("not a valid query!") == 0);
report("GetLastError", NWNX_SQL_GetLastError() != "");

// Test with null values
NWNX_SQL_ExecuteQuery("INSERT INTO sql_test(colInt, colFloat, colStr, colObjId, colObj) VALUES(5121, null, null, null, null)");
report("Select null", NWNX_SQL_ExecuteQuery("SELECT * FROM sql_test WHERE colInt=5121"));
if (NWNX_SQL_ReadyToReadNextRow())
{
NWNX_SQL_ReadNextRow();
int n = StringToInt(NWNX_SQL_ReadDataInActiveRow(0));
report("ReadInt", n == 5121);
float f = StringToFloat(NWNX_SQL_ReadDataInActiveRow(1));
report("ReadFloat", f == 0.0);
string s = NWNX_SQL_ReadDataInActiveRow(2);
report("ReadString", s == "");

string sObjId = NWNX_SQL_ReadDataInActiveRow(3); // In base 10
report("ReadObjectId", sObjId == "");

object obj = NWNX_SQL_ReadFullObjectInActiveRow(4);
report("ReadFullObject", obj == OBJECT_INVALID);
}

cleanup();
WriteTimestampedLogEntry("Testing database " + db_type + " complete.");
WriteTimestampedLogEntry("NWNX_SQL unit tests end.");
Expand Down
1 change: 1 addition & 0 deletions Plugins/SQL/Targets/MySQL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ NWNXLib::Maybe<ResultSet> MySQL::ExecuteQuery()
MYSQL_BIND binds[columns];
memset(binds, 0, sizeof(binds));
unsigned long lengths[columns];
memset(lengths, 0, sizeof(lengths));
for (unsigned i = 0; i < columns; i++)
binds[i].length = &lengths[i];
mysql_stmt_bind_result(m_stmt, binds);
Expand Down