Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Common direct db actions
  4.  *
  5.  * @author Christian Winther <cwin@mocsystems.com>
  6.  * @since 08.04.2010
  7.  */
  8. class MOC_DB {
  9.    
  10.     /**
  11.       * Table field cache
  12.      *
  13.      * @var array
  14.      */
  15.     protected static $tableCache = array();
  16.    
  17.     protected static $tableInformation = array();
  18.    
  19.     /**
  20.      * Get a list of fields in a table
  21.      *
  22.      * @return array
  23.      */
  24.     public static function tableFields($table) {
  25.         if (!array_key_exists($table, self::$tableCache)) {
  26.             self::$tableCache[$table] = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
  27.         }
  28.         return self::$tableCache[$table];
  29.     }
  30.    
  31.     public static function saveFields($table, $data) {
  32.         $fields = self::tableFields($table);
  33.         return array_intersect_key($data, $fields);
  34.     }
  35.    
  36.     public static function beginTransaction() {
  37.         $GLOBALS['TYPO3_DB']->sql_query('BEGIN');
  38.     }
  39.    
  40.     public static function rollbackTransaction() {
  41.         $GLOBALS['TYPO3_DB']->sql_query('ROLLBACK');
  42.     }
  43.    
  44.     public static function commitTransaction() {
  45.         $GLOBALS['TYPO3_DB']->sql_query('COMMIT');
  46.     }
  47.    
  48.     public static function checkTransactionSupportForTable($names) {
  49.         if (!is_array($names)) {
  50.             $names = array($names);
  51.         }
  52.        
  53.         self::loadTableInformation();
  54.        
  55.         foreach ($names as $name) {
  56.             if (!array_key_exists($name, self::$tableInformation)) {
  57.                 throw new MOC_DB_Exception(sprintf('Cant check table %s for transaction support, its not present in self::$tableInformation. (Maybe the table doesnt exist)', $name));
  58.             }
  59.            
  60.             if (self::$tableInformation[$name]['Engine'] !== 'InnoDB') {
  61.                 throw new MOC_DB_Exception(sprintf('Table %s does not have transaction support, its not an InnoDB table. Table type %s does not support transactions', $name, self::$tableInformation[$name]['Engine']));
  62.             }
  63.         }
  64.     }
  65.    
  66.     protected static function loadTableInformation() {
  67.         if (!empty(self::$tableInformation)) {
  68.             return;
  69.         }
  70.        
  71.         $resource = $GLOBALS['TYPO3_DB']->sql_query('SHOW TABLE STATUS');
  72.         while ($record = mysql_fetch_array($resource)) {
  73.             self::$tableInformation[$record['Name']] = $record;
  74.         }
  75.     }
  76.    
  77.     public static function error() {
  78.         $error = $GLOBALS['TYPO3_DB']->sql_error();
  79.         if (empty($error)) {
  80.             return false;
  81.         }
  82.         return $error;
  83.     }
  84.    
  85.     public static function insertId() {
  86.         return $GLOBALS['TYPO3_DB']->sql_insert_id();
  87.     }
  88.    
  89.     public static function affectedRows() {
  90.         return $GLOBALS['TYPO3_DB']->sql_affected_rows();
  91.     }
  92.    
  93.     public static function suppressHTMLErrors() {
  94.         $GLOBALS['TYPO3_DB']->debugOutput = 0;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement