TableDataSet.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: TableDataSet.php,v 1.3 2004/03/20 04:16:51 hlellelid Exp $ 00005 * 00006 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00007 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00008 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00009 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00010 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00011 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00012 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00013 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00014 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00015 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00016 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * This software consists of voluntary contributions made by many individuals 00019 * and is licensed under the LGPL. For more information please see 00020 * <http://creole.phpdb.org>. 00021 * 00022 * This product includes software based on the Village framework, 00023 * http://share.whichever.com/index.php?SCREEN=village. 00024 */ 00025 00026 require_once 'jargon/DataSet.php'; 00027 require_once 'jargon/KeyDef.php'; 00028 00039 class TableDataSet extends DataSet { 00040 00042 private $tableName; 00043 00045 private $tableInfo; 00046 00048 private $optimisticLockingCol; 00049 00051 private $where; 00052 00054 private $order; 00055 00057 private $other; 00058 00060 private $refreshOnSave = false; 00061 00075 public function __construct(Connection $conn, $tableName, $p3 = null, $p4 = null) 00076 { 00077 $this->conn = $conn; 00078 $this->columns = "*"; 00079 $this->tableName = $tableName; 00080 00081 if ($p4 !== null) { 00082 $this->columns = $p3; 00083 $this->keyDef = $p4; 00084 } elseif ($p3 !== null) { 00085 if ($p3 instanceof KeyDef) { 00086 $this->keyDef = $p3; 00087 } else { // it's a string (column list) 00088 $this->columns = $p3; 00089 } 00090 } 00091 } 00092 00098 public function tableName() 00099 { 00100 return $this->tableName; 00101 } 00102 00108 public function tableInfo() 00109 { 00110 if ($this->tableInfo === null) { 00111 $this->tableInfo = $this->conn->getDatabaseInfo()->getTable($this->tableName); 00112 } 00113 return $this->tableInfo; 00114 } 00115 00125 public function fetchRecords($p1 = 0, $p2 = null) 00126 { 00127 $this->buildSelectString(); 00128 return parent::fetchRecords($p1, $p2); 00129 } 00130 00138 public function addRecord() 00139 { 00140 $rec = new Record($this, true); 00141 $rec->markForInsert(); 00142 $this->records[] = $rec; 00143 return $rec; 00144 } 00145 00151 public function save() 00152 { 00153 $j = 0; 00154 foreach($this->records as $rec) { 00155 $rec->save(); 00156 $j++; 00157 } 00158 // now go through and remove any records 00159 // that were previously marked as a zombie by the 00160 // delete process 00161 $this->removeDeletedRecords(); 00162 return $j; 00163 } 00164 00170 public function removeDeletedRecords() 00171 { 00172 // this algorythm should be a fair bit 00173 // faster than calling DataSet::removeRecord() 00174 $new_recs = array(); 00175 foreach($this->records as $rec) { 00176 if (!$rec->isAZombie()) { 00177 $new_recs[] = $rec; 00178 } 00179 } 00180 $this->records = $new_recs; 00181 } 00182 00187 public function setOptimisticLockingColumn($olc) 00188 { 00189 $this->optimisticLockingCol = $olc; 00190 } 00191 00196 public function optimisticLockingCol() 00197 { 00198 return $this->optimisticLockingCol; 00199 } 00200 00206 public function where($where) 00207 { 00208 if ($where == null) { 00209 throw new DataSetException("null not allowed for where clause"); 00210 } 00211 $this->where = $where; 00212 return $this; 00213 } 00214 00219 public function getWhere() 00220 { 00221 return $this->where; 00222 } 00223 00230 public function order($order) 00231 { 00232 if ($order === null) { 00233 throw new DataSetException("null not allowed for order clause"); 00234 } 00235 $this->order = $order; 00236 return $this; 00237 } 00238 00243 public function getOrder() 00244 { 00245 return $this->order; 00246 } 00247 00254 public function other($other) 00255 { 00256 if ($other === null) { 00257 throw new DataSetException("null not allowed for other clause"); 00258 } 00259 $this->other = $other; 00260 return $this; 00261 } 00262 00267 public function getOther() 00268 { 00269 return $this->other; 00270 } 00271 00277 public function refresh() 00278 { 00279 foreach($this->records as $rec) { 00280 $rec->refresh($this->conn); 00281 } 00282 } 00283 00291 public function setRefreshOnSave($v) 00292 { 00293 $this->refreshOnSave = $v; 00294 } 00295 00303 public function refreshOnSave() 00304 { 00305 return $this->refreshOnSave; 00306 } 00307 00312 public function getSelectSql() 00313 { 00314 return $this->selectSql; 00315 } 00316 00324 private function buildSelectString () 00325 { 00326 $this->selectSql = "SELECT "; 00327 $this->selectSql .= $this->columns; 00328 $this->selectSql .= " FROM " . $this->tableName; 00329 if ($this->where !== null && $this->where !== "") { 00330 $this->selectSql .= " WHERE " . $this->where; 00331 } 00332 if ($this->order !== null && $this->order !== "") { 00333 $this->selectSql .= " ORDER BY " . $this->order; 00334 } 00335 if ($this->other !== null && $this->other !== "") { 00336 $this->selectSql .= $this->other; 00337 } 00338 } 00339 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS