1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
<?php
/* OpenDb - Open Media Lending Database
Copyright (C) 2001,2002 by Jason Pell
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
include_once("./functions/utils.php");
include_once("./functions/database.php");
include_once("./functions/logging.php");
include_once("./functions/patchutils.php");
/**
* This class is designed to manage the maintenance of a single table.
*
* This class will NOT create the table, its assumed that was done
* previously with a script.
*
* The basic idea is that this class's _handleRow method will be called
* for each row of CSV data from the file, used to populate the table.
* The subclass is responsible for working out whether to insert/update/delete
* the appropriate records, and to keep track of such an action.
*/
class Install_Table
{
var $_header_row = NULL;
var $_raw_header_row = NULL;
var $_table_name = NULL;
var $_insert_count = 0;
var $_update_count = 0;
var $_delete_count = 0;
var $_processed_count = 0;
var $_rowcount = 0;
var $_begin_row = 0;
var $_end_row = NULL;
/**
*/
function Install_Table($table)
{
$this->_table_name = $table;
}
/**
indicates what type of installation this class supports, currently only
one install type is supported at the moment - 'Install_Table'
*/
function getInstallType()
{
return 'Install_Table';
}
function getLastUpdated()
{
$query = "SELECT UNIX_TIMESTAMP(MAX(update_on)) as update_on FROM ".$this->_table_name;
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
if ($found!==FALSE)
return $found['update_on'];
}
//else
return FALSE;
}
function getRecordCount()
{
$query = "SELECT COUNT('x') as count FROM ".$this->_table_name;
$result = run_opendb_query($query);
if($result && mysql_num_rows($result)>0)
{
$found = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);
if ($found!==FALSE)
return $found['count'];
}
//else
return FALSE;
}
function getInstallTable()
{
return $this->_table_name;
}
function getInsertCount()
{
return $this->_insert_count;
}
function getUpdateCount()
{
return $this->_update_count;
}
function getDeleteCount()
{
return $this->_delete_count;
}
function getProcessedCount()
{
return $this->_processed_count;
}
function getRowCount()
{
return $this->_rowcount;
}
function addError($error, $row, $detail = NULL)
{
$this->_errors[] = array('error'=>$error, 'rowcount'=>$this->_rowcount, 'row'=>$row, 'details'=>$detail);
}
function getErrors()
{
return $this->_errors;
}
function setRowRange($begin, $end)
{
$this->_begin_row = $begin;
$this->_end_row = $end;
}
/**
@return
__UPDATE__
__UPDATE_FAILED__
__INSERT__
__INSERT_FAILED__
__DELETE__
__DELETE_FAILED__
*/
function handleRow($row_data, &$error)
{
return FALSE;
}
function _handleRow($row)
{
$this->_rowcount++;
if(is_array($row) && count($row)>0)
{
if($this->_header_row == NULL)
{
for($i=0; $i<count($row); $i++)
{
$this->_raw_header_row = $row;
// process it, so that we replace spaces with underscores in any of the names.
for($j=0; $j<count($this->_raw_header_row); $j++)
{
$this->_raw_header_row[$j] = strtolower(preg_replace("/[ \n\r\t]+/i", "_", trim($this->_raw_header_row[$j])));
}
// if a column mapping is provided.
if(isset($this->_column_mappings[$row[$i]]))
$this->_header_row[$i] = $this->_column_mappings[$row[$i]];
else
$this->_header_row[$i] = $row[$i];
}
return TRUE;
}
else
{
if(count($this->_raw_header_row) == count($row))
{
if($this->_rowcount >= $this->_begin_row)
{
$this->_processed_count++;
//convert $row to use index names matching the header column names
for($j=0; $j<count($row); $j++)
{
$row_data[$this->_raw_header_row[$j]] = $row[$j];
}
$returnVal = $this->handleRow($row_data, $error);
if($returnVal === FALSE)
{
$this->addError('Unknown Error', $row, $error);
return FALSE;
}
else if($returnVal === '__INSERT__')
{
$this->_insert_count++;
return TRUE;
}
else if($returnVal === '__UPDATE__')
{
$this->_update_count++;
return TRUE;
}
else if($returnVal === '__DELETE__')
{
$this->_delete_count++;
return TRUE;
}
else if($returnVal === '__INSERT_FAILED__')
{
$this->addError('Insert Failed', $row, $error);
return FALSE;
}
else if($returnVal === '__UPDATE_FAILED__')
{
$this->addError('Update Failed', $row, $error);
return FALSE;
}
else if($returnVal === '__DELETE_FAILED__')
{
$this->addError('Delete Failed', $row, $error);
return FALSE;
}
}//if($this_row_count >= $this->_begin_row)
else
{
return TRUE;
}
}
else
{
$this->addError('Invalid record', $row, 'Incorrect number or columns');
return FALSE; // mismatch row count
}
}
}
//else
return FALSE;
}
function isEndRowFound()
{
if(is_numeric($this->_end_row) && $this->_rowcount > $this->_end_row)
return TRUE;
else
return FALSE;
}
function getRawHeaderRow()
{
return $this->_raw_header_row;
}
function getHeaderRow()
{
return $this->_header_row;
}
}
?>
|