2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
#include "thirdparty/microtest/microtest.h"
|
|
|
|
#include "sql_asserts.h"
|
|
|
|
#include "SQLParser.h"
|
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
using hsql::kExprParameter;
|
|
|
|
using hsql::kExprLiteralInt;
|
2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
using hsql::kStmtDrop;
|
|
|
|
using hsql::kStmtExecute;
|
|
|
|
using hsql::kStmtInsert;
|
|
|
|
using hsql::kStmtPrepare;
|
|
|
|
using hsql::kStmtSelect;
|
|
|
|
|
2017-04-21 16:15:07 +02:00
|
|
|
using hsql::kDropPreparedStatement;
|
|
|
|
|
2017-04-09 13:34:40 +02:00
|
|
|
using hsql::DropStatement;
|
|
|
|
using hsql::ExecuteStatement;
|
|
|
|
using hsql::InsertStatement;
|
|
|
|
using hsql::PrepareStatement;
|
|
|
|
using hsql::SelectStatement;
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PrepareSingleStatementTest) {
|
2017-05-29 16:22:13 +02:00
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"PREPARE test FROM 'SELECT * FROM students WHERE grade = ?';",
|
|
|
|
kStmtPrepare,
|
|
|
|
PrepareStatement,
|
|
|
|
result,
|
|
|
|
prepare);
|
|
|
|
|
|
|
|
ASSERT_STREQ(prepare->name, "test");
|
|
|
|
ASSERT_STREQ(prepare->query, "SELECT * FROM students WHERE grade = ?");
|
|
|
|
|
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
prepare->query,
|
|
|
|
kStmtSelect,
|
|
|
|
SelectStatement,
|
|
|
|
result2,
|
|
|
|
select);
|
|
|
|
|
|
|
|
ASSERT_EQ(result2.parameters().size(), 1);
|
|
|
|
ASSERT(select->whereClause->expr2->isType(kExprParameter))
|
|
|
|
ASSERT_EQ(select->whereClause->expr2->ival, 0)
|
2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
TEST(DeallocatePrepareStatementTest) {
|
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"DEALLOCATE PREPARE test;",
|
|
|
|
kStmtDrop,
|
|
|
|
DropStatement,
|
|
|
|
result,
|
|
|
|
drop);
|
2017-04-09 13:34:40 +02:00
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
ASSERT_EQ(drop->type, kDropPreparedStatement);
|
|
|
|
ASSERT_STREQ(drop->name, "test");
|
|
|
|
}
|
2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
TEST(StatementWithParameters) {
|
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"SELECT * FROM test WHERE a = ? AND b = ?",
|
|
|
|
kStmtSelect,
|
|
|
|
SelectStatement,
|
|
|
|
result,
|
|
|
|
stmt);
|
2017-04-09 13:34:40 +02:00
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
const hsql::Expr* eq1 = stmt->whereClause->expr;
|
|
|
|
const hsql::Expr* eq2 = stmt->whereClause->expr2;
|
2017-04-09 13:34:40 +02:00
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
ASSERT_EQ(result.parameters().size(), 2);
|
2017-04-09 13:34:40 +02:00
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
ASSERT(eq1->isSimpleOp('='))
|
|
|
|
ASSERT(eq1->expr->isType(hsql::kExprColumnRef))
|
|
|
|
ASSERT(eq1->expr2->isType(kExprParameter))
|
|
|
|
ASSERT_EQ(eq1->expr2->ival, 0)
|
|
|
|
ASSERT_EQ(result.parameters()[0], eq1->expr2);
|
2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
ASSERT(eq2->isSimpleOp('='))
|
|
|
|
ASSERT(eq2->expr->isType(hsql::kExprColumnRef))
|
|
|
|
ASSERT(eq2->expr2->isType(kExprParameter))
|
|
|
|
ASSERT_EQ(eq2->expr2->ival, 1)
|
|
|
|
ASSERT_EQ(result.parameters()[1], eq2->expr2);
|
2017-04-09 13:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ExecuteStatementTest) {
|
2017-05-29 16:22:13 +02:00
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"EXECUTE test(1, 2);",
|
|
|
|
kStmtExecute,
|
|
|
|
ExecuteStatement,
|
|
|
|
result,
|
|
|
|
stmt);
|
2017-04-09 13:34:40 +02:00
|
|
|
|
|
|
|
ASSERT_STREQ(stmt->name, "test");
|
|
|
|
ASSERT_EQ(stmt->parameters->size(), 2);
|
|
|
|
}
|