This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
tsmac/modules/tsm/classes/lnApp/Database/Query/Builder/Insert.php
2014-10-28 16:56:56 +11:00

82 lines
2.0 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends Kohana's [ORM] class to create defaults for TSM.
*
* @package TSM Database Module
* @category Query
* @author Deon George
* @copyright (c) 2010-2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Database_Query_Builder_Insert extends Kohana_Database_Query_Builder_Insert {
private $_commands = array(
'NODES'=>array(
'OPTION_SET'=>'CLOPTSET',
'DOMAIN_NAME'=>'DOMAIN',
),
);
public function compile($db = NULL) {
if ( ! is_object($db)) {
// Get the database instance
$db = Database::instance($db);
}
switch ($this->_table) {
case 'ADMINS':
$query = sprintf('REGISTER ADMIN %s %s %s',$this->pop_value('ADMIN_NAME'),$this->pop_value('PASSWORD'),$this->insert_values());
break;
case 'NODES':
$query = sprintf('REGISTER NODE %s %s %s',$this->pop_value('NODE_NAME'),$this->pop_value('PASSWORD'),$this->insert_values());
break;
default:
throw HTTP_Exception::factory(501,'Not setup for table :table',array(':table'=>$this->_table));
}
return $query;
}
private function get_keyalt($key) {
return isset($this->_commands[$this->_table][$key]) ? $this->_commands[$this->_table][$key] : $key;
}
private function get_key($key) {
return array_search($key,$this->_columns);
}
// @todo Values always go in as an array - should see if we can stop that
private function get_value($key) {
return (is_numeric($x=$this->get_key($key))) ? $this->_values[0][$x] : '';
}
private function insert_values() {
$result = '';
foreach ($this->_columns as $k=>$v) {
if ($result)
$result .= ' ';
$result .= sprintf('%s=\'%s\'',$this->get_keyalt($v),$this->get_value($v));
}
return $result;
}
private function pop_value($key) {
$result = NULL;
$x = $this->get_key($key);
$result = $this->get_value($key);
unset($this->_columns[$x]);
unset($this->_values[0][$x]);
return $result;
}
}
?>