Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
CDataBaseAccessNoPDO.php
1 <?php
2 
3 /**
4  * CDataBaseAccessNoPDO.php - Implementation file for Ms Access DataBase class using ODBC.
5  *
6  * @package SellerDeck Extensions
7  *
8  * @author Péter Erdődi
9  * @copyright © SellerDeck Ltd 2015. All rights reserved.
10  */
11 
12 namespace SDExtension\DB;
13 
15  {
16 
17  /**
18  * __construct - Object constructor method
19  *
20  * @access public
21  * @param array $aConnectionDetails
22  * @return void
23  */
24  public function __construct($aConnectionDetails, $sDatabaseID)
25  {
26  \SDExtension\Helper\CLogger::get()->addDebug(__CLASS__);
27  try
28  {
29  $sDsn = arr_get($aConnectionDetails, 'sDsn', '');
30  $this->m_sDBUser = arr_get($aConnectionDetails, 'sUser', '');
31  $sPassword = arr_get($aConnectionDetails, 'sPassword', '');
32  $this->m_sDataBaseID = arr_get($aConnectionDetails, 'sDatabaseID', '');
33  $this->m_aDataBases = arr_get($aConnectionDetails, 'aDataBases', []);
34  $this->m_oDB = new CMockAccessOdbcPdo($sDsn, $this->m_sDBUser, $sPassword);
35  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addInfo("Connected to Database: $sDatabaseID");
36  }
37  catch (\Exception $e)
38  {
39  echo $e->getMessage();
40  }
41  }
42 
43  }
44 
45 /**
46  * CMockAccessOdbcPdo - Mock PDO Class to using ODBC
47  *
48  * @package SellerDeck Extensions
49  *
50  * @author Péter Erdődi
51  * @copyright © SellerDeck Ltd 2015. All rights reserved.
52  */
54  {
55 
56  /**
57  * @var object $m_oConnection Connection
58  */
59  protected $m_oConnection;
60 
61  /**
62  * @var object $m_oResult Result
63  */
64  protected $m_oResult = null;
65 
66  /**
67  * __construct - Object constructor method
68  *
69  * @access public
70  * @param string $sDsn DSN
71  * @param string $sDBUser DB User
72  * @param string $sPassword Password
73  * @return void
74  */
75  public function __construct($sDsn, $sDBUser, $sPassword)
76  {
77  $this->m_oConnection = odbc_connect($sDsn, $sDBUser, $sPassword);
78  }
79 
80  /**
81  * prepare - Prepare query
82  *
83  * @access public
84  * @param string $sQuery Query
85  * @return CMockAccessOdbcPdoStatement
86  */
87  public function prepare($sQuery)
88  {
89  return new CMockAccessOdbcPdoStatement($this->m_oConnection, $sQuery);
90  }
91 
92  /**
93  * tables - Getting tables
94  *
95  * @access public
96  * @return resource|bool An ODBC result identifier containing table information or FALSE on failure.
97  */
98  public function tables()
99  {
100  return odbc_tables($this->m_oConnection);
101  }
102 
103  /**
104  * tableExists - Does table exist?
105  *
106  * @access public
107  * @param string $sTableName Table Name
108  * @return bool Table exist
109  */
110  public function tableExists($sTableName)
111  {
112  $oResult = $this->tables();
113  $bFound = false;
114  while (!$bFound && $aResult = odbc_fetch_array($oResult))
115  {
116  $bFound = $sTableName == $aResult["TABLE_NAME"];
117  }
118  return $bFound;
119  }
120 
121  /**
122  * lastInsertId - Last Insert ID
123  *
124  * @access public
125  * @return mixed Last Insert ID
126  */
127  public function lastInsertId()
128  {
129  $oStatement = odbc_prepare($this->m_oConnection, "SELECT LAST_INSERT_ID()");
130  odbc_execute($oStatement);
131  $oResource = odbc_fetch_array($oStatement);
132  return $oResource['LAST_INSERT_ID'];
133  }
134 
135  /**
136  * exec - Execute query
137  *
138  * @access public
139  * @param string $sStatement Statement
140  * @return int|bool Number of rows or FALSE on failure
141  */
142  public function exec($sStatement)
143  {
144  $oResult = odbc_exec($this->m_oConnection, $sStatement);
145  return $oResult === false ? false : odbc_num_rows($oResult);
146  }
147 
148  /**
149  * query - Execute query
150  *
151  * @access public
152  * @param string $sStatement Statement
153  * @param int $nReturnType Return Type
154  * @return CMockAccessOdbcPdo This object
155  */
156  public function query($sStatement, $nReturnType = \PDO::FETCH_ASSOC)
157  {
158  $this->m_oResult = odbc_exec($this->m_oConnection, $sStatement);
159  return $this;
160  }
161 
162  /**
163  * fetch - Fetch result array
164  *
165  * @access public
166  * @param string $sFetchStyle Fetch Style
167  * @param string $sCursorOrientation Cursor Orientation
168  * @param int $nCursorOffset Cursor Offset
169  * @return array|bool An array that corresponds to the fetched row, or FALSE if there are no more rows.
170  */
171  public function fetch($sFetchStyle = null, $sCursorOrientation = \PDO::FETCH_ORI_NEXT, $nCursorOffset = 0)
172  {
173  return odbc_fetch_array($this->m_oResult);
174  }
175 
176  /**
177  * fetchAll - Fetch all results
178  *
179  * @access public
180  * @param string $sFetchStyle Fetch Style
181  * @param string $sFetchArgument Fetch Argument
182  * @param array $aCtorArgs Ctor Args
183  * @return array Array of results
184  */
185  public function fetchAll($sFetchStyle = null, $sFetchArgument = null, array $aCtorArgs = [])
186  {
187  $aResultSet = [];
188  while ($aResult = odbc_fetch_array($this->m_oResult))
189  {
190  $aResultSet[] = $aResult;
191  }
192  return $aResultSet;
193  }
194 
195  }
196 
197 /**
198  * CMockAccessOdbcPdoStatement - Mock PDO Statement Class to using ODBC
199  *
200  * @package SellerDeck Extensions
201  *
202  * @author Péter Erdődi
203  * @copyright © SellerDeck Ltd 2015. All rights reserved.
204  */
206  {
207 
208  /**
209  * @var string $m_sQuery Query
210  */
211  protected $m_sQuery;
212 
213  /**
214  * @var array $m_aParam Param
215  */
216  protected $m_aParams;
217 
218  /**
219  * @var object $m_oStatement Statement
220  */
221  protected $m_oStatement;
222 
223  /**
224  * __construct - Object constructor method
225  *
226  * @access public
227  * @param object $oConnection Connection
228  * @param string $sQuery Query
229  * @return void
230  */
231  public function __construct($oConnection, $sQuery)
232  {
233 // $this->m_sQuery = preg_replace('/(?<=\s|^):[^\s:]++/um', '?', $sQuery);
234  $this->m_sQuery = $sQuery;
235  $this->m_aParams = null;
236  $this->extractParam($sQuery);
237  $this->m_oStatement = odbc_prepare($oConnection, $this->m_sQuery);
238  }
239 
240  /**
241  * bindValue - Bind Value
242  *
243  * @access public
244  * @param string $sParam Parameter
245  * @param mixed $vValue Value
246  * @return void
247  */
248  public function bindValue($sParam, $vValue)
249  {
250  $this->m_aParams[$sParam] = $vValue;
251  }
252 
253  /**
254  * execute - Execute
255  *
256  * @access public
257  * @return void
258  */
259  public function execute()
260  {
261  if ($this->m_aParams == null)
262  {
263  odbc_execute($this->m_oStatement);
264  }
265  else
266  {
267  odbc_execute($this->m_oStatement, $this->m_aParams);
268  }
269  $this->clearParam();
270  }
271 
272  /**
273  * fetch - Fetch
274  *
275  * @access public
276  * @param mixed $vOption Options
277  * @return array|bool An array that corresponds to the fetched row, or FALSE if there are no more rows.
278  */
279  public function fetch($vOption = null)
280  {
281  return odbc_fetch_array($this->m_oStatement);
282  }
283 
284  /**
285  * extractParam - Extract Parameters
286  *
287  * @access protected
288  * @param mixed $sQuery Query
289  * @return void
290  */
291  protected function extractParam($sQuery)
292  {
293  $aQueries = explode(" ", $sQuery);
294  $nIndex = 0;
295 
296  while (isset($aQueries[$nIndex]))
297  {
298  if (preg_match("/^:/", $aQueries[$nIndex]))
299  {
300  $this->m_aParams[$aQueries[$nIndex]] = null;
301  }
302  ++$nIndex;
303  }
304  }
305 
306  /**
307  * clearParam - Clear Parameters
308  *
309  * @access protected
310  * @return void
311  */
312  protected function clearParam()
313  {
314  $nIndex = 0;
315 
316  while (isset($this->m_aParams[$nIndex]))
317  {
318  $this->m_aParams[$nIndex] = null;
319  ++$nIndex;
320  }
321  }
322 
323  }
query($sStatement, $nReturnType=\PDO::FETCH_ASSOC)
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
__construct($sDsn, $sDBUser, $sPassword)
__construct($aConnectionDetails, $sDatabaseID)
fetch($sFetchStyle=null, $sCursorOrientation=\PDO::FETCH_ORI_NEXT, $nCursorOffset=0)
fetchAll($sFetchStyle=null, $sFetchArgument=null, array $aCtorArgs=[])