Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TConnection.php

Documentation is available at TConnection.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use PDO;
  6. use Exception;
  7.  
  8. /**
  9.  * Singleton manager for database connections
  10.  *
  11.  * @version    7.4
  12.  * @package    database
  13.  * @author     Pablo Dall'Oglio
  14.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  15.  * @license    http://www.adianti.com.br/framework-license
  16.  */
  17. {
  18.     private static $config_path;
  19.     private static $conn_cache;
  20.     
  21.     /**
  22.      * Class Constructor
  23.      * There'll be no instances of this class
  24.      */
  25.     private function __construct({}
  26.     
  27.     /**
  28.      * Opens a database connection
  29.      * 
  30.      * @param $database Name of the database (an INI file).
  31.      * @return          PDO object if the $database exist,
  32.      *                   otherwise, throws an exception
  33.      * @exception       Exception
  34.      *                   if the $database is not found
  35.      */
  36.     public static function open($database)
  37.     {
  38.         $dbinfo self::getDatabaseInfo($database);
  39.         
  40.         if (!$dbinfo)
  41.         {
  42.             // if the database doesn't exists, throws an exception
  43.             throw new Exception(AdiantiCoreTranslator::translate('File not found'': ' ."'{$database}.ini'");
  44.         }
  45.         
  46.         return self::openArray$dbinfo );
  47.     }
  48.     
  49.     /**
  50.      * Change database configuration Path
  51.      * 
  52.      * @param $path Config path
  53.      */
  54.     public static function setConfigPath($path)
  55.     {
  56.         self::$config_path $path;
  57.     }
  58.     
  59.     /**
  60.      * Opens a database connection from array with db info
  61.      * 
  62.      * @param $db Array with database info
  63.      * @return          PDO object
  64.      */
  65.     public static function openArray($db)
  66.     {
  67.         // read the database properties
  68.         $user  = isset($db['user']$db['user'NULL;
  69.         $pass  = isset($db['pass']$db['pass'NULL;
  70.         $name  = isset($db['name']$db['name'NULL;
  71.         $host  = isset($db['host']$db['host'NULL;
  72.         $type  = isset($db['type']$db['type'NULL;
  73.         $port  = isset($db['port']$db['port'NULL;
  74.         $char  = isset($db['char']$db['char'NULL;
  75.         $flow  = isset($db['flow']$db['flow'NULL;
  76.         $fkey  = isset($db['fkey']$db['fkey'NULL;
  77.         $zone  = isset($db['zone']$db['zone'NULL;
  78.         $type  strtolower($type);
  79.         
  80.         // each database driver has a different instantiation process
  81.         switch ($type)
  82.         {
  83.             case 'pgsql':
  84.                 $port $port $port '5432';
  85.                 $conn new PDO("pgsql:dbname={$name};user={$user}; password={$pass};host=$host;port={$port}");
  86.                 if(!empty($char))
  87.                 {
  88.                     $conn->exec("SET CLIENT_ENCODING TO '{$char}';");
  89.                 }
  90.                 break;
  91.             case 'mysql':
  92.                 $port $port $port '3306';
  93.                 if ($char == 'ISO')
  94.                 {
  95.                     $options array();
  96.  
  97.                     if ($zone)
  98.                     {
  99.                         $options array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '{$zone}'");
  100.                     }
  101.  
  102.                     $conn new PDO("mysql:host={$host};port={$port};dbname={$name}"$user$pass$options);
  103.                 }
  104.                 elseif ($char == 'utf8mb4')
  105.                 {
  106.                     $zone $zone ";SET time_zone = '{$zone}'"";
  107.                     $conn new PDO("mysql:host={$host};port={$port};dbname={$name}"$user$passarray(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4{$zone}"));
  108.                 }
  109.                 else
  110.                 {
  111.                     $zone $zone ";SET time_zone = '{$zone}'"";
  112.                     $conn new PDO("mysql:host={$host};port={$port};dbname={$name}"$user$passarray(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8{$zone}"));
  113.                 }
  114.                 break;
  115.             case 'sqlite':
  116.                 $conn new PDO("sqlite:{$name}");
  117.                 if (is_null($fkeyOR $fkey == '1')
  118.                 {
  119.                     $conn->query('PRAGMA foreign_keys = ON')// referential integrity must be enabled
  120.                 }
  121.                 break;
  122.             case 'ibase':
  123.             case 'fbird':
  124.                 $db_string empty($port"{$host}:{$name}"{$host}/{$port}:{$name}";
  125.                 $charset $char ";charset={$char}'';
  126.                 $conn new PDO("firebird:dbname={$db_string}{$charset}"$user$pass);
  127.                 $conn->setAttributePDO::ATTR_AUTOCOMMIT0);
  128.                 break;
  129.             case 'oracle':
  130.                 $port    $port $port '1521';
  131.                 $charset $char ";charset={$char}'';
  132.                 $tns     = isset($db['tns']$db['tns'NULL;
  133.                 
  134.                 if ($tns)
  135.                 {
  136.                     $conn new PDO("oci:dbname={$tns}{$charset}"$user$pass);
  137.                 }
  138.                 else
  139.                 {
  140.                     $conn new PDO("oci:dbname={$host}:{$port}/{$name}{$charset}"$user$pass);
  141.                 }
  142.                 
  143.                 if (isset($db['date']))
  144.                 {
  145.                     $date $db['date'];
  146.                     $conn->query("ALTER SESSION SET NLS_DATE_FORMAT = '{$date}'");
  147.                 }
  148.                 if (isset($db['time']))
  149.                 {
  150.                     $time $db['time'];
  151.                     $conn->query("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = '{$time}'");
  152.                 }
  153.                 if (isset($db['nsep']))
  154.                 {
  155.                     $nsep $db['nsep'];
  156.                     $conn->query("ALTER SESSION SET NLS_NUMERIC_CHARACTERS = '{$nsep}'");
  157.                 }
  158.                 break;
  159.             case 'mssql':
  160.                 if (OS == 'WIN')
  161.                 {
  162.                     if ($port)
  163.                     {
  164.                         $conn new PDO("sqlsrv:Server={$host},{$port};Database={$name}"$user$pass);
  165.                     }
  166.                     else
  167.                     {
  168.                         $conn new PDO("sqlsrv:Server={$host};Database={$name}"$user$pass);
  169.                     }
  170.                 }
  171.                 else
  172.                 {
  173.                     $charset $char ";charset={$char}'';
  174.                     
  175.                     if ($port)
  176.                     {
  177.                         $conn new PDO("dblib:host={$host}:{$port};dbname={$name}{$charset}"$user$pass);
  178.                     }
  179.                     else
  180.                     {
  181.                         $conn new PDO("dblib:host={$host};dbname={$name}{$charset}"$user$pass);
  182.                     }
  183.                 }
  184.                 break;
  185.             case 'dblib':
  186.                 $charset $char ";charset={$char}'';
  187.                 
  188.                 if ($port)
  189.                 {
  190.                     $conn new PDO("dblib:host={$host}:{$port};dbname={$name}{$charset}"$user$pass);
  191.                 }
  192.                 else
  193.                 {
  194.                     $conn new PDO("dblib:host={$host};dbname={$name}{$charset}"$user$pass);
  195.                 }
  196.                 break;
  197.             case 'sqlsrv':
  198.                 if ($port)
  199.                 {
  200.                     $conn new PDO("sqlsrv:Server={$host},{$port};Database={$name}"$user$pass);
  201.                 }
  202.                 else
  203.                 {
  204.                     $conn new PDO("sqlsrv:Server={$host};Database={$name}"$user$pass);
  205.                 }
  206.                 if (!empty($db['ntyp']))
  207.                 {
  208.                     $conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPEtrue);
  209.                 }
  210.                 break;
  211.             default:
  212.                 throw new Exception(AdiantiCoreTranslator::translate('Driver not found'': ' $type);
  213.                 break;
  214.         }
  215.         
  216.         // define wich way will be used to report errors (EXCEPTION)
  217.         $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  218.         
  219.         if ($flow == '1')
  220.         {
  221.             $conn->setAttribute(PDO::ATTR_CASEPDO::CASE_LOWER);
  222.         }
  223.         
  224.         // return the PDO object
  225.         return $conn;
  226.     }
  227.     
  228.     /**
  229.      * Returns the database information as an array
  230.      * @param $database INI File
  231.      */
  232.     public static function getDatabaseInfo($database)
  233.     {
  234.         $path  empty(self::$config_path'app/config' self::$config_path;
  235.         $filei "{$path}/{$database}.ini";
  236.         $filep "{$path}/{$database}.php";
  237.         
  238.         if (!empty(self::$conn_cache$database ]))
  239.         {
  240.             return self::$conn_cache$database ];
  241.         }
  242.         
  243.         // check if the database configuration file exists
  244.         if (file_exists($filei))
  245.         {
  246.             // read the INI and retuns an array
  247.             $ini parse_ini_file($filei);
  248.             self::$conn_cache$database $ini;
  249.             return $ini;
  250.         }
  251.         else if (file_exists($filep))
  252.         {
  253.             $ini = require $filep;
  254.             self::$conn_cache$database $ini;
  255.             return $ini;
  256.         }
  257.         else
  258.         {
  259.             return FALSE;
  260.         }
  261.     }
  262.     
  263.     /**
  264.      * Set database info
  265.      * @param $database Database name
  266.      * @param $info Database connection information
  267.      */
  268.     public static function setDatabaseInfo($database$info)
  269.     {
  270.         self::$conn_cache$database $info;
  271.     }
  272. }