| | 12 | unsigned int h, i; |
| | 13 | int idx = odbx_host_index[type]++ % hosts.size(); |
| | 14 | |
| | 15 | |
| | 16 | if( m_handle[type] != NULL ) |
| | 17 | { |
| | 18 | odbx_unbind( m_handle[type] ); |
| | 19 | odbx_finish( m_handle[type] ); |
| | 20 | m_handle[type] = NULL; |
| | 21 | } |
| | 22 | |
| | 23 | for( i = 0; i < hosts.size(); i++ ) |
| | 24 | { |
| | 25 | h = ( idx + i ) % hosts.size(); |
| | 26 | |
| | 27 | if( ( err = odbx_init( &(m_handle[type]), getArg( "backend" ).c_str(), hosts[h].c_str(), getArg( "port" ).c_str() ) ) == ODBX_ERR_SUCCESS ) |
| | 28 | { |
| | 29 | if( ( err = odbx_bind( m_handle[type], getArg( "database" ).c_str(), getArg( "username" ).c_str(), getArg( "password" ).c_str(), ODBX_BIND_SIMPLE ) ) == ODBX_ERR_SUCCESS ) |
| | 30 | { |
| | 31 | L.log( m_myname + " Database connection (" + (type ? "write" : "read") + ") to '" + hosts[h] + "' succeeded", Logger::Notice ); |
| | 32 | return true; |
| | 33 | } |
| | 34 | |
| | 35 | L.log( m_myname + " Unable to bind to database on host " + hosts[h] + " - " + string( odbx_error( m_handle[type], err ) ), Logger::Error ); |
| | 36 | continue; |
| | 37 | } |
| | 38 | |
| | 39 | L.log( m_myname + " Unable to connect to server on host " + hosts[h] + " - " + string( odbx_error( m_handle[type], err ) ), Logger::Error ); |
| | 40 | } |
| | 41 | |
| | 42 | m_handle[type] = NULL; |
| | 43 | return false; |
| | 44 | } |
| | 45 | |
| | 46 | |
| | 47 | |
| | 48 | bool OdbxBackend::execStmt( const char* stmt, unsigned long length, QueryType type ) |
| | 49 | { |
| | 50 | int err; |
| 14 | | if( ( err = odbx_query( m_handle, stmt, length ) ) < 0 ) |
| 15 | | { |
| 16 | | L.log( m_myname + " execStmt: Unable to execute query - " + string( odbx_error( m_handle, err ) ), Logger::Error ); |
| 17 | | throw( AhuException( "Error: odbx_query() failed" ) ); |
| 18 | | } |
| 19 | | |
| 20 | | if( !select ) { while( getRecord() ); } |
| 21 | | } |
| 22 | | |
| 23 | | |
| 24 | | |
| 25 | | bool OdbxBackend::getRecord() |
| | 57 | if( ( err = odbx_query( m_handle[type], stmt, length ) ) < 0 ) |
| | 58 | { |
| | 59 | L.log( m_myname + " execStmt: Unable to execute query - " + string( odbx_error( m_handle[type], err ) ), Logger::Error ); |
| | 60 | |
| | 61 | if( err != -ODBX_ERR_PARAM && odbx_error_type( m_handle[type], err ) > 0 ) { return false; } // ODBX_ERR_PARAM workaround |
| | 62 | if( !connectTo( m_hosts[type], type ) ) { return false; } |
| | 63 | if( odbx_query( m_handle[type], stmt, length ) < 0 ) { return false; } |
| | 64 | } |
| | 65 | |
| | 66 | if( type == WRITE ) { while( getRecord( type ) ); } |
| | 67 | |
| | 68 | return true; |
| | 69 | } |
| | 70 | |
| | 71 | |
| | 72 | |
| | 73 | bool OdbxBackend::getRecord( QueryType type ) |
| 95 | | DLOG( L.log( m_myname + " escape()", Logger::Debug ) ); |
| 96 | | |
| 97 | | if( ( err = odbx_escape( m_handle, str.c_str(), str.size(), m_escbuf, &len ) ) < 0 ) |
| 98 | | { |
| 99 | | L.log( m_myname + " escape: Unable to escape string - " + string( odbx_error( m_handle, err ) ), Logger::Error ); |
| 100 | | throw( AhuException( "Error: odbx_escape() failed" ) ); |
| | 143 | DLOG( L.log( m_myname + " escape(string)", Logger::Debug ) ); |
| | 144 | |
| | 145 | if( ( err = odbx_escape( m_handle[type], str.c_str(), str.size(), m_escbuf, &len ) ) < 0 ) |
| | 146 | { |
| | 147 | L.log( m_myname + " escape(string): Unable to escape string - " + string( odbx_error( m_handle[type], err ) ), Logger::Error ); |
| | 148 | |
| | 149 | if( err != -ODBX_ERR_PARAM && odbx_error_type( m_handle[type], err ) > 0 ) { throw( runtime_error( "odbx_escape() failed" ) ); } // ODBX_ERR_PARAM workaround |
| | 150 | if( !connectTo( m_hosts[type], type ) ) { throw( runtime_error( "odbx_escape() failed" ) ); } |
| | 151 | if( odbx_escape( m_handle[type], str.c_str(), str.size(), m_escbuf, &len ) < 0 ) { throw( runtime_error( "odbx_escape() failed" ) ); } |