You can create a Connections class that would be your interface to the database in front of your CONN class, within which constructor to define what type of database you will use. After instantiating the Connections class according to the database you are going to access, simply create the methods in both classes with the same name, you should use the same name in the methods in both classes that handle queries. This will avoid flow deviations in your code.
Or create an Abstract class and inherit in its CONN class and within the Abstract class develop the method that defines which type of database will use making CONN access the methods also for handling the querys in both types of databases.
Suggestion for future maintenance purpose, the first way is more efficient.
I have some systems that work supporting older versions of the same so if there are still doubts I can post an example in php working.
I’ll set a simple example :
Arquivo Classe Modelo Connections
class Connections{
public function __construct($TDatabase){
if(strcasecmp($TDatabase,"MYSQL") == 0){
$CONN_MSSQL = new MSSQL();
}else{
$CONN_MYSQL = new MYSQL();
}
}
}
MSSQL ARQUIVO CONSTANTES mssql.var.php
define('HOST','ip de acesso ao BD');
define('Porta','porta que seu banco de dados trabalha');
define('user','usuario de acesso ao banco');
define('senha','senha de acesso ao banco');
Arquivo Classe Modelo MSSQL
include mssql.var.php
class MSSQL{
public function __construct(){
// estabelece conexão com o banco MSSQL, utilize as constantes
//definidas no arquivo var
}
public function GetAllUsuarios(){
/* metodo executa querie no banco de dados MSSQL pegando todos os
clientes cadastrados*/
}
}
MYSQL ARQUIVO CONSTANTES mysql.var.php
define('HOST','ip de acesso ao BD');
define('Porta','porta que seu banco de dados trabalha');
define('user','usuario de acesso ao banco');
define('senha','senha de acesso ao banco');
Arquivo Classe Modelo MYSQL
include mysql.var.php
class MYSQL{
public function __construct(){
// estabelece conexão com o banco Mysql, utilize as constantes
//definidas no arquivo var
}
public function GetAllUsuarios(){
/* metodo executa querie no banco de dados MYSQL pegando todos os
clientes cadastrados*/
}
}
Ready built a very simple operating structure just to increase understanding to access the bank you should call any interface of your system in this way :
$Connections = new Connections('MYSQL');
$listaUsuarios = $Connections->GetAllUsuarios();
print_r($listaUsuarios);
Note that I am reading the MYSQL data because I informed in the constructor of the Connections class the database I wanted to work on.
This form maintains the independence of the data, that is, it separates the system from the type of database it is using. It also reduces coupling in the system.
Follow connection in Singleton standard
I’m happy to help
Solomon hugs. = P
I do not know how MSSQL works, but considering that all dependencies are installed, it would not just change the parameter in the PDO instance?
– Woss