Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // Manataria Website Platform - Eats the seaweeds
- //
- // Copyright Kenneth Elliott. All rights reserved.
- //
- /**
- * MySQL Wrapper
- *
- * This class contains a uniform wrapper for communicating with a MySQL database.
- * Now build classes with the same interface for additional DBMS's ;)
- *
- * @author Kenneth Elliott <kenelliottmc@gmail.com>
- * @copyright Copyright © 2008 Kenneth Elliott <kenelliottmc@gmail.com>
- * @category Manataria
- * @package MySQL
- * @since Manataria 0.0.1
- */
- class MySQL_Adapter {
- var $db;
- var $link_id;
- var $error;
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- public function __construct($host = '', $user = '', $db = '', $pass = ''){
- $host = $this->not_null($host) ? $host : DB_SERVER;
- $user = $this->not_null($user) ? $user : DB_SERVER_USERNAME;
- $pass = $this->not_null($pass) ? $pass : DB_SERVER_PASSWORD;
- $this->db = $this->not_null($db) ? $db : DB_DATABASE;
- $this->connect_db($host, $user, $pass) or die ('Jay sucks');
- $this->select_db();
- //$this->error = Registry::getKey('Error');
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- public function connect_db($host, $user, $pass){
- $this->link_id = mysql_connect($host, $user, $pass);
- return;
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- public function select_db(){
- return mysql_select_db($this->db, $this->link_id);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- public function query($query, $timer = true){
- $resource = mysql_query($query, $this->link_id);
- return $resource;
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- public function query_unbuffered($query, $timer = true){
- $resource = mysql_unbuffered_query($query, $this->link_id);
- if(!is_resource($resource)) return array();
- $a = array();
- while($r = mysql_fetch_assoc($resource)) $a[] = $r;
- return $a;
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function fetch_array($resource_id, $type = MYSQL_ASSOC){
- return mysql_fetch_array($resource_id, $type);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function num_rows($resource_id){
- return mysql_num_rows($resource_id);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function insert_id() {
- return mysql_insert_id($this->link_id);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function free_result($resource_id){
- return mysql_free_result($resource_id);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function disconnect() {
- return mysql_close($this->link_id);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- // db_perform('users',array('username'=>'bigdog','password'='elliott'));
- function perform($table, $data, $action = 'insert', $parameters = '') {
- reset($data);
- if ($action == 'insert') {
- $query = 'INSERT INTO `'.$table.'` (';
- while (list($columns, ) = each($data)) {
- $query .= '`'.$columns.'`, ';
- }
- $query = rtrim($query, ', ').') values (';
- reset($data);
- while (list(, $value) = each($data)) {
- switch ((string)$value) {
- case 'now()':
- $query .= 'now(), ';
- break;
- case 'null':
- $query .= 'null, ';
- break;
- default:
- $query .= "'".Utilities::sanitize($value)."', ";
- break;
- }
- }
- $query = rtrim($query, ', ').')';
- } elseif ($action == 'update') {
- $query = 'UPDATE `'.$table.'` SET ';
- while (list($columns, $value) = each($data)) {
- switch ((string)$value) {
- case 'now()':
- $query .= '`' .$columns.'`=now(), ';
- break;
- case 'null':
- $query .= '`' .$columns .= '`=null, ';
- break;
- default:
- $query .= '`' .$columns."`='".Utilities::sanitize($value)."', ";
- break;
- }
- }
- $query = rtrim($query, ', ').' WHERE '.$parameters;
- }
- return $this->query($query);
- }
- /**
- * xxx
- *
- * @param xxx $xxx xxx
- * @return xxx
- * @access public
- */
- function not_null($value) {
- switch(gettype($value)){
- case 'boolean':
- case 'object':
- case 'resource':
- case 'integer':
- case 'double':
- return true;
- break;
- case 'string':
- if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)){
- return true;
- } else {
- return false;
- }
- break;
- case 'array':
- if (sizeof($value) > 0){
- return true;
- } else {
- return false;
- }
- break;
- case 'NULL':
- default:
- return false;
- break;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement