[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/creole/drivers/odbc/ -> ODBCIdGenerator.php (source)

   1  <?php
   2  
   3  require_once 'creole/IdGenerator.php';
   4  
   5  /**
   6   * ODBC IdGenerator implimenation.
   7   *
   8   * NOTE: I tried keeping the SQL as basic as possible in this class.
   9   *       If you need something more optimized, derive your own IdGenerator
  10   *       and use {@link ODBCAdapter::getIdGenerator()} to use it.
  11   *
  12   * @author    Dave Lawson <dlawson@masterytech.com>
  13   * @version   $Revision: 1.2 $
  14   * @package   creole.drivers.odbc
  15   */
  16  class ODBCIdGenerator implements IdGenerator {
  17  
  18      /** Connection object that instantiated this class */
  19      private $conn;
  20  
  21      /**
  22       * Creates a new IdGenerator class, saves passed connection for use
  23       * later by getId() method.
  24       * @param Connection $conn
  25       */
  26      public function __construct(Connection $conn)
  27      {
  28          $this->conn = $conn;
  29      }
  30  
  31      /**
  32       * @see IdGenerator::isBeforeInsert()
  33       */
  34      public function isBeforeInsert()
  35      {
  36          return true;
  37      }
  38  
  39      /**
  40       * @see IdGenerator::isAfterInsert()
  41       */
  42      public function isAfterInsert()
  43      {
  44          return false;
  45      }
  46  
  47      /**
  48       * @see IdGenerator::getIdMethod()
  49       */
  50      public function getIdMethod()
  51      {
  52          return self::SEQUENCE;
  53      }
  54  
  55      /**
  56       * @see IdGenerator::getId()
  57       */
  58      public function getId($seqname = null)
  59      {
  60          if ($seqname === null)
  61              throw new SQLException('You must specify the sequence name when calling getId() method.');
  62  
  63          $triedcreate = false;
  64  
  65          while (1)
  66          {
  67              try
  68              {
  69                  $n = $this->conn->executeUpdate("UPDATE $seqname SET id = id + 1", ResultSet::FETCHMODE_NUM);
  70  
  71                  if ($n == 0)
  72                      throw new SQLException('Failed to update IdGenerator id', $this->conn->nativeError());
  73  
  74                  $rs = $this->conn->executeQuery("SELECT id FROM $seqname", ResultSet::FETCHMODE_NUM);
  75              }
  76              catch (SQLException $e)
  77              {
  78                  //$odbcerr = odbc_error($this->conn->getResource());
  79  
  80                  if ($triedcreate)// || ($odbcerr != 'S0000' && $odbcerr != 'S0002'))
  81                      throw $e;
  82  
  83                  $this->drop($seqname, true);
  84                  $this->create($seqname);
  85                  $triedcreate = true;
  86                  continue;
  87              }
  88  
  89              break;
  90          }
  91  
  92          $rs->first();
  93  
  94          return $rs->getInt(1);
  95      }
  96  
  97      /**
  98       * Creates the sequence emulation table.
  99       */
 100      public function create($seqname)
 101      {
 102          $this->conn->executeUpdate("CREATE TABLE $seqname ( id numeric(19,0) NOT NULL )");
 103          $this->conn->executeUpdate("INSERT INTO $seqname ( id ) VALUES ( 0 )");
 104      }
 105  
 106      /**
 107       * Drops the sequence emulation table.
 108       */
 109      public function drop($seqname, $ignoreerrs = false)
 110      {
 111          try {
 112              $this->conn->executeUpdate("DROP TABLE $seqname");
 113          } catch (Exception $e) {
 114              if (!$ignoreerrs) throw $e;
 115          }
 116      }
 117  
 118  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7