Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
CDataBaseAccess.php
1 <?php
2 
3 /**
4  * CDataBase.php - Implementation file for Ms Access DataBase class.
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  * @var string $m_sColumnQuoteBegin Begin quote sign for DB Columns
19  */
20  protected $m_sColumnQuoteBegin = "[";
21 
22  /**
23  * @var string $m_sColumnQuoteEnd End quote sign for DB Columns
24  */
25  protected $m_sColumnQuoteEnd = "]";
26 
27  /**
28  * __construct - Object constructor method
29  *
30  * @access public
31  * @param array $aConnectionDetails
32  * @return void
33  */
34  public function __construct($aConnectionDetails, $sDatabaseID)
35  {
36  $sDsn = arr_get($aConnectionDetails, 'sDsn', '');
37  $this->m_sDBUser = arr_get($aConnectionDetails, 'sUser', '');
38  $sPassword = arr_get($aConnectionDetails, 'sPassword', '');
39  $this->m_sDataBaseID = arr_get($aConnectionDetails, 'sDatabaseID', '');
40  $this->m_sDBAuthType = arr_get($aConnectionDetails, 'sDBAuthType', '');
41  $this->m_aDataBases = arr_get($aConnectionDetails, 'aDataBases', []);
42  try
43  {
44  $this->m_oDB = new \PDO($sDsn, $this->m_sDBUser, $sPassword);
45  }
46  catch (\PDOException $oPDOException)
47  {
48  $sDsn = str_replace("Driver={Microsoft Access Driver (*.mdb)", "Driver={Microsoft Access Driver (*.mdb" . ACCDB_STRING . ")", $sDsn);
49  $this->m_oDB = new \PDO($sDsn, $this->m_sDBUser, $sPassword);
50  }
51  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addInfo("Connected to Database: $sDatabaseID");
52  }
53 
54  /**
55  * CompileSelectSql - Compiles SQL expression
56  *
57  * @access protected
58  * @param array $aWhere Array of conditions
59  * @return string WHERE statement
60  */
61  protected function CompileSelectSql($aQuery = [])
62  {
63  $sSelectSQL = "SELECT";
64  if (!empty($aQuery["limit"]))
65  {
66  $sSelectSQL .= " TOP $aQuery[limit] ";
67  }
68  if (!empty($aQuery["select"]))
69  {
70  $sSelectSQL .= " $aQuery[select] ";
71  }
72  else
73  {
74  $sSelectSQL .= " * ";
75  }
76  if (!empty($aQuery["from"]))
77  {
78  $sSelectSQL .= "FROM ";
79  if (!empty($aQuery["join"]))
80  {
81  $sSelectSQL .= "($aQuery[from] $aQuery[join])";
82  }
83  else
84  {
85  $sSelectSQL .= "$aQuery[from] ";
86  }
87  }
88  if (!empty($aQuery["where"]))
89  {
90  $sSelectSQL .= "WHERE $aQuery[where] ";
91  }
92  if (!empty($aQuery["group_by"]))
93  {
94  $sSelectSQL .= "GROUP BY $aQuery[group_by] ";
95  }
96  if (!empty($aQuery["order_by"]))
97  {
98  $sSelectSQL .= "ORDER BY $aQuery[order_by] ";
99  }
100  return $sSelectSQL;
101  }
102 
103  /**
104  * ConcatenateJoins - Concatenates JOIN expression
105  *
106  * @access protected
107  * @param array $aJoinDirections Array of JOIN Directions
108  * @param array $aJoinTables Array of JOIN Tables
109  * @param array $aJoinOns Array of JOIN ONs
110  * @return string JOIN expression
111  */
112  protected function ConcatenateJoins($aJoinDirections, $aJoinTables, $aJoinOns)
113  {
114  $sJoins = "";
115  $nMaxKey = count($aJoinTables) - 1;
116  foreach (array_reverse($aJoinTables) as $nKey => $sJoinTable)
117  {
118  $sJoinTmp = "";
119  if (empty($sJoins))
120  {
121  $sJoinTmp = " $sJoinTable ";
122  }
123  else
124  {
125  $sJoinTmp = " ($sJoinTable $sJoins) ";
126  }
127  $sJoins = arr_get($aJoinDirections, $nMaxKey - $nKey, "INNER JOIN")
128  . $sJoinTmp
129  . "ON (" . $this->CompileJoinOn(arr_get($aJoinOns, $nMaxKey - $nKey, "")) . ")";
130  }
131  return $sJoins;
132  }
133 
134  /**
135  * Sanitize - Sanitizes string
136  *
137  * @access protected
138  * @param string $sData Data to sanitize
139  * @return string Sanitized string
140  */
141  protected function Sanitize($sData)
142  {
143  $sData = iconv("UTF-8", "WINDOWS-1256", $sData);
144  return str_replace("'", "''", $sData);
145  }
146 
147  /**
148  * BoolValue - Converts Boolean to appropriate value depending on driver
149  *
150  * @access protected
151  * @param bool $bData Boolean value
152  * @return int
153  */
154  protected function BoolValue($bData)
155  {
156  return $bData ? -1 : 0;
157  }
158 
159  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
__construct($aConnectionDetails, $sDatabaseID)
ConcatenateJoins($aJoinDirections, $aJoinTables, $aJoinOns)
CompileJoinOn($aJoinOn=[])
Definition: CDataBase.php:778