<commit_before><?php

use Quibble\Sqlite\Adapter;
use Quibble\Query\UpdateException;
use Quibble\Query\Buildable;

/**
 * Updating
 */
return function () : Generator {
    $this->beforeEach(function () use (&$pdo) {
        $pdo = new class(':memory:') extends Adapter {
            use Buildable;
        };
        $pdo->exec(<<<EOT
DROP TABLE IF EXISTS test;
CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    foo VARCHAR(255) NOT NULL,
    bar TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO test (foo) VALUES ('bar'), ('baz'), ('buzz');

EOT
        );
    });

    /** update should update a row */
    yield function () use (&$pdo) {
        $res = $pdo->update('test')
            ->where('id = ?', 1)
            ->execute(['foo' => 'douglas']);
        assert($res === true);
    };

    /** We can update a column with a raw value */
    yield function () use (&$pdo) {
        $res = $pdo->update('test')
            ->where('id = ?', 1)
            ->execute(['bar' => [$pdo->now()]]);
        assert($res === true);
    };

    /** update should return false if nothing was updated */
    yield function () use (&$pdo) {
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        $res = $pdo->update('test')
            ->where('id = ?', 1234)
            ->execute(['foo' => 'adams']);
        assert($res === false);
    };

    /** update should throw an exception if nothing was updated */
    yield function () use (&$pdo) {
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $e = null;
        try {
            $pdo->update('test')
                ->where('id = ?', 12345)
                ->execute(['foo' => 'adams']);
        } catch (UpdateException $e) {
        }
        assert($e instanceof UpdateException);
    };
};
<commit_msg>add test for illegal update to check it returns an SqlException<commit_after><?php

use Quibble\Sqlite\Adapter;
use Quibble\Query\{ Buildable, UpdateException };
use Quibble\Dabble\SqlException;

/**
 * Updating
 */
return function () : Generator {
    $this->beforeEach(function () use (&$pdo) {
        $pdo = new class(':memory:') extends Adapter {
            use Buildable;
        };
        $pdo->exec(<<<EOT
DROP TABLE IF EXISTS test;
CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    foo VARCHAR(255) NOT NULL,
    bar TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO test (foo) VALUES ('bar'), ('baz'), ('buzz');

EOT
        );
    });

    /** update should update a row */
    yield function () use (&$pdo) {
        $res = $pdo->update('test')
            ->where('id = ?', 1)
            ->execute(['foo' => 'douglas']);
        assert($res === true);
    };

    /** We can update a column with a raw value */
    yield function () use (&$pdo) {
        $res = $pdo->update('test')
            ->where('id = ?', 1)
            ->execute(['bar' => [$pdo->now()]]);
        assert($res === true);
    };

    /** update should return false if nothing was updated */
    yield function () use (&$pdo) {
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        $res = $pdo->update('test')
            ->where('id = ?', 1234)
            ->execute(['foo' => 'adams']);
        assert($res === false);
    };

    /** update should throw an exception if nothing was updated */
    yield function () use (&$pdo) {
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $e = null;
        try {
            $pdo->update('test')
                ->where('id = ?', 12345)
                ->execute(['foo' => 'adams']);
        } catch (UpdateException $e) {
        }
        assert($e instanceof UpdateException);
        $e = null;
        try {
            $pdo->update('test')
                ->where('id = ?', 1)
                ->execute(['foo' => null]);
        } catch (Throwable $e) {
        }
        assert($e instanceof SqlException);
    };
};

{'fraction_non_alphanumeric': 0.1755359394703657, 'fraction_numerical': 0.007313997477931904, 'mean_word_length': 2.1105882352941174, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460382', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1215, 'n_words': 312}