Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
transaction_manager.inc
1 <?php
30 {
31 
37  private $_rollback = Array();
38 
44  private $_levels = Array();
45 
46 
52  {
53  $this->MySource_Object();
54 
55  }//end constructor
56 
57 
66  public function inTransaction($db_name)
67  {
68  return !empty($this->_levels[$db_name]);
69 
70  }//end inTransaction()
71 
72 
83  public function begin($db_name)
84  {
85  $dsn_array = MatrixDAL::getDsn($db_name);
86  $dsn = $dsn_array['dsn'];
87  if (isset($dsn_array['userName'])) $dsn .= ';user='.$dsn_array['userName'];
88 
89  if (isset($this->_levels[$dsn])) {
90  $this->_levels[$dsn]++;
91  } else {
92  $this->_levels[$dsn] = 1;
93  }
94 
95  if ($this->_levels[$dsn] == 1) {
96  try {
98  } catch (DALException $e) {
99  throw $e;
100  }
101  }
102 
103  return TRUE;
104 
105  }//end begin()
106 
107 
118  public function commit($db_name)
119  {
120  $dsn_array = MatrixDAL::getDsn($db_name);
121  $dsn = $dsn_array['dsn'];
122  if (isset($dsn_array['userName'])) $dsn .= ';user='.$dsn_array['userName'];
123 
124  if (empty($this->_levels[$dsn])) {
125  // TODO: -TOF- put that line back
126  //trigger_localised_error('SYS0219', E_USER_WARNING);
127  return FALSE;
128  }
129 
130  if (!empty($this->_rollback[$dsn])) {
131  trigger_localised_error('SYS0220', E_USER_WARNING);
132  try {
133  MatrixDAL::rollback();
134  } catch (DALException $e) {
135  throw $e;
136  }
137 
138  return FALSE;
139  }
140  $this->_levels[$dsn]--;
141 
142  if ($this->_levels[$dsn] == 0) {
143  // this is the last commit
144  try {
146  } catch (DALException $e) {
147  throw $e;
148  }
149  }
150 
151  return TRUE;
152 
153  }//end commit()
154 
155 
166  public function rollback($db_name)
167  {
168  $dsn_array = MatrixDAL::getDsn($db_name);
169  $dsn = $dsn_array['dsn'];
170  if (isset($dsn_array['userName'])) $dsn .= ';user='.$dsn_array['userName'];
171 
172  if (empty($this->_levels[$dsn])) {
173  trigger_localised_error('SYS0231', E_USER_WARNING);
174  return FALSE;
175  }
176 
177  // set the rollback flag
178  $this->_rollback[$dsn] = TRUE;
179  $this->_levels[$dsn]--;
180 
181  if ($this->_levels[$dsn] == 0) {
182 
183  // this is the last rollback
184  try {
185  MatrixDAL::rollback();
186  } catch (DALException $e) {
187  throw $e;
188  }
189 
190  // clear the rollback flag as there is no more transaction block
191  $this->_rollback[$dsn] = FALSE;
192  }
193 
194  return TRUE;
195 
196  }//end rollback()
197 
198 
199 }//end class
200 ?>