Skip to content

Commit

Permalink
Adding table columns for performance. Also made the first step of int…
Browse files Browse the repository at this point in the history
…ergrating PostgreSQL support.
  • Loading branch information
birkir committed Dec 10, 2013
1 parent 179fad7 commit ec93e06
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 25 deletions.
1 change: 1 addition & 0 deletions classes/Controller/Prime/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function action_create()
$page->visible = 1;
$page->disabled = 0;
$page->properties = '{}';
$page->position = 0;

// Let fieldset handle POST method
$this->fieldset($page, TRUE);
Expand Down
36 changes: 17 additions & 19 deletions classes/Model/Prime.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public function publish()
foreach ($columns as $column => $_)
{
// Append revisioned columns to
$sets[] = '`'.$this->_table_name.'`.`'.$column.'` = `'.$this->_table_name.'_rev`.`'.$column.'`';
$sets[] = $this->_db->quote_column($this->_table_name.'.'.$column).' = '.$this->_db->quote_column($this->_table_name.'_rev.'.$column);
}

// Create transfer query
$query = ' UPDATE `'.$this->_table_name.'`'.PHP_EOL
. ' JOIN `'.$this->_table_name.'_rev`'.PHP_EOL
. ' ON `'.$this->_table_name.'_rev`.`revision` = `'.$this->_table_name.'`.`revision`'.PHP_EOL
$query = ' UPDATE '.$this->_db->quote_column($this->_table_name).PHP_EOL
. ' JOIN '.$this->_db->quote_column($this->_table_name.'_rev').PHP_EOL
. ' ON '.$this->_db->quote_column($this->_table_name.'_rev.revision').' = '.$this->_db->quote_column($this->_table_name.'.revision').PHP_EOL
. ' SET '.implode(', ', $sets).PHP_EOL
. ' WHERE `'.$this->_table_name.'`.`id` ='.$this->id;
. ' WHERE '.$this->_db->quote_column($this->_table_name.'.id').' = '.$this->id;

// Load draft row into published row
DB::query(Database::UPDATE, $query)->execute();
Expand All @@ -99,7 +99,7 @@ public function discard()
throw new Kohana_Exception('Cannot publish :model model because its not revision controlled.', array(':model' => $this->_object_name));

DB::update($this->_table_name)
->set(array('revision' => DB::expr('`published`')))
->set(array('revision' => DB::expr($this->_db->quote_column('published'))))
->where('id', '=', $this->id)
->execute();
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public function position($reference_id = NULL, $drafts = TRUE)
// Get new position
$sel = $draft ? DB::select('position')
->from($this->_table_name.'_rev')
->where($this->_table_name.'_rev.revision', '=', DB::expr('`'.$this->_table_name.'`.`revision`'))
->where($this->_table_name.'_rev.revision', '=', DB::expr($this->_db->quote_column($this->_table_name.'.revision')))
->limit(1) : 'position';

// Set new position
Expand All @@ -154,7 +154,7 @@ public function position($reference_id = NULL, $drafts = TRUE)
else
{
// Setup query
$query = DB::select([DB::expr('MAX(`position`)'), 'pos'])
$query = DB::select([DB::expr('MAX('.$this->_db->quote_column('position').')'), 'pos'])
->from($this->_table_name.($draft ? '_rev' : NULL));

foreach ($this->_sortable as $key)
Expand All @@ -167,7 +167,7 @@ public function position($reference_id = NULL, $drafts = TRUE)
{
$query->where('revision', '=', DB::select('revision')
->from($this->_table_name)
->where($this->_table_name.'.id', '=', DB::expr('`'.$this->_table_name.'_rev`.`id`'))
->where($this->_table_name.'.id', '=', DB::expr($this->_db->quote_column($this->_table_name.'_rev.id')))
->limit(1));
}

Expand Down Expand Up @@ -197,7 +197,7 @@ public function position($reference_id = NULL, $drafts = TRUE)
$update = DB::update($this->_table_name.'_rev')
->or_where(DB::expr('(SELECT @pos := 0)'), DB::expr(NULL), DB::expr(NULL))
->or_where_open()
->and_where('revision', '=', DB::select('revision')->from($this->_table_name)->where('id', '=', DB::expr('`'.$this->_table_name.'_rev`.`id`'))->limit(1));
->and_where('revision', '=', DB::select('revision')->from($this->_table_name)->where('id', '=', DB::expr($this->_db->quote_column($this->_table_name.'_rev.id')))->limit(1));

}
else
Expand Down Expand Up @@ -259,7 +259,7 @@ public function count_all()
{
// Append join statement
$this->join(array($this->_table_name.'_rev', $this->_object_name.'_rev'))
->on($this->_object_name.'_rev.revision', '=', DB::expr('`'.$this->_object_name.'`.`revision`'));
->on($this->_object_name.'_rev.revision', '=', DB::expr($this->_db->quote_column($this->_object_name.'.revision')));
}

// Return parent function
Expand Down Expand Up @@ -337,14 +337,14 @@ public function create(Validation $validation = NULL)
}

// Get columns to transfer
$columns = '`'.implode('`, `', array_keys($this->_revision)).'`';
$columns = implode(', ', array_map(function ($column) { return $this->_db->quote_column($column); }, array_keys($this->_revision)));

// Build query
$query = 'INSERT INTO `'.$this->_table_name.'_rev`'.PHP_EOL
$query = ' INSERT INTO '.$this->_db->quote_column($this->_table_name.'_rev').PHP_EOL
. ' ('.$columns.')'.PHP_EOL
. 'SELECT '.$columns.PHP_EOL
. 'FROM `'.$this->_table_name.'`'.PHP_EOL
. 'WHERE `id` = '.$this->id;
. ' SELECT '.$columns.PHP_EOL
. ' FROM '.$this->_db->quote_column($this->_table_name).PHP_EOL
. ' WHERE '.$this->_db->quote_column('id').' = '.intval($this->id);

// Get inserted id
list($id, $rows) = DB::query(Database::INSERT, $query)->execute();
Expand Down Expand Up @@ -490,9 +490,7 @@ protected function _build_select()
// Append join statement
$this->_db_builder
->join(array($this->_table_name.'_rev', $this->_object_name.'_rev'))
->on($this->_object_name.'_rev.revision', '=', DB::expr('`'.$this->_object_name.'`.`revision`'));

// TODO: skip or alter join when `revision` IS NULL.
->on($this->_object_name.'_rev.revision', '=', DB::expr($this->_db->quote_column($this->_object_name.'.revision')));

if ( ! is_array($this->_revision))
{
Expand Down
20 changes: 20 additions & 0 deletions classes/Model/Prime/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ class Model_Prime_Field extends Model_Prime {
*/
protected $_sortable = ['resource_id', 'resource_type'];

protected $_table_columns = array(
'id' => array(),
'resource_id' => array(),
'resource_type' => array(),
'name' => array(),
'caption' => array(),
'group' => array(),
'field' => array(),
'default' => array(),
'visible' => array(),
'required' => array(),
'options' => array(),
'position' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array(),
'published' => array(),
'revision' => array()
);

/**
* Rules for the field model
*
Expand Down
21 changes: 21 additions & 0 deletions classes/Model/Prime/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,25 @@ class Model_Prime_File extends Model_Prime {
)
);

protected $_table_columns = array(
'id' => array(),
'parent_id' => array(),
'name' => array(),
'slug' => array(),
'type' => array(),
'ext' => array(),
'mime' => array(),
'size' => array(),
'width' => array(),
'height' => array(),
'bits' => array(),
'channels' => array(),
'filename' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array(),
'published' => array(),
'revision' => array()
);

}
14 changes: 13 additions & 1 deletion classes/Model/Prime/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @category Model
* @copyright (c) 2013 SOLID Productions
*/
class Model_Prime_Module extends ORM {
class Model_Prime_Module extends Model_Prime {

/**
* @var boolean Model is not sortable
Expand All @@ -18,6 +18,18 @@ class Model_Prime_Module extends ORM {
* @var boolean Model is not revision controlled
*/
protected $_revision = FALSE;
protected $_deletable = FALSE;

protected $_table_columns = array (
'id' => array(),
'controller' => array(),
'slug' => array(),
'name' => array(),
'description' => array(),
'version' => array(),
'js' => array(),
'position' => array()
);

/**
* Always order by ascending position
Expand Down
15 changes: 14 additions & 1 deletion classes/Model/Prime/Module/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @category Fieldset
* @copyright (c) 2013 SOLID Productions
*/
class Model_Prime_Module_Fieldset extends ORM {
class Model_Prime_Module_Fieldset extends Model_Prime {

/**
* @var array Fields container
Expand All @@ -24,6 +24,8 @@ class Model_Prime_Module_Fieldset extends ORM {
*/
protected $_revision = FALSE;

protected $_deletable = FALSE;

/**
* @var array Has many relationships
*/
Expand All @@ -33,6 +35,17 @@ class Model_Prime_Module_Fieldset extends ORM {
]
];

protected $_table_columns = array (
'id' => array(),
'parent_id' => array(),
'name' => array(),
'type' => array(),
'position' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array()
);

/**
* Find fieldset fields inherited.
*
Expand Down
20 changes: 16 additions & 4 deletions classes/Model/Prime/Module/Fieldset/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class Model_Prime_Module_Fieldset_Item extends Model_Prime {
)
);

protected $_table_columns = array(
'id' => array(),
'prime_module_fieldset_id' => array(),
'data' => array(),
'position' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array(),
'published' => array(),
'revision' => array()
);

/**
* Overload model getter for extraction of data JSON field.
*
Expand Down Expand Up @@ -54,10 +66,10 @@ public function data_to_columns($columns = [])
foreach ($columns as $column)
{
$this->select([DB::expr(
'SUBSTRING(REPLACE(REPLACE(REPLACE(`'.$this->_object_name.'`.`data`,\'{\',\'\'),\'}\',\',\'),\'"\',\'\'),'
. 'LOCATE(CONCAT(:column,\':\'),REPLACE(REPLACE(REPLACE(`'.$this->_object_name.'`.`data`,\'{\',\'\'),\'}\',\',\'),\'"\',\'\')) + CHAR_LENGTH(CONCAT(:column,\':\')),'
. 'LOCATE(\',\',SUBSTRING(REPLACE(REPLACE(REPLACE(`'.$this->_object_name.'`.`data`,\'{\',\'\'),\'}\',\',\'),\'"\',\'\'),'
. 'LOCATE(CONCAT(:column,\':\'),REPLACE(REPLACE(REPLACE(`'.$this->_object_name.'`.`data`,\'{\',\'\'),\'}\',\',\'),\'"\',\'\')) + CHAR_LENGTH(CONCAT(:column,\':\')))) - 1)'
'SUBSTRING(REPLACE(REPLACE(REPLACE('.$this->_db->quote_column($this->_object_name.'.data').',\'{\',\'\'),\'}\',\',\'),\'"\',\'\'),'
. 'LOCATE(CONCAT(:column,\':\'),REPLACE(REPLACE(REPLACE('.$this->_db->quote_column($this->_object_name.'.data').',\'{\',\'\'),\'}\',\',\'),\'"\',\'\')) + CHAR_LENGTH(CONCAT(:column,\':\')),'
. 'LOCATE(\',\',SUBSTRING(REPLACE(REPLACE(REPLACE('.$this->_db->quote_column($this->_object_name.'.data').',\'{\',\'\'),\'}\',\',\'),\'"\',\'\'),'
. 'LOCATE(CONCAT(:column,\':\'),REPLACE(REPLACE(REPLACE('.$this->_db->quote_column($this->_object_name.'.data').',\'{\',\'\'),\'}\',\',\'),\'"\',\'\')) + CHAR_LENGTH(CONCAT(:column,\':\')))) - 1)'
, [':column' => $column]), 'data_'.$column]);
}

Expand Down
28 changes: 28 additions & 0 deletions classes/Model/Prime/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ class Model_Prime_Page extends Model_Prime {
]
];

protected $_table_columns = array(
'id' => array(),
'parent_id' => array(),
'language' => array(),
'name' => array(),
'slug' => array(),
'slug_auto' => array(),
'template' => array(),
'properties' => array(),
'description' => array(),
'keywords' => array(),
'noindex' => array(),
'nofollow' => array(),
'redirect' => array(),
'redirect_url' => array(),
'protocol' => array(),
'method' => array(),
'ajax' => array(),
'visible' => array(),
'disabled' => array(),
'position' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array(),
'published' => array(),
'revision' => array()
);

/**
* Get selected page by URI
*
Expand Down
15 changes: 15 additions & 0 deletions classes/Model/Prime/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ class Model_Prime_Region extends Model_Prime {
]
];

protected $_table_columns = array(
'id' => array(),
'prime_page_id' => array(),
'prime_module_id' => array(),
'sticky' => array(),
'name' => array(),
'settings' => array(),
'position' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array(),
'published' => array(),
'revision' => array()
);

/**
* Get region module
*
Expand Down
12 changes: 12 additions & 0 deletions classes/Model/Prime/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ class Model_Prime_Url extends Model_Prime {
* @var boolean Model is not revision controlled
*/
protected $_revision = FALSE;

protected $_table_columns = array (
'id' => array(),
'uri' => array(),
'redirect_enabled' => array(),
'redirect' => array(),
'prime_page_id' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array()
);

}
14 changes: 14 additions & 0 deletions classes/Model/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Model_Role extends Model_Auth_Role {

protected $_table_columns = array(
'id' => array(),
'name' => array(),
'description' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array()
);

}
14 changes: 14 additions & 0 deletions classes/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class Model_User extends Model_Auth_User {
'roles' => array('model' => 'Role', 'through' => 'roles_users'),
);

protected $_table_columns = array(
'id' => array(),
'fullname' => array(),
'email' => array(),
'password' => array(),
'data' => array(),
'logins' => array(),
'language' => array(),
'last_login' => array(),
'updated_at' => array(),
'updated_by' => array(),
'deleted_at' => array()
);

/**
* Rules for the user model. Because the password is _always_ a hash
* when it's set,you need to run an additional not_empty rule in your controller
Expand Down
13 changes: 13 additions & 0 deletions classes/Model/User/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Model_User_Token extends Model_Auth_User_Token {

protected $_table_columns = array(
'id' => array(),
'user_id' => array(),
'user_agent' => array(),
'token' => array(),
'created' => array(),
'expires' => array()
);
}

0 comments on commit ec93e06

Please sign in to comment.