Packages

  • 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
  • DatabaseHelpers
  • Row
  • SqlLiteral
  • SqlPreprocessor
  • Statement

Interfaces

  • IReflection
  • 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 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 SqlPreprocessor extends Object
 22: {
 23:     /** @var Connection */
 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(Connection $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:             \?               ## placeholder
 68:         ~xs*/
 69:         $sql = Strings::replace($sql, '~\'.*?\'|".*?"|\?~s', array($this, 'callback'));
 70: 
 71:         while ($this->counter < count($params)) {
 72:             $sql .= ' ' . $this->formatValue($params[$this->counter++]);
 73:         }
 74: 
 75:         return array($sql, $this->remaining);
 76:     }
 77: 
 78: 
 79: 
 80:     /** @internal */
 81:     public function callback($m)
 82:     {
 83:         $m = $m[0];
 84:         if ($m[0] === "'" || $m[0] === '"') { // string
 85:             return $m;
 86: 
 87:         } else { // placeholder
 88:             return $this->formatValue($this->params[$this->counter++]);
 89:         }
 90:     }
 91: 
 92: 
 93: 
 94:     private function formatValue($value)
 95:     {
 96:         if (is_string($value)) {
 97:             if (strlen($value) > 20) {
 98:                 $this->remaining[] = $value;
 99:                 return '?';
100: 
101:             } else {
102:                 return $this->connection->quote($value);
103:             }
104: 
105:         } elseif (is_int($value)) {
106:             return (string) $value;
107: 
108:         } elseif (is_float($value)) {
109:             return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
110: 
111:         } elseif (is_bool($value)) {
112:             $this->remaining[] = $value;
113:             return '?';
114: 
115:         } elseif ($value === NULL) {
116:             return 'NULL';
117: 
118:         } elseif ($value instanceof TableRow) {
119:             return $value->getPrimary();
120: 
121:         } elseif (is_array($value) || $value instanceof Traversable) {
122:             $vx = $kx = array();
123: 
124:             if (isset($value[0])) { // non-associative; value, value, value
125:                 foreach ($value as $v) {
126:                     $vx[] = $this->formatValue($v);
127:                 }
128:                 return implode(', ', $vx);
129: 
130:             } elseif ($this->arrayMode === 'values') { // (key, key, ...) VALUES (value, value, ...)
131:                 $this->arrayMode = 'multi';
132:                 foreach ($value as $k => $v) {
133:                     $kx[] = $this->driver->delimite($k);
134:                     $vx[] = $this->formatValue($v);
135:                 }
136:                 return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
137: 
138:             } elseif ($this->arrayMode === 'assoc') { // key=value, key=value, ...
139:                 foreach ($value as $k => $v) {
140:                     $vx[] = $this->driver->delimite($k) . '=' . $this->formatValue($v);
141:                 }
142:                 return implode(', ', $vx);
143: 
144:             } elseif ($this->arrayMode === 'multi') { // multiple insert (value, value, ...), ...
145:                 foreach ($value as $k => $v) {
146:                     $vx[] = $this->formatValue($v);
147:                 }
148:                 return '(' . implode(', ', $vx) . ')';
149:             }
150: 
151:         } elseif ($value instanceof DateTime) {
152:             return $this->driver->formatDateTime($value);
153: 
154:         } elseif ($value instanceof SqlLiteral) {
155:             return $value->__toString();
156: 
157:         } else {
158:             $this->remaining[] = $value;
159:             return '?';
160:         }
161:     }
162: 
163: }
164: 
Nette Framework 2.0.3 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0