<?php
require_once 'Quicty/phplib/easy_db.class.php';
class move_db {

function set_from($dsn) {
	$this->from = new easy_db($dsn);
	return $this->from;
}

function set_to($dsn) {
	$this->to = new easy_db($dsn);
	return $this->to;
}

function get_function_result($to,$from_set,$from) {
	$function = array_shift($to);
	$column = array_shift($to);
	$result = call_user_func($function,$from_set,$from);
	if($result===false) {
		return false;
	} else {
		return  $result;
	}
}

function move_columns($from_set,$columns) {
	foreach($columns as $from=>$to) {
		if($from=='default_columns') {
			foreach($to as $key=>$value) {
				$to_set[$key] = $value;
			}
		} elseif($from=='ext_columns') {
			foreach($to as $key=>$value) {
				$to_set[$key] = $GLOBALS[$value];
			}
		} elseif($from=='update_condition') {
			foreach($to as $key=>$value) {
				$update_conditions[] = "$value=".$to_set[$value];
			}
			$this->update_condition = implode(' AND ',$update_conditions);
		} elseif(is_array($to)) {
			$test = $to[0];
			if(is_array($test)) {
				foreach($to as $each_to) {
					$function = array_shift($each_to);
					$column = array_shift($each_to);
					$result = call_user_func($function,$from_set,$from);
					if($result===false) {
						return false;
					} else {
						$to_set[$column] = $result;
					}
				}
			} else {
				$function = array_shift($to);
				$column = array_shift($to);
				$result = call_user_func($function,$from_set,$from);
				if($result===false) {
					return false;
				} else {
					$to_set[$column] = $result;
				}
			}
/*
		} elseif(is_array($to)) {
			$function = array_shift($to);
			$column = array_shift($to);
			$result = call_user_func($function,$from_set,$from);
			if($result===false) {
				return false;
			} else {
				$to_set[$column] = $result;
			}
*/
		} else {
			$to_set[$to] = $from_set[$from];
		}
	}
	return $to_set;
}

function move_table($move_set,$condition='') {
	global $ARRAY_BUFFER;
	$ARRAY_BUFFER = array();

	$from_db = $move_set['from_db'];
	$to_db = $move_set['to_db'];
	$this->set_from($from_db);
	$this->set_to($to_db);
	$form_table = $move_set['from_table'];
	$to_set = $move_set['to_set'];
	$FH = $this->from->get_table($form_table,$condition);
	while($row = $this->from->get_row($FH)) {
		//print_r($row);
		foreach($to_set as $key=>$value) {
			$test = $value;
			if(is_array(array_shift($test))) {
				list($to_table,$columns) = each($value);
			} else {
				$to_table = $key;
				$columns = $value;
			}
			$table_list[$to_table] = $to_table;
			do {
				$insert_set = $this->move_columns($row,$columns);
				if($insert_set!==false) {
						print_r($insert_set);
					if($this->update_condition) {
						echo "update condition=".$this->update_condition."\n";
						$result = $this->to->update_row($to_table,$insert_set,$this->update_condition);
						unset($this->update_condition);
					} else {
						$result = $this->to->insert_row($to_table,$insert_set);
					}
				}
			} while(count($ARRAY_BUFFER));
		} // foreach
		$c++;
		//if($c>7) break;
	}

	foreach($table_list as $table) {
		$this->set_table_sequencer($table);
	}
}

function set_table_sequencer($table) {
	$sql = "SELECT pg_catalog.setval('".$table."_id_seq', (select max(id) from $table), true)";
	echo "SQL=$sql\n";
	$this->to->exec_sql($sql);
	//$sql = "ALTER TABLE $table ALTER COLUMN id SET DEFAULT nextval('".$table."_id_seq'::regclass)";
	//echo "SQL=$sql\n";
	//$this->to->exec_sql($sql);
}


} // end of move_db class

?>
