COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libremotesql/RemoteSqlTextBook.cpp
Go to the documentation of this file.
00001 #include <coopy/Dbg.h>
00002 #include <coopy/RemoteSqlTextBook.h>
00003 #include <coopy/RemoteSqlSheet.h>
00004 #include <stdio.h>
00005 
00006 #include <sqlxx.h>
00007 #include <strutilsxx.h>
00008 
00009 #include <algorithm>
00010 
00011 using namespace coopy::store;
00012 using namespace coopy::store::remotesql;
00013 using namespace std;
00014 using namespace sqlxx;
00015 
00016 #define HELPER(x) (*((CSQL*)(x)))
00017 
00018 RemoteSqlTextBook::RemoteSqlTextBook() {
00019   implementation = NULL;
00020   dirty = false;
00021 }
00022 
00023 RemoteSqlTextBook::~RemoteSqlTextBook() {
00024   clear();
00025 }
00026 
00027 void RemoteSqlTextBook::clear() {
00028   if (implementation!=NULL) {
00029     HELPER(implementation).disconnect();
00030     delete &HELPER(implementation);
00031     implementation = NULL;
00032   }
00033 }
00034 
00035 bool RemoteSqlTextBook::open(const Property& config) {
00036   dirty = true;
00037   try {
00038     implementation = new CSQL();
00039     COOPY_ASSERT(implementation!=NULL);
00040     CSQL& SQL = HELPER(implementation);
00041     SQL.setUsername(config.get("username").asString().c_str());
00042     SQL.setPassword(config.get("password").asString().c_str());
00043     SQL.setHostname(config.get("host").asString().c_str());
00044     SQL.setPort(config.get("port",PolyValue::makeInt(3128)).asInt());
00045 //    SQL.setSocket("/var/run/mysqld/mysqld.sock");
00046     database_name = config.get("database").asString().c_str();
00047     SQL.setDatabase(database_name);
00048     SQL.setType(SQLXX_MYSQL);
00049     //SQL.setType(SQLXX_ODBC);
00050     //SQL.setDriver("/usr/lib/libmyodbc.so");
00051     SQL.connect();
00052     if (config.check("table")) {
00053       names_cache.clear();
00054       names_cache.push_back(config.get("table").asString());
00055       dirty = false;
00056     }
00057     return true;
00058   }
00059   catch (sqlxx_error E) {
00060     cerr << "remotesql: " << E.what() << endl;
00061     if (implementation!=NULL) {
00062       CSQL& SQL = HELPER(implementation);
00063       fprintf(stderr,"hostname %s database %s port %d username %s\n", 
00064               SQL.getHostname().c_str(),
00065               SQL.getDatabase().c_str(),
00066               SQL.getPortN(),
00067               SQL.getUsername().c_str());
00068       fprintf(stderr,"password [%s]\n", SQL.getPassword().c_str());
00069       delete &HELPER(implementation);
00070       implementation = NULL;
00071     }
00072     return false;
00073   }
00074   printf("RemoteSqlTextBook failed\n");
00075   return false;
00076 }
00077 
00078 std::vector<std::string> RemoteSqlTextBook::getNames() {
00079   if (!dirty) return names_cache;
00080   vector<string>& names = names_cache;
00081   names.clear();
00082   dirty = false;
00083   if (implementation==NULL) return names;
00084   CSQL& SQL = HELPER(implementation);
00085   string query = string("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA=")+quote(database_name);
00086   //printf("Query is %s\n", query.c_str());
00087   CSQLResult *result = SQL.openQuery(query);
00088   if (result==NULL) return names;
00089   while (result->fetch()) {
00090     //printf("Got %s\n", result->get(0).c_str());
00091     names.push_back(result->get(0));
00092   }
00093   SQL.closeQuery(result);
00094   return names;
00095 }
00096 
00097 PolySheet RemoteSqlTextBook::readSheet(const std::string& name) {
00098   vector<string> names = getNames();
00099   if (std::find(names.begin(), names.end(), name)==names.end()) {
00100     return PolySheet();
00101   }
00102   RemoteSqlSheet *sheet = new RemoteSqlSheet(this,name.c_str());
00103   return PolySheet(sheet,true);
00104 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines