1 #include "connection.h"
4 nodejs_db_informix::Connection::Connection()
8 sslVerifyServer(false),
15 this->connection =
new ITConnection();
18 nodejs_db_informix::Connection::Connection(
27 this->setPassword(pw);
32 nodejs_db_informix::Connection::~Connection() {
37 nodejs_db_informix::Connection::setCharset(
38 const std::string& charset
40 this->charset = charset;
44 nodejs_db_informix::Connection::setCompress(
const bool compress)
throw() {
45 this->compress = compress;
49 nodejs_db_informix::Connection::setInitCommand(
50 const std::string& initCommand
52 this->initCommand = initCommand;
56 nodejs_db_informix::Connection::setReadTimeout(
57 const uint32_t readTimeout
59 this->readTimeout = readTimeout;
63 nodejs_db_informix::Connection::setReconnect(
66 this->reconnect = reconnect;
70 nodejs_db_informix::Connection::setSocket(
const std::string& socket)
throw() {
71 this->socket = socket;
75 nodejs_db_informix::Connection::setSslVerifyServer(
76 const bool sslVerifyServer
78 this->sslVerifyServer = sslVerifyServer;
81 void nodejs_db_informix::Connection::setTimeout(
82 const uint32_t timeout
84 this->timeout = timeout;
88 nodejs_db_informix::Connection::setWriteTimeout(
89 const uint32_t writeTimeout
91 this->writeTimeout = writeTimeout;
95 nodejs_db_informix::Connection::isAlive(
bool ping)
throw() {
97 this->alive = this->connection->IsOpen();
101 std::cout <<
"IsAlive() == " << this->alive << std::endl;
115 std::string db = this->getDatabase();
118 && !dbInfo.SetDatabase(ITString(db.c_str()))) {
119 std::cerr <<
"Failed to set database " << db << std::endl;
122 std::string u = this->getUser();
125 && !dbInfo.SetUser(ITString(u.c_str()))) {
126 std::cerr <<
"Failed to set username " << u << std::endl;
129 std::string h = this->getHostname();
132 && !dbInfo.SetSystem(ITString(h.c_str()))) {
133 std::cerr <<
"Failed to set hostname " << h << std::endl;
136 std::string pw = this->getPassword();
139 && !dbInfo.SetPassword(ITString(pw.c_str()))) {
140 std::cerr <<
"Failed to set password " << pw << std::endl;
147 nodejs_db_informix::Connection::open() throw(nodejs_db::Exception&) {
152 if (this->connection->IsOpen()) {
156 ITDBInfo dbInfo = this->connection->GetDBInfo();
158 if (dbInfo.Frozen()) {
162 if (dbInfo.GetSystem().IsNull()) {
166 if (dbInfo.GetDatabase().IsNull()) {
170 if (dbInfo.GetUser().IsNull()) {
175 if (!this->_prepareITDBInfo(dbInfo)) {
180 std::cout <<
"Connecting with " << std::endl
181 <<
"User: " << dbInfo.GetUser().Data() << std::endl
182 <<
"System: " << dbInfo.GetSystem().Data() << std::endl
183 <<
"Database: " << dbInfo.GetDatabase().Data() << std::endl
188 if (!this->connection->SetDBInfo(dbInfo)) {
193 if (!(this->alive = this->connection->Open())) {
198 if (!this->connection->IsOpen()) {
205 nodejs_db_informix::Connection::close() {
207 if(this->connection->Close()) {
221 std::string result(s);
224 pos = result.find(
"'", pos);
225 if (pos == std::string::npos)
227 result.insert(pos,
"'");
234 pos = result.find(
"\n", pos);
235 if (pos == std::string::npos)
237 result.erase(pos, 1);
238 result.insert(pos,
"\\n");
243 pos = result.find(
"\r", pos);
244 if (pos == std::string::npos)
246 result.erase(pos, 1);
247 result.insert(pos,
"\\r");
260 return std::string(
"0.0.11");
267 nodejs_db_informix::Connection::_testExecForIteration()
const {
268 ITQuery q_tmp(*(this->connection));
269 std::string qry(
"select * from sysmaster:sysdatabases");
271 if (!q_tmp.ExecForIteration(qry.c_str())) {
272 std::cerr <<
"Could not execute query: " << qry << std::endl;
276 const ITTypeInfo *ti = q_tmp.RowType();
277 for (
long cc = 0; cc < ti->ColumnCount(); ++cc) {
278 if (!ti->ColumnName(cc).IsNull()) {
279 std::cout <<
"Column " << cc <<
": "
280 << ti->ColumnName(cc).Data() << std::endl;
282 std::cerr <<
"Column " << cc <<
": Error!" << std::endl;
288 while ((row = q_tmp.NextRow()) != NULL) {
290 std::cout << row->Printable() << std::endl;
293 std::cout << rc <<
" rows returned" << std::endl
294 <<
"Query: " << qry << std::endl;
305 const ITErrorManager& err
309 std::ostream *s = (std::ostream*) args;
310 (*s) <<
"Query: [" << errCode <<
"]"
311 << err.SqlState().Data() <<
' '
312 << err.ErrorText().Data()
315 return IT_NOTHANDLED;
325 const std::string& query
329 this->_testExecForIteration();
333 std::cout <<
"nodejs_db_informix::Connection::query" << std::endl;
336 ITQuery q(*(this->connection));
338 q.AddCallback(_QueryErrorHandler, (
void*) &std::cerr);
340 ITSet *rs = q.ExecToSet(query.c_str());
342 if (rs == NULL || q.RowCount() < 0) {
343 std::stringstream err;
347 << q.SqlState().Data()
349 << q.WarningText().Data()
356 << q.SqlState().Data()
358 << q.ErrorText().Data()
363 err <<
"Could not execute query: " << query;
379 const std::string& query
382 std::cout <<
"nodejs_db_informix::Connection::query_x" << std::endl;
384 ITQuery q(*(this->connection));
386 q.AddCallback(_QueryErrorHandler, (
void*) &std::cerr);
388 ITBool s = q.ExecForStatus(query.c_str());
391 std::stringstream err;
395 << q.SqlState().Data()
397 << q.WarningText().Data()
404 << q.SqlState().Data()
406 << q.ErrorText().Data()
411 err <<
"Could not execute query.";
417 ITString qt = q.Command();
419 std::cout <<
"Type of query: " << qt.Data() << std::endl;
420 std::cout <<
"Result of DML: " << s << std::endl;