Commit bd458d7618e941463d9203fc2e350fcf266eba90
1 parent
9bb26895
Import SQL commands from a file, splitting into separate SQL statements.
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3352 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
105 additions
and
0 deletions
lib/database/sqlfile.inc.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +class SQLFile { | ||
| 4 | + function sqlFromFile($path) { | ||
| 5 | + return SQLFile::splitSQL(file_get_contents($path)); | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + function splitSQL($sql) { | ||
| 9 | + $instring = false; | ||
| 10 | + $i = 0; | ||
| 11 | + $remaining = $sql; | ||
| 12 | + $query = ""; | ||
| 13 | + $aQueries = array(); | ||
| 14 | + | ||
| 15 | + $strlen = strlen($sql); | ||
| 16 | + | ||
| 17 | + for ($i = 0; $i < $strlen; $i++) { | ||
| 18 | + $c = $remaining[$i]; | ||
| 19 | + if ($c === ";") { | ||
| 20 | + $query .= substr($remaining, 0, $i + 1); | ||
| 21 | + $aQueries[] = $query; | ||
| 22 | + $query = ""; | ||
| 23 | + $remaining = trim(substr($remaining, $i + 1)); | ||
| 24 | + $i = 0; | ||
| 25 | + $strlen = strlen($remaining); | ||
| 26 | + continue; | ||
| 27 | + } | ||
| 28 | + if ($c === "`") { | ||
| 29 | + $next = strpos($remaining, "`", $i); | ||
| 30 | + if ($next === false) { | ||
| 31 | + $query .= $remaining; | ||
| 32 | + $aQueries[] = $query; | ||
| 33 | + return $aQueries; | ||
| 34 | + } | ||
| 35 | + $query .= substr($remaining, 0, $next); | ||
| 36 | + $remaining = substr($remaining, $next); | ||
| 37 | + $i = 0; | ||
| 38 | + $strlen = strlen($remaining); | ||
| 39 | + continue; | ||
| 40 | + } | ||
| 41 | + if (($c === "'") || ($c === '"')) { | ||
| 42 | + $stringchar = $c; | ||
| 43 | + $notfound = true; | ||
| 44 | + | ||
| 45 | + while ($notfound) { | ||
| 46 | + $next = strpos($remaining, $stringchar, $i + 1); | ||
| 47 | + if ($next === false) { | ||
| 48 | + $query .= $remaining; | ||
| 49 | + $aQueries[] = $query; | ||
| 50 | + return $aQueries; | ||
| 51 | + } | ||
| 52 | + $i = $next + 1; | ||
| 53 | + $quotes = true; | ||
| 54 | + $b = 1; | ||
| 55 | + while ($remaining[$next - $b] === "\\") { | ||
| 56 | + $quotes = !$quotes; | ||
| 57 | + $b++; | ||
| 58 | + } | ||
| 59 | + if ($quotes) { | ||
| 60 | + $notfound = false; | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + $query .= substr($remaining, 0, $next); | ||
| 64 | + $remaining = substr($remaining, $next); | ||
| 65 | + $i = 0; | ||
| 66 | + $strlen = strlen($remaining); | ||
| 67 | + continue; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + $nextdelim = SQLFile::_nextDelim($remaining); | ||
| 71 | + if ($nextdelim === false) { | ||
| 72 | + $query .= $remaining; | ||
| 73 | + $aQueries[] = $query; | ||
| 74 | + return $aQueries; | ||
| 75 | + } | ||
| 76 | + // $query .= substr($remaining, 0, $nextdelim); | ||
| 77 | + } | ||
| 78 | + return $aQueries; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + function _nextDelim($string) { | ||
| 82 | + $q = strpos($string, "'"); | ||
| 83 | + $d = strpos($string, '"'); | ||
| 84 | + $b = strpos($string, "`"); | ||
| 85 | + $s = strpos($string, ";"); | ||
| 86 | + | ||
| 87 | + $min = false; | ||
| 88 | + foreach (array($q, $d, $b, $s) as $c) { | ||
| 89 | + if ($min === false) { | ||
| 90 | + $min = $c; | ||
| 91 | + continue; | ||
| 92 | + } | ||
| 93 | + if ($c === false) { | ||
| 94 | + continue; | ||
| 95 | + } | ||
| 96 | + if ($c < $min) { | ||
| 97 | + $min = $c; | ||
| 98 | + continue; | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + return $min; | ||
| 102 | + } | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +?> |