Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. <?php
  2. //
  3. // Manataria Website Platform - Eats the seaweeds
  4. //
  5. // Copyright Kenneth Elliott. All rights reserved.
  6. //
  7.  
  8. /**
  9.  * MySQL Wrapper
  10.  *
  11.  * This class contains a uniform wrapper for communicating with a MySQL database.
  12.  * Now build classes with the same interface for additional DBMS's ;)
  13.  *
  14.  * @author      Kenneth Elliott <kenelliottmc@gmail.com>
  15.  * @copyright   Copyright &copy; 2008 Kenneth Elliott <kenelliottmc@gmail.com>
  16.  * @category    Manataria
  17.  * @package     MySQL
  18.  * @since       Manataria 0.0.1
  19.  */
  20. class MySQL_Adapter {
  21.     var $db;
  22.     var $link_id;
  23.     var $error;
  24.  
  25.     /**
  26.     * xxx
  27.     *
  28.     * @param  xxx   $xxx    xxx
  29.     * @return xxx
  30.     * @access public
  31.     */
  32.     public function __construct($host = '', $user = '', $db = '', $pass = ''){
  33.         $host = $this->not_null($host) ? $host : DB_SERVER;
  34.         $user = $this->not_null($user) ? $user : DB_SERVER_USERNAME;
  35.         $pass = $this->not_null($pass) ? $pass : DB_SERVER_PASSWORD;
  36.         $this->db = $this->not_null($db) ? $db : DB_DATABASE;
  37.         $this->connect_db($host, $user, $pass) or die ('Jay sucks');
  38.         $this->select_db();
  39.         //$this->error = Registry::getKey('Error');
  40.     }
  41.  
  42.     /**
  43.     * xxx
  44.     *
  45.     * @param  xxx   $xxx    xxx
  46.     * @return xxx
  47.     * @access public
  48.     */
  49.     public function connect_db($host, $user, $pass){
  50.         $this->link_id = mysql_connect($host, $user, $pass);
  51.         return;
  52.     }
  53.  
  54.     /**
  55.     * xxx
  56.     *
  57.     * @param  xxx   $xxx    xxx
  58.     * @return xxx
  59.     * @access public
  60.     */
  61.     public function select_db(){
  62.         return mysql_select_db($this->db, $this->link_id);
  63.     }
  64.  
  65.     /**
  66.     * xxx
  67.     *
  68.     * @param  xxx   $xxx    xxx
  69.     * @return xxx
  70.     * @access public
  71.     */
  72.     public function query($query, $timer = true){
  73.         $resource = mysql_query($query, $this->link_id);
  74.         return $resource;
  75.     }
  76.  
  77.     /**
  78.     * xxx
  79.     *
  80.     * @param  xxx   $xxx    xxx
  81.     * @return xxx
  82.     * @access public
  83.     */
  84.     public function query_unbuffered($query, $timer = true){
  85.         $resource = mysql_unbuffered_query($query, $this->link_id);
  86.         if(!is_resource($resource)) return array();
  87.         $a = array();
  88.         while($r = mysql_fetch_assoc($resource)) $a[] = $r;
  89.         return $a;
  90.     }
  91.  
  92.     /**
  93.     * xxx
  94.     *
  95.     * @param  xxx   $xxx    xxx
  96.     * @return xxx
  97.     * @access public
  98.     */
  99.     function fetch_array($resource_id, $type = MYSQL_ASSOC){
  100.         return mysql_fetch_array($resource_id, $type);
  101.     }
  102.  
  103.     /**
  104.     * xxx
  105.     *
  106.     * @param  xxx   $xxx    xxx
  107.     * @return xxx
  108.     * @access public
  109.     */
  110.     function num_rows($resource_id){
  111.         return mysql_num_rows($resource_id);
  112.     }
  113.  
  114.     /**
  115.     * xxx
  116.     *
  117.     * @param  xxx   $xxx    xxx
  118.     * @return xxx
  119.     * @access public
  120.     */
  121.     function insert_id() {
  122.         return mysql_insert_id($this->link_id);
  123.     }
  124.  
  125.     /**
  126.     * xxx
  127.     *
  128.     * @param  xxx   $xxx    xxx
  129.     * @return xxx
  130.     * @access public
  131.     */
  132.     function free_result($resource_id){
  133.         return mysql_free_result($resource_id);
  134.     }
  135.  
  136.     /**
  137.     * xxx
  138.     *
  139.     * @param  xxx   $xxx    xxx
  140.     * @return xxx
  141.     * @access public
  142.     */
  143.     function disconnect() {
  144.         return mysql_close($this->link_id);
  145.     }
  146.  
  147.     /**
  148.     * xxx
  149.     *
  150.     * @param  xxx   $xxx    xxx
  151.     * @return xxx
  152.     * @access public
  153.     */
  154.     // db_perform('users',array('username'=>'bigdog','password'='elliott'));
  155.     function perform($table, $data, $action = 'insert', $parameters = '') {
  156.         reset($data);
  157.         if ($action == 'insert') {
  158.             $query = 'INSERT INTO `'.$table.'` (';
  159.             while (list($columns, ) = each($data)) {
  160.                 $query .= '`'.$columns.'`, ';
  161.             }
  162.             $query = rtrim($query, ', ').') values (';
  163.             reset($data);
  164.             while (list(, $value) = each($data)) {
  165.                 switch ((string)$value) {
  166.                     case 'now()':
  167.                         $query .= 'now(), ';
  168.                         break;
  169.                     case 'null':
  170.                         $query .= 'null, ';
  171.                         break;
  172.                     default:
  173.                         $query .= "'".Utilities::sanitize($value)."', ";
  174.                         break;
  175.                 }
  176.             }
  177.             $query = rtrim($query, ', ').')';
  178.         } elseif ($action == 'update') {
  179.             $query = 'UPDATE `'.$table.'` SET ';
  180.             while (list($columns, $value) = each($data)) {
  181.                 switch ((string)$value) {
  182.                     case 'now()':
  183.                         $query .= '`' .$columns.'`=now(), ';
  184.                         break;
  185.                     case 'null':
  186.                         $query .= '`' .$columns .= '`=null, ';
  187.                         break;
  188.                     default:
  189.                         $query .= '`' .$columns."`='".Utilities::sanitize($value)."', ";
  190.                         break;
  191.                 }
  192.             }
  193.             $query = rtrim($query, ', ').' WHERE '.$parameters;
  194.         }
  195.  
  196.         return $this->query($query);
  197.     }
  198.  
  199.     /**
  200.     * xxx
  201.     *
  202.     * @param  xxx   $xxx    xxx
  203.     * @return xxx
  204.     * @access public
  205.     */
  206.     function not_null($value) {
  207.         switch(gettype($value)){
  208.             case 'boolean':
  209.             case 'object':
  210.             case 'resource':
  211.             case 'integer':
  212.             case 'double':
  213.                 return true;
  214.                 break;
  215.             case 'string':
  216.                 if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)){
  217.                     return true;
  218.                 } else {
  219.                     return false;
  220.                 }
  221.                 break;
  222.             case 'array':
  223.                 if (sizeof($value) > 0){
  224.                     return true;
  225.                 } else {
  226.                     return false;
  227.                 }
  228.                 break;
  229.             case 'NULL':
  230.             default:
  231.                 return false;
  232.                 break;
  233.         }
  234.     }
  235.  
  236. }
  237.  
  238. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement