Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/nex 334/abstract model factory #690

Open
wants to merge 4 commits into
base: feature/NEX-336/newsql-schema
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/persistence/class.SqlPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author
*/

use Doctrine\DBAL\DBALException;
/**
* Persistence base on SQL
*/
Expand All @@ -32,6 +33,7 @@ class common_persistence_SqlPersistence extends common_persistence_Persistence
* @param array $params
* @param array $types
* @return int number of updated rows
* @throws DBALException
*/
public function exec($statement, array $params = [], array $types = [])
{
Expand Down Expand Up @@ -60,6 +62,7 @@ public function getPlatForm(){
* @param array $data
* @param array $types
* @return int number of updated rows
* @throws DBALException
*/
public function insert($tableName, array $data, array $types = [])
{
Expand Down Expand Up @@ -96,6 +99,7 @@ public function updateMultiple($table, array $data)
* @param string $statement
* @param array $params
* @return \Doctrine\DBAL\Driver\Statement
* @throws DBALException
*/
public function query($statement, $params = [], array $types = [])
{
Expand Down
5 changes: 3 additions & 2 deletions common/persistence/sql/SetupDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use oat\generis\model\kernel\persistence\smoothsql\install\SmoothRdsModel;
use Doctrine\DBAL\Exception\ConnectionException;

class SetupDb implements LoggerAwareInterface
class SetupDb extends ConfigurableService implements LoggerAwareInterface
{
use LoggerAwareTrait;

Expand Down Expand Up @@ -78,8 +78,9 @@ private function setupTables(\common_persistence_SqlPersistence $p)
*/
public function getSchema(\common_persistence_SqlPersistence $p)
{
$smoothRdsModel = new SmoothRdsModel();
$schema = $p->getSchemaManager()->createSchema();
SmoothRdsModel::addSmoothTables($schema);
$smoothRdsModel->addSmoothTables($schema);
$this->addKeyValueStoreTable($schema);
return $schema;
}
Expand Down
14 changes: 10 additions & 4 deletions common/persistence/sql/dbal/class.Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

use Doctrine\DBAL\DBALException;

/**
* Dbal Driver
*
Expand Down Expand Up @@ -45,7 +47,7 @@ class common_persistence_sql_dbal_Driver implements common_persistence_sql_Drive
* @param string $id
* @param array $params
* @return common_persistence_Persistence|common_persistence_SqlPersistence
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function connect($id, array $params)
{
Expand All @@ -71,7 +73,7 @@ public function connect($id, array $params)
* Endless connection
*
* @param $connectionParams
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
protected function persistentConnect($connectionParams)
{
Expand Down Expand Up @@ -106,7 +108,7 @@ protected function persistentConnect($connectionParams)
* @param $config
* @return \Doctrine\DBAL\Connection
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*
*/
private function getConnection($params, $config)
Expand Down Expand Up @@ -157,6 +159,7 @@ public function getSchemaManager()
* @param array $params
* @param array $types
* @return integer number of affected row
* @throws DBALException;
*/
public function exec($statement, $params = [], array $types = [])
{
Expand All @@ -170,6 +173,7 @@ public function exec($statement, $params = [], array $types = [])
* @param array $params
* @param array $types
* @return \Doctrine\DBAL\Driver\Statement
* @throws DBALException;
*/
public function query($statement, $params = [], array $types = [])
{
Expand All @@ -189,7 +193,9 @@ public function quote($parameter, $parameter_type = PDO::PARAM_STR){
}

/**
* @inheritdoc
* (non-PHPdoc)
* @see common_persistence_sql_Driver::insert()
* @throws DBALException;
*/
public function insert($tableName, array $data, array $types = [])
{
Expand Down
17 changes: 14 additions & 3 deletions common/persistence/sql/interface.Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@
* @package generis
*
*/

use Doctrine\DBAL\DBALException;

interface common_persistence_sql_Driver extends common_persistence_Driver
{

/**
* @inheritdoc
* @throws DBALException;
*/
public function query($statement,$params, array $types = []);


/**
* @inheritdoc
* @throws DBALException;
*/
public function exec($statement,$params, array $types = []);

/**
Expand All @@ -35,7 +45,8 @@ public function exec($statement,$params, array $types = []);
*
* @param string $tableName name of the table
* @param array $data An associative array containing column-value pairs.
* @return integer The number of affected rows.
* @return integer The number of affected rows.
* @throws DBALException
*/
public function insert($tableName, array $data, array $types = []);

Expand Down
120 changes: 120 additions & 0 deletions core/kernel/api/NewSqlModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA
*/

namespace oat\generis\model\kernel\api;

use core_kernel_api_ModelFactory as ModelFactory;
use Doctrine\DBAL\Schema\Schema;
use oat\generis\Helper\UuidPrimaryKeyTrait;
use RuntimeException;

class NewSqlModelFactory extends ModelFactory
{
use UuidPrimaryKeyTrait;

/**
* @inheritdoc
*/
public function addNewModel($namespace)
{
$modelId = md5($namespace);
if ($this->getPersistence()->insert('models', ['modelid' => $modelId, 'modeluri' => $namespace]) === 0) {
throw new RuntimeException('A problem occurred while creating a new model.');
}
return $modelId;
}

/**
* @inheritdoc
*/
public function prepareStatement($modelId, $subject, $predicate, $object, $lang, $author)
{
return [
'id' => $this->getUniquePrimaryKey(),
'modelid' => $modelId,
'subject' => $subject,
'predicate' => $predicate,
'object' => $object,
'l_language' => $lang,
'author' => $author ?? '',
'epoch' => $this->getPersistence()->getPlatForm()->getNowExpression(),
];
}

/**
* @inheritdoc
*/
public function buildModelSqlCondition(array $models)
{
$models = array_map(
function ($a) {
return "'" . $a . "'";
},
$models
);
return parent::buildModelSqlCondition($models);
}

/**
* @inheritdoc
*/
public function getPropertySortingField()
{
return 'epoch';
}

/**
* @inheritdoc
*/
public function createModelsTable(Schema $schema)
{
$table = $schema->createTable('models');

$table->addColumn('modelid', 'string', ['length' => 36, 'notnull' => true]);
$table->addColumn('modeluri', 'string', ['length' => 255]);

$table->setPrimaryKey(['modelid']);

return $table;
}

/**
* @inheritdoc
*/
public function createStatementsTable(Schema $schema)
{
$table = $schema->createTable('statements');

$table->addColumn('id', 'string', ['length' => 36, 'notnull' => true]);
$table->addColumn('modelid', 'string', ['length' => 23, 'notnull' => true]);
$table->addColumn('subject', 'string', ['length' => 255]);
$table->addColumn('predicate', 'string', ['length' => 255]);
$table->addColumn('object', 'text', []);
$table->addColumn('l_language', 'string', ['length' => 255]);
$table->addColumn('author', 'string', ['length' => 255]);
$table->addColumn('epoch', 'string', ['notnull' => true]);

$table->setPrimaryKey(['id']);
$table->addIndex(['subject', 'predicate'], 'k_sp');
$table->addIndex(['predicate', 'object'], 'k_po');

return $table;
}
}
109 changes: 109 additions & 0 deletions core/kernel/api/RdsModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA
*/

namespace oat\generis\model\kernel\api;

use core_kernel_api_ModelFactory as ModelFactory;
use Doctrine\DBAL\Schema\Schema;
use RuntimeException;

class RdsModelFactory extends ModelFactory
{
/**
* @inheritdoc
*/
public function addNewModel($namespace)
{
$persistence = $this->getPersistence();
if ($persistence->insert('models', ['modeluri' => $namespace]) === 0) {
throw new RuntimeException('A problem occurred while creating a new model.');
}

// Retrieving the inserted modelid (auto-increment).
$result = $persistence->query('select modelid from models where modeluri = ?', [$namespace]);
$modelId = $result->fetch();
return $modelId['modelid'];
}

/**
* @inheritdoc
*/
public function prepareStatement($modelId, $subject, $predicate, $object, $lang, $author)
{
return [
'modelid' => $modelId,
'subject' => $subject,
'predicate' => $predicate,
'object' => $object,
'l_language' => $lang,
'author' => $author ?? '',
'epoch' => $this->getPersistence()->getPlatForm()->getNowExpression(),
];
}

/**
* @inheritdoc
*/
public function getPropertySortingField()
{
return 'id';
}

/**
* @inheritdoc
*/
public function createModelsTable(Schema $schema)
{
// Models table.
$table = $schema->createTable('models');

$table->addColumn('modelid', 'integer', ['notnull' => true, 'autoincrement' => true]);
$table->addColumn('modeluri', 'string', ['length' => 255, 'default' => null]);

$table->setPrimaryKey(['modelid']);
$table->addOption('engine', 'MyISAM');

return $table;
}

/**
* @inheritdoc
*/
public function createStatementsTable(Schema $schema)
{
$table = $schema->createTable('statements');

$table->addColumn('id', 'integer', ['notnull' => true, 'autoincrement' => true]);
$table->addColumn('modelid', 'integer', ['notnull' => true, 'default' => 0]);
$table->addColumn('subject', 'string', ['length' => 255, 'default' => null]);
$table->addColumn('predicate', 'string', ['length' => 255, 'default' => null]);
$table->addColumn('object', 'text', ['default' => null, 'notnull' => false]);
$table->addColumn('l_language', 'string', ['length' => 255, 'default' => null, 'notnull' => false]);
$table->addColumn('author', 'string', ['length' => 255, 'default' => null, 'notnull' => false]);
$table->addColumn('epoch', 'string', ['notnull' => null]);

$table->setPrimaryKey(['id']);
$table->addIndex(['subject', 'predicate'], 'k_sp', [], ['lengths' => [164, 164]]);
$table->addIndex(['predicate', 'object'], 'k_po', [], ['lengths' => [164, 164]]);
$table->addOption('engine', 'MyISAM');

return $table;
}
}
Loading