From efeb9c4a97668672f8aaa263e4937461f0b1b725 Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Wed, 27 Jan 2021 12:29:59 +0100 Subject: special handling of boolean columns (accept false as value without warning) --- it_dbi.class | 5 +++++ test/it_dbi.t | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/it_dbi.class b/it_dbi.class index c9cef54..6e34002 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -956,6 +956,11 @@ function _get_field_info() $this->_convertfunc[$name] = $field['_convertfunc']; $this->_escapefunc[$name] = $field['_escapefunc']; } + else if ($field['Type'] == 'tinyint(1)') + { + $this->_convertfunc[$name] = 'boolval'; + $this->_escapefunc[$name] = 'it_dbi::escape_bool'; + } else if (preg_match('/^(tiny|small|medium|)int|^float|^double$/', $field['Type'])) $this->_convertfunc[$name] = it::match('int', $field['Type']) ? "intval" : "floatval"; } diff --git a/test/it_dbi.t b/test/it_dbi.t index 2a601b6..73d1e1f 100755 --- a/test/it_dbi.t +++ b/test/it_dbi.t @@ -45,8 +45,8 @@ try { is(it::match('syntax', $e->getMessage()), 'syntax', "Syntax error triggers error"); } -$record->insert(['x' => 42, 'y' => 0.0001, 'z' => 0, 'foo' => null, 'flag' => 1]); -$record->insert(['foo' => "bar", 'y' => 1e-10, 'z' => 0, 'flag' => 0]); +$record->insert(['x' => 42, 'y' => 0.0001, 'z' => 0, 'foo' => null, 'flag' => true]); +$record->insert(['foo' => "bar", 'y' => 1e-10, 'z' => 0, 'flag' => false]); $record->insert(['x' => 64738, 'y' => 1, 'z' => 0, 'foo' => "q'uux"]); is( @@ -67,9 +67,9 @@ is( [1, 42, 0.0001, null], "read" ); -ok(!!$record->flag, "read true boolean"); +is($record->flag, true, "read true boolean"); $record->read(2); -ok(!$record->flag, "read false boolean"); +is($record->flag, false, "read false boolean"); is( $record->select(), @@ -511,17 +511,17 @@ static function _write_preprocess($data) $record = new my_dbi_test; $record->delete(['WHERE TRUE' ]); -$record->upsert(['ID' => 1, 'x' => 42, 'y' => 2.5, 'foo' => ['value' => "a"], 'flag' => 0]); +$record->upsert(['ID' => 1, 'x' => 42, 'y' => 2.5, 'foo' => ['value' => "a"], 'flag' => false]); $record->read(1); is($record->x, 42, 'value of x after reading with _read_postprocess'); -is($record->_data, ["ID" => 1, "x" => 42, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"a"}', 'flag' => 0], 'data after reading with _read_postprocess'); +is($record->_data, ["ID" => 1, "x" => 42, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"a"}', 'flag' => false], 'data after reading with _read_postprocess'); is($record->foo, ['value' => "a"], 'field after reading with _read_postprocess'); $record2 = new it_dbi_test; $record2->read(1); is($record2->x, 43, 'raw value of x after writing with _write_preprocess'); -is($record2->_data, ["ID" => 1, "x" => 43, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"a"}', 'flag' => 0], 'raw data after writing with _write_preprocess'); +is($record2->_data, ["ID" => 1, "x" => 43, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"a"}', 'flag' => false], 'raw data after writing with _write_preprocess'); $record->update(['foo' => ['value' => "b"]]); -is($record->_data, ["ID" => 1, "x" => 42, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"b"}', 'flag' => 0], 'raw data after updating with _write_preprocess'); +is($record->_data, ["ID" => 1, "x" => 42, 'y' => 2.5, 'z' => NULL, 'foo' => '{"value":"b"}', 'flag' => false], 'raw data after updating with _write_preprocess'); is($record->foo, ['value' => "b"], 'field data after updating with _write_preprocess'); -- cgit v1.2.3