TableDataSet.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: TableDataSet.php,v 1.3 2004/05/22 12:56:30 micha 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 00041 class TableDataSet extends DataSet 00042 { 00044 var $tableName; 00045 00047 var $tableInfo; 00048 00050 var $optimisticLockingCol; 00051 00053 var $where; 00054 00056 var $order; 00057 00059 var $other; 00060 00062 var $refreshOnSave = false; 00063 00077 function TableDataSet(/*Connection*/ &$conn, $tableName, $p3 = null, $p4 = null) 00078 { 00079 if (! is_a($conn, 'Connection')) { 00080 trigger_error ( 00081 "TableDataSet::TableDataSet(): parameter 1 not of type 'Connection' !", 00082 E_USER_ERROR 00083 ); 00084 } 00085 00086 $this->conn =& $conn; 00087 $this->columns = "*"; 00088 $this->tableName = $tableName; 00089 00090 if ($p4 !== null) { 00091 $this->columns = $p3; 00092 $this->keyDef = $p4; 00093 } 00094 else if ($p3 !== null) { 00095 if (is_a($p3, 'KeyDef')) { 00096 $this->keyDef =& $p3; 00097 } else { // it's a string (column list) 00098 $this->columns = $p3; 00099 } 00100 } 00101 } 00102 00107 function tableName() 00108 { 00109 return $this->tableName; 00110 } 00111 00116 function & tableInfo() 00117 { 00118 if ($this->tableInfo === null) { 00119 $dbInfo =& $this->conn->getDatabaseInfo(); 00120 $tblInfo =& $dbInfo->getTable($this->tableName); 00121 00122 if (Creole::isError($tblInfo)) { 00123 return $tblInfo; 00124 } 00125 $this->tableInfo =& $tblInfo; 00126 } 00127 return $this->tableInfo; 00128 } 00129 00137 function & fetchRecords($p1 = 0, $p2 = null) 00138 { 00139 $this->buildSelectString(); 00140 return parent::fetchRecords($p1, $p2); 00141 } 00142 00148 function & addRecord() 00149 { 00150 $rec =& new Record($this, true); 00151 00152 if (Creole::isError($rec)) { 00153 return $rec; 00154 } 00155 /* 00156 * no need to check return value here as markForInsert() only returns an 00157 * exception, if $rec->ds is not a TableDataSet 00158 */ 00159 $rec->markForInsert(); 00160 $this->records[] =& $rec; 00161 return $rec; 00162 } 00163 00168 function save() 00169 { 00170 $i = 0; 00171 for($j=count($this->records); $i < $j; $i++) { 00172 $rec =& $this->records[$i]; 00173 if (($e = $rec->save()) !== true) { 00174 return $e; 00175 } 00176 } 00177 // now go through and remove any records 00178 // that were previously marked as a zombie by the 00179 // delete process 00180 $this->removeDeletedRecords(); 00181 return $i; 00182 } 00183 00188 function removeDeletedRecords() 00189 { 00190 // this algorythm should be a fair bit 00191 // faster than calling DataSet::removeRecord() 00192 $new_recs = array(); 00193 for($i=0,$j=count($this->records); $i < $j; $i++) { 00194 $rec =& $this->records[$i]; 00195 if (!$rec->isAZombie()) { 00196 $new_recs[] =& $rec; 00197 } 00198 } 00199 $this->records =& $new_recs; 00200 } 00201 00206 function setOptimisticLockingColumn($olc) 00207 { 00208 $this->optimisticLockingCol = $olc; 00209 } 00210 00215 function optimisticLockingCol() 00216 { 00217 return $this->optimisticLockingCol; 00218 } 00219 00224 function & where($where) 00225 { 00226 if ($where == null) { 00227 return new DataSetException(0, "null not allowed for where clause"); 00228 } 00229 $this->where =& $where; 00230 return $this; 00231 } 00232 00237 function & getWhere() 00238 { 00239 return $this->where; 00240 } 00241 00247 function & order($order) 00248 { 00249 if ($order === null) { 00250 return new DataSetException(0, "null not allowed for order clause"); 00251 } 00252 $this->order =& $order; 00253 return $this; 00254 } 00255 00260 function & getOrder() 00261 { 00262 return $this->order; 00263 } 00264 00271 function & other($other) 00272 { 00273 if ($other === null) { 00274 return new DataSetException(0, "null not allowed for other clause"); 00275 } 00276 $this->other =& $other; 00277 return $this; 00278 } 00279 00284 function & getOther() 00285 { 00286 return $this->other; 00287 } 00288 00294 function refresh() 00295 { 00296 for($i=0,$j=count($this->records); $i < $j; $i++) { 00297 $rec =& $this->records[$i]; 00298 if (($e = $rec->refresh()) !== true) { 00299 return $e; 00300 } 00301 } 00302 return true; 00303 } 00304 00312 function setRefreshOnSave($v) 00313 { 00314 $this->refreshOnSave = $v; 00315 } 00316 00324 function refreshOnSave() 00325 { 00326 return $this->refreshOnSave; 00327 } 00328 00333 function getSelectSql() 00334 { 00335 return $this->selectSql; 00336 } 00337 00346 function buildSelectString () 00347 { 00348 $this->selectSql = "SELECT "; 00349 $this->selectSql .= $this->columns; 00350 $this->selectSql .= " FROM " . $this->tableName; 00351 if ($this->where !== null && $this->where !== "") { 00352 $this->selectSql .= " WHERE " . $this->where; 00353 } 00354 if ($this->order !== null && $this->order !== "") { 00355 $this->selectSql .= " ORDER BY " . $this->order; 00356 } 00357 if ($this->other !== null && $this->other !== "") { 00358 $this->selectSql .= $this->other; 00359 } 00360 } 00361 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS