Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NConnection
  • NRow
  • NSqlLiteral
  • NSqlPreprocessor
  • NStatement

Interfaces

  • ISupplementalDriver
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Database
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * SQL preprocessor.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Database
 20:  */
 21: class NSqlPreprocessor extends NObject
 22: {
 23:     /** @var NConnection */
 24:     private $connection;
 25: 
 26:     /** @var ISupplementalDriver */
 27:     private $driver;
 28: 
 29:     /** @var array of input parameters */
 30:     private $params;
 31: 
 32:     /** @var array of parameters to be processed by PDO */
 33:     private $remaining;
 34: 
 35:     /** @var int */
 36:     private $counter;
 37: 
 38:     /** @var string values|assoc|multi */
 39:     private $arrayMode;
 40: 
 41: 
 42: 
 43:     public function __construct(NConnection $connection)
 44:     {
 45:         $this->connection = $connection;
 46:         $this->driver = $connection->getSupplementalDriver();
 47:     }
 48: 
 49: 
 50: 
 51:     /**
 52:      * @param  string
 53:      * @param  array
 54:      * @return array of [sql, params]
 55:      */
 56:     public function process($sql, $params)
 57:     {
 58:         $this->params = $params;
 59:         $this->counter = 0;
 60:         $this->remaining = array();
 61: 
 62:         $cmd = strtoupper(substr(ltrim($sql), 0, 6)); // detect array mode
 63:         $this->arrayMode = $cmd === 'INSERT' || $cmd === 'REPLAC' ? 'values' : 'assoc';
 64: 
 65:         /*~
 66:             \'.*?\'|".*?"|   ## string
 67:             :[a-zA-Z0-9_]+:| ## :substitution:
 68:             \?               ## placeholder
 69:         ~xs*/
 70:         $sql = NStrings::replace($sql, '~\'.*?\'|".*?"|:[a-zA-Z0-9_]+:|\?~s', array($this, 'callback'));
 71: 
 72:         while ($this->counter < count($params)) {
 73:             $sql .= ' ' . $this->formatValue($params[$this->counter++]);
 74:         }
 75: 
 76:         return array($sql, $this->remaining);
 77:     }
 78: 
 79: 
 80: 
 81:     /** @internal */
 82:     public function callback($m)
 83:     {
 84:         $m = $m[0];
 85:         if ($m[0] === "'" || $m[0] === '"') { // string
 86:             return $m;
 87: 
 88:         } elseif ($m[0] === '?') { // placeholder
 89:             return $this->formatValue($this->params[$this->counter++]);
 90: 
 91:         } elseif ($m[0] === ':') { // substitution
 92:             $s = substr($m, 1, -1);
 93:             return isset($this->connection->substitutions[$s]) ? $this->connection->substitutions[$s] : $m;
 94:         }
 95:     }
 96: 
 97: 
 98: 
 99:     private function formatValue($value)
100:     {
101:         if (is_string($value)) {
102:             if (strlen($value) > 20) {
103:                 $this->remaining[] = $value;
104:                 return '?';
105: 
106:             } else {
107:                 return $this->connection->quote($value);
108:             }
109: 
110:         } elseif (is_int($value)) {
111:             return (string) $value;
112: 
113:         } elseif (is_float($value)) {
114:             return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
115: 
116:         } elseif (is_bool($value)) {
117:             $this->remaining[] = $value;
118:             return '?';
119: 
120:         } elseif ($value === NULL) {
121:             return 'NULL';
122: 
123:         } elseif (is_array($value) || $value instanceof Traversable) {
124:             $vx = $kx = array();
125: 
126:             if (isset($value[0])) { // non-associative; value, value, value
127:                 foreach ($value as $v) {
128:                     $vx[] = $this->formatValue($v);
129:                 }
130:                 return implode(', ', $vx);
131: 
132:             } elseif ($this->arrayMode === 'values') { // (key, key, ...) VALUES (value, value, ...)
133:                 $this->arrayMode = 'multi';
134:                 foreach ($value as $k => $v) {
135:                     $kx[] = $this->driver->delimite($k);
136:                     $vx[] = $this->formatValue($v);
137:                 }
138:                 return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
139: 
140:             } elseif ($this->arrayMode === 'assoc') { // key=value, key=value, ...
141:                 foreach ($value as $k => $v) {
142:                     $vx[] = $this->driver->delimite($k) . '=' . $this->formatValue($v);
143:                 }
144:                 return implode(', ', $vx);
145: 
146:             } elseif ($this->arrayMode === 'multi') { // multiple insert (value, value, ...), ...
147:                 foreach ($value as $k => $v) {
148:                     $vx[] = $this->formatValue($v);
149:                 }
150:                 return ', (' . implode(', ', $vx) . ')';
151:             }
152: 
153:         } elseif ($value instanceof DateTime) {
154:             return $this->driver->formatDateTime($value);
155: 
156:         } elseif ($value instanceof NSqlLiteral) {
157:             return $value->value;
158: 
159:         } else {
160:             $this->remaining[] = $value;
161:             return '?';
162:         }
163:     }
164: 
165: }
166: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0