1: <?php
2:
3: namespace Himedia\Padocc\DB;
4:
5: 6: 7:
8: class DeploymentMapper
9: {
10: 11: 12:
13: private $oDB;
14:
15: 16: 17:
18: private $sTable;
19:
20: 21: 22:
23: private $sPK;
24:
25: 26: 27: 28: 29:
30: public function __construct (DBAdapterInterface $oDB)
31: {
32: $this->oDB = $oDB;
33: $this->sTable = 'deployments';
34: $this->sPK = 'exec_id';
35: }
36:
37: 38: 39: 40: 41:
42: public function insert (array $aParameters)
43: {
44: if (count($aParameters) > 0) {
45: $sColumns = implode(', ', array_keys($aParameters));
46: $sPlaceHolders = implode(',', array_fill(0, count($aParameters), '?'));
47: $sQuery = "INSERT INTO $this->sTable ($sColumns) VALUES ($sPlaceHolders)";
48:
49: $oStmt = $this->oDB->prepare($sQuery);
50: $oStmt->execute(array_values($aParameters));
51: }
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66:
67: public function select (array $aFilter, $aOrderBy = array(), $iLimit = 100, $iOffset = 0)
68: {
69:
70: if (count($aFilter) > 0) {
71: $aWhere = array();
72: $aParameters = array();
73: foreach ($aFilter as $aClauses) {
74: $aClause = array();
75: foreach ($aClauses as $aLiteral) {
76: list($sColumn, $sValue) = each($aLiteral);
77: $aClause[] = "$sColumn=?";
78: $aParameters[] = $sValue;
79: }
80: $aWhere[] = implode(' OR ', $aClause);
81: }
82: $sWhere = 'WHERE (' . implode(') AND (', $aWhere) . ')';
83: } else {
84: $sWhere = '';
85: $aParameters = array();
86: }
87:
88:
89: if (count($aOrderBy) > 0) {
90: $sOrderBy = 'ORDER BY ' . implode(', ', $aOrderBy);
91: } else {
92: $sOrderBy = '';
93: }
94:
95: $sQuery = "SELECT * FROM $this->sTable $sWhere $sOrderBy LIMIT $iLimit OFFSET $iOffset";
96:
97: $oStmt = $this->oDB->prepare($sQuery);
98: $oStmt->execute($aParameters);
99: $aResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
100: return $aResult;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function update (array $aParameters)
110: {
111: if (empty($aParameters[$this->sPK])) {
112: $sMsg = "Missing primary key '$this->sPK' in parameters: " . print_r($aParameters, true);
113: throw new \RuntimeException($sMsg);
114: } elseif (count($aParameters) > 1) {
115: $sPKValue = $aParameters[$this->sPK];
116: unset($aParameters[$this->sPK]);
117: $sPlaceHolders = implode('=?, ', array_keys($aParameters)) . '=?';
118: $aParameters[] = $sPKValue;
119: $sQuery = "UPDATE $this->sTable SET $sPlaceHolders WHERE $this->sPK=?";
120:
121: $oStmt = $this->oDB->prepare($sQuery);
122: $oStmt->execute(array_values($aParameters));
123: }
124: }
125: }
126: