<?php
	require_once dirname(__FILE__)."/../bin/data/databaseFactory.php";
	class registryDAL{
		static private $registry;

		//讀取網站配置表值
		static public function getRegistryValue($name){
			if(!isset(self::$registry)){
				self::$registry = self::getRegistryValues();
			}
			return self::$registry[$name];
		}

		//讀取網站配置表值,以陣列形式返回;Name為鍵,Value為值
		static public function getRegistryValues(){
			$returnValue = array();
			$data = databaseFactory::createDatabase();
			$sql = "SELECT * FROM `registry`";
			$rs = $data->executeRecordset($sql);
			foreach($rs as $arr){
				$returnValue[$arr["Name"]] = $arr["Value"];
			}
			return $returnValue;
		}

		//儲存網站配置表.如果已經存在該項,則更新;否則新增
		static public function saveRegistry($name,$value){
			$data = databaseFactory::createDatabase();
			$sql = "SELECT COUNT(*) FROM `registry` WHERE `Name`='".$name."'";
			if($data->executeScalar($sql) > 0)
				$sql = "UPDATE `registry` SET `Value`='".$value."' WHERE `Name`='".$name."'";
			else
				$sql = "INSERT INTO `registry` (`Name`, `Value`) VALUES ('".$name."','".$value."')";
			$data->executeNonQuery($sql);
			self::$registry = null;
		}
	}
?>