function importDataFromDumpFile($sid, DatabaseHandler $dbHandler, $userId, $filePath, $my_pligg_base_no_slash)
{
    $ktrExeDao = new KTRExecutorDAO();
    $tableNames = $dbHandler->loadTables();
    $logIds = array();
    foreach ($tableNames as $tableName) {
        $logIds[$tableName] = $ktrExeDao->addExecutionInfoTuple($sid, $tableName, $userId);
    }
    try {
        $dbImporter = DatabaseImporterFactory::createDatabaseImporter($dbHandler->getDriver(), $sid, $my_pligg_base_no_slash);
        $dbImporter->importDbData($filePath);
        foreach ($logIds as $logId) {
            $ktrExeDao->updateExecutionInfoStatus($logId, 'success');
        }
    } catch (Exception $e) {
        foreach ($logIds as $logId) {
            $ktrExeDao->updateExecutionInfoStatus($logId, 'error');
            $ktrExeDao->updateExecutionInfoErrorMessage($logId, $e->getMessage());
        }
    }
    $queryEngine = new QueryEngine();
    foreach ($logIds as $tableName => $logId) {
        $numProcessed = $queryEngine->GetTotalNumberTuplesInTableBySidAndNameFromExternalDB($sid, $tableName);
        $ktrExeDao->updateExecutionInfoTupleAfterPanTerminated($logId, 0, '', $numProcessed, 'success');
    }
}
 public function testMSSQLImport()
 {
     $sid = 12345;
     $filePath = 'E:/colfusion_test_files/mssql_schema.sql';
     $mssqlImporter = DatabaseImporterFactory::createDatabaseImporter('mssql', $sid);
     // $mssqlImporter->importDbSchema($filePath);
     $mssqlImporter->importDbData($filePath);
     //$dbh = new PDO("sqlsrv:Server=localhost;Database=master", 'ExternalConnTester', 'gz3000gz3000');
     //$db = $mssqlImporter->getDatabase();
     //$dbh->exec("drop database $db");
 }
function upload_0($sid, $uploadTimestamp, $fileType, $excelFileMode, $dbType)
{
    $_SESSION["excelFileMode_{$sid}"] = $excelFileMode;
    // check file type
    $explodeRes = explode(".", $_FILES["upload_file"]["name"]);
    $extension = end($explodeRes);
    $mimes = array('xls', 'xlsx', 'csv', 'sql', 'zip');
    if (count($_FILES) <= 0) {
        $error = "ERROR: No file was uploaded.";
        $_SESSION["upload_file_{$sid}"]['error'] = $error;
    } else {
        if (!in_array($extension, $mimes)) {
            $error = "ERROR: please upload excel, csv, sql, or zip file.";
            $_SESSION["upload_file_{$sid}"]['error'] = $error;
        } else {
            //save upload file
            if ($_FILES['upload_file']['error'] > 0) {
                $error = "ERROR: " . $_FILES['upload_file']['error'] . "</br>";
                $_SESSION["upload_file_{$sid}"]['error'] = $_FILES['upload_file']['error'];
            } else {
                $upload_dir = mnmpath . "upload_raw_data/{$sid}/";
                if (!file_exists($upload_dir)) {
                    mkdir($upload_dir);
                }
                // Delete former files if user upload more than one time.
                if (isset($_SESSION["uploadTimestamp_{$sid}"]) && $_SESSION["uploadTimestamp_{$sid}"] != $uploadTimestamp) {
                    emptyDir($upload_dir);
                }
                $_SESSION["uploadTimestamp_{$sid}"] = $uploadTimestamp;
                // the file name that should be uploaded
                $file_tmp = $_FILES['upload_file']['tmp_name'];
                $raw_file_name = $_FILES['upload_file']['name'];
                $ext = pathinfo($raw_file_name, PATHINFO_EXTENSION);
                $_SESSION["fileExt_{$sid}"] = $ext;
                $upload_path = $upload_dir . $raw_file_name;
                // check upload status
                if (!move_uploaded_file($file_tmp, $upload_path)) {
                    $error = "ERROR: failed to save file.";
                    $_SESSION["upload_file_{$sid}"]['error'] = $_FILES['upload_file']['error'];
                } else {
                    // If a csv file is provided, create a excel file and write the csv value to it.
                    if (strtolower($ext) == 'csv') {
                        // $csvFilePath = $upload_path;
                        // $xlsxFilePath = FileUtil::convertCSVtoXLSX($csvFilePath);
                        // $xlsxFileName = pathinfo($xlsxFilePath, PATHINFO_BASENAME);
                        // $raw_file_name = $xlsxFileName;
                        //unlink($csvFilePath);
                    } else {
                        if (strtolower($ext) == 'zip') {
                            $_SESSION["excelFileMode_{$sid}"] = "append";
                            // If a zip file is provided, unzip all files.
                            $zipFilePath = $upload_path;
                            $zip = new ZipArchive();
                            $res = $zip->open($zipFilePath);
                            if ($res === TRUE) {
                                $zip->extractTo($upload_dir);
                                $zip->close();
                            } else {
                                $error = "ERROR: failed to unzip file.";
                                $_SESSION["upload_file_{$sid}"]['error'] = $error;
                            }
                            // Convert all csv files into xlsx files.
                            $dirFiles = scandir($upload_dir);
                            $i = 1;
                            foreach ($dirFiles as $filename) {
                                $filePath = $upload_dir . $filename;
                                if (FileUtil::isCSVFile($filePath)) {
                                    $_SESSION["fileExt_{$sid}"] = "csv";
                                    break;
                                    // $newFilePath = $upload_dir . $i++ . '.csv';
                                    // rename($filePath, $newFilePath);
                                    // $filePath = $newFilePath;
                                    // $csvFilePath = $filePath;
                                    // $xlsxFilePath = FileUtil::convertCSVtoXLSX($csvFilePath);
                                    // $xlsxFileName = pathinfo($xlsxFilePath, PATHINFO_BASENAME);
                                }
                                //unlink($filePath);
                            }
                        } else {
                            if (strtolower($ext) == 'sql') {
                                try {
                                    $dbImporter = DatabaseImporterFactory::createDatabaseImporter($dbType, $sid, my_pligg_base_no_slash);
                                    $dbImporter->importDbSchema($upload_path);
                                    // Store dump file path for the last step.
                                    $_SESSION["dump_file_{$sid}"] = $upload_path;
                                } catch (Exception $e) {
                                    $_SESSION["upload_file_{$sid}"]['error'] = $e->getMessage();
                                }
                            }
                        }
                    }
                    $loc_msg = "uploaded successfully";
                    $_SESSION["upload_file_{$sid}"]['loc'] = $loc_msg;
                }
            }
        }
    }
    $json_response = array();
    if (isset($_SESSION["upload_file_{$sid}"]['error'])) {
        $json_response['isSuccessful'] = false;
        $json_response['message'] = $_SESSION["upload_file_{$sid}"]['error'];
    } else {
        $json_response['isSuccessful'] = true;
        $json_response['message'] = $_SESSION["upload_file_{$sid}"]['loc'];
    }
    unset($_SESSION["upload_file_{$sid}"]['error']);
    unset($_SESSION["upload_file_{$sid}"]['loc']);
    echo json_encode($json_response);
}
function importDataFromDumpFile($sid, DatabaseHandler $dbHandler, $userId, $filePath)
{
    $ktrExeDao = new KTRExecutorDAO();
    $tables = $dbHandler->loadTables();
    $logIds = array();
    foreach ($tables as $table) {
        $logIds[] = $ktrExeDao->addExecutionInfoTuple($sid, $table, $userId);
    }
    try {
        $dbImporter = DatabaseImporterFactory::createDatabaseImporter($dbHandler->getDriver(), $sid, "colfusion");
        $dbImporter->importDbData($filePath);
        foreach ($logIds as $logId) {
            $ktrExeDao->updateExecutionInfoTimeEnd($logId);
            $ktrExeDao->updateExecutionInfoStatus($logId, 'success');
        }
    } catch (Exception $e) {
        foreach ($logIds as $logId) {
            $ktrExeDao->updateExecutionInfoStatus($logId, 'error');
            $ktrExeDao->updateExecutionInfoErrorMessage($logId, $e->getMessage());
        }
    }
}