PHP - MySQL класс оболочки совет - PullRequest
0 голосов
/ 04 марта 2011

Я пишу класс-оболочку MySQL для:

a.) Замените текущий b.) Упростите жизнь с точки зрения пользовательской функциональности c.) Изучите и улучшите

IЯ ищу советы о том, как улучшить мои методы и стиль кодирования, поэтому я был бы признателен за любой вклад, который вы могли бы предоставить.

ОТКАЗ ОТ ОТВЕТСТВЕННОСТИ: я знаю, что есть другие методы абстракции базы данных, такие как PDO, но я хотел написать свой собственный по вышеуказанным причинам

Спасибо, Ли


TestЗвоните

#!/usr/bin/php
<?php

include('mysql.class.php');

$_mysql = new mysqli_ls( array( 'debug_log' => TRUE, 'query_log' => TRUE ) );

if ($_mysql->connect('host','user','password','db') === FALSE) print_r( $_mysql->get_error() );
else print "#1 connected\n";

if ($_mysql->set_database('auth_tracker_test') === FALSE) print_r( $_mysql->get_error() );
else print "#1 database changed\n";

/// Execute standard query
$sql = "SELECT * from user";

if ($_mysql->SQLQuery($sql) === TRUE) print "#1 SELECT Query worked\n";
else print print_r( $_mysql->get_error() );


#print_r($_mysql->getArray());
print_r($_mysql->getRow());

$_mysql->disconnect();

?>






<?php

class mysqli_ls
{

/**************************************************************************/
/*             SETUP VARIABLES                                            */
/**************************************************************************/

private $E_OK    = TRUE;
private $E_ERROR = FALSE;

private $db_host; 
private $db_user;
private $db_port;
private $db_name;
private $db_pass;


private $result;
private $link = FALSE;
private $errorArr  = array();

private $config = array(  // Location of exisiting 
                          'config_load' => FALSE, 
                          'config_path' => 'database.cfg.php',

                          // Record errors to a file
                          'debug_log'      => FALSE,
                          'debug_log_path' => '/tmp/mysql.debug.log',

                          // Record queries to a file
                          'query_log'      => FALSE,
                          'query_log_path' => '/tmp/mysql.debug.log' );

private $fh_debug  = FALSE;
private $fh_query  = FALSE;
private $fh_config = FALSE;

/**************************************************************************/
/*             MAGIC FUNCTIONS                                            */
/**************************************************************************/

public function __construct( $config = '' )
{
   // Config vars
   if ( !empty($config) && is_array($config) ) $this->set_config($config);

   // Open file handles if logs are required
   // Debug Log
   if ($this->config['debug_log'] === TRUE) 
   {
      if (! $this->fh_debug = fopen($this->config['debug_log_path'], 'a') )
      {
         $this->handle_error('#01A', 'could not open debug log');
         return $this->E_ERROR;
      }
   } 

   // Query Log
   if ($this->config['query_log'] === TRUE) 
   {
      if (! $this->fh_query = fopen($this->config['query_log_path'], 'a') )
      {
         $this->handle_error('#01B', 'could not open query log');
         return $this->E_ERROR;
      }
   }

   // Check mysqli functions are available
   if (!function_exists('mysqli_connect')) 
   { 
      $this->handle_error('#01C', 'mysqli not installed');
      return $this->E_ERROR;
   }

   return $this->E_OK;
} 

public function __deconstruct()
{
   if ($this->link) $this->disconnect();
   return $this->E_OK;
} 

/**************************************************************************/
/*             CONNECTION MANAGEMENT                                      */
/**************************************************************************/

public function connect($db_host='', $db_user='', $db_pass='', $db_name, $db_port='3306')
{
   if (empty($db_host) || empty($db_user) || empty($db_pass) || empty($db_name) || empty($db_port))
   {
      $this->handle_error('#02A', 'Missing connection variables');
      return $this->E_ERROR;
   }

   $this->db_host = $db_host;
   $this->db_user = $db_user;
   $this->db_pass = $db_pass;
   $this->db_name = $db_name;
   $this->db_port = $db_port;

   $this->link = @new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name, $this->db_port);
   if (mysqli_connect_error($this->link))
   {
      $this->handle_error(mysqli_connect_errno($this->link), mysqli_connect_error($this->link));
      return $this->E_ERROR;
   }

   return $this->E_OK;
}

public function disconnect()
{
   if ($this->link)
   {
      if ($this->link->close() === TRUE) return $this->E_OK;
      else
      {
         $this->handle_error($this->link->errno, $this->link->error);
         return $this->E_ERROR;
      }      
   }

   $this->handle_error('#03A','no activate database connection');
   return $this->E_ERROR;
}

public function connect_existing()
{
}

public function set_database($database)
{
   if ( $this->link->select_db($database) === FALSE ) 
   {
      $this->handle_error($this->link->errno, $this->link->error);
      return $this->E_ERROR;   
   }

   $this->E_OK;
}

/**************************************************************************/
/*             SQL INTERFACE                                              */
/**************************************************************************/

public function insert()
{
}

public function update()
{
}

public function delete()
{
}

public function select()
{
}

public function query($sql)
{
   // If the result set has cleaned up, do so before a new query
   if ($this->result) $this->result->close();  

   // Record query
   if ($this->config['query_log'] === TRUE) $this->write_log('query', $sql);

   if ($result = $this->link->query($sql));
   {
       $this->result = $result;
       return $this->E_OK; 
   }

   // Clean up the result set
   $result->close();

   // Query failed, handle error
   $this->handle_error($this->link->errno, $this->link->error);
   return $this->E_ERROR;
}

/**************************************************************************/
/*             RESULT FUNCTIONS                                           */
/**************************************************************************/

public function getArray($type = 'assoc')
{
   switch($type)
   {
      case 'num':
         $type = MYSQLI_NUM;
      break;

      case 'assoc':
         $type = MYSQLI_ASSOC;
      break;

      case 'both':
         $type = MYSQLI_BOTH;
      break;

      default:
         $this->handle_error('#12A','invalid field type. Options are include num, assoc, both');
         return $this->E_ERROR;
      break;
   }

   $resultArr = array();
   while( $row = $this->result->fetch_array( $type ) )
   {
      $resultArr[] = $row;
   }

   return $resultArr;
}

public function getRow($type = 'assoc')
{
   switch($type)
   {
      case 'num':
         $type = MYSQLI_NUM;
      break;

      case 'assoc':
         $type = MYSQLI_ASSOC;
      break;

      case 'both':
         $type = MYSQLI_BOTH;
      break;

      default:
         $this->handle_error('#13A','invalid field type. Options are include num, assoc, both');
         return $this->E_ERROR;
      break;
   }

   return $this->result->fetch_array( $type );
}

public function num_row()
{
   return $this->result->num_rows;
}

public function insert_id()
{
   return $this->link->insert_id;
}

public function affected_rows()
{
   return $this->link->affected_rows;
}

/**************************************************************************/
/*             LEGACY SUPPORT                                             */
/**************************************************************************/

public function SQLQuery($sql='')
{
   if (empty($sql))
   {
      $this->handle_error('#19A','missing query string');
      return $this->E_ERROR;
   }

   // Check for a select statement
   if ( preg_match("/^select/i",$sql) === 0)
   {
      $this->handle_error('#19A','incorrect query type, SELECT expected');
      return $this->E_ERROR;         
   }

   // Execute query
   if ($this->query($sql) === $this->E_ERROR) return $this->E_ERROR;

   // Return number of rows
   return $this->num_row();
}

public function SQLModify($sql='')
{
   if (empty($sql))
   {
      $this->handle_error('#19A','missing query string');
      return $this->E_ERROR;
   }

   // Execute query
   if ($this->query($sql) === $this->E_ERROR) return $this->E_ERROR;

   // Return affected rows
   $this->affected_rows();
}

public function numRow()
{
   return $this->num_row();
}

/**************************************************************************/
/*             LOGGING AND DEBUGGING                                      */
/**************************************************************************/

private function write_log($type, $msg)
{
   $msg = date('Y-m-d H:i:s') ."\t". $type ."\t". $msg ."\n";

   switch($type)
   {
      case 'error':
         fwrite($this->fh_debug, $msg);
      break;

      case 'query':
         fwrite($this->fh_query, $msg);
      break;

      default:
         return $this->E_ERROR;
      break;      
   }
}

private function handle_error($errormsg, $errorno)
{
   $this->errorArr[] = array( 'code'  => $errorno,
                              'error' => $errormsg ); 

   if ($this->config['debug_log'] === TRUE)
   {
      $msg = "($errorno) $errormsg";                              
      $this->write_log('error', $msg);
   }

   return $this->E_OK;
}

public function get_error($type = 'string')
{
   switch($string)
   {
      case 'string':
         $error = end($this->errorArr);
         return $error['error'] .' ('. $error['code'] .')';
         break;

      case 'array':
         return end($this->errorArr);
         break;
   }

   return false;  
}

/**************************************************************************/
/*             SET CONFIG VARS                                            */
/**************************************************************************/

public function set_config($config)
{
   foreach ($config as $key => &$value)
   {
      if ( ! isset($this->config[$key]) )
      {
         $this->handle_error('#19A','invalid field type');
         return $this->E_ERROR;
      }

      $this->config[$key] = $value;
   }

   return $this->E_OK;
}

/**************************************************************************/

} // Class END

?>

Ответы [ 2 ]

1 голос
/ 04 марта 2011

Я сам создал один раз и добавил другой класс с именем MySqlTable, который представлял собой таблицу. Я вернул его в функцию __GET, чтобы вы могли вызывать таблицу с

$sql->tablename->select();

Вот код для функции __get:

function __GET($name)
{
      return new MySqlTable($this, $name);
}

Класс был такой:

class MySqlTable
{
    private $table;
    private $mySql;

    function MySqlTable(&$oMySql, $sTable)
    {
      $this->mySql = $oMySql;
      $this->table = $sTable;
    }

    function &select($sWhere = '')
    {        
      if (empty($sWhere))
      {
        $data = $this->mySql->query("SELECT * FROM " . $this->table);
      }
      else
      {
        $data = $this->mySql->query("SELECT * FROM " . $this->table . " WHERE " . $sWhere);
      }

      return $this->mySql->resultToArray($data);
  }
}
1 голос
/ 04 марта 2011

В настоящее время ваш класс mysqli_ls содержит результат запроса. Это делает невозможным выполнение двух запросов и использование результатов первого запроса после выполнения второго запроса.

Лучший способ - позволить методу SQLQuery () возвращать объект результата , который содержит дескриптор результата и методы для извлечения строк из результата.

...