<?php
class Model_Manager_Model extends Model {
	function __construct(){
		parent::__construct();
		$this->db	=	new Database();
	}
	function getModelList(){
		return $this->db->get('model')->result_array();
	}
	function getModel($model_id){
		$this->db->where("model_id",$model_id);
		return $this->db->get('model')->current();
	}
	function runquery($sql) {
		global $lang, $dbcharset, $db, $dbcharset;
		$ret = array();
		$num = 0;
		foreach(explode(";", trim($sql)) as $query){
			$queries = explode("\n", trim($query));
			foreach($queries as $query) {
				@$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
			}
			$num++;
		}
		unset($sql);
		foreach($ret as $query) {
			$query = trim($query);
			if($query) {
				if(!$this->db->query($query)){
					return false;
				}
			}
		}
		return true;
	}
	function createtable($sql, $dbcharset) {
		$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
		$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
		return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
	}
	function add_model($model){
		$this->db->insert('model',$model);
		return $this->db->insert_id();
	}
	function delete_model($model_id){
		$this->db->where("model_id",$model_id);
		return $this->db->delete('model');
	}
	function valid_model($model_name,$model_value){
		$this->db->where("model_name",$model_name);
		$this->db->orwhere("model_value",$model_value);
		return $this->db->get('model')->count();
	}
	function drop_table($table){
		return $this->db->query("drop table ".DBFIX."$table");
	}
	function update($model,$Modeid){
		return $this->db->where("model_id",$Modeid)->update("model",$model);
	}
}