Skip to content

Add transactions to import? #23

@afagard

Description

@afagard

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);
		}
	}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions