Beispiel #1
0
function queryFromFile($sql_file_path)
{
    global $db, $progress, $errors;
    $tables = array();
    if (!file_exists($sql_file_path)) {
        return false;
    }
    $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
    SqlUtility::splitSqlFile($pieces, $sql_query);
    foreach ($pieces as $piece) {
        $piece = trim($piece);
        // [0] contains the prefixed query
        // [4] contains unprefixed table name
        if ($_POST['tb_prefix'] || $_POST['tb_prefix'] == '') {
            $prefixed_query = SqlUtility::prefixQuery($piece, $_POST['tb_prefix']);
        } else {
            $prefixed_query = $piece;
        }
        if ($prefixed_query != false) {
            $table = $_POST['tb_prefix'] . $prefixed_query[4];
            if ($prefixed_query[1] == 'CREATE TABLE') {
                $result = queryDB($prefixed_query[0], array());
                if ($result > 0) {
                    $progress[] = 'Table <strong>' . $table . '</strong> created successfully.';
                } else {
                    if (at_db_errno($db) == 1050) {
                        $progress[] = 'Table <strong>' . $table . '</strong> already exists. Skipping.';
                    } else {
                        $errors[] = 'Table <strong>' . $table . '</strong> creation failed.';
                    }
                }
            } elseif ($prefixed_query[1] == 'INSERT INTO') {
                queryDB($prefixed_query[0], array());
            } elseif ($prefixed_query[1] == 'REPLACE INTO') {
                queryDB($prefixed_query[0], array());
            } elseif ($prefixed_query[1] == 'ALTER TABLE') {
                $result = queryDB($prefixed_query[0], array());
                if ($result > 0) {
                    $progress[] = 'Table <strong>' . $table . '</strong> altered successfully.';
                } else {
                    if (at_db_errno($db) == 1060) {
                        $progress[] = 'Table <strong>' . $table . '</strong> fields already exists. Skipping.';
                    } elseif (at_db_errno($db) == 1091) {
                        $progress[] = 'Table <strong>' . $table . '</strong> fields already dropped. Skipping.';
                    } else {
                        $errors[] = 'Table <strong>' . $table . '</strong> alteration failed.';
                    }
                }
            } elseif ($prefixed_query[1] == 'DROP TABLE') {
                queryDB($prefixed_query[1] . ' ' . $table, array());
            } elseif ($prefixed_query[1] == 'UPDATE') {
                queryDB($prefixed_query[0], array());
            }
        }
    }
    return true;
}
Beispiel #2
0
 public static function queryFromFile($sql_file_path, $table_prefix = null, $in_plain_msg = true)
 {
     global $progress, $errors, $msg;
     $tables = array();
     if (!file_exists($sql_file_path)) {
         return false;
     }
     $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
     SqlUtility::splitSqlFile($pieces, $sql_query);
     foreach ($pieces as $piece) {
         $piece = trim($piece);
         // [0] contains the prefixed query
         // [4] contains unprefixed table name
         if ($table_prefix || $table_prefix == '') {
             $prefixed_query = SqlUtility::prefixQuery($piece, $table_prefix);
         } else {
             $prefixed_query = $piece;
         }
         if ($prefixed_query != false) {
             $table = $table_prefix . $prefixed_query[4];
             if ($prefixed_query[1] == 'CREATE TABLE' || $prefixed_query[1] == 'CREATE TABLE IF NOT EXISTS') {
                 $result = queryDB($prefixed_query[0], array(), FALSE, FALSE);
                 if (count($result) > 0) {
                     if ($in_plain_msg) {
                         $progress[] = 'Table <b>' . $table . '</b> created successfully.';
                     } else {
                         $msg->addFeedback(array('TABLE_CREATED', $table));
                     }
                 } else {
                     if (at_db_errno() == 1050) {
                         //// NOTE STATIC ERRNO --- if (mysql_errno($db) == 1050) {
                         if ($in_plain_msg) {
                             $progress[] = 'Table <b>' . $table . '</b> already exists. Skipping.';
                         } else {
                             $msg->addFeedback(array('TABLE_EXIST', $table));
                         }
                     } else {
                         if ($in_plain_msg) {
                             $errors[] = 'Table <b>' . $table . '</b> creation failed.';
                         } else {
                             $msg->addError(array('CREATE_TABLE_FAIL', $table));
                         }
                     }
                 }
             } elseif ($prefixed_query[1] == 'INSERT INTO') {
                 queryDB($prefixed_query[0], array(), FALSE, FALSE);
             } elseif ($prefixed_query[1] == 'REPLACE INTO') {
                 queryDB($prefixed_query[0], array(), FALSE, FALSE);
             } elseif ($prefixed_query[1] == 'ALTER TABLE') {
                 $result = queryDB($prefixed_query[0], array());
                 if ($result > 0) {
                     if ($in_plain_msg) {
                         $progress[] = 'Table <strong>' . $table . '</strong> altered successfully.';
                     } else {
                         $msg->addFeedback(array('TABLE_ALTERED', $table));
                     }
                 } else {
                     if (at_db_errno() == 1060) {
                         ////// NOTE STATIC ERRNO --- if (mysql_errno($db) == 1060) {
                         if ($in_plain_msg) {
                             $progress[] = 'Table <strong>' . $table . '</strong> fields already exists. Skipping.';
                         } else {
                             $msg->addFeedback(array('TABLE_FIELD_EXIST', $table));
                         }
                         ////// NOTE STATIC ERRNO --- } elseif (mysql_errno($db) == 1091) {
                     } elseif (at_db_errno() == 1091) {
                         if ($in_plain_msg) {
                             $progress[] = 'Table <strong>' . $table . '</strong> fields already dropped. Skipping.';
                         } else {
                             $msg->addFeedback(array('TABLE_FIELD_DROPPED', $table));
                         }
                     } else {
                         if ($in_plain_msg) {
                             $errors[] = 'Table <strong>' . $table . '</strong> alteration failed.';
                         } else {
                             $msg->addError(array('ALTER_TABLE_FAIL', $table));
                         }
                     }
                 }
             } elseif ($prefixed_query[1] == 'DROP TABLE') {
                 queryDB($prefixed_query[1] . ' ' . $table, array());
             } elseif ($prefixed_query[1] == 'UPDATE') {
                 queryDB($prefixed_query[0], array());
             }
         }
     }
     return true;
 }