| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Scaffolder { |
|---|
| 4 | protected $dir; |
|---|
| 5 | |
|---|
| 6 | public function __construct($dir) { |
|---|
| 7 | $this->dir = $dir; |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | public function run() { |
|---|
| 11 | $this->parseSQL($this->dir.'install.sql'); |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Deletes the comments from an sql file. |
|---|
| 16 | * |
|---|
| 17 | * @param string $queries |
|---|
| 18 | * @return string $queries |
|---|
| 19 | */ |
|---|
| 20 | public function deleteComments($queries) { |
|---|
| 21 | // $queries = preg_replace("~('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(?:(?:--|#).*|/\*(?:.|[\r\n])*?\*/)~", '$1', $queries); |
|---|
| 22 | $queries = preg_replace("~('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(?:(?:--|#)[^\n]*|/\*.*?\*/)~s", '$1', $queries); |
|---|
| 23 | |
|---|
| 24 | // strip whitespace |
|---|
| 25 | return trim($queries); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * This function reads the logable data out of the statement and |
|---|
| 30 | * calls the log function if so. |
|---|
| 31 | * |
|---|
| 32 | * @param string $filename |
|---|
| 33 | */ |
|---|
| 34 | protected function parseSQL($filename) { |
|---|
| 35 | $queries = file_get_contents($filename); |
|---|
| 36 | |
|---|
| 37 | $alterStatement = $columnName = $columns = array(); |
|---|
| 38 | |
|---|
| 39 | $queries = $this->deleteComments($queries); |
|---|
| 40 | // Put all queries in the $queries array. |
|---|
| 41 | $queryArray = array(); |
|---|
| 42 | $char = ''; |
|---|
| 43 | $lastChar = ''; |
|---|
| 44 | $inString = false; |
|---|
| 45 | $stringOpen = ''; |
|---|
| 46 | for ($i = 0; $i < strlen($queries); $i++) { |
|---|
| 47 | $char = $queries[$i]; |
|---|
| 48 | |
|---|
| 49 | // if delimiter found, add the parsed part to the returned array |
|---|
| 50 | if ($char == ';' && !$inString) { |
|---|
| 51 | $queryArray[] = substr($queries, 0, $i); |
|---|
| 52 | $queries = substr($queries, $i + 1); |
|---|
| 53 | $i = 0; |
|---|
| 54 | $lastChar = ''; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | else if (!$inString) { |
|---|
| 58 | if ($char == "'" || $char == '"') { |
|---|
| 59 | $inString = true; |
|---|
| 60 | $stringOpen = $char; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | else if ($inString) { |
|---|
| 64 | if ($lastChar != '\\' && $char == $stringOpen) { |
|---|
| 65 | $inString = false; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | $lastChar = $char; |
|---|
| 69 | } |
|---|
| 70 | if ($queries != '') { |
|---|
| 71 | $queryArray[] = $queries; |
|---|
| 72 | } |
|---|
| 73 | unset($queries); |
|---|
| 74 | |
|---|
| 75 | // array for setup log data. |
|---|
| 76 | $tableLogData = array(); |
|---|
| 77 | |
|---|
| 78 | // loop through the queries and handle them one by one |
|---|
| 79 | $c = count($queryArray); |
|---|
| 80 | for ($i = 0; $i < $c; $i++) { |
|---|
| 81 | |
|---|
| 82 | // ignore errors syntax? |
|---|
| 83 | if (substr($queryArray[$i], 0, 1) == '@') { |
|---|
| 84 | $queryArray[$i] = substr($queryArray[$i], 1); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // query for CREATE TABLE |
|---|
| 88 | $this->parseSingleSQL($queryArray[$i]); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | protected function parseSingleSQL($sql) { |
|---|
| 93 | // query for CREATE TABLE |
|---|
| 94 | if(preg_match("%CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?\s+`?(\w+)`?(.*)%i", $sql, $matches)) { |
|---|
| 95 | $this->parseCreateTable($matches[1], $sql); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | protected function parseCreateTable($table, $sql) { |
|---|
| 100 | var_dump($table, $sql); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | $scaff = new Scaffolder('/var/www/admanager/'); |
|---|
| 105 | $scaff->run(); |
|---|