Advertisement
Tirus

MySQL adapter

Jun 4th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.12 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Skoldat Database Mysql driver
  5.  */
  6.  
  7.  if(!function_exists('sk_error')){
  8.      function sk_error($str){
  9.          die('<!DOCTYPE html>
  10. <html>
  11. <head>
  12. <meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT">
  13. <meta http-equiv="Pragma" content="no-cache">
  14. <meta http-equiv="Cache-Control" content="no-cache">
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <meta http-equiv="Lang" content="en">
  17. <meta name="author" content="">
  18. <meta http-equiv="Reply-to" content="@.com">
  19. <meta name="generator" content="PhpED 6.0">
  20. <meta name="description" content="">
  21. <meta name="keywords" content="">
  22. <meta name="creation-date" content="06/01/2011">
  23. <meta name="revisit-after" content="15 days">
  24. <title>Untitled</title>
  25. <link rel="stylesheet" type="text/css" href="my.css">
  26. </head>
  27. <body>
  28.  <h1>'.$str.'</h1>
  29. </body>
  30. </html>');
  31.      }
  32.  }
  33.  
  34. class SKDatabasemysql {
  35.  
  36.     var $connection = null;
  37.     var $in_transaction = false;
  38.     var $count_query = false;
  39.     var $last_query = false;
  40.     private $fetched_row = array();
  41.     protected $in_params = array();
  42.  
  43.     function __construct() {
  44.         if(!defined('SK_INT') || !defined('SK_VARCHAR') || !defined('SK_DATETIME') || !defined('SK_TIMESTAMP') || !defined('SK_DATE') || !defined('SK_TBNAME')){
  45.             sk_error('You are trying to use an undefined constant');
  46.         }
  47.         $this->count_query = 0;
  48.     }
  49.    
  50.     public function create_db($dbname){
  51.         if($this->is_connected()){
  52.             if(mysql_create_db($dbname,$this->connection)){
  53.                 return true;
  54.             }
  55.         }
  56.         return false;        
  57.     }
  58.  
  59.     public function connect($servername=null, $connection_info = array(),$charset = 'utf8') {
  60.         if($servername == null){
  61.             $servername = ini_get("mysql.default_host");
  62.         }
  63.         if(!isset($connection_info['UID']) || $connection_info['UID'] == null){
  64.             $connection_info['UID'] = ini_get("default_user");
  65.         }
  66.         if(!isset($connection_info['PWD']) || $connection_info['PWD'] == null){
  67.             $connection_info['PWD'] = ini_get("default_password");
  68.         }
  69.         if(!isset($connection_info['Database']) || $connection_info['Database'] == null){
  70.             return false;
  71.         }                                  
  72.        $this->connection = mysql_connect($servername,$connection_info['UID'],$connection_info['PWD']);
  73.        if($this->is_connected()){
  74.            if($this->set_database($connection_info['Database'])){
  75.                
  76.                $this->exec('SET CHARACTER SET '.$this->param('charset',SK_VARCHAR),array('charset'=>$charset));
  77.                return true;
  78.            }
  79.        }
  80.        $this->disconnect();
  81.        return false;
  82.     }
  83.    
  84.     public function truncate_tb($table_name){
  85.         if($this->is_connected()){
  86.             if($table_name != null){
  87.                 $c = $this->exec('TRUNCATE TABLE '.$this->param('table_name',SK_TBNAME),array('table_name'=>$table_name));
  88.                 if($c){
  89.                     $this->close_resultset($c);
  90.                     return true;
  91.                 }
  92.                 $this->close_resultset($c);
  93.             }              
  94.         }
  95.         return false;
  96.     }
  97.  
  98.     public function set_database($db_name) {
  99.         if($this->is_connected()){
  100.             if(mysql_select_db($db_name,$this->connection)){
  101.                 return true;
  102.             }
  103.         }
  104.         return false;
  105.     }
  106.    
  107.     public function is_connected() {
  108.         if($this->connection == null || $this->connection === false){
  109.             return false;
  110.         }
  111.         return mysql_ping($this->connection);    
  112.     }
  113.  
  114.     public function disconnect() {
  115.         if($this->is_connected()){
  116.             if(mysql_close($this->connection)){
  117.                 return true;
  118.             }
  119.         }
  120.         return false;
  121.     }
  122.  
  123.     public function fetch_row(&$cursor) {
  124.         if($this->is_connected()){
  125.             if($cursor != null){
  126.                 $this->fetched_row[$cursor] = mysql_fetch_array($cursor, MYSQL_ASSOC);  
  127.                 if ($this->fetched_row[$cursor] != null) {
  128.                     $this->fetched_row[$cursor] = array_change_key_case($this->fetched_row[$cursor], CASE_UPPER);
  129.                     return true;
  130.                 }            
  131.             }
  132.         }
  133.        
  134.         return false;                                                                                    
  135.     }
  136.    
  137.     public function fetch_value($cursor, $column_name) {
  138.         if (!$cursor) {
  139.             return false;
  140.         }
  141.         if (!isset($this->fetched_row[$cursor][strtoupper($column_name)])) {
  142.             return false;
  143.         }
  144.         return $this->fetched_row[$cursor][strtoupper($column_name)];
  145.     }
  146.    
  147.     public function exec($sql_statement, $param_array = null) {
  148.         if($param_array != null){
  149.             foreach($this->in_params as $key => $value){
  150.                 if(isset($param_array[$value['value']])){
  151.                     if($value['type'] == SK_INT || $value['type']==SK_VARCHAR || $value['type']==SK_TBNAME){
  152.                         $sql_statement = str_replace(':'.$value['value'].':',mysql_real_escape_string($param_array[$value['value']],$this->connection),$sql_statement);
  153.                     }else{
  154.                        /**
  155.                        * You must complete the operator types: TIMESTAMP, DATE, DATETIME
  156.                        */
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         $this->in_params = array();
  162.         $this->count_query++;
  163.         $this->last_query = $sql_statement;
  164.         return mysql_query($sql_statement);
  165.     }
  166.                
  167.     public function get_one_row(&$row, $sql_statement, $params = null) {
  168.         if($sql_statement != null){
  169.             $c = $this->exec($sql_statement,$params);
  170.             if($this->fetch_row($c)){
  171.                 $row = $this->fetched_row[$c];
  172.             }
  173.             $this->close_resultset($c);
  174.             if($row != null){
  175.                 return true;
  176.             }
  177.         }
  178.         return false;    
  179.     }
  180.  
  181.     public function param($key,$type = SK_VARCHAR) {
  182.        switch($type){
  183.            case SK_INT:
  184.            case SK_TBNAME:
  185.             $param = ':'.$key.':';
  186.            break;  
  187.            default:
  188.             $param = '":'.$key.':"';
  189.            break;          
  190.        }
  191.        $count = count($this->in_params);
  192.        $this->in_params[($count+1)]['value']=$key;
  193.        $this->in_params[($count+1)]['type']=$type;
  194.        return $param;
  195.     }
  196.    
  197.     public function num_rows($cursor) {
  198.         if($this->is_connected() && $cursor != null){
  199.             return mysql_num_rows($cursor);
  200.         }
  201.         return false;    
  202.     }
  203.  
  204.     public function data_seek($cursor, $row = 0) {
  205.         $count = $this->num_rows($cursor);
  206.         if($this->is_connected() && $row != null && ($count >= $row) == true){
  207.             return mysql_data_seek($cursor,$row);
  208.         }
  209.         else{
  210.             return false;
  211.         }
  212.     }
  213.  
  214.     public function evaluate_bolean_expresion($sql_statement, $param_array = null) {
  215.         $res = false;
  216.         if($this->is_connected()){
  217.             $c = $this->exec($sql_statement,$param_array);
  218.             if($this->num_rows($c)){
  219.                 $res = true;
  220.             }
  221.             $this->close_resultset($c);
  222.         }
  223.         return $res;
  224.     }
  225.  
  226.     public function close_resultset(&$cursor) {    
  227.        
  228.         if($this->is_connected()){
  229.             if($cursor != null){
  230.                 if(mysql_free_result($cursor)){
  231.                     if(isset($this->fetched_row[$cursor])){
  232.                         unset($this->fetched_row[$cursor]);
  233.                     }
  234.                     $cursor = null;
  235.                     return true;
  236.                 }
  237.             }
  238.         }        
  239.         return false;
  240.     }
  241.    
  242.     public function drop_database($db_name){
  243.         if($this->is_connected() && $db_name != null){
  244.             return mysql_drop_db($db_name,$this->connection);
  245.         }
  246.         return false;
  247.     }
  248.    
  249.     public function get_database_list(){
  250.         if($this->is_connected()){
  251.             $c = mysql_list_dbs($this->connection);
  252.             $db_list = null;
  253.             while ($row = mysql_fetch_object($c)) {
  254.                 $db_list[] = $row->Database;
  255.             }
  256.             $this->close_resultset($c);              
  257.             if($db_list != null){
  258.                 return $db_list;
  259.             }
  260.         }
  261.         return false;
  262.  
  263.     }
  264.    
  265.     public function get_table_list($db_name){
  266.         if($this->is_connected()){
  267.             $c = $this->exec('SHOW TABLES FROM '.$db_name);
  268.             $table_list = null;
  269.             while ($this->fetch_row($c)) {
  270.                 $table_list[] = $this->fetched_row[$c]['TABLES_IN_'.strtoupper($db_name)];
  271.             }  
  272.             $this->close_resultset($c);
  273.             if($table_list != null){
  274.                 return $table_list;
  275.             }
  276.         }
  277.         return false;
  278.  
  279.     }
  280.  
  281.     public function get_server_info() {
  282.         if($this->is_connected()){
  283.             return "MySQL server version: ".mysql_get_server_info($this->connection);
  284.         }
  285.         return false;      
  286.     }
  287.    
  288.     public function get_db_stats() {
  289.         if($this->is_connected()){
  290.             return explode('  ', mysql_stat($this->connection));
  291.         }
  292.         return false;
  293.     }
  294.  
  295.     public function get_client_info() {
  296.         if($this->is_connected()){
  297.             return "MySQL client info: ". mysql_get_client_info();
  298.         }
  299.        return false;
  300.     }
  301.    
  302.     public function get_host_info() {
  303.         if($this->is_connected()){
  304.             return "MySQL host info: ". mysql_get_host_info($this->connection);
  305.         }
  306.        return false;
  307.     }
  308.    
  309.     public function get_affected_row(){
  310.         if($this->is_connected()){
  311.             return mysql_affected_rows($this->connection);
  312.         }
  313.         return false;        
  314.     }
  315.    
  316.     function __destruct() {
  317.         if($this->is_connected()){
  318.             $this->disconnect();
  319.         }                      
  320.     }
  321.  
  322. }
  323.  
  324. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement