Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transactions to import? #23

Open
afagard opened this issue Mar 11, 2017 · 0 comments
Open

Add transactions to import? #23

afagard opened this issue Mar 11, 2017 · 0 comments
Milestone

Comments

@afagard
Copy link

afagard commented Mar 11, 2017

Currently the function executes query by query and fails on one with incorrect syntax. This means that prior queries were executed. So we are left with a half-baked import. Either all or none of the queries should be executed. So maybe it would be a good idea to implement transactions into the import function?

Sorry if this is not how one should suggest this.

protected function import()
	{
		$query = '';
		$this->pdo->beginTransaction();
		try {
			while (!feof($this->file)) {
				$line = fgets($this->file);
				$trim = trim($line);

				if ($trim === '' || strpos($trim, '--') === 0 || strpos($trim, '/*') === 0) {
					continue;
				}

				if (strpos($trim, 'DELIMITER ') === 0) {
					$this->delimiter = substr($trim, 10);
					continue;
				}

				$query .= $line;

				if (substr($trim, strlen($this->delimiter) * -1) === $this->delimiter) {
					$this->pdo->exec(substr(trim($query), 0, strlen($this->delimiter) * -1));
					$query = '';
				}
			}
			$this->pdo->commit();
		} catch (\PDOException $e) {
			$this->pdo->rollBack();
                        // throws \PDOException as Exception to make things easier
			throw new Exception($e->getMessage(), $e->getCode(), $e);
		}
	}
@gocom gocom added this to the 2.8.0 milestone Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants