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: * Mysql
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 an interface to a MySQL database
43: */
44: class Mysql extends Driver {
45:
46: /**
47: * Constructor which sets database system specific options
48: */
49: function __construct() {
50: $this->OptionQuote("'");
51: $this->OptionEscapeQuoteWith("\\");
52: $this->OptionDateTimeFormat("Y-d-m H:i:s");
53: }
54:
55: /**
56: * Opens a MySQL database connection
57: *
58: * @param string $DSN
59: * @param string $User
60: * @param string $Password
61: * @param null|string $Database
62: *
63: * @return bool
64: */
65: public function Open( $DSN, $User, $Password, $Database = null ){
66: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
67: if( false == ( $Resource = mysql_connect( $DSN, $User, $Password ) ) ) {
68: if( strlen( $Error = mysql_error() ) ) { $this->DebugError( mysql_error().' '.$Error ); }
69: return false;
70: } else {
71: $this->SetResource( $Resource );
72: return true;
73: }
74: }
75:
76: /**
77: * Executes a SQL query
78: *
79: * @param int $FETCH_AS
80: *
81: * @return array|bool
82: */
83: public function Execute( $FETCH_AS = self::RESULT_AS_ARRAY_ASSOC ) {
84: if( !$this->GetResource() ) {
85: return false;
86: }
87: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
88: if( false === ( $Result = mysql_query( $this->GetQuery(), $this->GetResource() ) ) ) {
89: if( strlen( $Error = mysql_error() ) ) { $this->DebugError( mysql_error().' '.$Error."\n\n".$this->GetQuery() ); }
90: return false;
91: }
92: switch( $FETCH_AS ) {
93: case self::RESULT_AS_ARRAY_ASSOC: {
94: return $this->FetchAsArrayAssoc( $Result );
95: }
96: default: {
97: return $this->FetchAsArray( $Result );
98: }
99: }
100: }
101:
102: /**
103: * Fetches a query result as an array
104: *
105: * @param resource $Result
106: *
107: * @return array
108: */
109: protected function FetchAsArray( $Result ) {
110: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
111: $Return = array();
112: $RowCount = mysql_num_rows( $Result );
113: while( false !== ( $Row = mysql_fetch_array( $Result ) ) ) {
114: array_push( $Return, array_values( $Row ) );
115: }
116: $this->DebugMessage( 'Affected Rows: '.( $RowCount == -1 ? $RowCount = mysql_num_rows( $Result ) : $RowCount ) );
117: $this->DebugMessage( array_slice( $Return, 0, ( $RowCount > 1 ? 1 : $RowCount ), true ) );
118: mysql_free_result( $Result );
119: return $Return;
120: }
121:
122: /**
123: * Fetches a query result as an associative array
124: *
125: * @param resource $Result
126: *
127: * @return array
128: */
129: protected function FetchAsArrayAssoc( $Result ) {
130: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
131: $Return = array();
132: $RowCount = mysql_num_rows( $Result );
133: while( false !== ( $Row = mysql_fetch_array( $Result ) ) ) {
134: array_push( $Return, $Row );
135: }
136: $this->DebugError( 'Affected Rows: '.( $RowCount == -1 ? $RowCount = mysql_num_rows( $Result ) : $RowCount ) );
137: mysql_free_result( $Result );
138: return $Return;
139: }
140:
141: /**
142: * Closes a database connection
143: */
144: public function Close(){
145: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
146: mysql_close( $this->GetResource() );
147: $this->SetResource(null);
148: }
149:
150: /**
151: * Starts a Transaction
152: *
153: * @todo Implement Transaction
154: */
155: public function TransactionStart() {
156: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
157: }
158:
159: /**
160: * Ends a Transaction with Commit
161: *
162: * @todo Implement Commit
163: */
164: public function TransactionCommit() {
165: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
166: }
167:
168: /**
169: * Ends a Transaction with Rollback
170: *
171: * @todo Implement Rollback
172: */
173: public function TransactionRollback() {
174: $this->DebugMessage( get_class( $this ).'::'.__FUNCTION__ );
175: }
176: }
177: