]> git.mxchange.org Git - friendica.git/blob - library/dddbl2/inc/database.func.php
The worker settings are now available in the admin settings
[friendica.git] / library / dddbl2 / inc / database.func.php
1 <?php
2
3 namespace DDDBL;
4
5 /**
6   * @returns (PDO) - reference to PDO object
7   * @returns (boolean) false, if there is no connection to the database
8   *
9   * if there is a connection to the database,
10   * the PDO object is returned otherwise false
11   *
12   **/
13 function getDB() {
14
15   if(!isConnected())
16     return false;
17   
18   $objDB = getDBDataObject();
19   
20   return $objDB->get('PDO');
21
22 }
23
24 /**
25   * @returns (boolean) true, if connection exists or is established
26   * @returns (boolean) false, if connection could not be established
27   *
28   * if no connection to the database exists, establishe one.
29   *
30   **/
31 function connect() {
32
33   if(isConnected())
34     return true;
35
36   $objDB = getDBDataObject();
37
38   try {
39     $objPDO = new \PDO($objDB->get('CONNECTION'),
40                        $objDB->get('USER'),
41                        $objDB->get('PASS'));
42   } catch (\Exception $objException) {
43     return false;
44   }
45
46   $objDB->update(array('PDO' => $objPDO));
47
48   return true;
49
50 }
51
52 /**
53   * disconnect from the database
54   *
55   **/
56 function disconnect() {
57
58   $objDB = getDBDataObject();
59   
60   $objPDO = $objDB->get('PDO');
61   $objPDO = null;
62   
63   $objDB->delete('PDO');
64
65 }
66
67 /**
68   * check if a connection to the database is established
69   *
70   **/
71 function isConnected() {
72
73   $objDB = getDBDataObject();
74   
75   if(!$objDB->exists('PDO'))
76     return false;
77   
78   return true;
79
80 }
81
82 /**
83   * @returns (boolean) true, if transaction started
84   * @returns (boolean) false, if transaction could not be started
85   * @returns (boolean) false, if no connection to database exists
86   *
87   * start a transaction
88   *
89   **/
90 function startTransaction() {
91
92   return mapMethod('beginTransaction');
93
94 }
95
96 /**
97   * @returns (boolean) true, if there is an active transaction
98   * @returns (boolean) false, if there is no active transaction
99   * @returns (boolean) false, if no connection to database exists
100   *
101   * check if there is an active transaction
102   *
103   **/
104 function inTransaction() {
105
106   return mapMethod('inTransaction');
107
108 }
109
110 /**
111   * @returns (boolean) true, if rollback was successfull
112   * @returns (boolean) false, if rollback was not successfull
113   * @returns (boolean) false, if no connection to database exists
114   *
115   * perform a rollback of the active transaction
116   *
117   **/
118 function rollback() {
119
120   return mapMethod('rollback');
121
122 }
123
124 /**
125   * @returns (boolean) true, if commit was successfull
126   * @returns (boolean) false, if commit was not successfull
127   * @returns (boolean) false, if no connection to database exists
128   *
129   * commit the active transaction
130   *
131   **/
132 function commit() {
133
134   return mapMethod('commit');
135
136 }
137
138 /**
139   * @returns (array) - list of error-information
140   *
141   * get information about an error
142   *
143   **/
144 function getErrorInfo() {
145
146   return mapMethod('errorInfo');
147
148 }
149
150 /**
151   * change the active database-connection. all db-functions
152   * are performed at the new connection.
153   *
154   * ATTENTION: the old connection is *not* closed!
155   *
156   **/
157 function changeDB($strIdentifier) {
158
159   $objDataObjectPool = new DataObjectPool('Database-Definition');
160   
161   $objNewDB = $objDataObjectPool->get($strIdentifier);
162   
163   $objDataObjectPool->delete('DEFAULT');
164   $objDataObjectPool->add('DEFAULT', $objNewDB->getAll());
165
166 }
167
168
169 /**
170   * @returns (DataObject) - reference to the DataObject of default connection
171   *
172   * returns the DataObject of the default connection.
173   *
174   **/
175 function getDBDataObject() {
176
177   $objDataObjectPool = new DataObjectPool('Database-Definition');
178   return $objDataObjectPool->get('DEFAULT');
179
180 }
181
182 /**
183   * @throws UnexpectedParameterTypeException - if the given parameter is not a string
184   *
185   * @returns (boolean) false, if no connection is established
186   *
187   * check if a connection to the database is established. if so,
188   * the given parameter is used, as method to call at the 
189   * PDO object. the result of the call is returned
190   *
191   **/
192 function mapMethod($strMethod) {
193
194   if(!is_string($strMethod))
195     throw new UnexpectedParameterTypeException('string', $strMethod);
196
197   if(!isConnected())
198     return false;
199     
200   $objDB = getDBDataObject();
201   
202   $objPDO = $objDB->get('PDO');
203   
204   return $objPDO->$strMethod();
205
206 }