2008-12-02 09:03:33 +00:00
< ? php
2008-12-05 13:32:38 +00:00
/**
* Класс модели данных
*
2008-12-09 05:58:08 +00:00
* @ copyright
* @ link
2008-12-05 13:32:38 +00:00
* @ package Majestic
* @ subpackage DB
2008-12-09 05:58:08 +00:00
* @ since
2008-12-05 13:32:38 +00:00
* @ version SVN : $Id $
* @ filesource $URL $
2008-12-02 09:03:33 +00:00
*/
abstract class Model
{
private $handler ;
protected $table = false ;
protected $primary_key = 'id' ;
function __construct ()
{
$this -> handler = DBConnector :: getConnect ( Env :: getParam ( 'db_settings' ));
}
/**
* Выполняет запрос и возвращает сырой результат
*
* @ param string $sql
* @ return resource
*/
function exec ( $sql )
{
$time = microtime ( true );
$res = mysqli_query ( $this -> handler , $sql );
if ( mysqli_errno ( $this -> handler )) {
throw new MJException ( " <b>Query Error:</b> \n " . $sql . " \n <b>Error:</b> \n " . mysqli_error ( $this -> handler ), 1 );
}
if ( DEBUG_ENABLE ) {
DBConnector :: $queries [] = $sql . '; (' . round (( microtime ( true ) - $time ) * 1000 , 1 ) . 'ms)' ;
}
return $res ;
}
/**
* Выполняет запрос и возвращает объект результата
*
* @ param string $sql
* @ return object
*/
function query ( $sql )
{
$res = $this -> exec ( $sql );
switch ( strtolower ( substr ( $sql , 0 , 6 ))) {
case 'select' :
2008-12-09 13:19:13 +00:00
case '(selec' :
2008-12-02 09:03:33 +00:00
return new ModelSelectResult ( $res );
case 'insert' :
2008-12-16 08:13:54 +00:00
case 'replac' :
2008-12-02 09:03:33 +00:00
return new ModelInsertResult ( $this -> handler );
default :
return new ModelChangeResult ( $this -> handler );
}
}
/**
* Экранирует строку
*
* @ param string $data - строка для экранирования
* @ return string
*/
function escape ( $data )
{
return mysqli_real_escape_string ( $this -> handler , $data );
}
//////////////////////////
function update ( $id , $data )
{
$sql = '' ;
foreach ( $data as $key => $val ) {
$sql .= $key . " =' " . $this -> escape ( $val ) . " ', " ;
}
return $this -> query ( 'UPDATE ' . $this -> table . ' SET ' . rtrim ( $sql , ', ' ) . ' WHERE ' . $this -> primary_key . '=' . ( int ) $id );
}
function insert ( $data , $postfix = '' )
{
$sql = '' ;
foreach ( $data as $key => $val ) {
$sql .= $key . '="' . $this -> escape ( $val ) . '", ' ;
}
return $this -> query ( 'INSERT ' . $this -> table . ' SET ' . rtrim ( $sql , ', ' ) . ' ' . $postfix );
}
function delete ( $id )
{
return $this -> query ( 'DELETE FROM ' . $this -> table . ' WHERE ' . $this -> primary_key . '=' . ( int ) $id );
}
function get ( $id )
{
return $this -> query ( 'SELECT * FROM ' . $this -> table . ' WHERE ' . $this -> primary_key . '=' . ( int ) $id );
}
function getList ( $limit = false , $sort = 'ASC' )
{
return $this -> query ( 'SELECT * FROM ' . $this -> table . ' ORDER BY ' . $this -> primary_key . ' ' . ( $sort == 'ASC' ? 'ASC' : 'DESC' ) . ( $limit === false ? ' LIMIT ' . ( int ) $limit : '' ));
}
}
class ModelResult
{
function __call ( $name , $args )
{
throw new MJException ( 'Call undeclared method "' . $name . '" in "' . get_class ( $this ) . '" class' , - 1 );
}
}
class ModelSelectResult extends ModelResult
{
public $result ;
function __construct ( $res )
{
$this -> result = $res ;
}
2008-12-09 05:58:08 +00:00
function fetch ( $class_name = false )
2008-12-02 09:03:33 +00:00
{
2008-12-09 05:58:08 +00:00
return $class_name ? mysqli_fetch_object ( $this -> result , $class_name ) : mysqli_fetch_object ( $this -> result );
2008-12-02 09:03:33 +00:00
}
function fetchField ( $field , $default = false )
{
$row = $this -> fetch ();
return isset ( $row -> $field ) ? $row -> $field : $default ;
}
2008-12-03 11:22:33 +00:00
function fetchAll ( $key = false )
2008-12-02 09:03:33 +00:00
{
$array = array ();
2008-12-03 11:22:33 +00:00
if ( $key ) {
while ( $row = mysqli_fetch_object ( $this -> result )) {
$array [ $row -> $key ] = $row ;
}
} else {
while ( $row = mysqli_fetch_object ( $this -> result )) {
$array [] = $row ;
}
2008-12-02 09:03:33 +00:00
}
return $array ;
}
function count ()
{
return mysqli_num_rows ( $this -> result );
}
function free ()
{
mysqli_free_result ( $this -> result );
}
function __destruct () {
$this -> free ();
}
}
class ModelChangeResult extends ModelResult
{
public $affected ;
function __construct ( $resource )
{
2008-12-03 11:22:33 +00:00
$this -> affected = mysqli_affected_rows ( $resource );
2008-12-02 09:03:33 +00:00
}
function count ()
{
return $this -> affected ;
}
}
2008-12-17 13:29:49 +00:00
class ModelInsertResult extends ModelChangeResult
2008-12-02 09:03:33 +00:00
{
public $id ;
function __construct ( $resource )
{
2008-12-17 13:29:49 +00:00
parent :: __construct ( $resource );
2008-12-02 09:03:33 +00:00
$this -> id = mysqli_insert_id ( $resource );
}
function getId ()
{
return $this -> id ;
}
}
?>