1: <?php
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: namespace MOC\Module\Database;
39: require_once('Configuration.php');
40: 41: 42:
43: abstract class Driver extends Configuration {
44:
45: private $Resource = null;
46:
47: private $Statement = array();
48:
49: private $StatementCount = 0;
50:
51: private $Parameter = array();
52:
53: private $Debug = false;
54:
55: 56: 57: 58: 59: 60: 61: 62:
63: abstract public function Open( $Host, $User, $Password, $Database = null );
64:
65: 66: 67:
68: abstract public function Close();
69:
70: 71: 72: 73: 74:
75: public function Statement( $SqlTemplate ) {
76: $this->SetStatement( $SqlTemplate );
77: return $this;
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public function Parameter( $Value, $Key = null, $Type = Driver::PARAM_TYPE_NONE ) {
88: $this->SetParameter( $Value, $Key, $Type );
89: return $this;
90: }
91:
92: 93: 94: 95: 96:
97: abstract public function Execute( $FETCH_AS = Driver::RESULT_AS_ARRAY_ASSOC );
98:
99:
100: abstract public function TransactionStart();
101:
102: abstract public function TransactionCommit();
103:
104: abstract public function TransactionRollback();
105:
106: 107: 108: 109: 110:
111: abstract protected function FetchAsArray( $Result );
112:
113: 114: 115: 116: 117:
118: abstract protected function FetchAsArrayAssoc( $Result );
119:
120: 121: 122:
123:
124: 125: 126: 127: 128:
129: final protected function SetStatement( $SqlTemplate ) {
130: array_push( $this->Statement, $SqlTemplate );
131: $this->StatementCount++;
132: return $this;
133: }
134:
135: 136: 137: 138: 139: 140: 141:
142: final protected function SetParameter( $Value, $Key = null, $Type = Driver::PARAM_TYPE_NONE ) {
143: if( !isset( $this->Parameter[$this->StatementCount] ) ) {
144: $this->Parameter[$this->StatementCount] = array();
145: }
146: array_push( $this->Parameter[$this->StatementCount], array( $Value, $Key, $Type ) );
147: return $this;
148: }
149:
150: 151: 152:
153: final protected function GetQuery() {
154: if( $this->StatementCount > 0 ) {
155: $Statement = array_pop( $this->Statement );
156: $this->DebugMessage( 'Statement: '.$Statement );
157: if( isset( $this->Parameter[$this->StatementCount] ) ) {
158: $ParameterList = array_pop( $this->Parameter );
159: } else {
160: $ParameterList = array();
161: }
162: foreach( (array)$ParameterList as $Parameter ) {
163: if( !$Parameter[1] ) {
164: $Parameter[1] = Driver::PARAM_KEY_UNDEFINED;
165: }
166: switch( $Parameter[2] ) {
167: case Driver::PARAM_TYPE_STRING: {
168: 169: 170:
171:
172: $Parameter[0] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0] );
173:
174: $Parameter[0] = $this->OptionQuote().$Parameter[0].$this->OptionQuote();
175: break;
176: }
177: case Driver::PARAM_TYPE_STRING_LIST: {
178: 179: 180:
181: foreach( (array)$Parameter[0] as $Index => $String ) {
182:
183: $Parameter[0][$Index] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0][$Index] );
184:
185: $Parameter[0][$Index] = $this->OptionQuote().$Parameter[0][$Index].$this->OptionQuote();
186: }
187: $Parameter[0] = implode( ', ', $Parameter[0] );
188: break;
189: }
190: case Driver::PARAM_TYPE_INTEGER: {
191: 192: 193:
194: $Parameter[0] = (integer)$Parameter[0];
195: break;
196: }
197: case Driver::PARAM_TYPE_DATETIME: {
198: 199: 200:
201: if( is_integer( $Parameter[0] ) ) {
202: $Parameter[0] = date( $this->OptionDateTimeFormat(), $Parameter[0] );
203: } else {
204: $Parameter[0] = date( $this->OptionDateTimeFormat(), strtotime( $Parameter[0] ) );
205: }
206:
207: $Parameter[0] = $this->OptionQuote().$Parameter[0].$this->OptionQuote();
208: break;
209: }
210: default: {
211: 212: 213: 214:
215: if( null != $Parameter[0] ) {
216:
217: $Parameter[0] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0] );
218:
219: $Parameter[0] = $this->OptionQuote().$Parameter[0].$this->OptionQuote();
220: } else {
221: $Parameter[0] = 'NULL';
222: }
223: break;
224: }
225: }
226: $this->DebugMessage( 'Parameter-Key: '.$Parameter[1].' Parameter-Value: '.$Parameter[0].' Parameter-Type: '.$Parameter[2] );
227: $Statement = preg_replace( '!'.$this->RegExpEncode( $Parameter[1] ).'!s', $Parameter[0], $Statement, 1 );
228: }
229: $this->StatementCount--;
230: $this->DebugMessage( 'Query: '.$Statement );
231: return $Statement;
232: } else {
233: return false;
234: }
235: }
236:
237: 238: 239:
240: final protected function SetResource( $Resource ) {
241: $this->Resource = $Resource;
242: }
243:
244: 245: 246:
247: final protected function GetResource() {
248: return $this->Resource;
249: }
250:
251: 252: 253: 254: 255:
256: final private function RegExpEncode( $String ) {
257: return str_replace(
258: array( '?' ),
259: array( '\?' ),
260: $String
261: );
262: }
263:
264: 265: 266:
267:
268: 269: 270:
271: final public function EnableDebug( $Type = Driver::DEBUG_HTML ) {
272: $this->Debug = $Type;
273: }
274: final protected function DebugError( $Content ) {
275: if( !$this->Debug ) {
276: return false;
277: }
278: switch( $this->Debug ) {
279: default: {
280: $this->DebugErrorAsHtml( $Content );
281: break;
282: }
283: }
284: return true;
285: }
286: final protected function DebugMessage( $Content ) {
287: if( !$this->Debug ) {
288: return false;
289: }
290: switch( $this->Debug ) {
291: default: {
292: $this->DebugAsHtml( $Content );
293: break;
294: }
295: }
296: return true;
297: }
298: final private function DebugAsHtml( $Content ) {
299: if( is_array( $Content ) ) {
300: print '<div style="font-family: arial; font-size: 12px; background-color: #F3F3F3; color: #999999; border: 1px dotted #CCCCCC; margin: 1px; padding: 5px;">Sample:<pre>'.htmlspecialchars(print_r($Content,true)).'</pre></div>';
301: } else {
302: print '<div style="font-family: arial; font-size: 12px; background-color: #F3F3F3; color: #999999; border: 1px dotted #CCCCCC; margin: 1px; padding: 5px;">'.nl2br($Content).'</div>';
303: }
304: }
305: final private function DebugErrorAsHtml( $Content ) {
306: if( is_array( $Content ) ) {
307: print '<div style="font-family: arial; font-size: 12px; background-color: #FFF3F3; color: #FF9999; border: 1px dotted #FFCCCC; margin: 1px; padding: 5px;">Sample:<pre>'.htmlspecialchars(print_r($Content,true)).'</pre></div>';
308: } else {
309: print '<div style="font-family: arial; font-size: 12px; background-color: #FFF3F3; color: #FF9999; border: 1px dotted #FFCCCC; margin: 1px; padding: 5px;">'.nl2br($Content).'</div>';
310: }
311: }
312: }
313: