Skip to content

Instantly share code, notes, and snippets.

@tistaharahap
Created January 20, 2012 03:35
Show Gist options
  • Save tistaharahap/1644812 to your computer and use it in GitHub Desktop.
Save tistaharahap/1644812 to your computer and use it in GitHub Desktop.
HandlerSocket Client for CodeIgniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* HandlerSocket Library for CodeIgniter
*
* To use this class, please use HandlerSocket's PHP extension available at
* http://code.google.com/p/php-handlersocket/
*
* @package CodeIgniter Library
* @subpackage HandlerSocket
* @category Database
* @author Batista R. Harahap <[email protected]>
* @link http://www.bango29.com
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
class Hsock {
protected static $CI;
protected static $conn;
/**
* HandlerSocket Constructor
*
* Initialize with $write = TRUE to connect to HandlerSocket's write Port
*
* @access public
* @param bool write
* @return none
*/
function __construct($write = FALSE) {
$this->CI =& get_instance();
if($write) {
$port =& $this->CI->config->item('hsock_port_write');
} else {
$port =& $this->CI->config->item('hsock_port_read');
}
$this->conn = new HandlerSocket(
$this->CI->config->item('hsock_host'),
$port
);
}
/**
* Open a MySQL table index for query
*
* The parameter $index_id is a free select of any integer, use as if it was the connection ID
*
* @access public
* @param int index_id
* @param string db_name
* @param string table_name
* @param string where_index_name
* @param mixed table_select_fields
* @return bool
*/
function open_index($index_id, $db_name, $table_name, $where_index_name, $table_select_fields) {
if(!is_array($table_select_fields))
throw new HandlerSocketException("Table Fields must be an array while opening index");
$table_select_fields = implode(",", $table_select_fields);
$open = $this->conn->openIndex(
$index_id,
$db_name,
$table_name,
$where_index_name,
$table_select_fields
);
if($open) {
return $open;
} else {
return FALSE;
}
}
/**
* Execute a single query for an opened index
*
* @access public
* @param int index_id
* @param string operand
* @param mixed where_value_fields
* @param int limit
* @param int offset
* @return bool
*/
function execute_single($index_id, $operand, $where_value_fields, $limit = -1, $offset = 0) {
if(!is_array($where_value_fields))
throw new HandlerSocketException("WHERE Value Fields must be an array while executing single query");
$where_value_fields = implode(',', $where_value_fields);
$exec = $this->conn->executeSingle(
$index_id,
$operand,
$where_value_fields,
$limit,
(int)$offset
);
if(!empty($exec)) {
return $exec;
} else {
return FALSE;
}
}
/**
* Execute a multiple query for an opened index
*
* @access public
* @param mixed queries
* @return bool
*/
function execute_multi($queries = array()) {
if(!is_array($queries))
throw new HandlerSocketException("Queries must be an array while executing multi query");
$exec = $this->conn->executeMulti($queries);
if(!empty($exec))
return $exec;
else
return FALSE;
}
/**
* Execute an insert query to an open index's MySQL table
*
* @access public
* @param int index_id
* @param mixed column_values
* @return bool
*/
function execute_insert($index_id, $column_values = array()) {
return $this->conn->executeInsert($index_id, $column_values);
}
/**
* Get errors for queries
*
* @access public
* @return mixed
*/
function get_error() {
return $this->conn->getError();
}
}
/* End of file Hsock.php */
/* Location: application/libraries/ */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* HandlerSocket Config for CodeIgniter - Please autoload the config!
*
* To use this class, please use HandlerSocket's PHP extension available at
* http://code.google.com/p/php-handlersocket/
*
* @package CodeIgniter Config
* @subpackage HandlerSocket
* @category Database
* @author Batista R. Harahap <[email protected]>
* @link http://www.bango29.com
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
if(!defined('HSOCK_WRITE_MODE') && !defined('HSOCK_READ_MODE')) {
define('HSOCK_WRITE_MODE', TRUE);
define('HSOCK_READ_MODE', FALSE);
}
$config['hsock_host'] = '127.0.0.1';
$config['hsock_port_read'] = 9998;
$config['hsock_port_write'] = 9999;
/* End of file hsock.php */
/* Location: application/config/ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment