Namespaces

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

Classes

  • Connection
  • Helpers
  • Row
  • SqlLiteral
  • SqlPreprocessor
  • Statement

Interfaces

  • IReflection
  • 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 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:             \?               ## placeholder
 70:         ~xs*/
 71:         $sql = Nette\Utils\Strings::replace($sql, '~\'.*?\'|".*?"|\?~s', array($this, 'callback'));
 72: 
 73:         while ($this->counter < count($params)) {
 74:             $sql .= ' ' . $this->formatValue($params[$this->counter++]);
 75:         }
 76: 
 77:         return array($sql, $this->remaining);
 78:     }
 79: 
 80: 
 81: 
 82:     /** @internal */
 83:     public function callback($m)
 84:     {
 85:         $m = $m[0];
 86:         if ($m[0] === "'" || $m[0] === '"') { // string
 87:             return $m;
 88: 
 89:         } else { // placeholder
 90:             return $this->formatValue($this->params[$this->counter++]);
 91:         }
 92:     }
 93: 
 94: 
 95: 
 96:     private function formatValue($value)
 97:     {
 98:         if (is_string($value)) {
 99:             if (strlen($value) > 20) {
100:                 $this->remaining[] = $value;
101:                 return '?';
102: 
103:             } else {
104:                 return $this->connection->quote($value);
105:             }
106: 
107:         } elseif (is_int($value)) {
108:             return (string) $value;
109: 
110:         } elseif (is_float($value)) {
111:             return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
112: 
113:         } elseif (is_bool($value)) {
114:             $this->remaining[] = $value;
115:             return '?';
116: 
117:         } elseif ($value === NULL) {
118:             return 'NULL';
119: 
120:         } elseif ($value instanceof Table\ActiveRow) {
121:             return $value->getPrimary();
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 SqlLiteral) {
157:             return $value->__toString();
158: 
159:         } else {
160:             $this->remaining[] = $value;
161:             return '?';
162:         }
163:     }
164: 
165: }
166: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0