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

  • NConnection
  • NDatabaseHelpers
  • NRow
  • NSqlLiteral
  • NSqlPreprocessor
  • NStatement

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