| 1 | #include "odbxbackend.hh" |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | void OdbxBackend::execStmt( const char* stmt, unsigned long length, bool select ) |
|---|
| 6 | { |
|---|
| 7 | int err; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | DLOG( L.log( m_myname + " execStmt()", Logger::Debug ) ); |
|---|
| 11 | |
|---|
| 12 | if( m_qlog ) { L.log( m_myname + " Query: " + stmt, Logger::Info ); } |
|---|
| 13 | |
|---|
| 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() |
|---|
| 26 | { |
|---|
| 27 | int err = 3; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | DLOG( L.log( m_myname + " getRecord()", Logger::Debug ) ); |
|---|
| 31 | |
|---|
| 32 | do |
|---|
| 33 | { |
|---|
| 34 | if( m_result != NULL ) |
|---|
| 35 | { |
|---|
| 36 | if( err == 3 ) |
|---|
| 37 | { |
|---|
| 38 | if( ( err = odbx_row_fetch( m_result ) ) < 0 ) |
|---|
| 39 | { |
|---|
| 40 | L.log( m_myname + " getRecord: Unable to get next row - " + string( odbx_error( m_handle, err ) ), Logger::Error ); |
|---|
| 41 | throw( AhuException( "Error: odbx_row_fetch() failed" ) ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | if( err > 0 ) |
|---|
| 45 | { |
|---|
| 46 | #ifdef VERBOSELOG |
|---|
| 47 | unsigned int i; |
|---|
| 48 | string fields; |
|---|
| 49 | |
|---|
| 50 | for( i = 0; i < odbx_column_count( m_result ); i++ ) |
|---|
| 51 | { |
|---|
| 52 | fields += string( odbx_column_name( m_result, i ) ); |
|---|
| 53 | |
|---|
| 54 | if( odbx_field_value( m_result, i ) != NULL ) |
|---|
| 55 | { |
|---|
| 56 | fields += "=" + string( odbx_field_value( m_result, i ) ) + ", "; |
|---|
| 57 | } |
|---|
| 58 | else |
|---|
| 59 | { |
|---|
| 60 | fields += "=NULL, "; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | L.log( m_myname + " Values: " + fields, Logger::Error ); |
|---|
| 65 | #endif |
|---|
| 66 | return true; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | odbx_result_free( m_result ); |
|---|
| 72 | m_result = NULL; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 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 | } |
|---|
| 82 | |
|---|
| 83 | m_result = NULL; |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | string OdbxBackend::escape( const string& str ) |
|---|
| 90 | { |
|---|
| 91 | int err; |
|---|
| 92 | unsigned long len = sizeof( m_escbuf ); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 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" ) ); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return string( m_escbuf, len ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | void OdbxBackend::getDomainList( const string& stmt, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ) |
|---|
| 109 | { |
|---|
| 110 | const char* tmp; |
|---|
| 111 | u_int32_t nlast, nserial; |
|---|
| 112 | DomainInfo di; |
|---|
| 113 | SOAData sd; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | DLOG( L.log( m_myname + " getDomainList()", Logger::Debug ) ); |
|---|
| 117 | |
|---|
| 118 | execStmt( stmt.c_str(), stmt.size(), true ); |
|---|
| 119 | |
|---|
| 120 | if( !getRecord() ) { return; } |
|---|
| 121 | |
|---|
| 122 | do |
|---|
| 123 | { |
|---|
| 124 | nlast = 0; |
|---|
| 125 | nserial = 0; |
|---|
| 126 | sd.serial = 0; |
|---|
| 127 | sd.refresh = 0; |
|---|
| 128 | |
|---|
| 129 | if( ( tmp = odbx_field_value( m_result, 6 ) ) != NULL ) |
|---|
| 130 | { |
|---|
| 131 | DNSPacket::fillSOAData( string( tmp ), sd ); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | if( !sd.serial && ( tmp = odbx_field_value( m_result, 5 ) ) != NULL ) |
|---|
| 135 | { |
|---|
| 136 | sd.serial = strtol( tmp, NULL, 10 ); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | if( ( tmp = odbx_field_value( m_result, 4 ) ) != NULL ) |
|---|
| 140 | { |
|---|
| 141 | nlast = strtol( tmp, NULL, 10 ); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if( ( tmp = odbx_field_value( m_result, 3 ) ) != NULL ) |
|---|
| 145 | { |
|---|
| 146 | nserial = strtol( tmp, NULL, 10 ); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if( (*check_fcn)( nlast, nserial, &sd, &di ) ) |
|---|
| 150 | { |
|---|
| 151 | if( ( tmp = odbx_field_value( m_result, 2 ) ) != NULL ) |
|---|
| 152 | { |
|---|
| 153 | di.master = string( tmp, odbx_field_length( m_result, 2 ) ); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if( ( tmp = odbx_field_value( m_result, 1 ) ) != NULL ) |
|---|
| 157 | { |
|---|
| 158 | di.zone = string( tmp, odbx_field_length( m_result, 1 ) ); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | if( ( tmp = odbx_field_value( m_result, 0 ) ) != NULL ) |
|---|
| 162 | { |
|---|
| 163 | di.id = strtol( tmp, NULL, 10 ); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | di.last_check = nlast; |
|---|
| 167 | di.notified_serial = nserial; |
|---|
| 168 | di.serial = sd.serial; |
|---|
| 169 | di.backend = this; |
|---|
| 170 | |
|---|
| 171 | list->push_back( di ); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | while( getRecord() ); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | bool checkSlave( u_int32_t nlast, u_int32_t nserial, SOAData* sd, DomainInfo* di ) |
|---|
| 180 | { |
|---|
| 181 | if( nlast + sd->refresh < (u_int32_t) time( 0 ) ) |
|---|
| 182 | { |
|---|
| 183 | di->kind = DomainInfo::Slave; |
|---|
| 184 | return true; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | bool checkMaster( u_int32_t nlast, u_int32_t nserial, SOAData* sd, DomainInfo* di ) |
|---|
| 193 | { |
|---|
| 194 | if( nserial != sd->serial ) |
|---|
| 195 | { |
|---|
| 196 | di->kind = DomainInfo::Master; |
|---|
| 197 | return true; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | return false; |
|---|
| 201 | } |
|---|