Overview

Namespaces

  • GAubry
    • ErrorHandler
    • Helpers
    • Logger
    • Shell
  • Himedia
    • Padocc
      • DB
      • Minifier
      • Numbering
      • Properties
      • Task
        • Base
        • Extended
  • None
  • Psr
    • Log

Classes

  • DeploymentMapper
  • PDOAdapter

Interfaces

  • DBAdapterInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Himedia\Padocc\DB;
  4: 
  5: /**
  6:  * Deployment entity
  7:  */
  8: class DeploymentMapper
  9: {
 10:     /**
 11:      * @var DBAdapterInterface
 12:      */
 13:     private $oDB;
 14: 
 15:     /**
 16:      * @var string Name of SQL table.
 17:      */
 18:     private $sTable;
 19: 
 20:     /**
 21:      * @var string Primary key column.
 22:      */
 23:     private $sPK;
 24: 
 25:     /**
 26:      * Constructor.
 27:      *
 28:      * @param DBAdapterInterface $oDB
 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:      * Build and execute an INSERT on $this->sTable filling specified columns.
 39:      *
 40:      * @param array $aParameters key-value list of columns and values to insert
 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:             /* @var $oStmt \PDOStatement */
 49:             $oStmt = $this->oDB->prepare($sQuery);
 50:             $oStmt->execute(array_values($aParameters));
 51:         }
 52:     }
 53: 
 54:     /**
 55:      * Build and execute a SELECT, and return all rows.
 56:      *
 57:      * @param array $aFilter in conjunctive normal form (CNF), i.e. a conjunction of clauses where clauses
 58:      * are disjunction of literals:
 59:      *     <filter>  := array(<clause>, …)
 60:      *     <clause>  := array(<literal>, …)
 61:      *     <literal> := array('column' => 'value')
 62:      * @param array $aOrderBy list of 'column ASC|DESC'
 63:      * @param int $iLimit
 64:      * @param int $iOffset
 65:      * @return array all rows with \PDO::FETCH_ASSOC mode
 66:      */
 67:     public function select (array $aFilter, $aOrderBy = array(), $iLimit = 100, $iOffset = 0)
 68:     {
 69:         // WHERE clause:
 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:         // ORDER BY clause:
 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:         /* @var $oStmt \PDOStatement */
 97:         $oStmt = $this->oDB->prepare($sQuery);
 98:         $oStmt->execute($aParameters);
 99:         $aResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
100:         return $aResult;
101:     }
102: 
103:     /**
104:      * Build and execute an UPDATE on $this->sTable.
105:      *
106:      * @param array $aParameters key-value list of columns and values to update, including primary key value.
107:      * @throws \RuntimeException if no value for primary key is found in $aParameters.
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:             /* @var $oStmt \PDOStatement */
121:             $oStmt = $this->oDB->prepare($sQuery);
122:             $oStmt->execute(array_values($aParameters));
123:         }
124:     }
125: }
126: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0