Namespaces

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

Classes

  • Connection
  • Row
  • SqlLiteral
  • SqlPreprocessor
  • Statement

Interfaces

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