| 104 | | |
|---|
| 105 | | // Try prepare_now() on an already prepared statement |
|---|
| 106 | | C.prepare_now("ReadPGTables"); |
|---|
| 107 | | |
|---|
| 108 | | // Pro forma check: same thing but with name passed as C-style string |
|---|
| 109 | | COMPARE_RESULTS("ReadPGTables_char", |
|---|
| 110 | | T.prepared("ReadPGTables").exec(), |
|---|
| 111 | | T.exec(Q_readpgtables)); |
|---|
| 112 | | |
|---|
| 113 | | cout << "Dropping prepared statement..." << endl; |
|---|
| 114 | | C.unprepare("ReadPGTables"); |
|---|
| 115 | | |
|---|
| 116 | | PQXX_CHECK_THROWS( |
|---|
| 117 | | C.prepare_now("ReadPGTables"), |
|---|
| 118 | | exception, |
|---|
| 119 | | "prepare_now() succeeded on dropped statement."); |
|---|
| 120 | | |
|---|
| 121 | | // Just to try and confuse things, "unprepare" twice |
|---|
| 122 | | cout << "Testing error detection and handling..." << endl; |
|---|
| 123 | | try { C.unprepare("ReadPGTables"); } |
|---|
| 124 | | catch (const exception &e) { cout << "(Expected) " << e.what() << endl; } |
|---|
| 125 | | |
|---|
| 126 | | // Verify that attempt to execute unprepared statement fails |
|---|
| 127 | | PQXX_CHECK_THROWS( |
|---|
| 128 | | T.prepared("ReadPGTables").exec(), |
|---|
| 129 | | exception, |
|---|
| 130 | | "Execute unprepared statement didn't fail."); |
|---|
| 131 | | |
|---|
| 132 | | // Re-prepare the same statement and test again |
|---|
| 133 | | C.prepare("ReadPGTables", Q_readpgtables); |
|---|
| 134 | | C.prepare_now("ReadPGTables"); |
|---|
| 135 | | COMPARE_RESULTS("ReadPGTables_2", |
|---|
| 136 | | T.prepared("ReadPGTables").exec(), |
|---|
| 137 | | T.exec(Q_readpgtables)); |
|---|
| 138 | | |
|---|
| 139 | | // Double preparation of identical statement should be ignored... |
|---|
| 140 | | C.prepare("ReadPGTables", Q_readpgtables); |
|---|
| 141 | | COMPARE_RESULTS("ReadPGTables_double", |
|---|
| 142 | | T.prepared("ReadPGTables").exec(), |
|---|
| 143 | | T.exec(Q_readpgtables)); |
|---|
| 144 | | |
|---|
| 145 | | // ...But a modified definition shouldn't |
|---|
| 146 | | PQXX_CHECK_THROWS( |
|---|
| 147 | | C.prepare("ReadPGTables", Q_readpgtables + " ORDER BY tablename"), |
|---|
| 148 | | exception, |
|---|
| 149 | | "Bad redefinition of statement went unnoticed."); |
|---|
| 150 | | |
|---|
| 151 | | cout << "Testing prepared statement with parameter..." << endl; |
|---|
| 152 | | |
|---|
| 153 | | C.prepare("SeeTable", Q_seetable)("varchar", pqxx::prepare::treat_string); |
|---|
| 154 | | |
|---|
| 155 | | vector<string> args; |
|---|
| 156 | | args.push_back("pg_type"); |
|---|
| 157 | | COMPARE_RESULTS("SeeTable_seq", |
|---|
| 158 | | T.prepared("SeeTable")(args[0]).exec(), |
|---|
| 159 | | T.exec(subst(T,Q_seetable,args))); |
|---|
| 160 | | |
|---|
| 161 | | cout << "Testing prepared statement with 2 parameters..." << endl; |
|---|
| 162 | | |
|---|
| 163 | | C.prepare("SeeTables", Q_seetables) |
|---|
| 164 | | ("varchar",pqxx::prepare::treat_string) |
|---|
| 165 | | ("varchar",pqxx::prepare::treat_string); |
|---|
| 166 | | args.push_back("pg_index"); |
|---|
| 167 | | COMPARE_RESULTS("SeeTables_seq", |
|---|
| 168 | | T.prepared("SeeTables")(args[0])(args[1]).exec(), |
|---|
| 169 | | T.exec(subst(T,Q_seetables,args))); |
|---|
| 170 | | |
|---|
| 171 | | cout << "Testing prepared statement with a null parameter..." << endl; |
|---|
| 172 | | vector<const char *> ptrs; |
|---|
| 173 | | ptrs.push_back(0); |
|---|
| 174 | | ptrs.push_back("pg_index"); |
|---|
| 175 | | COMPARE_RESULTS("SeeTables_null1", |
|---|
| 176 | | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| 177 | | T.exec(subst(T,Q_seetables,ptrs))); |
|---|
| 178 | | COMPARE_RESULTS("SeeTables_null2", |
|---|
| 179 | | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| 180 | | T.prepared("SeeTables")()(ptrs[1]).exec()); |
|---|
| 181 | | COMPARE_RESULTS("SeeTables_null3", |
|---|
| 182 | | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| 183 | | T.prepared("SeeTables")("somestring",false)(ptrs[1]).exec()); |
|---|
| 184 | | COMPARE_RESULTS("SeeTables_null4", |
|---|
| 185 | | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| 186 | | T.prepared("SeeTables")(42,false)(ptrs[1]).exec()); |
|---|
| 187 | | COMPARE_RESULTS("SeeTables_null5", |
|---|
| 188 | | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| 189 | | T.prepared("SeeTables")(0,false)(ptrs[1]).exec()); |
|---|
| 190 | | |
|---|
| 191 | | cout << "Testing wrong numbers of parameters..." << endl; |
|---|
| 192 | | PQXX_CHECK_THROWS( |
|---|
| 193 | | T.prepared("SeeTables")()()("hi mom!").exec(), |
|---|
| 194 | | exception, |
|---|
| 195 | | "No error for too many parameters."); |
|---|
| 196 | | |
|---|
| 197 | | PQXX_CHECK_THROWS( |
|---|
| 198 | | T.prepared("SeeTables")("who, me?").exec(), |
|---|
| 199 | | exception, |
|---|
| 200 | | "No error for too few parameters."); |
|---|
| | 111 | |
|---|
| | 112 | // Try prepare_now() on an already prepared statement |
|---|
| | 113 | C.prepare_now("ReadPGTables"); |
|---|
| | 114 | |
|---|
| | 115 | // Pro forma check: same thing but with name passed as C-style string |
|---|
| | 116 | COMPARE_RESULTS("ReadPGTables_char", |
|---|
| | 117 | T.prepared("ReadPGTables").exec(), |
|---|
| | 118 | T.exec(Q_readpgtables)); |
|---|
| | 119 | |
|---|
| | 120 | cout << "Dropping prepared statement..." << endl; |
|---|
| | 121 | C.unprepare("ReadPGTables"); |
|---|
| | 122 | |
|---|
| | 123 | PQXX_CHECK_THROWS( |
|---|
| | 124 | C.prepare_now("ReadPGTables"), |
|---|
| | 125 | exception, |
|---|
| | 126 | "prepare_now() succeeded on dropped statement."); |
|---|
| | 127 | |
|---|
| | 128 | // Just to try and confuse things, "unprepare" twice |
|---|
| | 129 | cout << "Testing error detection and handling..." << endl; |
|---|
| | 130 | try { C.unprepare("ReadPGTables"); } |
|---|
| | 131 | catch (const exception &e) { cout << "(Expected) " << e.what() << endl; } |
|---|
| | 132 | |
|---|
| | 133 | // Verify that attempt to execute unprepared statement fails |
|---|
| | 134 | PQXX_CHECK_THROWS( |
|---|
| | 135 | T.prepared("ReadPGTables").exec(), |
|---|
| | 136 | exception, |
|---|
| | 137 | "Execute unprepared statement didn't fail."); |
|---|
| | 138 | |
|---|
| | 139 | // Re-prepare the same statement and test again |
|---|
| | 140 | C.prepare("ReadPGTables", Q_readpgtables); |
|---|
| | 141 | C.prepare_now("ReadPGTables"); |
|---|
| | 142 | COMPARE_RESULTS("ReadPGTables_2", |
|---|
| | 143 | T.prepared("ReadPGTables").exec(), |
|---|
| | 144 | T.exec(Q_readpgtables)); |
|---|
| | 145 | |
|---|
| | 146 | // Double preparation of identical statement should be ignored... |
|---|
| | 147 | C.prepare("ReadPGTables", Q_readpgtables); |
|---|
| | 148 | COMPARE_RESULTS("ReadPGTables_double", |
|---|
| | 149 | T.prepared("ReadPGTables").exec(), |
|---|
| | 150 | T.exec(Q_readpgtables)); |
|---|
| | 151 | |
|---|
| | 152 | // ...But a modified definition shouldn't |
|---|
| | 153 | PQXX_CHECK_THROWS( |
|---|
| | 154 | C.prepare("ReadPGTables", Q_readpgtables + " ORDER BY tablename"), |
|---|
| | 155 | exception, |
|---|
| | 156 | "Bad redefinition of statement went unnoticed."); |
|---|
| | 157 | |
|---|
| | 158 | cout << "Testing prepared statement with parameter..." << endl; |
|---|
| | 159 | |
|---|
| | 160 | C.prepare("SeeTable", Q_seetable)("varchar", pqxx::prepare::treat_string); |
|---|
| | 161 | |
|---|
| | 162 | vector<string> args; |
|---|
| | 163 | args.push_back("pg_type"); |
|---|
| | 164 | COMPARE_RESULTS("SeeTable_seq", |
|---|
| | 165 | T.prepared("SeeTable")(args[0]).exec(), |
|---|
| | 166 | T.exec(subst(T,Q_seetable,args))); |
|---|
| | 167 | |
|---|
| | 168 | cout << "Testing prepared statement with 2 parameters..." << endl; |
|---|
| | 169 | |
|---|
| | 170 | C.prepare("SeeTables", Q_seetables) |
|---|
| | 171 | ("varchar",pqxx::prepare::treat_string) |
|---|
| | 172 | ("varchar",pqxx::prepare::treat_string); |
|---|
| | 173 | args.push_back("pg_index"); |
|---|
| | 174 | COMPARE_RESULTS("SeeTables_seq", |
|---|
| | 175 | T.prepared("SeeTables")(args[0])(args[1]).exec(), |
|---|
| | 176 | T.exec(subst(T,Q_seetables,args))); |
|---|
| | 177 | |
|---|
| | 178 | cout << "Testing prepared statement with a null parameter..." << endl; |
|---|
| | 179 | vector<const char *> ptrs; |
|---|
| | 180 | ptrs.push_back(0); |
|---|
| | 181 | ptrs.push_back("pg_index"); |
|---|
| | 182 | COMPARE_RESULTS("SeeTables_null1", |
|---|
| | 183 | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| | 184 | T.exec(subst(T,Q_seetables,ptrs))); |
|---|
| | 185 | COMPARE_RESULTS("SeeTables_null2", |
|---|
| | 186 | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| | 187 | T.prepared("SeeTables")()(ptrs[1]).exec()); |
|---|
| | 188 | COMPARE_RESULTS("SeeTables_null3", |
|---|
| | 189 | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| | 190 | T.prepared("SeeTables")("somestring",false)(ptrs[1]).exec()); |
|---|
| | 191 | COMPARE_RESULTS("SeeTables_null4", |
|---|
| | 192 | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| | 193 | T.prepared("SeeTables")(42,false)(ptrs[1]).exec()); |
|---|
| | 194 | COMPARE_RESULTS("SeeTables_null5", |
|---|
| | 195 | T.prepared("SeeTables")(ptrs[0])(ptrs[1]).exec(), |
|---|
| | 196 | T.prepared("SeeTables")(0,false)(ptrs[1]).exec()); |
|---|
| | 197 | |
|---|
| | 198 | cout << "Testing wrong numbers of parameters..." << endl; |
|---|
| | 199 | PQXX_CHECK_THROWS( |
|---|
| | 200 | T.prepared("SeeTables")()()("hi mom!").exec(), |
|---|
| | 201 | exception, |
|---|
| | 202 | "No error for too many parameters."); |
|---|
| | 203 | |
|---|
| | 204 | PQXX_CHECK_THROWS( |
|---|
| | 205 | T.prepared("SeeTables")("who, me?").exec(), |
|---|
| | 206 | exception, |
|---|
| | 207 | "No error for too few parameters."); |
|---|