Overview

Namespaces

  • MOC
    • Adapter
    • Core
      • Changelog
      • Depending
      • Drive
        • Directory
        • File
      • Error
        • Register
        • Type
      • Journal
      • Proxy
      • Template
      • Xml
    • Extension
      • Excel
      • Flot
      • Mail
      • Pdf
      • Word
      • Xml
      • YUICompressor
      • Zip
    • Generic
      • Common
      • Device
        • Extension
        • Widget
    • Module
      • Database
        • Driver
      • Drive
      • Image
        • Font
      • Installer
      • Network
        • Ftp
          • Directory
          • File
          • Transport
        • Http
        • ParcelTracker
          • Carrier
      • Office
        • Chart
          • Axis
        • Document
          • Excel
            • Cell
              • Format
              • Style
                • Border
                  • Bottom
                  • Left
                  • Right
                  • Top
                • Font
            • Close
            • Page
            • Worksheet
          • Pdf
            • Close
            • Font
            • Page
              • Margin
              • Position
            • Text
          • Xml
            • Close
        • Image
        • Mail
          • Address
          • Content
      • Packer
        • Yui
        • Zip
      • Template
    • Plugin
      • Gateway
      • Repository
      • Shared
  • PHP

Classes

  • Configuration
  • Constant
  • Driver
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  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:  * Driver
 36:  * 14.01.2013 20:35
 37:  */
 38: namespace MOC\Module\Database;
 39: require_once('Configuration.php');
 40: /**
 41:  *
 42:  */
 43: abstract class Driver extends Configuration {
 44:     /** @var null|\resource $Resource */
 45:     private $Resource = null;
 46:     /** @var array $Statement */
 47:     private $Statement = array();
 48:     /** @var int $StatementCount */
 49:     private $StatementCount = 0;
 50:     /** @var array $Parameter */
 51:     private $Parameter = array();
 52: 
 53:     private $Debug = false;
 54: 
 55:     /**
 56:      * @param string $Host
 57:      * @param string $User
 58:      * @param string $Password
 59:      * @param null|string $Database
 60:      *
 61:      * @return bool
 62:      */
 63:     abstract public function Open( $Host, $User, $Password, $Database = null );
 64: 
 65:     /**
 66:      * @return bool
 67:      */
 68:     abstract public function Close();
 69: 
 70:     /**
 71:      * @param string $SqlTemplate
 72:      *
 73:      * @return Driver
 74:      */
 75:     public function Statement( $SqlTemplate ) {
 76:         $this->SetStatement( $SqlTemplate );
 77:         return $this;
 78:     }
 79: 
 80:     /**
 81:      * @param mixed $Value
 82:      * @param null|string $Key
 83:      * @param int $Type
 84:      *
 85:      * @return Driver
 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:      * @param int $FETCH_AS
 94:      *
 95:      * @return array
 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:      * @param \resource $Result
108:      *
109:      * @return array
110:      */
111:     abstract protected function FetchAsArray( $Result );
112: 
113:     /**
114:      * @param \resource $Result
115:      *
116:      * @return array
117:      */
118:     abstract protected function FetchAsArrayAssoc( $Result );
119: 
120:     /**
121:      * Final Driver Methods
122:      */
123: 
124:     /**
125:      * @param string $SqlTemplate
126:      *
127:      * @return Driver
128:      */
129:     final protected function SetStatement( $SqlTemplate ) {
130:         array_push( $this->Statement, $SqlTemplate );
131:         $this->StatementCount++;
132:         return $this;
133:     }
134: 
135:     /**
136:      * @param mixed $Value
137:      * @param null|string $Key
138:      * @param int $Type
139:      *
140:      * @return Driver
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:      * @return bool|string
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:                          * mixed => 'mixed'
170:                          */
171:                         // Escape Quote-Char
172:                         $Parameter[0] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0] );
173:                         // Quote Parameter
174:                         $Parameter[0] = $this->OptionQuote().$Parameter[0].$this->OptionQuote();
175:                         break;
176:                     }
177:                     case Driver::PARAM_TYPE_STRING_LIST: {
178:                         /**
179:                          * array('string', '..') => string('string', '..')
180:                          */
181:                         foreach( (array)$Parameter[0] as $Index => $String ) {
182:                             // Escape Quote-Char
183:                             $Parameter[0][$Index] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0][$Index] );
184:                             // Quote Parameter
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:                          * mixed => integer
193:                          */
194:                         $Parameter[0] = (integer)$Parameter[0];
195:                         break;
196:                     }
197:                     case Driver::PARAM_TYPE_DATETIME: {
198:                         /**
199:                          * string => datetime
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:                         // Quote Parameter
207:                         $Parameter[0] = $this->OptionQuote().$Parameter[0].$this->OptionQuote();
208:                         break;
209:                     }
210:                     default: {
211:                     /**
212:                      * null => NULL
213:                      * mixed => 'mixed'
214:                      */
215:                         if( null != $Parameter[0] ) {
216:                             // Escape Quote-Char
217:                             $Parameter[0] = str_replace( $this->OptionQuote(), $this->OptionEscapeQuoteWith(), $Parameter[0] );
218:                             // Quote Parameter
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:      * @param null|\resource $Resource
239:      */
240:     final protected function SetResource( $Resource ) {
241:         $this->Resource = $Resource;
242:     }
243: 
244:     /**
245:      * @return null|\resource
246:      */
247:     final protected function GetResource() {
248:         return $this->Resource;
249:     }
250: 
251:     /**
252:      * @param string $String
253:      *
254:      * @return string
255:      */
256:     final private function RegExpEncode( $String ) {
257:         return str_replace(
258:             array( '?'  ),
259:             array( '\?' ),
260:             $String
261:         );
262:     }
263: 
264:     /**
265:      * Debug
266:      */
267: 
268:     /**
269:      * @param int $Type
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: 
API documentation generated by ApiGen 2.8.0