@@ -27,7 +27,7 @@ $peachySql = new PeachySQL\SqlServer($sqlSrvConn);
2727```
2828
2929After instantiation, arbitrary statements can be prepared by passing a
30- SQL string and array of bound parameters to the ` prepare ` method:
30+ SQL string and array of bound parameters to the ` prepare() ` method:
3131
3232``` php
3333$sql = "UPDATE Users SET fname = ? WHERE user_id = ?";
@@ -46,7 +46,7 @@ $stmt->close();
4646```
4747
4848Most of the time prepared statements only need to be executed a single time.
49- To make this easier, PeachySQL provides a ` query ` method which automatically
49+ To make this easier, PeachySQL provides a ` query() ` method which automatically
5050prepares, executes, and closes a statement after results are retrieved:
5151
5252``` php
@@ -55,20 +55,20 @@ $result = $peachySql->query($sql, ['theo%', 'b%']);
5555echo json_encode($result->getAll());
5656```
5757
58- Both ` prepare ` and ` query ` return a ` Statement ` object with the following methods:
58+ Both ` prepare() ` and ` query() ` return a ` Statement ` object with the following methods:
5959
60- | Method | Behavior |
61- | ---------------| ----------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------|
62- | ` execute ` | Executes the prepared statement (automatically called when using ` query ` ). |
63- | ` getIterator ` | Returns a [ Generator] ( http://php.net/manual/en/language.generators.overview.php ) object which can be used to iterate over large result sets without caching them in memory. |
64- | ` getAll ` | Returns all selected rows as an array of associative arrays. |
65- | ` getFirst ` | Returns the first selected row as an associative array (or ` null ` if no rows were selected). |
66- | ` getAffected ` | Returns the number of rows affected by the query. |
67- | ` close ` | Closes the prepared statement and frees its resources (automatically called when using ` query ` ). |
60+ | Method | Behavior |
61+ | ----------------- | ------------------------------------------------------------------------------------------------------------------|
62+ | ` execute() ` | Executes the prepared statement (automatically called when using ` query() ` ). |
63+ | ` getIterator() ` | Returns a ` Generator ` object which can be used to iterate over large result sets without caching them in memory. |
64+ | ` getAll() ` | Returns all selected rows as an array of associative arrays. |
65+ | ` getFirst() ` | Returns the first selected row as an associative array (or ` null ` if no rows were selected). |
66+ | ` getAffected() ` | Returns the number of rows affected by the query. |
67+ | ` close() ` | Closes the prepared statement and frees its resources (automatically called when using ` query() ` ). |
6868
69- If using MySQL, the ` Mysql\Statement ` object additionally includes a ` getInsertId ` method.
69+ If using MySQL, the ` Mysql\Statement ` object additionally includes a ` getInsertId() ` method.
7070
71- Internally, ` getAll ` and ` getFirst ` are implemented using ` getIterator ` .
71+ Internally, ` getAll() ` and ` getFirst() ` are implemented using ` getIterator() ` .
7272As such they can only be called once for a given statement.
7373
7474### Shorthand methods
@@ -81,15 +81,15 @@ always use bound parameters for values, and column names are automatically escap
8181
8282#### select / selectFrom
8383
84- The ` selectFrom ` method takes a single string argument containing a SQL SELECT query.
84+ The ` selectFrom() ` method takes a single string argument containing a SQL SELECT query.
8585It returns an object with three chainable methods:
8686
87- 1 . ` where `
88- 2 . ` orderBy `
89- 3 . ` offset `
87+ 1 . ` where() `
88+ 2 . ` orderBy() `
89+ 3 . ` offset() `
9090
91- Additionally, the object has a ` getSqlParams ` method which builds the select query,
92- and a ` query ` method which executes the query and returns a ` Statement ` object.
91+ Additionally, the object has a ` getSqlParams() ` method which builds the select query,
92+ and a ` query() ` method which executes the query and returns a ` Statement ` object.
9393
9494``` php
9595// select all columns and rows in a table, ordered by last name and then first name
@@ -105,8 +105,8 @@ $rows = $peachySql->selectFrom("SELECT * FROM Users u INNER JOIN Customers c ON
105105 ->query()->getIterator();
106106```
107107
108- The ` select ` method works the same as ` selectFrom ` , but takes a ` SqlParams ` object
109- rather than a string and supports bound params in the select query:
108+ The ` select() ` method works the same as ` selectFrom() ` , but takes a ` SqlParams `
109+ object rather than a string and supports bound params in the select query:
110110
111111``` php
112112use PeachySQL\QueryBuilder\SqlParams;
@@ -123,15 +123,16 @@ $sql = "
123123 FROM Users u
124124 INNER JOIN UserVisits uv ON uv.user_id = u.user_id";
125125
126- $date = new DateTime('2 months ago');
127- $rows = $peachySql->select(new SqlParams($sql, $date->format('Y-m-d')))
126+ $date = (new DateTime('2 months ago'))->format('Y-m-d');
127+
128+ $rows = $peachySql->select(new SqlParams($sql, [$date]))
128129 ->where(['u.status' => 'verified'])
129130 ->query()->getIterator();
130131```
131132
132133##### Where clause generation
133134
134- In addition to passing basic column => value arrays to the ` where ` method, you can
135+ In addition to passing basic column => value arrays to the ` where() ` method, you can
135136specify more complex conditions by using arrays as values. For example, passing
136137` ['col' => ['lt' => 15, 'gt' => 5]] ` would generate the condition ` WHERE col < 15 AND col > 5 ` .
137138
@@ -157,22 +158,22 @@ IN(...) or NOT IN(...) condition, respectively. Passing a list with the `lk`, `n
157158
158159#### insertRow
159160
160- The ` insertRow ` method allows a single row to be inserted from an associative array.
161- It returns an ` InsertResult ` object with ` getId ` and ` getAffected ` methods .
161+ The ` insertRow() ` method allows a single row to be inserted from an associative array.
162+ It returns an ` InsertResult ` object with readonly ` id ` and ` affected ` properties .
162163
163164``` php
164165$userData = [
165166 'fname' => 'Donald',
166167 'lname' => 'Chamberlin'
167168];
168169
169- $id = $peachySql->insertRow('Users', $userData)->getId() ;
170+ $id = $peachySql->insertRow('Users', $userData)->id ;
170171```
171172
172173#### insertRows
173174
174- The ` insertRows ` method makes it possible to bulk-insert multiple rows from an array.
175- It returns a ` BulkInsertResult ` object with ` getIds ` , ` getAffected ` , and ` getQueryCount ` methods .
175+ The ` insertRows() ` method makes it possible to bulk-insert multiple rows from an array.
176+ It returns a ` BulkInsertResult ` object with readonly ` ids ` , ` affected ` , and ` queryCount ` properties .
176177
177178``` php
178179$userData = [
@@ -191,32 +192,32 @@ $userData = [
191192];
192193
193194$result = $peachySql->insertRows('Users', $userData);
194- $ids = $result->getIds() ; // e.g. [64, 65, 66]
195- $affected = $result->getAffected() ; // 3
196- $queries = $result->getQueryCount() ; // 1
195+ $ids = $result->ids ; // e.g. [64, 65, 66]
196+ $affected = $result->affected ; // 3
197+ $queries = $result->queryCount ; // 1
197198```
198199
199- An optional third parameter can be passed to ` insertRows ` to override the default
200+ An optional third parameter can be passed to ` insertRows() ` to override the default
200201identity increment value:
201202
202203``` php
203204$result = $peachySql->insertRows('Users', $userData, 2);
204- $ids = $result->getIds() ; // e.g. [64, 66, 68]
205+ $ids = $result->ids ; // e.g. [64, 66, 68]
205206```
206207
207208Note: SQL Server allows a maximum of 1,000 rows to be inserted at a time, and limits
208209individual queries to 2,099 or fewer bound parameters. MySQL supports a maximum of
20921065,536 bound parameters per query. These limits can be easily reached when attempting
210211to bulk-insert hundreds or thousands of rows at a time. To avoid these limits, the
211- ` insertRows ` method automatically splits large queries into batches to efficiently
212- handle any number of rows (` getQueryCount ` returns the number of required batches).
212+ ` insertRows() ` method automatically splits large queries into batches to efficiently
213+ handle any number of rows (` queryCount ` contains the number of required batches).
213214
214215#### updateRows and deleteFrom
215216
216- The ` updateRows ` method takes three arguments: a table name, an associative array of
217+ The ` updateRows() ` method takes three arguments: a table name, an associative array of
217218columns/values to update, and a WHERE array to filter which rows are updated.
218219
219- The ` deleteFrom ` method takes a table name and a WHERE array to filter the rows to delete.
220+ The ` deleteFrom() ` method takes a table name and a WHERE array to filter the rows to delete.
220221
221222Both methods return the number of affected rows.
222223
@@ -231,14 +232,14 @@ $userTable->deleteFrom('Users', ['user_id' => [1, 2, 3]]);
231232
232233### Transactions
233234
234- Call the ` begin ` method to start a transaction. ` prepare ` , ` execute ` , ` query `
235+ Call the ` begin() ` method to start a transaction. ` prepare() ` , ` execute() ` , ` query() `
235236and any of the shorthand methods can then be called as needed, before committing
236- or rolling back the transaction with ` commit ` or ` rollback ` .
237+ or rolling back the transaction with ` commit() ` or ` rollback() ` .
237238
238239## Author
239240
240241Theodore Brown
241- < http ://theodorejb.me>
242+ < https ://theodorejb.me>
242243
243244## License
244245
0 commit comments