function splitSQL($sql) { $instring = false; $i = 0; $remaining = $sql; $query = ""; $aQueries = array(); $strlen = strlen($sql); for ($i = 0; $i < $strlen; $i++) { $c = $remaining[$i]; if ($c === ";") { $query .= substr($remaining, 0, $i + 1); $aQueries[] = $query; $query = ""; $remaining = trim(substr($remaining, $i + 1)); $i = 0; $strlen = strlen($remaining); continue; } if ($c === "`") { $next = strpos($remaining, "`", $i); if ($next === false) { $query .= $remaining; $aQueries[] = $query; return $aQueries; } $query .= substr($remaining, 0, $next); $remaining = substr($remaining, $next); $i = 0; $strlen = strlen($remaining); continue; } if ($c === "'" || $c === '"') { $stringchar = $c; $notfound = true; while ($notfound) { $next = strpos($remaining, $stringchar, $i + 1); if ($next === false) { $query .= $remaining; $aQueries[] = $query; return $aQueries; } $i = $next + 1; $quotes = true; $b = 1; while ($remaining[$next - $b] === "\\") { $quotes = !$quotes; $b++; } if ($quotes) { $notfound = false; } } $query .= substr($remaining, 0, $next); $remaining = substr($remaining, $next); $i = 0; $strlen = strlen($remaining); continue; } $nextdelim = SQLFile::_nextDelim($remaining); if ($nextdelim === false) { $query .= $remaining; $aQueries[] = $query; return $aQueries; } // $query .= substr($remaining, 0, $nextdelim); } return $aQueries; }
function upgradePlugin($iStart, $iEnd) { if (is_null($this->sSQLDir)) { return $iEnd; // no db changes, must reach the "end". } global $default; DBUtil::setupAdminDatabase(); for ($i = $iStart; $i <= $iEnd; $i++) { $sqlfile = sprintf("%s/upgradeto%d.sql", $this->sSQLDir, $i); if (!file_exists($sqlfile)) { continue; // skip it. } $queries = SQLFile::sqlFromFile($sqlfile); $res = DBUtil::runQueries($queries, $default->_admindb); if (PEAR::isError($res)) { return $i; // break out completely, indicating how far we got pre-error. } } return $iEnd; }
function _performUpgrade() { $dbType = 'mysql'; $sqlupgradedir = KT_DIR . 'sql/' . $dbType . '/upgrade/'; $queries = SQLFile::sqlFromFile($sqlupgradedir . $this->name); return $this->dbUtilities->runQueries($queries); }
/** * applies queries to the database * @return * @param $filename Object */ function applySQL($filename) { global $default; DBUtil::setupAdminDatabase(); $db = $default->_admindb; $content = file_get_contents($filename); $aQueries = SQLFile::splitSQL($content); DBUtil::startTransaction(); foreach ($aQueries as $sQuery) { $res = DBUtil::runQuery($sQuery, $db); if (PEAR::isError($res)) { continue; } } DBUtil::commit(); }
function SQLSchema($title, $params) { parent::SQLFile($title, $params); $this->interactive = true; $this->grouping = false; }
function _performUpgrade() { global $default; $sqlupgradedir = KT_DIR . '/sql/' . $default->dbType . '/upgrade/'; $queries = SQLFile::sqlFromFile($sqlupgradedir . $this->name); return DBUtil::runQueries($queries, $default->_admindb); }
function SQLOptions($title, $params) { parent::BaseAction($title, $params); $this->interactive = true; $this->grouping = true; }
function testSQLFile() { $aExpected = array("SELECT \"as;\";", "SELECT \"as\\\";\";", "SELECT \"as\\\\\";", "SELECT \"as\\\\\";", "SELECT \"as\\\\\";", "SELECT \"as\\\\\";", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\';';", "SELECT \"'as\\'\"';';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT 'as\\\\';", "SELECT '\nasdf; \"\\\" \\'\n\nasdf; ';"); $aReceived = SQLFile::sqlFromFile(dirname(__FILE__) . "/test_sqlfile.sql"); $this->assertExpectedResults($aExpected, $aReceived); }