class SybaseManager { public: SybaseManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned int port); ~SybaseManager(); /* * Init SQL Server * @param hosts: Host IP address * @param userName: Login UserName * @param password: Login Password * @param dbName: Database Name * @param port: Host listen port number */ void initConnection(); /* * Making query from database * @param mysql: MySQL Object * @param sql: Running SQL command */ bool runSQLCommand(std::string sql); /** * Destroy MySQL object * @param mysql MySQL object */ void destroyConnection(); bool getConnectionStatus(); vector<vector<string> > getResult(); protected: void setUserName(std::string userName); void setHosts(std::string hosts); void setPassword(std::string password); void setDBName(std::string dbName); void setPort(unsigned int port); private: bool IsConnected; DBPROCESS *dbProcess; vector< vector<string> > resultList; unsigned int DEFAULTPORT; char * HOSTS; char * USERNAME; char * PASSWORD; char * DBNAME; };
#endif /* SYBASEMANAGER_H_ */
/* * SyBaseManager.cpp * * Created .: Feb 18, 2009 * Author: Steven Wee */ #include "SybaseManager.h"
SybaseManager::SybaseManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned int port) { IsConnected = false; this ->setHosts(hosts); this ->setUserName(userName); this ->setPassword(password); this ->setDBName(dbName); this ->setPort(port); }
void SybaseManager::setDBName(string dbName) { if ( dbName.empty() ) { std::cout << "DBName is null! Used default value: master" << std::endl; this ->DBNAME = new char[5]; strcpy(this ->DBNAME, "master"); } else { this ->DBNAME = new char[dbName.length()]; strcpy(this ->DBNAME, dbName.c_str()); } }
void SybaseManager::setHosts(string hosts) { if ( hosts.empty() ) { std::cout << "Hosts is null! Used default value: localhost" << std::endl; this ->HOSTS = new char[9]; strcpy(this ->HOSTS, "localhost"); } else { this ->HOSTS = new char[hosts.length()]; strcpy(this ->HOSTS, hosts.c_str()); } }
void SybaseManager::setPassword(string password) { if ( password.empty() ) { std::cout << "Password is null! Used default value: " << std::endl; this ->PASSWORD = new char[1]; strcpy(this ->PASSWORD, ""); } else { this ->PASSWORD = new char[password.length()]; strcpy(this ->PASSWORD, password.c_str()); } }
void SybaseManager::setPort(unsigned int port) { if ( port ) { std::cout << "Port number is null! Used default value: 0" << std::endl; this ->DEFAULTPORT = 0; } else { this ->DEFAULTPORT = port; } }
void SybaseManager::setUserName(string userName) { if ( userName.empty() ) { std::cout << "UserName is null! Used default value: sa" << std::endl; this ->USERNAME = new char[4]; strcpy(this ->USERNAME, "sa"); } else { this ->USERNAME = new char[userName.length()]; strcpy(this ->USERNAME, userName.c_str()); } }
void SybaseManager::initConnection() { string Charset = "UTF-8"; dbinit(); LOGINREC *loginREC = dblogin(); DBSETLUSER(loginREC, this ->USERNAME); DBSETLPWD(loginREC, this ->PASSWORD); DBSETLCHARSET(loginREC, Charset.c_str()); dbProcess = dbopen(loginREC, this ->HOSTS); if ( dbProcess == FAIL ) { std::cout << "Connect to SQL Server failed!" << std::endl; } if ( dbuse( dbProcess, this ->DBNAME ) == FAIL ) { std::cout << "Use table failed!" << std::endl; } }