2016-02-02 23:53:08 +08:00
< ? php
2016-07-21 22:01:57 +08:00
namespace App\Services ;
2016-03-18 17:49:52 +08:00
2016-07-21 22:01:57 +08:00
use App\Exceptions\E ;
2016-03-18 17:49:52 +08:00
2016-07-21 22:01:57 +08:00
/**
* Light - weight database helper
*
* @ author < h @ prinzeugen . net >
*/
class Database
2016-02-02 23:53:08 +08:00
{
2016-07-21 22:01:57 +08:00
/**
* Instance of MySQLi
* @ var null
*/
2016-02-02 23:53:08 +08:00
private $connection = null ;
2016-07-21 22:01:57 +08:00
/**
* Table name to do operations in
* @ var string
*/
2016-04-02 22:53:55 +08:00
private $table_name = " " ;
2016-02-02 23:53:08 +08:00
2016-07-21 22:01:57 +08:00
/**
* Construct with table name and another config optionally
*
* @ param string $table_name
* @ param array $config
*/
2016-08-10 22:46:17 +08:00
function __construct ( $table_name = '' , $config = null , $no_prefix = false )
2016-07-28 12:01:00 +08:00
{
2016-08-10 22:46:17 +08:00
$config = is_null ( $config ) ? Config :: getDbConfig () : $config ;
2016-07-28 12:01:00 +08:00
@ $this -> connection = new \mysqli (
$config [ 'host' ],
$config [ 'username' ],
$config [ 'password' ],
$config [ 'database' ],
$config [ 'port' ]
);
if ( $this -> connection -> connect_error )
2016-08-10 22:46:17 +08:00
throw new E ( " Could not connect to MySQL database. Check your configuration: " .
2016-07-28 12:01:00 +08:00
$this -> connection -> connect_error , $this -> connection -> connect_errno , true );
2016-08-09 21:41:45 +08:00
$this -> connection -> query ( " SET names 'utf8' " );
2016-08-10 22:46:17 +08:00
$this -> table_name = $no_prefix ? $table_name : $config [ 'prefix' ] . $table_name ;
2016-03-26 19:05:09 +08:00
}
2016-03-18 17:54:19 +08:00
2016-07-28 12:01:00 +08:00
public function query ( $sql )
{
2016-02-02 23:53:08 +08:00
$result = $this -> connection -> query ( $sql );
2016-04-02 22:53:55 +08:00
if ( $this -> connection -> error )
throw new E ( " Database query error: " . $this -> connection -> error . " , Statement: " . $sql , - 1 );
return $result ;
2016-02-02 23:53:08 +08:00
}
2016-07-28 12:01:00 +08:00
public function fetchArray ( $sql )
{
2016-02-02 23:53:08 +08:00
return $this -> query ( $sql ) -> fetch_array ();
}
2016-04-02 22:53:55 +08:00
/**
* Select records from table
*
* @ param string $key
* @ param string $value
* @ param array $condition , see function `where`
* @ param string $table , which table to operate
* @ param boolean $dont_fetch_array , return resources if true
* @ return array | resources
*/
2016-07-28 12:01:00 +08:00
public function select ( $key , $value , $condition = null , $table = null , $dont_fetch_array = false )
{
2016-04-02 22:53:55 +08:00
$table = is_null ( $table ) ? $this -> table_name : $table ;
if ( isset ( $condition [ 'where' ])) {
$sql = " SELECT * FROM $table " . $this -> where ( $condition );
} else {
$sql = " SELECT * FROM $table WHERE $key =' $value ' " ;
}
if ( $dont_fetch_array ) {
return $this -> query ( $sql );
} else {
return $this -> fetchArray ( $sql );
}
2016-02-02 23:53:08 +08:00
}
2016-07-28 12:01:00 +08:00
public function insert ( $data , $table = null )
{
2016-04-02 22:53:55 +08:00
$keys = " " ;
$values = " " ;
$table = is_null ( $table ) ? $this -> table_name : $table ;
foreach ( $data as $key => $value ) {
if ( $value == end ( $data )) {
$keys .= '`' . $key . '`' ;
$values .= '"' . $value . '"' ;
} else {
$keys .= '`' . $key . '`,' ;
$values .= '"' . $value . '", ' ;
}
}
$sql = " INSERT INTO $table ( { $keys } ) VALUES ( $values ) " ;
return $this -> query ( $sql );
2016-03-06 14:12:12 +08:00
}
2016-08-10 22:46:17 +08:00
public function has ( $key , $value , $table = null )
{
return ( $this -> getNumRows ( $key , $value , $table ) != 0 ) ? true : false ;
}
public function hasTable ( $table_name )
{
$sql = " SELECT table_name FROM `INFORMATION_SCHEMA`.`TABLES` WHERE (table_name = ' $table_name ') AND TABLE_SCHEMA=' " . Config :: getDbConfig ()[ 'database' ] . " ' " ;
return ( $this -> query ( $sql ) -> num_rows != 0 ) ? true : false ;
}
2016-07-28 12:01:00 +08:00
public function update ( $key , $value , $condition = null , $table = null )
{
2016-04-02 22:53:55 +08:00
$table = is_null ( $table ) ? $this -> table_name : $table ;
return $this -> query ( " UPDATE $table SET ` $key `=' $value ' " . $this -> where ( $condition ));
2016-02-02 23:53:08 +08:00
}
2016-07-28 12:01:00 +08:00
public function delete ( $condition = null , $table = null )
{
2016-04-02 22:53:55 +08:00
$table = is_null ( $table ) ? $this -> table_name : $table ;
return $this -> query ( " DELETE FROM $table " . $this -> where ( $condition ));
2016-02-02 23:53:08 +08:00
}
2016-07-28 12:01:00 +08:00
public function getNumRows ( $key , $value , $table = null )
{
2016-04-02 22:53:55 +08:00
$table = is_null ( $table ) ? $this -> table_name : $table ;
$sql = " SELECT * FROM $table WHERE $key =' $value ' " ;
return $this -> query ( $sql ) -> num_rows ;
2016-02-02 23:53:08 +08:00
}
2016-07-28 12:01:00 +08:00
public function getRecordNum ( $table = null )
{
2016-04-02 22:53:55 +08:00
$table = is_null ( $table ) ? $this -> table_name : $table ;
$sql = " SELECT * FROM $table WHERE 1 " ;
return $this -> query ( $sql ) -> num_rows ;
2016-02-02 23:53:08 +08:00
}
2016-04-02 22:53:55 +08:00
/**
* Generate where statement
*
* @ param array $condition , e . g . array ( 'where' => 'username="shit"' , 'limit' => 10 , 'order' => 'uid' )
* @ return string
*/
2016-07-28 12:01:00 +08:00
private function where ( $condition )
{
2016-04-02 22:53:55 +08:00
$statement = " " ;
if ( isset ( $condition [ 'where' ]) && $condition [ 'where' ] != " " ) {
$statement .= ' WHERE ' . $condition [ 'where' ];
}
if ( isset ( $condition [ 'order' ])) {
$statement .= ' ORDER BY `' . $condition [ 'order' ] . '`' ;
}
if ( isset ( $condition [ 'limit' ])) {
$statement .= ' LIMIT ' . $condition [ 'limit' ];
}
return $statement ;
}
2016-07-28 12:01:00 +08:00
function __destruct ()
{
2016-04-11 17:11:19 +08:00
if ( ! is_null ( $this -> connection ))
$this -> connection -> close ();
2016-03-13 15:38:34 +08:00
}
2016-02-02 23:53:08 +08:00
}