1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2013, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * Odbc
36: * 14.01.2013 20:33
37: */
38: namespace MOC\Module\Database\Driver;
39: use MOC\Module\Database\Driver;
40:
41: /**
42: * Class which provides a common ODBC interface
43: */
44: class Odbc extends Driver {
45:
46: /**
47: * Opens a ODBC database connection
48: *
49: * @param string $DSN
50: * @param string $User
51: * @param string $Password
52: * @param null|string $Database
53: *
54: * @return bool
55: */
56: public function Open( $DSN, $User, $Password, $Database = null ){
57: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
58: if( false == ( $Resource = odbc_connect( $DSN, $User, $Password ) ) ) {
59: if( strlen( $Error = odbc_errormsg() ) ) { $this->DebugError( odbc_error().' '.$Error ); }
60: return false;
61: } else {
62: $this->SetResource( $Resource );
63: return true;
64: }
65: }
66:
67: /**
68: * Executes a SQL query
69: *
70: * @param int $FETCH_AS
71: *
72: * @return array|bool
73: */
74: public function Execute( $FETCH_AS = self::RESULT_AS_ARRAY_ASSOC ) {
75: if( !$this->GetResource() ) {
76: return false;
77: }
78: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
79: if( false === ( $Result = odbc_exec( $this->GetResource(), $this->GetQuery() ) ) ) {
80: if( strlen( $Error = odbc_errormsg() ) ) { $this->DebugError( odbc_error().' '.$Error."\n\n".$this->GetQuery() ); }
81: return false;
82: }
83: switch( $FETCH_AS ) {
84: case self::RESULT_AS_ARRAY_ASSOC: {
85: return $this->FetchAsArrayAssoc( $Result );
86: }
87: default: {
88: return $this->FetchAsArray( $Result );
89: }
90: }
91: }
92:
93: /**
94: * Fetches a query result as an array
95: *
96: * @param resource $Result
97: *
98: * @return array
99: */
100: protected function FetchAsArray( $Result ) {
101: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
102: $Return = array();
103: $RowCount = odbc_num_rows( $Result );
104: while( false !== ( $Row = odbc_fetch_array( $Result ) ) ) {
105: //if( strlen( $Error = odbc_errormsg() ) ) { $this->DebugError( odbc_error().' '.$Error ); }
106: array_push( $Return, array_values( $Row ) );
107: }
108: $this->DebugMessage( 'Affected Rows: '.( $RowCount == -1 ? $RowCount = odbc_num_rows( $Result ) : $RowCount ) );
109: $this->DebugMessage( array_slice( $Return, 0, ( $RowCount > 1 ? 1 : $RowCount ), true ) );
110: odbc_free_result( $Result );
111: return $Return;
112: }
113:
114: /**
115: * Fetches a query result as an associative array
116: *
117: * @param resource $Result
118: *
119: * @return array
120: */
121: protected function FetchAsArrayAssoc( $Result ) {
122: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
123: $Return = array();
124: $RowCount = odbc_num_rows( $Result );
125: while( false !== ( $Row = odbc_fetch_array( $Result ) ) ) {
126: //if( strlen( $Error = odbc_errormsg() ) ) { $this->DebugError( odbc_error().' '.$Error ); }
127: array_push( $Return, $Row );
128: }
129: $this->DebugError( 'Affected Rows: '.( $RowCount == -1 ? $RowCount = odbc_num_rows( $Result ) : $RowCount ) );
130: //$this->DebugMessage( array_slice( $Return, 0, ( $RowCount > 1 ? 1 : $RowCount ), true ) );
131: odbc_free_result( $Result );
132: return $Return;
133: }
134:
135: /**
136: * Closes a database connection
137: */
138: public function Close(){
139: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
140: odbc_close( $this->GetResource() );
141: $this->SetResource(null);
142: }
143:
144: /**
145: * Starts a Transaction
146: */
147: public function TransactionStart() {
148: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
149: odbc_autocommit( $this->GetResource(), false );
150: }
151:
152: /**
153: * Ends a Transaction with Commit
154: */
155: public function TransactionCommit() {
156: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
157: odbc_commit( $this->GetResource() );
158: odbc_autocommit( $this->GetResource(), true );
159: }
160:
161: /**
162: * Ends a Transaction with Rollback
163: */
164: public function TransactionRollback() {
165: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
166: odbc_rollback( $this->GetResource() );
167: odbc_autocommit( $this->GetResource(), true );
168: }
169: }
170: