61{
62 boost::mysql::statement stmt;
63 boost::mysql::results results;
64 boost::mysql::static_results<std::tuple<>> empty_result;
65 conn->execute("CREATE DATABASE IF NOT EXISTS `horizon`", results);
66
67 try {
68 conn->execute("CREATE TABLE IF NOT EXISTS `horizon`.`test` ( \
69 `idtest` INT NOT NULL, \
70 `testint` INT NOT NULL, \
71 `teststring` VARCHAR(45) NOT NULL, \
72 `testbool` TINYINT NOT NULL, \
73 `testdate` DATE NOT NULL, \
74 `testdatetime` DATETIME NOT NULL, \
75 PRIMARY KEY(`idtest`)); \
76 ", empty_result);
77 std::cout << "Created Table `horizon`.`test`..." << std::endl;
78
79 stmt = conn->prepare_statement("INSERT INTO `horizon`.`test` (`idtest`, `testint`, `teststring`, `testbool`, `testdate`, `testdatetime`) VALUES (?, ?, ?, ?, ?, ?);");
80 auto b = stmt.bind(1, 100, "test string", true, "1990-10-01", "1990-11-01 01:01:01");
81 conn->execute(b, empty_result);
82
83 std::cout << "Inserted 1 column into `horizon`.`test`..." << std::endl;
84 }
85 catch (boost::mysql::error_with_diagnostics& error) {
86 std::cout << "error:" << error.what() << std::endl;
87 }
88
89 try {
90 std::cout << "Fetching 1 column from `horizon`.`test`..." << std::endl;
91 stmt = conn->prepare_statement("SELECT `idtest`, `testint`, `teststring`, `testbool`, `testdate`, `testdatetime` FROM `horizon`.`test` WHERE `idtest` = ?");
92 auto b1 = stmt.bind(1);
93
94 boost::mysql::static_results<test> test_rows;
95
96 conn->execute(b1, test_rows);
97
98 if (test_rows.rows().empty()) {
99 BOOST_TEST_FAIL("Failed to retrieve rows for test table");
100 }
101
102 const test& t = test_rows.rows()[0];
103
104 BOOST_CHECK_EQUAL(t.
idtest, 1);
105 BOOST_CHECK_EQUAL(t.
testint, 100);
106 BOOST_CHECK_EQUAL(t.
teststring.compare(
"test string"), 0);
107 BOOST_CHECK_EQUAL(t.
testbool,
true);
108 BOOST_CHECK_EQUAL(t.
testdate.day(), 01);
110
111 std::cout << "Dropping table `horizon`.`test`..." << std::endl;
112
113 conn->execute("DROP TABLE `horizon`.`test`;", empty_result);
114
115 std::cout << "Executed all queries successfully." << std::endl;
116 }
117 catch (boost::mysql::error_with_diagnostics& error) {
118 std::cout << "error:" << error.what() << std::endl;
119 }
120}
Definition: MySQLTest.cpp:50
std::string teststring
Definition: MySQLTest.cpp:53
int testint
Definition: MySQLTest.cpp:52
boost::mysql::date testdate
Definition: MySQLTest.cpp:55
boost::mysql::datetime testdatetime
Definition: MySQLTest.cpp:56
bool testbool
Definition: MySQLTest.cpp:54
int idtest
Definition: MySQLTest.cpp:51