Show
Ignore:
Timestamp:
03/18/07 16:26:42 (6 years ago)
Author:
ahu
Message:

ldap, opendbx updates

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/modules/opendbxbackend/odbxprivate.cc

    r973 r977  
    33 
    44 
    5 void OdbxBackend::execStmt( const char* stmt, unsigned long length, bool select ) 
     5unsigned int odbx_host_index[2] = { 0, 0 }; 
     6 
     7 
     8 
     9bool OdbxBackend::connectTo( const vector<string>& hosts, QueryType type ) 
    610{ 
    711        int err; 
     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 
     48bool OdbxBackend::execStmt( const char* stmt, unsigned long length, QueryType type ) 
     49{ 
     50        int err; 
    851 
    952 
     
    1255        if( m_qlog ) { L.log( m_myname + " Query: " + stmt, Logger::Info ); } 
    1356 
    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 
     73bool OdbxBackend::getRecord( QueryType type ) 
    2674{ 
    2775        int err = 3; 
     
    3280        do 
    3381        { 
     82                if( err < 0 ) 
     83                { 
     84                        L.log( m_myname + " getRecord: Unable to get next result - " + string( odbx_error( m_handle[type], err ) ),  Logger::Error ); 
     85                        throw( AhuException( "Error: odbx_result() failed" ) ); 
     86                } 
     87 
    3488                if( m_result != NULL ) 
    3589                { 
     
    3892                                if( ( err = odbx_row_fetch( m_result ) ) < 0 ) 
    3993                                { 
    40                                         L.log( m_myname + " getRecord: Unable to get next row - " + string( odbx_error( m_handle, err ) ),  Logger::Error ); 
     94                                        L.log( m_myname + " getRecord: Unable to get next row - " + string( odbx_error( m_handle[type], err ) ),  Logger::Error ); 
    4195                                        throw( AhuException( "Error: odbx_row_fetch() failed" ) ); 
    4296                                } 
     
    73127                } 
    74128        } 
    75         while( ( err =  odbx_result( m_handle, &m_result, NULL, 0 ) ) > 0 ); 
    76  
    77         if( err < 0 ) 
    78         { 
    79                 L.log( m_myname + " getRecord: Unable to get next result - " + string( odbx_error( m_handle, err ) ),  Logger::Error ); 
    80                 throw( AhuException( "Error: odbx_result() failed" ) ); 
    81         } 
     129        while( ( err =  odbx_result( m_handle[type], &m_result, NULL, 0 ) ) != 0 ); 
    82130 
    83131        m_result = NULL; 
     
    87135 
    88136 
    89 string OdbxBackend::escape( const string& str ) 
     137string OdbxBackend::escape( const string& str, QueryType type ) 
    90138{ 
    91139        int err; 
     
    93141 
    94142 
    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" ) ); } 
    101152        } 
    102153 
     
    106157 
    107158 
    108 void OdbxBackend::getDomainList( const string& stmt, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ) 
     159bool OdbxBackend::getDomainList( const string& stmt, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ) 
    109160{ 
    110161        const char* tmp; 
     
    116167        DLOG( L.log( m_myname + " getDomainList()", Logger::Debug ) ); 
    117168 
    118         execStmt( stmt.c_str(), stmt.size(), true ); 
    119  
    120         if( !getRecord() ) { return; } 
     169        if( !execStmt( stmt.c_str(), stmt.size(), READ ) ) { return false; } 
     170        if( !getRecord( READ ) ) { return false; } 
    121171 
    122172        do 
     
    129179                if( ( tmp = odbx_field_value( m_result, 6 ) ) != NULL ) 
    130180                { 
    131                         fillSOAData( string( tmp ), sd ); 
     181                        fillSOAData( string( tmp, odbx_field_length( m_result, 6 ) ), sd ); 
    132182                } 
    133183 
     
    172222                } 
    173223        } 
    174         while( getRecord() ); 
     224        while( getRecord( READ ) ); 
     225 
     226        return true; 
    175227} 
    176228