// SqlSelectionCatalog_t.cxx #include "dataset_catalog/SqlSelectionCatalog.h" #include #include #include #include #include "dataset_util/ThreadCredential.h" #include "dataset_sql/SqlQuery.h" #include "dataset_sql/SelectQuery.h" #include "dataset_sql/InsertQuery.h" #include "dataset_sql/SqlTable.h" #include "dataset_sql/SqlResult.h" #include "dataset_sql/SqlResultTable.h" using std::string; using std::cout; using std::map; using std::vector; using std::endl; using dset::SqlRow; using dset::SqlQuery; using dset::SelectQuery; using dset::InsertQuery; using dset::SqlResult; using dset::SqlResultTable; using dset::SqlSelectionCatalog; typedef SqlResult::Row Row; typedef SqlSelectionCatalog::Name Name; typedef SqlSelectionCatalog::NameList NameList; typedef SqlSelectionCatalog::Attributes Attributes; namespace { void msg(string txt) { cout << "--- " << txt << " ---" << endl; } } int SqlSelectionCatalog_t(string type) { msg("***** Begin testing SqlSelectionCatalog for type " + type + " *****"); msg("Create starting result"); NameList cols; cols.push_back("name"); if ( type == "uid" ) { cols.push_back("uid"); } else if ( type == "idhi" ) { cols.push_back("idhi"); cols.push_back("idlo"); } else if ( type == "vidh" ) { cols.push_back("vidh"); cols.push_back("vidl"); } else { assert(false); } cols.push_back("nevt"); cols.push_back("owner"); SqlResult res(cols); msg("Insert type row"); Row row0; if ( type == "uid" ) { row0["uid"] = "0-0"; } else if ( type == "idhi" ) { row0["idhi"] = "0"; row0["idlo"] = "0"; } else if ( type == "vidh" ) { row0["vidh"] = "0"; row0["vidl"] = "0"; } row0["name"] = ".TYPE:SomekindaSelectionCatalog"; cout << row0 << endl; res.insert_row(row0); assert( res.num_rows() == 1 ); msg("Insert a row"); Row row1; if ( type == "uid" ) { row1["uid"] = "101-201"; } else if ( type == "idhi" ) { row1["idhi"] = "101"; row1["idlo"] = "201"; } else if ( type == "vidh" ) { row1["vidh"] = "101"; row1["vidl"] = "201"; } row1["name"] = "firstname"; row1["owner"] = "Abigail"; cout << row1 << endl; res.insert_row(row1); assert( res.num_rows() == 2 ); msg("Insert another row"); Row row2; if ( type == "uid" ) { row2["uid"] = "101-202"; } else if ( type == "idhi" ) { row2["idhi"] = "101"; row2["idlo"] = "202"; } else if ( type == "vidh" ) { row2["vidh"] = "101"; row2["vidl"] = "202"; } row2["name"] = "entry 2"; row2["owner"] = "Bill Bailey"; cout << row2 << endl; assert( res.insert_row(row2) == 0 ); assert( res.num_rows() == 3 ); msg("Set thread credential owner"); ThreadCredential().set_default_owner("me"); cout << ThreadCredential() << endl; msg("Create catalog"); SqlSelectionCatalog cat(new SqlResultTable(res)); cout << cat << endl; assert( cat.is_valid() ); assert( cat.catalog_type() == "SomekindaSelectionCatalog" ); assert( cat.size() == 2 ); msg("Testing has_name"); assert( ! cat.has_name("entry two") ); assert( cat.has_name("entry 2") ); assert( cat.id("who") == "" ); assert( cat.id("entry 2") == "101-202" ); msg("Testing insertion"); Attributes atts3; atts3["name"] = "Third entry"; atts3["owner"] = "Clara"; cout << atts3 << endl; assert( ! cat.has_name("Third entry") ); assert( cat.insert("Third entry", atts3) == 0 ); cout << cat << endl; assert( cat.has_name("Third entry") ); msg("Testing schema"); if ( type == "uid" ) { assert( cat.schema().size() == 4 ); assert( cat.schema().front() == "name" ); assert( cat.schema().back() == "owner" ); } else { assert( cat.schema().size() == 5 ); assert( cat.schema().front() == "name" ); assert( cat.schema().back() == "owner" ); } msg("Testing query"); assert( cat.query_count(SqlQuery("owner = 'Will Bailey'")) == 0 ); assert( cat.query_count(SqlQuery("")) == 3 ); assert( cat.query_count(SqlQuery("owner = 'Bill Bailey'")) == 1 ); assert( cat.query(SqlQuery("owner = 'Bill Bailey'"), 100).front() == "entry 2" ); msg("Testing update entry"); Attributes newatts3; newatts3["name"] = "Entry 3"; assert( cat.size() == 3 ); assert( cat.has_name("Third entry") ); assert( ! cat.has_name("Entry 3") ); assert( cat.update("Third entry", newatts3) == 0 ); cout << cat << endl; assert( cat.size() == 3 ); assert( ! cat.has_name("Third entry") ); assert( cat.has_name("Entry 3") ); msg( "Testing get_row" ); Attributes atts2; atts2["name"] = "entry 2"; if ( type == "uid" ) { atts2["uid"] = "101-202"; } else if ( type == "idhi" ) { atts2["idhi"] = "101"; atts2["idlo"] = "202"; } else if ( type == "vidh" ) { atts2["vidh"] = "101"; atts2["vidl"] = "202"; } atts2["owner"] = "Bill Bailey"; assert( cat.has_name("entry 2") ); Attributes atts = cat.attributes("entry 2"); cout << atts << endl; assert( atts == atts2 ); msg( "Testing remove" ); assert( cat.has_name("Entry 3") ); assert( cat.size() == 3 ); assert( cat.remove("Entry 3") == 0 ); cout << cat << endl; assert( ! cat.has_name("Entry 3") ); assert( cat.size() == 2 ); msg("Web page"); Text wp = cat.web_page("http::/www.dial.com/SomeRepository", "", ""); cout << wp << endl; assert( wp.size() ); msg("All tests passed successfully"); return 0; } #ifdef CTEST_MAIN int main() { int stat = SqlSelectionCatalog_t("uid"); if ( stat != 0 ) return 100 + stat; stat = SqlSelectionCatalog_t("idhi"); if ( stat != 0 ) return 100 + stat; stat = SqlSelectionCatalog_t("vidh"); if ( stat != 0 ) return 100 + stat; return 0; } #endif