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
  • Deprecated
  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:  * Database helpers.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Database
 20:  */
 21: class NDatabaseHelpers
 22: {
 23:     /** @var array */
 24:     public static $typePatterns = array(
 25:         '^_' => IReflection::FIELD_TEXT, // PostgreSQL arrays
 26:         'BYTEA|BLOB|BIN' => IReflection::FIELD_BINARY,
 27:         'TEXT|CHAR|POINT|INTERVAL' => IReflection::FIELD_TEXT,
 28:         'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => IReflection::FIELD_INTEGER,
 29:         'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => IReflection::FIELD_FLOAT,
 30:         '^TIME$' => IReflection::FIELD_TIME,
 31:         'TIME' => IReflection::FIELD_DATETIME, // DATETIME, TIMESTAMP
 32:         'DATE' => IReflection::FIELD_DATE,
 33:         'BOOL' => IReflection::FIELD_BOOL,
 34:     );
 35: 
 36: 
 37: 
 38:     /**
 39:      * Displays complete result set as HTML table for debug purposes.
 40:      * @return void
 41:      */
 42:     public static function dumpResult(NStatement $statement)
 43:     {
 44:         echo "\n<table class=\"dump\">\n<caption>" . htmlSpecialChars($statement->queryString) . "</caption>\n";
 45:         if (!$statement->columnCount()) {
 46:             echo "\t<tr>\n\t\t<th>Affected rows:</th>\n\t\t<td>", $statement->rowCount(), "</td>\n\t</tr>\n</table>\n";
 47:             return;
 48:         }
 49:         $i = 0;
 50:         foreach ($statement as $row) {
 51:             if ($i === 0) {
 52:                 echo "<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
 53:                 foreach ($row as $col => $foo) {
 54:                     echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
 55:                 }
 56:                 echo "\t</tr>\n</thead>\n<tbody>\n";
 57:             }
 58:             echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
 59:             foreach ($row as $col) {
 60:                 //if (is_object($col)) $col = $col->__toString();
 61:                 echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
 62:             }
 63:             echo "\t</tr>\n";
 64:             $i++;
 65:         }
 66: 
 67:         if ($i === 0) {
 68:             echo "\t<tr>\n\t\t<td><em>empty result set</em></td>\n\t</tr>\n</table>\n";
 69:         } else {
 70:             echo "</tbody>\n</table>\n";
 71:         }
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns syntax highlighted SQL command.
 78:      * @param  string
 79:      * @return string
 80:      */
 81:     public static function dumpSql($sql)
 82:     {
 83:         static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
 84:         static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
 85: 
 86:         // insert new lines
 87:         $sql = " $sql ";
 88:         $sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
 89: 
 90:         // reduce spaces
 91:         $sql = preg_replace('#[ \t]{2,}#', " ", $sql);
 92: 
 93:         $sql = wordwrap($sql, 100);
 94:         $sql = preg_replace('#([ \t]*\r?\n){2,}#', "\n", $sql);
 95: 
 96:         // syntax highlight
 97:         $sql = htmlSpecialChars($sql);
 98:         $sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", create_function('$matches', '
 99:             if (!empty($matches[1])) { // comment
100:                 return \'<em style="color:gray">\' . $matches[1] . \'</em>\';
101: 
102:             } elseif (!empty($matches[2])) { // error
103:                 return \'<strong style="color:red">\' . $matches[2] . \'</strong>\';
104: 
105:             } elseif (!empty($matches[3])) { // most important keywords
106:                 return \'<strong style="color:blue">\' . $matches[3] . \'</strong>\';
107: 
108:             } elseif (!empty($matches[4])) { // other keywords
109:                 return \'<strong style="color:green">\' . $matches[4] . \'</strong>\';
110:             }
111:         '), $sql);
112: 
113:         return '<pre class="dump">' . trim($sql) . "</pre>\n";
114:     }
115: 
116: 
117: 
118:     /**
119:      * Heuristic type detection.
120:      * @param  string
121:      * @return string
122:      * @internal
123:      */
124:     public static function detectType($type)
125:     {
126:         static $cache;
127:         if (!isset($cache[$type])) {
128:             $cache[$type] = 'string';
129:             foreach (self::$typePatterns as $s => $val) {
130:                 if (preg_match("#$s#i", $type)) {
131:                     return $cache[$type] = $val;
132:                 }
133:             }
134:         }
135:         return $cache[$type];
136:     }
137: 
138: 
139: 
140:     /**
141:      * Import SQL dump from file - extreme fast.
142:      * @return int  count of commands
143:      */
144:     public static function loadFromFile(NConnection $connection, $file)
145:     {
146:         @set_time_limit(0); // intentionally @
147: 
148:         $handle = @fopen($file, 'r'); // intentionally @
149:         if (!$handle) {
150:             throw new FileNotFoundException("Cannot open file '$file'.");
151:         }
152: 
153:         $count = 0;
154:         $sql = '';
155:         while (!feof($handle)) {
156:             $s = fgets($handle);
157:             $sql .= $s;
158:             if (substr(rtrim($s), -1) === ';') {
159:                 $connection->exec($sql); // native query without logging
160:                 $sql = '';
161:                 $count++;
162:             }
163:         }
164:         if (trim($sql) !== '') {
165:             $connection->exec($sql);
166:             $count++;
167:         }
168:         fclose($handle);
169:         return $count;
170:     }
171: 
172: }
173: 
Nette Framework 2.0.10 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0