00001 <?php
00002
00003 require_once 'creole/IdGenerator.php';
00004
00016 class ODBCIdGenerator implements IdGenerator {
00017
00019 private $conn;
00020
00026 public function __construct(Connection $conn)
00027 {
00028 $this->conn = $conn;
00029 }
00030
00034 public function isBeforeInsert()
00035 {
00036 return true;
00037 }
00038
00042 public function isAfterInsert()
00043 {
00044 return false;
00045 }
00046
00050 public function getIdMethod()
00051 {
00052 return self::SEQUENCE;
00053 }
00054
00058 public function getId($seqname = null)
00059 {
00060 if ($seqname === null)
00061 throw new SQLException('You must specify the sequence name when calling getId() method.');
00062
00063 $triedcreate = false;
00064
00065 while (1)
00066 {
00067 try
00068 {
00069 $n = $this->conn->executeUpdate("UPDATE $seqname SET id = id + 1", ResultSet::FETCHMODE_NUM);
00070
00071 if ($n == 0)
00072 throw new SQLException('Failed to update IdGenerator id', $this->conn->nativeError());
00073
00074 $rs = $this->conn->executeQuery("SELECT id FROM $seqname", ResultSet::FETCHMODE_NUM);
00075 }
00076 catch (SQLException $e)
00077 {
00078
00079
00080 if ($triedcreate)
00081 throw $e;
00082
00083 $this->drop($seqname, true);
00084 $this->create($seqname);
00085 $triedcreate = true;
00086 continue;
00087 }
00088
00089 break;
00090 }
00091
00092 $rs->first();
00093
00094 return $rs->getInt(1);
00095 }
00096
00100 public function create($seqname)
00101 {
00102 $this->conn->executeUpdate("CREATE TABLE $seqname ( id numeric(19,0) NOT NULL )");
00103 $this->conn->executeUpdate("INSERT INTO $seqname ( id ) VALUES ( 0 )");
00104 }
00105
00109 public function drop($seqname, $ignoreerrs = false)
00110 {
00111 try {
00112 $this->conn->executeUpdate("DROP TABLE $seqname");
00113 } catch (Exception $e) {
00114 if (!$ignoreerrs) throw $e;
00115 }
00116 }
00117
00118 }