Exemplo n.º 1
0
 function create($dbname)
 {
     $dbhost = $this->conf->dbhost;
     $dbuser = $this->conf->dbuser;
     $dbpass = $this->conf->dbpass;
     $file = ROOT . 'install/app.sql';
     //$this->import($dbhost,$dbuser,$dbpass,$dbname,$sql);
     try {
         $conn = new PDO($this->conf->driver . ':host=' . $dbhost, $dbuser, $dbpass);
         //$stmt = $conn->query("SHOW DATABASES");
         //var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
         $sql = 'CREATE Database ' . $dbname . ' CHARACTER SET utf8 COLLATE utf8_general_ci';
         $stmt = $conn->query($sql);
     } catch (PDOException $e) {
         die('Could not connect: ' . $e->getMessage());
     }
     //disconnect PDO connection
     $conn = null;
     //now connecting with new DSN
     $dsn = $this->conf->driver . ':host=' . $dbhost . ';dbname=' . $dbname;
     Coder::code($dsn);
     try {
         $conn = new PDO($dsn, $dbuser, $dbpass);
     } catch (PDOException $e) {
         die('Problem with connection' . $e->getMessage());
     }
     echo 'Processing SQL script';
     $query = file_get_contents($file);
     $sqlList = SQLParser::parse($query);
     Coder::code_var($sqlList);
     try {
         foreach ($sqlList as $sqlline) {
             // Perform the query
             $conn->query($sqlline);
         }
     } catch (PDOException $e) {
         echo $e->getMessage();
         die;
     }
     echo 'Final';
     return true;
 }
Exemplo n.º 2
0
 public static function parse($content)
 {
     $sqlList = array();
     // Processing the SQL file content
     $lines = explode("\n", $content);
     $query = "";
     // Parsing the SQL file content
     foreach ($lines as $sql_line) {
         $sql_line = trim($sql_line);
         if ($sql_line === "") {
             continue;
         } else {
             if (strpos($sql_line, "--") === 0) {
                 continue;
             } else {
                 if (strpos($sql_line, "#") === 0) {
                     continue;
                 }
             }
         }
         $query .= $sql_line;
         // Checking whether the line is a valid statement
         if (preg_match("/(.*);/", $sql_line)) {
             $query = trim($query);
             $query = substr($query, 0, strlen($query) - 1);
             $query = SQLParser::outComments($query);
             //store this query
             $sqlList[] = $query;
             //reset the variable
             $query = "";
         }
     }
     return $sqlList;
 }