Library /lib/yadal/class.ODBC.php

Language PHP Lines 262
MD5 Hash 8c90a11277b1325d06b426900774a912
Repository https://github.com/reshadf/Library.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
 * Yadal interface for ODBC
 *
 * @todo This file is not finished at all!!!!
 *
 * @package Yadal
 */

/**
 * NOTE!! THIS VERSION DOES NOT WORK
 * SEE test/test.odbc.php!!
 */

/**
 * class ODBC
 *
 * Yadal - Yet Another Database Abstraction Layer
 * ODBC
 *
 * @author Teye Heimans
 * @package Yadal
 */
class ODBC extends Yadal {
    var $_dsn;		// dsn

    /**
     * ODBC::ODBC()
     *
     * Constructor
     *
     * @return ODBC
     * @access public
     * @author Teye Heimans
     */
    function ODBC( $db ) {
        $this->Yadal( $db );
        $this->_nameQuote = "'";
    }

    /**
     * ODBC::connect()
     *
     * Make a connection with the database and
     * select the database.
     *
     * @param string dns: the dns to connect to
     * @param string username: the username which should be used to login
     * @param string password: the password which should be used to login
     * @return resource: The connection resource or false on failure
     * @access public
     * @author Teye Heimans
     */
    function connect( $dsn = '', $username = '', $password = '' ) {
    	// connect with odbc
    	$this->_conn = odbc_connect( $dsn, $username, $password );
    	$this->_dsn  = $dsn;

    	if( $this->_conn ) {
    	   $this->_isConnected = true;

    	   // return the connection resource
    	   return $this->_conn;
    	}

    	return false;
    }

    /**
     * ODBC::query()
     *
     * Execute the query
     *
     * @param string $query: the query
     * @return resource
     * @access public
     * @author Teye Heimans
     */
    function query( $query ) {
    	$this->_lastQuery = $query;

        // execute the query
        return odbc_exec( $this->_conn, $query );
    }

    /**
     * ODBC::getInsertId()
     *
     * Get the id of the last inserted record
     *
     * @return int
     * @access public
     * @author Teye Heimans
     */
    function getInsertId() {
        //
    }

    /**
     * ODBC::getError()
     *
     * Return the last error
     *
     * @return string
     * @access public
     * @author Teye Heimans
     */
    function getError() {
        return odbc_errormsg( $this->_conn );
    }

    /**
     * ODBC::recordCount()
     *
     * Return the number of records found by the query
     *
     * @param resource $sql: The sql resource we have to count
     * @return int
     * @access public
     * @author Teye Heimans
     */
    function recordCount( $sql) {
        return odbc_num_rows( $sql );
    }

    /**
     * ODBC::getRecord()
     *
     * Fetch a record in assoc mode and return it
     *
     * @param resource $sql: The sql resource where we have to get a row from
     * @return: assoc array or false when there are no records left
     * @access public
     * @author Teye Heimans
     */
    function getRecord( $sql ) {
        return mysql_fetch_assoc( $sql );
    }

    /**
     * ODBC::getFieldNames()
     *
     * Return the field names of the table
     *
     * @param string $table: the table where we should fetch the field names from
     * @return array
     * @access public
     * @author Teye Heimans
     */
    function getFieldNames( $table  ) {

    	$sql = odbc_columns( $this->_conn );

        $result = array();

        $num = odbc_num_fields( $sql );
		for ( $i = 1; $i <= $num; $i++ ) {
		  	$result[$i-1] = odbc_field_name($sql, $i);
		}

		$num = odbc_num_rows( $sql );
		echo "Aantal rows: $num<br />\n";
		for( $i = 0; $i <= $num; $i++ ) {
			echo odbc_result( $sql, 4) ."<br >\n";
		}

		return $result;
    }


    /**
     * ODBC::escapeString()
     *
     * Public: escape the string we are going to save from dangerous characters
     *
     * @param string $string
     * @return string
     */
    function escapeString( $string ) {
        return mysql_real_escape_string( $string );
    }

    /**
     * ODBC::fetchKeys()
     *
     * Public: fetch the keys from the table
     *
     * @return array of the keys which are found
     */
    function fetchKeys( $table = null) {
        $table = is_null($table) ? $this->_table : $table;

        $tmp = $this->_sql;

        //odbc_primarykeys( $this->_sql

        $sql = $this->query("SHOW KEYS FROM `".$table."`");
        $keys = array();
        while( $r = $this->getRecord() ) {
            if ( $r['Key_name'] == 'PRIMARY' ) {
                $keys['PR'][] = $r['Column_name'];
            } else {
            	$keys[$r['Key_name']][] = $r['Column_name'];
            }
        }

        mysql_free_result($sql);
        $this->_sql = $tmp;

        // if no keys are found...
        if(sizeof($keys) == 0) {
            trigger_error(
              "Error, could not fetch the indexes from table '".$table."'! ".
              "If you didn't define a primary key or another index type, ".
              "please set the name of the field (which should be used for indexing) ".
              "manually in the dbinfo() function!",
              E_USER_WARNING
            );
            return null;
        }

        if(isset($keys['PR'])) {
        	return $keys['PR'];
        } else {
        	$d = each( $keys );
        	return $d[1];
        }
    }

    /**
     * ODBC::fetchUniqueFields()
     *
     * Public: fetch the unique fields from the table
     *
     * @param string $table
     * @return array
     */
    function fetchUniqueFields( $table = null ) {
        $table = is_null($table) ? $this->_table : $table;

        $tmp = $this->_sql;

        $sql = $this->query("SHOW KEYS FROM `".$table."`");
        $unique = array();
        while( $r = $this->getRecord() ) {
            if ( $r['Non_unique'] == 0) {
                $unique[] = $r['Column_name'];
            }
        }

        mysql_free_result($sql);
        $this->_sql = $tmp;

        if(sizeof($unique) > 0) {
        	return $unique;
        } else {
        	return array();
        }
    }
}

?>
Back to Top