Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DatabaseEntryWorkUnit.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet;
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file DatabaseEntryWorkUnit.cc
11 ///
12 /// @brief A work unit that runs a database query, processes the results, and returns a string (presumably a database insert statement)
13 
14 /// @author Tim Jacobs
15 
16 //Unit
18 
19 //Basic
20 #include <basic/Tracer.hh>
21 
22 //Utility and basic
23 #include <basic/database/sql_utils.hh>
24 #include <utility/sql_database/DatabaseSessionManager.hh>
25 #include <utility/string_util.hh>
26 
27 //C++
28 #include <string>
29 #include <map>
30 
31 static basic::Tracer TR("DatabaseEntryWorkUnit");
32 
33 namespace protocols{
34 namespace wum{
35 
36 using namespace std;
37 
38 DatabaseEntryWorkUnit::DatabaseEntryWorkUnit(utility::sql_database::sessionOP db_session):
39 db_session_(db_session)
40 {}
41 
42 DatabaseEntryWorkUnit::DatabaseEntryWorkUnit( std::map<std::string,std::string> row_map ):
43 WorkUnitBase(),
44 row_map_(row_map)
45 {}
46 
47 void
49 
50  TR << "Serializing db entry data" << endl;
51 
52  //serialize the row map using commas to separate column name and value and a forward slash to separate columns
53  string data("");
54  for( map<string,string>::const_iterator it = row_map_.begin();
55  it != row_map_.end(); ++it){
56 
57  data += it->first + "," + it->second + "/";
58  }
59 
60  //Add the results_query_string_ data to the end of the serial data separate by a pipe
61  data+="|" + result_query_string_;
62  serial_data() = data;
63 }
64 
65 void
67  const std::string data(serial_data());
68 
69  TR << "De-serializing db entry data" << endl;
70 
71  //split between the serialized row map data and the query string
72  utility::vector1< std::string > tokens = utility::string_split(data, '|');
73  if(tokens.size() != 2){
74  utility_exit_with_message("Error: DatabaseEntryWorkUnit failed to deserialize");
75  }
76  result_query_string_=tokens[2];
77 
78  //deserialize the map
79  utility::vector1< std::string > entries = utility::string_split(tokens[1], '/');
80 
81  TR << "Total columns: " << entries.size() << endl;
82 
83  for(Size i=0; i<entries.size(); ++i){
84  if(!entries[i].empty()){
85  utility::vector1< std::string > key_values = utility::string_split(entries[i], ',');
86  if(key_values.size() != 2){
87  utility_exit_with_message("Error: DatabaseEntryWorkUnit failed to deserialize the results map");
88  }
89  row_map_[key_values[0]] = key_values[1];
90  }
91  }
92 }
93 
94 }//namespace wum
95 }//namespace protocols