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\EmptyDatasetException;
28: use DSchoenbauer\Sql\Exception\ExecutionErrorException;
29: use PDO;
30:
31: /**
32: * adds values to a PDO connected resource
33: * @author David Schoenbauer <dschoenbauer@gmail.com>
34: * @since v1.0.0
35: */
36: class Create implements CommandInterface
37: {
38:
39: private $table;
40: private $data;
41:
42: /**
43: * @param string $table table with which you wish to append to
44: * @param array $data a single level associative array containing keys that
45: * represent the fields and values that represent new values to be added into
46: * the table
47: * @since v1.0.0
48: */
49: public function __construct($table, $data)
50: {
51: $this->setTable($table)->setData($data);
52: }
53:
54: /**
55: * Generates a SQL statement ready to be prepared for execution with the
56: * intent of updating data
57: * @return string a string that represents an update statement ready to be
58: * prepared by PDO
59: * @throws EmptyDatasetException if no data has been set no fields can be
60: * discerned and no query can be made
61: * @since v1.0.0
62: */
63: public function getSql()
64: {
65: if (count($this->getData()) === 0) {
66: throw new EmptyDatasetException();
67: }
68:
69: $sqlTemplate = "INSERT INTO %s (%s) VALUES (:%s)";
70: return sprintf(
71: $sqlTemplate,
72: $this->getTable(),
73: implode(', ', array_keys($this->getData())),
74: implode(', :', array_keys($this->getData()))
75: );
76: }
77:
78: /**
79: * takes the SQL and the data provided and executes the query with the data
80: * @param PDO $pdo a connection object that defines where the connection is
81: * to be executed
82: * @return string will return the lastInsertId from the PDO connection object
83: * @throws ExecutionErrorException thrown when any exception or SQL failure
84: * occurs
85: * @since v1.0.0
86: */
87: public function execute(PDO $pdo)
88: {
89: try {
90: $sql = $this->getSql();
91: $stmt = $pdo->prepare($sql);
92: if (!$stmt->execute($this->getData())) {
93: throw new ExecutionErrorException($stmt->errorInfo()[2]);
94: }
95: return $pdo->lastInsertId();
96: } catch (\Exception $exc) {
97: throw new ExecutionErrorException($exc->getMessage());
98: }
99: }
100:
101: /**
102: * retrieves the table with which you wish to append to
103: * @return string table with which you wish to append to
104: * @since v1.0.0
105: */
106: public function getTable()
107: {
108: return $this->table;
109: }
110:
111: /**
112: * retrieves the data that is used to generate the create statement. The
113: * fields of the array are used to generate the field list.
114: * @return array a single level associative array containing keys that
115: * represent the fields and values that represent new values to be added
116: * into the table
117: * @since v1.0.0
118: */
119: public function getData()
120: {
121: return $this->data;
122: }
123:
124: /**
125: * defines a table with which you wish to append to
126: * @param string $table a table with which you wish to append to
127: * @return Create for method chaining
128: * @since v1.0.0
129: */
130: public function setTable($table)
131: {
132: $this->table = $table;
133: return $this;
134: }
135:
136: /**
137: * sets the data that is used to generate the create statement. The fields
138: * of the array are used to generate the field list.
139: * @param array $data a single level associative array containing keys that
140: * represent the fields and values that represent new values to be added
141: * into the table
142: * @return Create for method chaining
143: * @since v1.0.0
144: */
145: public function setData(array $data)
146: {
147: $this->data = $data;
148: return $this;
149: }
150: }
151: