<?php
/* $Id: SystemTools.class.php,v 1.7 2003/12/10 10:44:43 k-fish Exp $ */

class SystemTools{

    function seqCheck(){
	global $conn;
	$errors = array();
	$result = array();

	// fetch tables
	$tables = $conn->MetaTables();
	if(is_array($tables)){
	    foreach($tables as $table){
		// only work on mgw tables and skip sequence tables
		if(!substr($table,0,4) == "mgw_")
		    continue;
		if(substr($table,0,9) == "mgw__seq_")
		    continue;

		// fetch primary keys and column info. primary keys can only
		// be gathered on oci8, mysql and pgsql
		$pkeys = $conn->MetaPrimaryKeys($table);
		$cols = $conn->MetaColumns($table);

		if(is_array($pkeys)){
		    foreach($pkeys as $pkey){
			// check if the key is an int value - handle only those
			if(!stristr($cols[strtoupper($pkey)]->type,'int')){
			    $result[] = "&nbsp;&nbsp;&nbsp;&nbsp;".Lang::getLanguageString('skipped')." 'mgw__seq_".substr($table,4)."' ('$pkv', =&gt; '".$cols[strtoupper($pkey)]->type."').";
			    continue;
			}

			// fetch max value
			$pkv = $conn->GetOne("SELECT MAX(".$pkey.") FROM $table");
			if($pkv != ''){
			    // using GenID in case the sequence is there but has a
			    // wrong value - CreateSequence would fail
			    if(mgw_genID("mgw__seq_".substr($table,4), $pkv) === false)
				$errors[] = $conn->ErrorMsg().'<br />';
			    $result[] = Lang::getLanguageString('triedseq')." 'mgw__seq_".substr($table,4)."' ('$pkv', =&gt; '".$cols[strtoupper($pkey)]->type."').";
			}
		    }
		}
	    }
	    return array('errors' => $errors, 'result' => $result);
	}
    }
}
?>