From 3b0d0e2b1f3fec67e18402b0b0ecaf03dcb6fed8 Mon Sep 17 00:00:00 2001 From: Oliver Gasser Date: Tue, 17 Dec 2013 22:46:45 +0100 Subject: [PATCH 1/5] DB: Support DECIMAL(precision,scale) syntax in XML Add support for specifying the precision and scale of a decimal data type to the XML description language. See owncloud/core#6475 --- lib/private/db/mdb2schemareader.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/private/db/mdb2schemareader.php b/lib/private/db/mdb2schemareader.php index 511bd1c90b..b1fd2454cb 100644 --- a/lib/private/db/mdb2schemareader.php +++ b/lib/private/db/mdb2schemareader.php @@ -183,6 +183,14 @@ class MDB2SchemaReader { $primary = $this->asBool($child); $options['primary'] = $primary; break; + case 'precision': + $precision = (string)$child; + $options['precision'] = $precision; + break; + case 'scale': + $scale = (string)$child; + $options['scale'] = $scale; + break; default: throw new \DomainException('Unknown element: ' . $child->getName()); From 5fcbe5350c88d5949fd7fd0fdb654707d9e42d5f Mon Sep 17 00:00:00 2001 From: Oliver Gasser Date: Tue, 17 Dec 2013 23:08:05 +0100 Subject: [PATCH 2/5] Add decimal(precision,scale) column to unit tests --- tests/lib/db/mdb2schemareader.php | 3 +++ tests/lib/db/testschema.xml | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/tests/lib/db/mdb2schemareader.php b/tests/lib/db/mdb2schemareader.php index 57cafa7c76..faa3b8d7da 100644 --- a/tests/lib/db/mdb2schemareader.php +++ b/tests/lib/db/mdb2schemareader.php @@ -69,6 +69,9 @@ class MDB2SchemaReader extends \PHPUnit_Framework_TestCase { $this->assertTrue($table->getColumn('booleanfield_true')->getDefault()); $this->assertFalse($table->getColumn('booleanfield_false')->getDefault()); + $this->assertEquals(12, $table->getColumn('decimalfield_precision_scale')->getPrecision()); + $this->assertEquals(2, $table->getColumn('decimalfield_precision_scale')->getScale()); + $this->assertCount(2, $table->getIndexes()); $this->assertEquals(array('integerfield'), $table->getIndex('primary')->getUnquotedColumns()); $this->assertTrue($table->getIndex('primary')->isPrimary()); diff --git a/tests/lib/db/testschema.xml b/tests/lib/db/testschema.xml index 509b55ee81..9de804b929 100644 --- a/tests/lib/db/testschema.xml +++ b/tests/lib/db/testschema.xml @@ -53,6 +53,12 @@ boolean false + + decimalfield_precision_scale + decimal + 12 + 2 + index_primary From cb9e87ecedc1b4e61c4d8967cfaa39b22137666f Mon Sep 17 00:00:00 2001 From: Oliver Gasser Date: Tue, 17 Dec 2013 23:46:36 +0100 Subject: [PATCH 3/5] Typo fixed --- tests/lib/db/testschema.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/db/testschema.xml b/tests/lib/db/testschema.xml index 9de804b929..dfca920a0e 100644 --- a/tests/lib/db/testschema.xml +++ b/tests/lib/db/testschema.xml @@ -55,7 +55,7 @@ decimalfield_precision_scale - decimal + decimal 12 2 From f0962c99dcebdb1d58033eb1b33d57cf48d88bf7 Mon Sep 17 00:00:00 2001 From: Oliver Gasser Date: Wed, 18 Dec 2013 00:37:09 +0100 Subject: [PATCH 4/5] Increment number of columns by one --- tests/lib/db/mdb2schemareader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/db/mdb2schemareader.php b/tests/lib/db/mdb2schemareader.php index faa3b8d7da..f08996cbea 100644 --- a/tests/lib/db/mdb2schemareader.php +++ b/tests/lib/db/mdb2schemareader.php @@ -39,7 +39,7 @@ class MDB2SchemaReader extends \PHPUnit_Framework_TestCase { $this->assertCount(1, $schema->getTables()); $table = $schema->getTable('test_table'); - $this->assertCount(7, $table->getColumns()); + $this->assertCount(8, $table->getColumns()); $this->assertEquals(4, $table->getColumn('integerfield')->getLength()); $this->assertTrue($table->getColumn('integerfield')->getAutoincrement()); From cacb66480b4bebc8511842bcc588ffd750ef0c38 Mon Sep 17 00:00:00 2001 From: Oliver Gasser Date: Wed, 18 Dec 2013 23:40:11 +0100 Subject: [PATCH 5/5] Add unit tests for decimal type usage --- tests/data/db_structure.xml | 3 ++- tests/data/db_structure2.xml | 3 ++- tests/lib/db.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 5f2edbbc51..bfff214334 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -216,7 +216,8 @@ decimal true - 15 + 12 + 2 diff --git a/tests/data/db_structure2.xml b/tests/data/db_structure2.xml index 6cd071451d..ae5f22e957 100644 --- a/tests/data/db_structure2.xml +++ b/tests/data/db_structure2.xml @@ -113,7 +113,8 @@ decimal true - 15 + 12 + 2 diff --git a/tests/lib/db.php b/tests/lib/db.php index 3fcdf8a7dc..96d5f873b5 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -40,6 +40,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { $this->table1 = $this->test_prefix.'cntcts_addrsbks'; $this->table2 = $this->test_prefix.'cntcts_cards'; $this->table3 = $this->test_prefix.'vcategory'; + $this->table4 = $this->test_prefix.'decimal'; } public function tearDown() { @@ -172,4 +173,32 @@ class Test_DB extends PHPUnit_Framework_TestCase { $actual = OC_DB::prepare("SELECT `fullname` FROM `$table`")->execute()->fetchOne(); $this->assertSame($expected, $actual); } + + public function testDecimal() { + $table = "*PREFIX*" . $this->table4; + $rowname = 'decimaltest'; + + // Insert, select and delete decimal(12,2) values + $inserts = array('1337133713.37', '1234567890'); + $expects = array('1337133713.37', '1234567890.00'); + + for ($i = 0; $i < count($inserts); $i++) { + $insert = $inserts[$i]; + $expect = $expects[$i]; + + $query = OC_DB::prepare('INSERT INTO `' . $table . '` (`' . $rowname . '`) VALUES (?)'); + $result = $query->execute(array($insert)); + $this->assertEquals(1, $result); + $query = OC_DB::prepare('SELECT `' . $rowname . '` FROM `' . $table . '`'); + $result = $query->execute(); + $this->assertTrue((bool)$result); + $row = $result->fetchRow(); + $this->assertArrayHasKey($rowname, $row); + $this->assertEquals($expect, $row[$rowname]); + $query = OC_DB::prepare('DELETE FROM `' . $table . '`'); + $result = $query->execute(); + $this->assertTrue((bool)$result); + } + } + }