Overview

Namespaces

  • DSchoenbauer
    • Sql
      • Command
      • Exception
      • Where

Classes

  • DSchoenbauer\Sql\Command\Create
  • DSchoenbauer\Sql\Command\Delete
  • DSchoenbauer\Sql\Command\Select
  • DSchoenbauer\Sql\Command\Update
  • DSchoenbauer\Sql\Exception\EmptyDatasetException
  • DSchoenbauer\Sql\Exception\ExecutionErrorException
  • DSchoenbauer\Sql\Exception\MethodNotValidException
  • DSchoenbauer\Sql\Query
  • DSchoenbauer\Sql\Where\ArrayWhere

Interfaces

  • DSchoenbauer\Sql\Command\CommandInterface
  • DSchoenbauer\Sql\Exception\SqlExceptionInterface
  • DSchoenbauer\Sql\Where\WhereStatementInterface

Traits

  • DSchoenbauer\Sql\Command\WhereTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3:  * The MIT License
  4:  *
  5:  * Copyright 2017 David Schoenbauer <dschoenbauer@gmail.com>.
  6:  *
  7:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8:  * of this software and associated documentation files (the "Software"), to deal
  9:  * in the Software without restriction, including without limitation the rights
 10:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11:  * copies of the Software, and to permit persons to whom the Software is
 12:  * furnished to do so, subject to the following conditions:
 13:  *
 14:  * The above copyright notice and this permission notice shall be included in
 15:  * all copies or substantial portions of the Software.
 16:  *
 17:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23:  * THE SOFTWARE.
 24:  */
 25: namespace DSchoenbauer\Sql\Command;
 26: 
 27: use DSchoenbauer\Sql\Exception\ExecutionErrorException;
 28: use DSchoenbauer\Sql\Where\WhereStatementInterface;
 29: use PDO;
 30: 
 31: /**
 32:  * removes records from a PDO connected resource
 33:  *
 34:  * @author David Schoenbauer <dschoenbauer@gmail.com>
 35:  */
 36: class Delete implements CommandInterface
 37: {
 38: 
 39:     private $table;
 40: 
 41:     use WhereTrait;
 42: 
 43:     /**
 44:      * @param string $table  table with which you wish to remove records from
 45:      * @param WhereStatementInterface $where an object that is designed to
 46:      * return a where statement to limit the data that is affected by the delete
 47:      * @since v1.0.0
 48:      */
 49:     public function __construct($table, WhereStatementInterface $where = null)
 50:     {
 51:         $this->setTable($table)->setWhere($where);
 52:     }
 53: 
 54:     /**
 55:      * takes the SQL and the data provided and executes the query with the data
 56:      * @param PDO $pdo a connection object that defines where the connection is to be executed
 57:      * @return bool TRUE on success or FALSE on failure.
 58:      * @throws ExecutionErrorException  thrown when any exception or SQL failure occurs
 59:      * @since v1.0.0
 60:      */
 61:     public function execute(PDO $pdo)
 62:     {
 63:         try {
 64:             $stmt = $pdo->prepare($this->getSql());
 65:             if (count($this->getData()) > 0) {
 66:                 return $stmt->execute($this->getData());
 67:             }
 68:             return $stmt->execute();
 69:         } catch (\Exception $exc) {
 70:             throw new ExecutionErrorException($exc->getMessage());
 71:         }
 72:     }
 73: 
 74:     /**
 75:      * retrieves the data that is uses to fulfill the requirements of a prepared
 76:      * statement
 77:      * @return array a single level associative array containing keys that
 78:      * represent the fields and values that represent items to fulfill the
 79:      * requirements of a prepared statement
 80:      * @since v1.0.0
 81:      */
 82:     public function getData()
 83:     {
 84:         return $this->getWhereData();
 85:     }
 86: 
 87:     /**
 88:      * Generates a SQL statement ready to be prepared for execution with the intent of removing data
 89:      * @return string a string that represents a delete statement ready to be prepared by PDO
 90:      * @since v1.0.0
 91:      */
 92:     public function getSql()
 93:     {
 94:         $sqlTemplate = 'DELETE FROM %1$s %2$s';
 95:         return trim(sprintf($sqlTemplate, $this->getTable(), $this->getWhereStatement()));
 96:     }
 97: 
 98:     /**
 99:      * retrieves the table with which you wish to remove from
100:      * @return string  table with which you wish to remove from
101:      * @since v1.0.0
102:      */
103:     public function getTable()
104:     {
105:         return $this->table;
106:     }
107: 
108:     /**
109:      * defines a table with which you wish to remove from
110:      * @param string $table table with which you wish to remove from
111:      * @return Delete for method chaining
112:      * @since v1.0.0
113:      */
114:     public function setTable($table)
115:     {
116:         $this->table = $table;
117:         return $this;
118:     }
119: }
120: 
API documentation generated by ApiGen