Commit 209b82c8 authored by Volker Krause's avatar Volker Krause
Browse files

Normalize schema entry element column names

parent e2ed6d3c
......@@ -38,7 +38,7 @@ class Sample
if (!$entry->isScalar())
continue;
foreach ($entry->elements as $elem)
$scalarSql .= ', ' . $elem->name;
$scalarSql .= ', ' . $elem->dataColumnName();
}
$scalarSql .= ' FROM ' . $product->dataTableName() . ' ORDER BY timestamp ASC';
$scalarStmt = $db->prepare($scalarSql);
......@@ -51,7 +51,7 @@ class Sample
continue;
$entryData = null;
foreach ($entry->elements as $elem)
$entryData[$elem->name] = self::valueFromDb($elem, $scalarRow[$elem->name]);
$entryData[$elem->name] = self::valueFromDb($elem, $scalarRow[$elem->dataColumnName()]);
$rowData[$entry->name] = $entryData;
}
array_push($data, $rowData);
......@@ -66,14 +66,14 @@ class Sample
if ($entry->type == SchemaEntry::MAP_TYPE)
$sql .= ', key';
foreach ($entry->elements as $elem)
$sql .= ', ' . $elem->name;
$sql .= ', ' . $elem->dataColumnName();
$sql .= ' FROM ' . $entry->dataTableName() . ' ORDER BY id ASC';
$stmt = $db->prepare($sql);
$db->execute($stmt, array());
foreach ($stmt as $row) {
$entryData = null;
foreach ($entry->elements as $elem)
$entryData[$elem->name] = self::valueFromDb($elem, $row[$elem->name]);
$entryData[$elem->name] = self::valueFromDb($elem, $row[$elem->dataColumnName()]);
$idx = $sampleIdIndex[$row['sampleId']];
if (!array_key_exists($entry->name, $data[$idx]))
$data[$idx][$entry->name] = array();
......@@ -140,8 +140,8 @@ class Sample
continue;
}
$bind = ':' . $elem->name;
array_push($columns, $elem->name);
$bind = ':' . $elem->dataColumnName();
array_push($columns, $elem->dataColumnName());
array_push($binds, $bind);
$values[$bind] = $v;
}
......@@ -184,8 +184,8 @@ class Sample
array_push($binds, ':key');
}
foreach ($schemaEntry->elements as $elem) {
array_push($columns, $elem->name);
array_push($binds, ':' . $elem->name);
array_push($columns, $elem->dataColumnName());
array_push($binds, ':' . $elem->dataColumnName());
}
$sql = 'INSERT INTO ' . $schemaEntry->dataTableName()
. ' (' . implode(', ', $columns) . ') VALUES ('
......@@ -200,11 +200,11 @@ class Sample
$bindValues[':key'] = $key;
}
foreach ($schemaEntry->elements as $elem) {
$bindValues[':' . $elem->name] = null;
$bindValues[':' . $elem->dataColumnName()] = null;
if (!property_exists($entry, $elem->name))
continue;
// TODO type check, as done for scalar values
$bindValues[':' . $elem->name] = $entry->{$elem->name};
$bindValues[':' . $elem->dataColumnName()] = $entry->{$elem->name};
}
$db->execute($stmt, $bindValues);
}
......
......@@ -47,6 +47,12 @@ class SchemaEntryElement
return true;
}
/** Column name in the data table. */
public function dataColumnName()
{
return Utils::normalizeString($this->schemaEntry->name) . '_' . Utils::normalizeString($this->name);
}
/** Insert this element into storage. */
public function insert(Datastore $db, $entryId)
{
......@@ -113,7 +119,7 @@ class SchemaEntryElement
private function createScalarDataTableColumn(Datastore $db)
{
$stmt = $db->prepare('ALTER TABLE ' . $this->schemaEntry->product()->dataTableName()
. ' ADD COLUMN ' . $this->name . ' ' . $this->sqlType());
. ' ADD COLUMN ' . $this->dataColumnName() . ' ' . $this->sqlType());
$db->execute($stmt);
}
......@@ -121,7 +127,7 @@ class SchemaEntryElement
private function createNonScalarDataTableColumn(Datastore $db)
{
$stmt = $db->prepare('ALTER TABLE ' . $this->schemaEntry->dataTableName()
. ' ADD COLUMN ' . $this->name . ' ' . $this->sqlType());
. ' ADD COLUMN ' . $this->dataColumnName() . ' ' . $this->sqlType());
$db->execute($stmt);
}
}
......
......@@ -82,7 +82,6 @@ private slots:
QVERIFY(p.isValid());
reply = RESTApi::createProduct(&client, p);
QVERIFY(waitForFinished(reply));
QEXPECT_FAIL("", "field name validation not not implemented", Continue);
QCOMPARE(reply->error(), QNetworkReply::NoError);
// submit data
......
......@@ -22,6 +22,26 @@ require_once('../../server/shared/schemaentryelement.php');
class SchemaEntryElementTest extends PHPUnit_Framework_TestCase
{
public function testDataColumnName_data()
{
return [
'normal' => [ 'foo', 'entry_foo' ],
'dot' => [ 'my.value', 'entry_my_value' ]
];
}
/** @dataProvider testDataColumnName_data */
public function testDataTableName($input, $output)
{
$p = new Product;
$p->name = 'org.kde.TestProduct';
$se = new SchemaEntry($p);
$se->name = 'entry';
$see = new SchemaEntryElement($se);
$see->name = $input;
$this->assertEquals($output, $see->dataColumnName());
}
public function testInvalidJson_data()
{
return [
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment