-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbrm.php
executable file
·189 lines (179 loc) · 6.18 KB
/
dbrm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace jemdev\dbrm;
use jemdev\dbrm\table;
use jemdev\dbrm\init\genereconf;
/**
* DataBase Relational Mapping class.
*
* Classe principale du package permettant de récupérer une instance de connexion ainsi
* que des instances nécessaires pour toutes les opération SQL.
*
* @author Jean Molliné <jmolline@jem-dev.com.com>
* @version 2.1 Ré-écriture du package pour PHP 8.*
*
*/
class dbrm
{
/**
*
* @var array
*/
private $_aDbConf;
private $_server;
/**
* Nom du schéma de données sur lequel on travaille.
* @var string
*/
private $_schema;
private $_user;
private $_mdp;
private $_type;
private $_port;
private $_cheminRepertoireDbConf;
private $_cheminFichierDbConf;
/**
* Instance de la classe jemdev\dbrm\vue
*
* Note importante : il faut interprêter le mot « vue » au sens SQL du terme et non
* par rapport à une méthode d'affichage quelconque.
*
* @var vue
*/
private $_oVue;
/**
* Constructeur : point d'accès unique du package d'accès aux données.
*
* @param string $server Adresse du serveur, Ex. « localhost »
* @param string $schema Nom du schéma de données dont on va définir la configuration
* @param string $user Utilisateur pouvant se connecter, Ex. « root »
* @param string $mdp Mot-de-Passe de l'utilisateur
* @param string $type Type de SGBDR, Ex. « mysql »
* @param string $port Port sur lequel doit être effectuée la connexion, Ex. « 3306 »
*/
public function __construct(string $server, string $schema, string $user, string $mdp, string $type = 'mysql', string $port = '3306')
{
$this->_server = $server;
$this->_schema = $schema;
$this->_user = $user;
$this->_mdp = $mdp;
$this->_type = $type;
$this->_port = $port;
$this->_cheminRepertoireDbConf = realpath(__DIR__ . DIRECTORY_SEPARATOR .'conf'. DIRECTORY_SEPARATOR);
$this->_cheminFichierDbConf = $this->_cheminRepertoireDbConf .'dbConf.php';
}
/**
* Récupération d'une instance de ligne de table permettant d'y effectuer des écritures.
* @param string $nomTable
* @return jemdev\dbrm\ligneInstance ou FALSE si le fichier de configuration n'est pas défini.
*/
public function getLigneInstance(string $nomTable): ligneInstance|false
{
/**
* Chargement du système d'accès aux données et aux objets.
* Attention au chemin, il est relatif au fichier courant.
*/
if(!is_null($this->_aDbConf))
{
$oTable = new table($this->_schema, $nomTable, $this->_aDbConf);
$objet = $oTable->getInstance();
}
else
{
$objet = false;
}
return $objet;
}
/**
* Récupération de l'instance d'accès aux données.
*
* @return vue
*/
public function getVueInstance(): vue
{
if(is_null($this->_oVue))
{
$this->_oVue = vue::getInstance($this->_aDbConf[0]);
}
return($this->_oVue);
}
/**
* Ré-initialisation du fichier de configuration du schéma de données.
* @param string $cheminRepertoireConf Chemin absolu vers le répertoire où sera stocké le ficher de configuration
* @return boolean
*/
public function resetDbConf(string $cheminRepertoireConf): bool
{
if(is_null($this->_oVue))
{
$this->getVueInstance();
}
$this->setCheminFichierConf($cheminRepertoireConf);
$oConfGenerator = new genereconf($this->_schema, $this->_user, $this->_mdp, $this->_user, $this->_mdp, $this->_type, $this->_server, $this->_port);
$bSetConf = $oConfGenerator->genererConf($this->_oVue, $this->_cheminFichierDbConf);
return($bSetConf);
}
/**
* [Re]définit le chemin vers le fichier de configuration du schéma de données.
*
* Si le répertoire n'existe pas, une exception sera levée.
* Si le fichier n'existe pas, il sera créé.
*
* @param string $cheminRepertoireConf
* @throws jemdevDbrmException
*/
public function setCheminFichierConf(string $cheminRepertoireConf): void
{
$cheminRep = preg_replace("#/#", DIRECTORY_SEPARATOR, $cheminRepertoireConf);
$this->_cheminRepertoireDbConf = rtrim($cheminRep, DIRECTORY_SEPARATOR);
if(file_exists($this->_cheminRepertoireDbConf) && is_dir($this->_cheminRepertoireDbConf))
{
$this->_cheminFichierDbConf = $this->_cheminRepertoireDbConf . DIRECTORY_SEPARATOR . 'dbConf.php';
}
else
{
throw new jemdevDbrmException("Le répertoire de stockage du fichier de configuration est introuvable", E_USER_ERROR);
}
if(is_null($this->_cheminFichierDbConf) || !file_exists($this->_cheminFichierDbConf))
{
$this->_aDbConf = array(
0 => array(
'schema' => array(
'name' => $this->_schema,
'SGBD' => $this->_type,
'server' => $this->_server,
'port' => $this->_port,
'user' => $this->_user,
'mdp' => $this->_mdp,
'pilote' => $this->_type
),
'tables' => [],
'relations' => [],
'vues' => []
)
);
$this->_oVue = $this->getVueInstance();
$this->resetDbConf($cheminRepertoireConf);
}
else
{
include($this->_cheminFichierDbConf);
$this->_aDbConf = $dbConf;
}
}
public function startTransaction(): void
{
if(is_null($this->_oVue))
{
$this->getVueInstance();
}
$this->_oVue->startTransaction();
}
public function finishTransaction($bOk): void
{
if(is_null($this->_oVue))
{
$this->getVueInstance();
}
$this->_oVue->finishTransaction($bOk);
}
}