From: dj3c1t Date: Sun, 8 Jun 2014 22:19:01 +0000 (+0200) Subject: implementation sgbd herite de mw_sgbd X-Git-Tag: mtweb.1.0.0~5 X-Git-Url: http://git.dj3c1t.com/index.cgi?a=commitdiff_plain;ds=sidebyside;h=8da84cf3aa4d10d91f19b6df06ce4c5e9fcb79da;p=mtweb implementation sgbd herite de mw_sgbd --- diff --git a/mw/app/controllers/install/index.php b/mw/app/controllers/install/index.php index 0f7b27c..96b917d 100644 --- a/mw/app/controllers/install/index.php +++ b/mw/app/controllers/install/index.php @@ -77,14 +77,9 @@ else $env->message("merci de choisir un mot de passe pour l'administrateur"); if(!$env->messages()){ $sgbds[$current_sgbd] = null; - $impl = new $current_sgbd($params); + $impl = new $current_sgbd($env, $params); $sgbds[$current_sgbd] = $impl; - $data->set_sgbd( - new mw_sgbd( - $sgbds[$current_sgbd], - $env - ) - ); + $data->set_sgbd($sgbds[$current_sgbd]); $data->load_modules($env->path("mw_dir")."app/", "data/modules/".($current_sgbd == "mw_xml" ? "xml" : "sql")."/"); if( $current_sgbd == "mw_xml" diff --git a/mw/app/data/impl/mw_mysql.php b/mw/app/data/impl/mw_mysql.php index 8a58384..5128c04 100644 --- a/mw/app/data/impl/mw_mysql.php +++ b/mw/app/data/impl/mw_mysql.php @@ -1,32 +1,26 @@ host = isset($params["host"]) ? $params["host"] : "localhost"; - $this->base = isset($params["base"]) ? $params["base"] : "mtweb"; - $this->user = isset($params["user"]) ? $params["user"] : ""; - $this->password = isset($params["password"]) ? $params["password"] : ""; - $this->EXTENTION_OK = function_exists("mysql_connect"); + class mw_mysql extends mw_sgbd{ + + public function name(){ + return "MySql"; } - public function extention_ok(&$env){ - return $this->EXTENTION_OK; + public function default_params(){ + return array( + "host" => "localhost", + "base" => "mtweb", + "user" => "", + "password" => "" + ); } - public function authentication_required(){ - return true; + public function validate_extention(){ + return function_exists("mysql_connect"); } - public function sgbd_name(){ - return "MySql"; + public function authentication_required(){ + return true; } public function connect($host, $base, $user, $password){ @@ -47,6 +41,7 @@ } public function desc_table($table_name){ + $table_name = $this->replace_prefixes($table_name); $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'"; $rst = $this->query($sql); $desc = array( @@ -69,7 +64,8 @@ return $desc; } - public function table_exists($table_name){ + public function table_exists($table_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); $EXISTS = false; try{ @@ -89,6 +85,7 @@ } public function field_exists($table_name, $field_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); if(!($desc = $this->desc_table($table_name))){ throw new Exception($this->exception_out("Impossible de lire la description de la table")); @@ -103,7 +100,8 @@ return $EXISTS; } - public function query($query_string){ + public function query($query_string){ + $query_string = $this->replace_prefixes($query_string); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); $result = @mysql_query($query_string, $this->link); if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete")); diff --git a/mw/app/data/impl/mw_pdo_mysql.php b/mw/app/data/impl/mw_pdo_mysql.php index e8ae3ee..a230626 100644 --- a/mw/app/data/impl/mw_pdo_mysql.php +++ b/mw/app/data/impl/mw_pdo_mysql.php @@ -1,38 +1,28 @@ host = isset($params["host"]) ? $params["host"] : "localhost"; - $this->base = isset($params["base"]) ? $params["base"] : "mtweb"; - $this->user = isset($params["user"]) ? $params["user"] : ""; - $this->password = isset($params["password"]) ? $params["password"] : ""; - $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_mysql")); + class mw_pdo_mysql extends mw_sgbd{ + + public function name(){ + return "PDO MySql"; } - public function get_link(){ - return $this->link; + public function default_params(){ + return array( + "host" => "localhost", + "base" => "mtweb", + "user" => "", + "password" => "" + ); } - public function extention_ok(&$env){ - return $this->EXTENTION_OK; + public function validate_extention(){ + return extension_loaded("pdo") && extension_loaded("pdo_mysql"); } public function authentication_required(){ return true; } - public function sgbd_name(){ - return "PDO MySql"; - } - public function connect($host, $base, $user, $password){ try{ $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password); @@ -51,6 +41,7 @@ } public function desc_table($table_name){ + $table_name = $this->replace_prefixes($table_name); $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'"; $rst = $this->query($sql); $desc = array( @@ -74,6 +65,7 @@ } public function table_exists($table_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); $EXISTS = false; try{ @@ -93,6 +85,7 @@ } public function field_exists($table_name, $field_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); if(!($desc = $this->desc_table($table_name))){ throw new Exception($this->exception_out("Impossible de lire la description de la table")); @@ -107,7 +100,8 @@ return $EXISTS; } - public function query($query_string){ + public function query($query_string){ + $query_string = $this->replace_prefixes($query_string); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); if(!($result = $this->link->query($query_string))){ throw new Exception($this->exception_out("Syntaxe invalide dans une requete")); diff --git a/mw/app/data/impl/mw_pdo_sqlite.php b/mw/app/data/impl/mw_pdo_sqlite.php index 8fd7deb..918bc8a 100644 --- a/mw/app/data/impl/mw_pdo_sqlite.php +++ b/mw/app/data/impl/mw_pdo_sqlite.php @@ -1,38 +1,28 @@ host = isset($params["host"]) ? $params["host"] : "content/data/sqlite"; - $this->base = isset($params["base"]) ? $params["base"] : "mtweb.db"; - $this->user = isset($params["user"]) ? $params["user"] : ""; - $this->password = isset($params["password"]) ? $params["password"] : ""; - $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_sqlite")); + class mw_pdo_sqlite extends mw_sgbd{ + + public function name(){ + return "PDO SQLite"; } - public function get_link(){ - return $this->link; + public function default_params(){ + return array( + "host" => "content/data/sqlite", + "base" => "mtweb.db", + "user" => "", + "password" => "" + ); } - public function extention_ok(&$env){ - return $this->EXTENTION_OK; + public function validate_extention(){ + return extension_loaded("pdo") && extension_loaded("pdo_sqlite"); } public function authentication_required(){ return false; } - public function sgbd_name(){ - return "PDO SQLite"; - } - public function connect($host, $base, $user, $password){ if($host) $host .= substr($host, -1) != "/" ? "/" : ""; try{ @@ -53,6 +43,7 @@ } public function desc_table($table_name){ + $table_name = $this->replace_prefixes($table_name); if(strpos($table_name, "'") !== false){ throw new Exception($this->exception_out("nom de table avec un simple quote")); } @@ -78,6 +69,7 @@ } public function table_exists($table_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); $EXISTS = false; try{ @@ -97,6 +89,7 @@ } public function field_exists($table_name, $field_name){ + $table_name = $this->replace_prefixes($table_name); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); if(!($desc = $this->desc_table($table_name))){ throw new Exception($this->exception_out("Impossible de lire la description de la table")); @@ -111,7 +104,8 @@ return $EXISTS; } - public function query($query_string){ + public function query($query_string){ + $query_string = $this->replace_prefixes($query_string); if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); if(!($result = $this->link->query($query_string))){ throw new Exception($this->exception_out("Syntaxe invalide dans une requete")); diff --git a/mw/app/data/impl/mw_xml.php b/mw/app/data/impl/mw_xml.php index 3d09caf..67f2447 100644 --- a/mw/app/data/impl/mw_xml.php +++ b/mw/app/data/impl/mw_xml.php @@ -1,67 +1,61 @@ init_xml_env( - isset($params["host"]) ? $params["host"] : "content/data/xml", - isset($params["base"]) ? $params["base"] : "mw", - isset($params["user"]) ? $params["user"] : "", - isset($params["password"]) ? $params["password"] : "" + public function name(){ + return "XML"; + } + + public function default_params(){ + return array( + "host" => "content/data/xml", + "base" => "mw", + "user" => "", + "password" => "" ); - $this->EXTENTION_OK = true; - } - - public function extention_ok(&$env){ - if($this->EXTENTION_OK){ - if( - file_exists($env->app_file("data/impl/xml/mw_xml_data.php")) - && file_exists($env->app_file("data/impl/xml/mw_xml_data_handler.php")) - ){ - if(!class_exists("mw_xml_data")) require_once $env->app_file("data/impl/xml/mw_xml_data.php"); - if(!class_exists("mw_xml_data_handler")) require_once $env->app_file("data/impl/xml/mw_xml_data_handler.php"); - if( - class_exists("mw_xml_data") - && class_exists("mw_xml_data_handler") - ){ - $this->xml_data = new mw_xml_data($this->host, $this->base); - } - else $this->EXTENTION_OK = false; - } - else $this->EXTENTION_OK = false; - } - return $this->EXTENTION_OK; } public function authentication_required(){ return false; } - public function sgbd_name(){ - return "XML"; - } - - public function init_xml_env($host, $base, $user, $password){ - $this->host = $host.($host && substr($host, -1) != "/" ? "/" : ""); - $this->base = $base.($base && substr($base, -1) != "/" ? "/" : ""); - $this->user = $user; - $this->password = $password; + public function prepare_params($params){ + if(isset($params["host"])){ + $params["host"] .= $params["host"] && substr($params["host"], -1) != "/" ? "/" : ""; + } + if(isset($params["base"])){ + $params["base"] .= $params["base"] && substr($params["base"], -1) != "/" ? "/" : ""; + } $this->data_handlers = array(); $this->last_data_handler = 0; + return $params; + } + + public function validate_extention(){ + if( + !file_exists($this->env->app_file("data/impl/xml/mw_xml_data.php")) + || !file_exists($this->env->app_file("data/impl/xml/mw_xml_data_handler.php")) + ){ + return false; + } + if(!class_exists("mw_xml_data")) require_once $this->env->app_file("data/impl/xml/mw_xml_data.php"); + if(!class_exists("mw_xml_data_handler")) require_once $this->env->app_file("data/impl/xml/mw_xml_data_handler.php"); + if( + !class_exists("mw_xml_data") + || !class_exists("mw_xml_data_handler") + ){ + return false; + } + $this->xml_data = new mw_xml_data($this->host, $this->base); + return true; } public function connect($host, $base, $user, $password){ if($host.$base && is_dir($host.$base) && is_writable($host.$base)){ - $this->init_xml_env($host, $base, $user, $password); $this->xml_data = new mw_xml_data($this->host, $this->base); return true; } diff --git a/mw/app/data/modules/share/mw_data_sgbds.php b/mw/app/data/modules/share/mw_data_sgbds.php index c93d814..c40af56 100644 --- a/mw/app/data/modules/share/mw_data_sgbds.php +++ b/mw/app/data/modules/share/mw_data_sgbds.php @@ -16,10 +16,10 @@ require_once $impls_dir."/".$impl_file; if(class_exists($class_name = substr($impl_file, 0, -4))){ if( - method_exists($class_name, "sgbd_name") + method_exists($class_name, "name") && method_exists($class_name, "extention_ok") ){ - $impl = new $class_name(); + $impl = new $class_name($env); if($impl->extention_ok($env)) $sgbds[$class_name] = $impl; } } diff --git a/mw/app/init/0300_data.php b/mw/app/init/0300_data.php index fd785f7..672de25 100644 --- a/mw/app/init/0300_data.php +++ b/mw/app/init/0300_data.php @@ -13,16 +13,14 @@ if(!class_exists($sgbd_impl)){ $this->erreur("Impossible de trouver la classe d'implementation du sgbd ".$this->bdd("sgbd"), true); } - $sgbd = new mw_sgbd( - new $sgbd_impl( - array( - "host" => $this->bdd("host"), - "base" => $this->bdd("base"), - "user" => $this->bdd("user"), - "password" => $this->bdd("password") - ) - ), - $this + $sgbd = new $sgbd_impl( + $this, + array( + "host" => $this->bdd("host"), + "base" => $this->bdd("base"), + "user" => $this->bdd("user"), + "password" => $this->bdd("password") + ) ); if(!$sgbd->extention_ok()){ $this->erreur("L'extention php ".$this->bdd("sgbd")." n'est pas installée", true); diff --git a/mw/app/out/default/views/install/index.php b/mw/app/out/default/views/install/index.php index 426ffcf..525623c 100644 --- a/mw/app/out/default/views/install/index.php +++ b/mw/app/out/default/views/install/index.php @@ -10,7 +10,7 @@ diff --git a/mw/env/modules/mw_env_data.php b/mw/env/modules/mw_env_data.php index e5c3c4c..3cb19d1 100644 --- a/mw/env/modules/mw_env_data.php +++ b/mw/env/modules/mw_env_data.php @@ -14,6 +14,10 @@ } + // ------------------------------------------------------------------------------------------- + // class mw_data + // + class mw_data extends empty_class{ public function call_default($inst, $method_name, $arguments){ @@ -22,151 +26,70 @@ } - class mw_sgbd{ + // ------------------------------------------------------------------------------------------- + // class mw_sgbd + // - public $sgbd_impl; - public $env; + abstract class mw_sgbd{ - public function __construct($sgbd_impl, $env){ - $this->sgbd_impl = $sgbd_impl; + public $env; + public $link; + public $host; + public $base; + public $user; + public $password; + public $EXTENTION_OK; + + public function __construct($env, $params = array()){ $this->env = $env; + $default_params = $this->default_params(); + $params = $this->prepare_params($params); + $this->host = isset($params["host"]) ? $params["host"] : $default_params["host"]; + $this->base = isset($params["base"]) ? $params["base"] : $default_params["base"]; + $this->user = isset($params["user"]) ? $params["user"] : $default_params["user"]; + $this->password = isset($params["password"]) ? $params["password"] : $default_params["password"]; + $this->EXTENTION_OK = $this->validate_extention(); } - public function extention_ok(){ - return $this->sgbd_impl->extention_ok($this->env); + public function name(){ + return ""; } - public function authentication_required(){ - return $this->sgbd_impl->authentication_required(); + public function default_params(){ + return array( + "host" => "", + "base" => "", + "user" => "", + "password" => "" + ); } - public function connect($host, $base, $user, $password){ - return $this->sgbd_impl->connect($host, $base, $user, $password); + public function prepare_params($params){ + return $params; } - public function select_db($db_name){ - return $this->sgbd_impl->select_db($db_name); + public function validate_extention(){ + return false; } - # --------------------------------------------------------------------------------- - # SQL - # - - public function desc_table($table_name){ - if(!method_exists($this->sgbd_impl, "desc_table")) return false; - return $this->sgbd_impl->desc_table( - ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name - ); + public function authentication_required(){ + return false; } - public function table_exists($table_name){ - if(!method_exists($this->sgbd_impl, "table_exists")) return false; - return $this->sgbd_impl->table_exists( - ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name - ); + public function get_link(){ + return $this->link; } - public function field_exists($table_name, $field_name){ - if(!method_exists($this->sgbd_impl, "field_exists")) return false; - return $this->sgbd_impl->field_exists( - ( - $prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name, - $field_name - ); + public function extention_ok(){ + return $this->EXTENTION_OK; } - public function query($sql){ - if(!method_exists($this->sgbd_impl, "query")) return false; - return $this->sgbd_impl->query( + public function replace_prefixes($content){ + return ( ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $sql) - : $sql + str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $content) + : $content ); } - public function insert_id(){ - if(!method_exists($this->sgbd_impl, "insert_id")) return false; - return $this->sgbd_impl->insert_id(); - } - - public function fetch_assoc($rst){ - if(!method_exists($this->sgbd_impl, "fetch_assoc")) return false; - return $this->sgbd_impl->fetch_assoc($rst); - } - - public function free_result($rst){ - if(!method_exists($this->sgbd_impl, "")) return false; - return $this->sgbd_impl->free_result($rst); - } - - public function close(){ - if(!method_exists($this->sgbd_impl, "")) return false; - return $this->sgbd_impl->close(); - } - - # --------------------------------------------------------------------------------- - # XML - # - - public function data_exists($data_path){ - if(!method_exists($this->sgbd_impl, "data_exists")) return false; - return $this->sgbd_impl->data_exists($data_path); - } - - public function create_data($data_path){ - if(!method_exists($this->sgbd_impl, "create_data")) return false; - return $this->sgbd_impl->create_data($data_path); - } - - public function get_data($data_path, $data_id){ - if(!method_exists($this->sgbd_impl, "get_data")) return false; - return $this->sgbd_impl->get_data($data_path, $data_id); - } - - public function open_data($data_path, $FETCH = true){ - if(!method_exists($this->sgbd_impl, "open_data")) return false; - return $this->sgbd_impl->open_data($data_path, $FETCH); - } - - public function fetch_data($dh){ - if(!method_exists($this->sgbd_impl, "fetch_data")) return false; - return $this->sgbd_impl->fetch_data($dh); - } - - public function add_data($data_path, $data, $index = null){ - if(!method_exists($this->sgbd_impl, "add_data")) return false; - return $this->sgbd_impl->add_data($data_path, $data, $index); - } - - public function last_index($dh){ - if(!method_exists($this->sgbd_impl, "last_index")) return false; - return $this->sgbd_impl->last_index($dh); - } - - public function set_data($data_path, $data_id, $data){ - if(!method_exists($this->sgbd_impl, "set_data")) return false; - return $this->sgbd_impl->set_data($data_path, $data_id, $data); - } - - public function del_data($data_path, $data_id){ - if(!method_exists($this->sgbd_impl, "del_data")) return false; - return $this->sgbd_impl->del_data($data_path, $data_id); - } - - public function close_data($dh){ - if(!method_exists($this->sgbd_impl, "close_data")) return false; - return $this->sgbd_impl->close_data($dh); - } - - public function remove_data($data_path){ - if(!method_exists($this->sgbd_impl, "remove_data")) return false; - return $this->sgbd_impl->remove_data($data_path); - } - }