Advertisement
JBHUTT09

Untitled

Aug 23rd, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. class Database {
  2.         const RETURN_QUERY = 0;
  3.         const RETURN_ROW_COUNT = 1;
  4.         const RETURN_LAST_INSERTED = 2;
  5.         const RETURN_ALL_ROWS = 3;
  6.        
  7.        
  8.         protected $available_connections = [];
  9.         public $connections = [];
  10.        
  11.         public function __construct( array $db_connections ) {
  12.             $this->available_connections = $db_connections;
  13.             return $this;
  14.         }
  15.        
  16.         /**
  17.          * Checks $connections for the specified index. If the index is not found, attempts to create a PDO instance using the provided settings and store it in $connections.
  18.          *
  19.          * @param string $connection_type The index of the desired connection config array passed to the constructor.
  20.          * @param bool $return (optional) If true, return the PDO object. If false, return bool true. Defaults to false.
  21.          * @return bool|PDO Returns bool false on failure. Success return type determined by $return param.
  22.          */
  23.         private function openLink( string $connection_type, $return=false ) {
  24.             if ( !isset( $this->connections[ $connection_type ] ) ) {
  25.                 if ( !isset( $this->available_connections[ $connection_type ] ) || !ArrayHelper::multiIsset( $this->available_connections[ $connection_type ], [ 'host', 'database', 'charset', 'user', 'pass' ] ) ) return false;
  26.                 try {
  27.                     $this->connections[ $connection_type ] = new PDO(
  28.                         'mysql:host='.$this->available_connections[ $connection_type ][ 'host' ].';dbname='.$this->available_connections[ $connection_type ][ 'database' ].';charset='.$this->available_connections[ $connection_type ][ 'charset' ],
  29.                         ''.$this->available_connections[ $connection_type ][ 'user' ].'',
  30.                         ''.$this->available_connections[ $connection_type ][ 'pass' ].''
  31.                     );
  32.                 }
  33.                 catch ( Exception $e ) {
  34.                     return false;
  35.                 }
  36.             }
  37.             return $return ? $this->connections[ $connection_type ] : true;
  38.         }
  39.        
  40.         public function closeLink( $connection_type ) {
  41.             if ( isset( $this->connections[ $connection_type ] ) ) {
  42.                 $this->connections[ $connection_type ] = NULL;
  43.             }
  44.         }
  45.        
  46.         public function query( string $connection_type, string $query_text, array $prepared = [], $return=self::RETURN_QUERY ) {
  47.             if ( $this->openLink( $connection_type ) === false ) return false;
  48.             $query = $this->connections[ $connection_type ]->prepare( $query_text );
  49.             if ( $query->execute( $prepared ) !== false ) {
  50.                 if ( $return === self::RETURN_QUERY ) return $query;
  51.                 elseif ( $return === self::RETURN_ROW_COUNT ) return $query->rowCount();
  52.                 elseif ( $return === self::RETURN_ALL_ROWS ) return $query->fetchAll( PDO::FETCH_ASSOC );
  53.                 elseif ( $return === self::RETURN_LAST_INSERTED ) return $this->connections[ $connection_type ]->lastInsertId();
  54.             }
  55.             return false;
  56.         }
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement