Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.90 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 ('Ken sucks');
  38.         $this->connect_db('localhost', 'xxxxxxx', 'xxxxxx') or die ('Ken sucks');
  39.         $this->select_db();
  40.         //$this->error = Registry::getKey('Error');
  41.     }
  42.  
  43.     /**
  44.     * xxx
  45.     *
  46.     * @param  xxx   $xxx    xxx
  47.     * @return xxx
  48.     * @access public
  49.     */
  50.     public function connect_db($host, $user, $pass){
  51.         $this->link_id = mysql_connect($host, $user, $pass);
  52.         return;
  53.     }
  54.  
  55.     /**
  56.     * xxx
  57.     *
  58.     * @param  xxx   $xxx    xxx
  59.     * @return xxx
  60.     * @access public
  61.     */
  62.     public function select_db(){
  63.         return mysql_select_db($this->db, $this->link_id);
  64.     }
  65.  
  66.     /**
  67.     * xxx
  68.     *
  69.     * @param  xxx   $xxx    xxx
  70.     * @return xxx
  71.     * @access public
  72.     */
  73.     public function query($query, $timer = true){
  74.         $resource = mysql_query($query, $this->link_id);
  75.         return $resource;
  76.     }
  77.  
  78.     /**
  79.     * xxx
  80.     *
  81.     * @param  xxx   $xxx    xxx
  82.     * @return xxx
  83.     * @access public
  84.     */
  85.     public function query_unbuffered($query, $timer = true){
  86.         $resource = mysql_unbuffered_query($query, $this->link_id);
  87.         if(!is_resource($resource)) return array();
  88.         $a = array();
  89.         while($r = mysql_fetch_assoc($resource)) $a[] = $r;
  90.         return $a;
  91.     }
  92.  
  93.     /**
  94.     * xxx
  95.     *
  96.     * @param  xxx   $xxx    xxx
  97.     * @return xxx
  98.     * @access public
  99.     */
  100.     function fetch_array($resource_id, $type = MYSQL_ASSOC){
  101.         return mysql_fetch_array($resource_id, $type);
  102.     }
  103.  
  104.     /**
  105.     * xxx
  106.     *
  107.     * @param  xxx   $xxx    xxx
  108.     * @return xxx
  109.     * @access public
  110.     */
  111.     function num_rows($resource_id){
  112.         return mysql_num_rows($resource_id);
  113.     }
  114.  
  115.     /**
  116.     * xxx
  117.     *
  118.     * @param  xxx   $xxx    xxx
  119.     * @return xxx
  120.     * @access public
  121.     */
  122.     function insert_id() {
  123.         return mysql_insert_id($this->link_id);
  124.     }
  125.  
  126.     /**
  127.     * xxx
  128.     *
  129.     * @param  xxx   $xxx    xxx
  130.     * @return xxx
  131.     * @access public
  132.     */
  133.     function free_result($resource_id){
  134.         return mysql_free_result($resource_id);
  135.     }
  136.  
  137.     /**
  138.     * xxx
  139.     *
  140.     * @param  xxx   $xxx    xxx
  141.     * @return xxx
  142.     * @access public
  143.     */
  144.     function disconnect() {
  145.         return mysql_close($this->link_id);
  146.     }
  147.  
  148.     /**
  149.     * xxx
  150.     *
  151.     * @param  xxx   $xxx    xxx
  152.     * @return xxx
  153.     * @access public
  154.     */
  155.     // db_perform('users',array('username'=>'bigdog','password'='elliott'));
  156.     function perform($table, $data, $action = 'insert', $parameters = '') {
  157.         reset($data);
  158.         if ($action == 'insert') {
  159.             $query = 'INSERT INTO `'.$table.'` (';
  160.             while (list($columns, ) = each($data)) {
  161.                 $query .= '`'.$columns.'`, ';
  162.             }
  163.             $query = rtrim($query, ', ').') values (';
  164.             reset($data);
  165.             while (list(, $value) = each($data)) {
  166.                 switch ((string)$value) {
  167.                     case 'now()':
  168.                         $query .= 'now(), ';
  169.                         break;
  170.                     case 'null':
  171.                         $query .= 'null, ';
  172.                         break;
  173.                     default:
  174.                         $query .= "'".Utilities::sanitize($value)."', ";
  175.                         break;
  176.                 }
  177.             }
  178.             $query = rtrim($query, ', ').')';
  179.         } elseif ($action == 'update') {
  180.             $query = 'UPDATE `'.$table.'` SET ';
  181.             while (list($columns, $value) = each($data)) {
  182.                 switch ((string)$value) {
  183.                     case 'now()':
  184.                         $query .= '`' .$columns.'`=now(), ';
  185.                         break;
  186.                     case 'null':
  187.                         $query .= '`' .$columns .= '`=null, ';
  188.                         break;
  189.                     default:
  190.                         $query .= '`' .$columns."`='".Utilities::sanitize($value)."', ";
  191.                         break;
  192.                 }
  193.             }
  194.             $query = rtrim($query, ', ').' WHERE '.$parameters;
  195.         }
  196.  
  197.         return $this->query($query);
  198.     }
  199.  
  200.     /**
  201.     * xxx
  202.     *
  203.     * @param  xxx   $xxx    xxx
  204.     * @return xxx
  205.     * @access public
  206.     */
  207.     function not_null($value) {
  208.         switch(gettype($value)){
  209.             case 'boolean':
  210.             case 'object':
  211.             case 'resource':
  212.             case 'integer':
  213.             case 'double':
  214.                 return true;
  215.                 break;
  216.             case 'string':
  217.                 if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)){
  218.                     return true;
  219.                 } else {
  220.                     return false;
  221.                 }
  222.                 break;
  223.             case 'array':
  224.                 if (sizeof($value) > 0){
  225.                     return true;
  226.                 } else {
  227.                     return false;
  228.                 }
  229.                 break;
  230.             case 'NULL':
  231.             default:
  232.                 return false;
  233.                 break;
  234.         }
  235.     }
  236.  
  237. }
  238.  
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement