1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class DatabaseHelpers
22: {
23:
24: public static $typePatterns = array(
25: '^_' => IReflection::FIELD_TEXT,
26: 'BYTEA|BLOB|BIN' => IReflection::FIELD_BINARY,
27: 'TEXT|CHAR' => IReflection::FIELD_TEXT,
28: 'YEAR|BYTE|COUNTER|SERIAL|INT|LONG' => 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,
32: 'DATE' => IReflection::FIELD_DATE,
33: 'BOOL|BIT' => IReflection::FIELD_BOOL,
34: );
35:
36:
37:
38: 39: 40: 41:
42: public static function dumpResult(Statement $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:
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: 78: 79: 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:
87: $sql = " $sql ";
88: $sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
89:
90:
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:
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: if (!empty($matches[2])) // error
103: return \'<strong style="color:red">\' . $matches[2] . \'</strong>\';
104:
105: if (!empty($matches[3])) // most important keywords
106: return \'<strong style="color:blue">\' . $matches[3] . \'</strong>\';
107:
108: if (!empty($matches[4])) // other keywords
109: return \'<strong style="color:green">\' . $matches[4] . \'</strong>\';
110: '), $sql);
111:
112: return '<pre class="dump">' . trim($sql) . "</pre>\n";
113: }
114:
115:
116:
117: 118: 119: 120: 121: 122:
123: public static function detectType($type)
124: {
125: static $cache;
126: if (!isset($cache[$type])) {
127: $cache[$type] = 'string';
128: foreach (self::$typePatterns as $s => $val) {
129: if (preg_match("#$s#i", $type)) {
130: return $cache[$type] = $val;
131: }
132: }
133: }
134: return $cache[$type];
135: }
136:
137:
138:
139: 140: 141: 142: 143:
144: public static function loadFromFile(Connection $connection, $file)
145: {
146: @set_time_limit(0);
147:
148: $handle = @fopen($file, 'r');
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);
160: $sql = '';
161: $count++;
162: }
163: }
164: fclose($handle);
165: return $count;
166: }
167:
168: }
169: