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 TTransaction.php

Documentation is available at TTransaction.php

  1. <?php
  2. namespace Adianti\Database;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use Adianti\Database\TConnection;
  6. use Adianti\Log\TLogger;
  7. use Adianti\Log\TLoggerSTD;
  8. use Adianti\Log\TLoggerTXT;
  9. use Adianti\Log\AdiantiLoggerInterface;
  10.  
  11. use PDO;
  12. use Closure;
  13. use Exception;
  14.  
  15. /**
  16.  * Manage Database transactions
  17.  *
  18.  * @version    7.4
  19.  * @package    database
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. {
  25.     private static $conn;     // active connection
  26.     private static $logger;   // Logger object
  27.     private static $database// database name
  28.     private static $dbinfo;   // database info
  29.     private static $counter;
  30.     private static $uniqid;
  31.     
  32.     /**
  33.      * Class Constructor
  34.      * There won't be instances of this class
  35.      */
  36.     private function __construct(){}
  37.     
  38.     /**
  39.      * Open a connection and Initiates a transaction
  40.      * @param $database Name of the database (an INI file).
  41.      * @param $dbinfo Optional array with database information
  42.      */
  43.     public static function open($database$dbinfo NULL)
  44.     {
  45.         if (!isset(self::$counter))
  46.         {
  47.             self::$counter 0;
  48.         }
  49.         else
  50.         {
  51.             self::$counter ++;
  52.         }
  53.         
  54.         if ($dbinfo)
  55.         {
  56.             self::$conn[self::$counter]   TConnection::openArray($dbinfo);
  57.             self::$dbinfo[self::$counter$dbinfo;
  58.         }
  59.         else
  60.         {
  61.             $dbinfo TConnection::getDatabaseInfo($database);
  62.             self::$conn[self::$counter]   TConnection::open($database);
  63.             self::$dbinfo[self::$counter$dbinfo;
  64.         }
  65.         
  66.         self::$database[self::$counter$database;
  67.         self::$uniqid[self::$counteruniqid();
  68.         
  69.         $driver self::$conn[self::$counter]->getAttribute(PDO::ATTR_DRIVER_NAME);
  70.         
  71.         $fake = isset($dbinfo['fake']$dbinfo['fake'FALSE;
  72.         
  73.         if (!$fake)
  74.         {
  75.             // begins transaction
  76.             self::$conn[self::$counter]->beginTransaction();
  77.         }
  78.         
  79.         if (!empty(self::$dbinfo[self::$counter]['slog']))
  80.         {
  81.             $logClass self::$dbinfo[self::$counter]['slog'];
  82.             if (class_exists($logClass))
  83.             {
  84.                 self::setLogger(new $logClass);
  85.             }
  86.         }
  87.         else
  88.         {
  89.             // turn OFF the log
  90.             self::$logger[self::$counterNULL;
  91.         }
  92.         
  93.         return self::$conn[self::$counter];
  94.     }
  95.     
  96.     /**
  97.      * Open fake transaction
  98.      * @param $database Name of the database (an INI file).
  99.      */
  100.     public static function openFake($database)
  101.     {
  102.         $info TConnection::getDatabaseInfo($database);
  103.         $info['fake'1;
  104.         
  105.         TTransaction::open(null$info);
  106.     }
  107.     
  108.     /**
  109.      * Returns the current active connection
  110.      * @return PDO 
  111.      */
  112.     public static function get()
  113.     {
  114.         if (isset(self::$conn[self::$counter]))
  115.         {
  116.             return self::$conn[self::$counter];
  117.         }
  118.     }
  119.     
  120.     /**
  121.      * Rollback all pending operations
  122.      */
  123.     public static function rollback()
  124.     {
  125.         if (isset(self::$conn[self::$counter]))
  126.         {
  127.             $driver self::$conn[self::$counter]->getAttribute(PDO::ATTR_DRIVER_NAME);
  128.             // rollback
  129.             self::$conn[self::$counter]->rollBack();
  130.             self::$conn[self::$counterNULL;
  131.             self::$uniqid[self::$counterNULL;
  132.             self::$counter --;
  133.             
  134.             return true;
  135.         }
  136.     }
  137.     
  138.     /**
  139.      * Commit all the pending operations
  140.      */
  141.     public static function close()
  142.     {
  143.         if (isset(self::$conn[self::$counter]))
  144.         {
  145.             $driver self::$conn[self::$counter]->getAttribute(PDO::ATTR_DRIVER_NAME);
  146.             $info self::getDatabaseInfo();
  147.             $fake = isset($info['fake']$info['fake'FALSE;
  148.             
  149.             if (!$fake)
  150.             {
  151.                 // apply the pending operations
  152.                 self::$conn[self::$counter]->commit();
  153.             }
  154.             
  155.             self::$conn[self::$counterNULL;
  156.             self::$uniqid[self::$counterNULL;
  157.             self::$counter --;
  158.             
  159.             return true;
  160.         }
  161.     }
  162.     
  163.     /**
  164.      * close all transactions
  165.      */
  166.     public static function closeAll()
  167.     {
  168.         $has_connection true;
  169.         
  170.         while ($has_connection)
  171.         {
  172.             $has_connection self::close();
  173.         }
  174.     }
  175.     
  176.     /**
  177.      * rollback all transactions
  178.      */
  179.     public static function rollbackAll()
  180.     {
  181.         $has_connection true;
  182.         
  183.         while ($has_connection)
  184.         {
  185.             $has_connection self::rollback();
  186.         }
  187.     }
  188.     
  189.     /**
  190.      * Assign a Logger closure function
  191.      * @param $logger A Closure
  192.      */
  193.     public static function setLoggerFunction(Closure $logger)
  194.     {
  195.         if (isset(self::$conn[self::$counter]))
  196.         {
  197.             self::$logger[self::$counter$logger;
  198.         }
  199.         else
  200.         {
  201.             // if there's no active transaction opened
  202.             throw new Exception(AdiantiCoreTranslator::translate('No active transactions'': ' . __METHOD__);
  203.         }
  204.     }
  205.     
  206.     /**
  207.      * Assign a Logger strategy
  208.      * @param $logger A TLogger child object
  209.      */
  210.     public static function setLogger(AdiantiLoggerInterface $logger)
  211.     {
  212.         if (isset(self::$conn[self::$counter]))
  213.         {
  214.             self::$logger[self::$counter$logger;
  215.         }
  216.         else
  217.         {
  218.             // if there's no active transaction opened
  219.             throw new Exception(AdiantiCoreTranslator::translate('No active transactions'': ' . __METHOD__);
  220.         }
  221.     }
  222.     
  223.     /**
  224.      * Write a message in the LOG file, using the user strategy
  225.      * @param $message Message to be logged
  226.      */
  227.     public static function log($message)
  228.     {
  229.         // check if exist a logger
  230.         if (!empty(self::$logger[self::$counter]))
  231.         {
  232.             $log self::$logger[self::$counter];
  233.             
  234.             // avoid recursive log
  235.             self::$logger[self::$counterNULL;
  236.             
  237.             if ($log instanceof AdiantiLoggerInterface)
  238.             {
  239.                 // call log method
  240.                 $log->write($message);
  241.             }
  242.             else if ($log instanceof Closure)
  243.             {
  244.                 $log($message);
  245.             }
  246.             
  247.             // restore logger
  248.             self::$logger[self::$counter$log;
  249.         }
  250.     }
  251.     
  252.     /**
  253.      * Return the Database Name
  254.      */
  255.     public static function getDatabase()
  256.     {
  257.         if (!empty(self::$database[self::$counter]))
  258.         {
  259.             return self::$database[self::$counter];
  260.         }
  261.     }
  262.     
  263.     /**
  264.      * Returns the Database Information
  265.      */
  266.     public static function getDatabaseInfo()
  267.     {
  268.         if (!empty(self::$dbinfo[self::$counter]))
  269.         {
  270.             return self::$dbinfo[self::$counter];
  271.         }
  272.     }
  273.     
  274.     /**
  275.      * Returns the Transaction uniqid
  276.      */
  277.     public static function getUniqId()
  278.     {
  279.         if (!empty(self::$uniqid[self::$counter]))
  280.         {
  281.             return self::$uniqid[self::$counter];
  282.         }
  283.     }
  284.     
  285.     /**
  286.      * Enable transaction log
  287.      */
  288.     public static function dump$file null )
  289.     {
  290.         if ($file)
  291.         {
  292.             self::setLoggernew TLoggerTXT($file) );
  293.         }
  294.         else
  295.         {
  296.             self::setLoggernew TLoggerSTD );
  297.         }
  298.     }
  299. }