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:     Nette\ObjectMixin,
 16:     PDO;
 17: 
 18: 
 19: 
 20: /**
 21:  * Represents a connection between PHP and a database server.
 22:  *
 23:  * @author     David Grudl
 24:  */
 25: class Connection extends PDO
 26: {
 27:     /** @var string */
 28:     private $dsn;
 29: 
 30:     /** @var ISupplementalDriver */
 31:     private $driver;
 32: 
 33:     /** @var SqlPreprocessor */
 34:     private $preprocessor;
 35: 
 36:     /** @var Nette\Database\Reflection\DatabaseReflection */
 37:     public $databaseReflection;
 38: 
 39:     /** @var Nette\Caching\Cache */
 40:     private $cache;
 41: 
 42:     /** @var array */
 43:     public $substitutions = array();
 44: 
 45:     /** @var array of function(Statement $result, $params); Occurs after query is executed */
 46:     public $onQuery;
 47: 
 48: 
 49: 
 50:     public function __construct($dsn, $username = NULL, $password  = NULL, array $options = NULL, Reflection\DatabaseReflection $databaseReflection = NULL)
 51:     {
 52:         parent::__construct($this->dsn = $dsn, $username, $password, $options);
 53:         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 54:         $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Nette\Database\Statement', array($this)));
 55: 
 56:         $class = 'Nette\Database\Drivers\\' . $this->getAttribute(PDO::ATTR_DRIVER_NAME) . 'Driver';
 57:         if (class_exists($class)) {
 58:             $this->driver = new $class($this, (array) $options);
 59:         }
 60: 
 61:         $this->preprocessor = new SqlPreprocessor($this);
 62:         $this->databaseReflection = $databaseReflection ?: new Reflection\DatabaseReflection;
 63: 
 64:         Diagnostics\ConnectionPanel::initialize($this);
 65:     }
 66: 
 67: 
 68: 
 69:     /** @return ISupplementalDriver */
 70:     public function getSupplementalDriver()
 71:     {
 72:         return $this->driver;
 73:     }
 74: 
 75: 
 76: 
 77:     public function setCacheStorage(Nette\Caching\IStorage $storage = NULL)
 78:     {
 79:         $this->cache = $storage ? new Nette\Caching\Cache($storage, "Nette.Database/$this->dsn") : NULL;
 80:     }
 81: 
 82: 
 83: 
 84:     public function getCache()
 85:     {
 86:         return $this->cache;
 87:     }
 88: 
 89: 
 90: 
 91:     /**
 92:      * Generates and executes SQL query.
 93:      * @param  string  statement
 94:      * @param  mixed   [parameters, ...]
 95:      * @return Statement
 96:      */
 97:     public function query($statement)
 98:     {
 99:         $args = func_get_args();
100:         return $this->queryArgs(array_shift($args), $args);
101:     }
102: 
103: 
104: 
105:     /**
106:      * Generates and executes SQL query.
107:      * @param  string  statement
108:      * @param  mixed   [parameters, ...]
109:      * @return int     number of affected rows
110:      */
111:     public function exec($statement)
112:     {
113:         $args = func_get_args();
114:         return $this->queryArgs(array_shift($args), $args)->rowCount();
115:     }
116: 
117: 
118: 
119:     /**
120:      * @param  string  statement
121:      * @param  array
122:      * @return Statement
123:      */
124:     public function queryArgs($statement, $params)
125:     {
126:         foreach ($params as $value) {
127:             if (is_array($value) || is_object($value)) {
128:                 $need = TRUE; break;
129:             }
130:         }
131:         if (isset($need) || strpos($statement, ':') !== FALSE && $this->preprocessor !== NULL) {
132:             list($statement, $params) = $this->preprocessor->process($statement, $params);
133:         }
134: 
135:         return $this->prepare($statement)->execute($params);
136:     }
137: 
138: 
139: 
140:     /********************* shortcuts ****************d*g**/
141: 
142: 
143: 
144:     /**
145:      * Shortcut for query()->fetch()
146:      * @param  string  statement
147:      * @param  mixed   [parameters, ...]
148:      * @return Row
149:      */
150:     public function fetch($args)
151:     {
152:         $args = func_get_args();
153:         return $this->queryArgs(array_shift($args), $args)->fetch();
154:     }
155: 
156: 
157: 
158:     /**
159:      * Shortcut for query()->fetchColumn()
160:      * @param  string  statement
161:      * @param  mixed   [parameters, ...]
162:      * @return mixed
163:      */
164:     public function fetchColumn($args)
165:     {
166:         $args = func_get_args();
167:         return $this->queryArgs(array_shift($args), $args)->fetchColumn();
168:     }
169: 
170: 
171: 
172:     /**
173:      * Shortcut for query()->fetchPairs()
174:      * @param  string  statement
175:      * @param  mixed   [parameters, ...]
176:      * @return array
177:      */
178:     public function fetchPairs($args)
179:     {
180:         $args = func_get_args();
181:         return $this->queryArgs(array_shift($args), $args)->fetchPairs();
182:     }
183: 
184: 
185: 
186:     /**
187:      * Shortcut for query()->fetchAll()
188:      * @param  string  statement
189:      * @param  mixed   [parameters, ...]
190:      * @return array
191:      */
192:     public function fetchAll($args)
193:     {
194:         $args = func_get_args();
195:         return $this->queryArgs(array_shift($args), $args)->fetchAll();
196:     }
197: 
198: 
199: 
200:     /********************* selector ****************d*g**/
201: 
202: 
203: 
204:     /**
205:      * Creates selector for table.
206:      * @param  string
207:      * @return Nette\Database\Table\Selection
208:      */
209:     public function table($table)
210:     {
211:         return new Table\Selection($table, $this);
212:     }
213: 
214: 
215: 
216:     /********************* misc ****************d*g**/
217: 
218: 
219: 
220:     /**
221:      * Import SQL dump from file - extreme fast.
222:      * @param  string  filename
223:      * @return int  count of commands
224:      */
225:     public function loadFile($file)
226:     {
227:         @set_time_limit(0); // intentionally @
228: 
229:         $handle = @fopen($file, 'r'); // intentionally @
230:         if (!$handle) {
231:             throw new Nette\FileNotFoundException("Cannot open file '$file'.");
232:         }
233: 
234:         $count = 0;
235:         $sql = '';
236:         while (!feof($handle)) {
237:             $s = fgets($handle);
238:             $sql .= $s;
239:             if (substr(rtrim($s), -1) === ';') {
240:                 parent::exec($sql); // native query without logging
241:                 $sql = '';
242:                 $count++;
243:             }
244:         }
245:         fclose($handle);
246:         return $count;
247:     }
248: 
249: 
250: 
251:     /**
252:      * Returns syntax highlighted SQL command.
253:      * @param  string
254:      * @return string
255:      */
256:     public static function highlightSql($sql)
257:     {
258:         static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
259:         static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
260: 
261:         // insert new lines
262:         $sql = " $sql ";
263:         $sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
264: 
265:         // reduce spaces
266:         $sql = preg_replace('#[ \t]{2,}#', " ", $sql);
267: 
268:         $sql = wordwrap($sql, 100);
269:         $sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
270: 
271:         // syntax highlight
272:         $sql = htmlSpecialChars($sql);
273:         $sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", function($matches) {
274:             if (!empty($matches[1])) // comment
275:                 return '<em style="color:gray">' . $matches[1] . '</em>';
276: 
277:             if (!empty($matches[2])) // error
278:                 return '<strong style="color:red">' . $matches[2] . '</strong>';
279: 
280:             if (!empty($matches[3])) // most important keywords
281:                 return '<strong style="color:blue">' . $matches[3] . '</strong>';
282: 
283:             if (!empty($matches[4])) // other keywords
284:                 return '<strong style="color:green">' . $matches[4] . '</strong>';
285:         }, $sql);
286: 
287:         return '<pre class="dump">' . trim($sql) . "</pre>\n";
288:     }
289: 
290: 
291: 
292:     /********************* Nette\Object behaviour ****************d*g**/
293: 
294: 
295: 
296:     /**
297:      * @return Nette\Reflection\ClassType
298:      */
299:     public static function getReflection()
300:     {
301:         return new Nette\Reflection\ClassType(get_called_class());
302:     }
303: 
304: 
305: 
306:     public function __call($name, $args)
307:     {
308:         return ObjectMixin::call($this, $name, $args);
309:     }
310: 
311: 
312: 
313:     public function &__get($name)
314:     {
315:         return ObjectMixin::get($this, $name);
316:     }
317: 
318: 
319: 
320:     public function __set($name, $value)
321:     {
322:         return ObjectMixin::set($this, $name, $value);
323:     }
324: 
325: 
326: 
327:     public function __isset($name)
328:     {
329:         return ObjectMixin::has($this, $name);
330:     }
331: 
332: 
333: 
334:     public function __unset($name)
335:     {
336:         ObjectMixin::remove($this, $name);
337:     }
338: 
339: }
340: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0