1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Core\DB;
10: use Core\System\System;
11: use \PDO;
12: use \Exception;
13:
14: class DB
15: {
16: private $dsn=null;
17: private $DBCON=null;
18: private $table=null;
19: private $selects=null;
20: private $andWhereConds=null;
21: private $orWhereConds=null;
22: private $inConds=null;
23: private $notInConds=null;
24: private $query="";
25: private $statement=null;
26:
27: 28: 29: 30:
31: public static function ADO()
32: {
33: $db = ADONewConnection($_ENV['DB_ADO_DRIVER']);
34: $db->connect($_ENV['DB_SERVER'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
35: return $db;
36: }
37:
38: 39: 40:
41: protected function __construct()
42: {
43: $this->dsn=$_ENV['DB_TYPE'].':dbname='.$_ENV['DB_NAME'].';host='.$_ENV['DB_SERVER'].';port='.$_ENV['DB_PORT'].';charset='.$_ENV['DB_CHARSET'];
44: try
45: {
46: $this->DBCON = new PDO($this->dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
47: $this->DBCON->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, $_ENV['DB_FETCH_MODE']);
48: }
49: catch (PDOException $e)
50: {
51: echo 'Databse Connection failed: ' . $e->getMessage();
52: die;
53: }
54: }
55:
56: 57: 58: 59:
60: public static function table($tableName)
61: {
62: $db = new DB();
63: $db->table =$tableName;
64: return $db;
65: }
66:
67:
68: 69: 70:
71: private function closeCON()
72: {
73: $this->DBCON = null;
74: unset($this->DBCON);
75: }
76:
77: 78: 79:
80: public function select()
81: {
82: $args=func_get_args();
83: $numArgs = func_num_args();
84: if($numArgs == 0)
85: {
86: $this->selects = '*';
87: }
88: else
89: {
90: $this->selects = implode(',', $args);
91: }
92: return $this;
93: }
94:
95: 96: 97: 98:
99: public function where()
100: {
101: $numArgs = func_num_args();
102: $args = func_get_args();
103: $col = null;
104: if($numArgs == 2)
105: {
106: if(gettype($args[1]) == 'integer')
107: {
108: $col = $args[1];
109: }
110: else
111: {
112: $col = '"'.$args[1].'"';
113: }
114: $this->andWhereConds[] = $args[0].' = '.$col;
115: }
116: else if($numArgs == 3)
117: {
118: if(gettype($args[2]) == 'integer')
119: {
120: $col = $args[2];
121: }
122: else
123: {
124: $col = '"'.$args[2].'"';
125: }
126: $this->andWhereConds[] = $args[0].' '.$args[1].' '.$col;
127: }
128: else
129: {
130: throw new Exception("Invalid / Missing Parameters for WHERE clause");
131: }
132: return $this;
133: }
134:
135: 136: 137: 138:
139: public function orWhere()
140: {
141: $numArgs = func_num_args();
142: $args = func_get_args();
143: $col = null;
144: if($numArgs == 2)
145: {
146: if(gettype($args[1]) == 'integer')
147: {
148: $col = $args[1];
149: }
150: else
151: {
152: $col = '"'.$args[1].'"';
153: }
154: $this->orWhereConds[] = $args[0].' = '.$col;
155: }
156: else if($numArgs == 3)
157: {
158: if(gettype($args[2]) == 'integer')
159: {
160: $col = $args[2];
161: }
162: else
163: {
164: $col = '"'.$args[2].'"';
165: }
166: $this->orWhereConds[] = $args[0].' '.$args[1].' '.$col;
167: }
168: else
169: {
170: throw new Exception("Invalid / Missing Parameters for WHERE clause");
171: }
172: return $this;
173: }
174:
175: 176: 177: 178: 179:
180: public function whereIn($column, $inArray)
181: {
182: $this->inConds[] = $column.' IN ('.implode(',', $inArray).')';
183: return $this;
184: }
185:
186: 187: 188: 189: 190:
191: public function notIn($column, $notInArray)
192: {
193: $this->notInConds[] = $column.' NOT IN ('.implode(',', $notInArray).')';
194: return $this;
195: }
196:
197:
198:
199: protected function selectionQueryMaker()
200: {
201: $this->query = 'SELECT '.$this->selects.' FROM '.$this->table;
202: $where="";
203: $flag = 0;
204: if($this->andWhereConds != null)
205: {
206: if($flag == 0)
207: {
208: $where .= ' WHERE ';
209: }
210: $flag = 1;
211: foreach ($this->andWhereConds as $conds)
212: {
213: $where.= '('.$conds.')';
214: $where.= ' AND ';
215: }
216: }
217: if($this->orWhereConds != null)
218: {
219: if($flag == 0)
220: {
221: $where .= ' WHERE ';
222: }
223: else
224: {
225: $where = substr($where, 0, -4);
226: $where.=' OR ';
227: }
228: $flag = 1;
229: foreach ($this->orWhereConds as $conds)
230: {
231: $where.= '('.$conds.')';
232: $where.= ' OR ';
233: }
234: }
235: if($this->inConds != null)
236: {
237: if($flag == 0)
238: {
239: $where .= ' WHERE ';
240: }
241: else
242: {
243: $where = substr($where, 0, -4);
244: $where.=' AND ';
245: }
246: $flag = 1;
247: foreach ($this->inConds as $conds)
248: {
249: $where.= '('.$conds.')';
250: $where.= ' AND ';
251: }
252: }
253: if($this->notInConds != null)
254: {
255: if($flag == 0)
256: {
257: $where .= ' WHERE ';
258: }
259: else
260: {
261: $where = substr($where, 0, -4);
262: $where.=' AND ';
263: }
264: $flag = 1;
265: foreach ($this->notInConds as $conds)
266: {
267: $where.= '('.$conds.')';
268: $where.= ' AND ';
269: }
270: }
271:
272: if($flag == 1)
273: {
274: $where = substr($where, 0, -4);
275: }
276: $this->query.= $where;
277: }
278:
279: 280: 281:
282: public function get()
283: {
284: $this->selectionQueryMaker();
285: $this->statement = $this->DBCON->prepare($this->query);
286: $this->statement->execute();
287: $this->closeCON();
288: return $this->statement->fetchAll();
289: }
290: public function first()
291: {
292: $this->selectionQueryMaker();
293: $this->statement = $this->DBCON->prepare($this->query);
294: $this->statement->execute();
295: $this->closeCON();
296: return $this->statement->fetch();
297: }
298: public function update()
299: {
300:
301: }
302: public function delete()
303: {
304:
305: }
306:
307: public static function raw($query)
308: {
309: $db = new DB();
310: $db->statement = $db->DBCON->prepare($query);
311: $db->statement->execute();
312: $db->closeCON();
313: return $db->statement->fetch();
314: }
315: }