Skip to content

Commit 8c941de

Browse files
committed
minor bc, docs/doc-block updates, added function
- renamed cleanInput to clean_string - renamed createCertificate to create_certificate - added global get_results to return result sets in different formats
1 parent a13097e commit 8c941de

File tree

5 files changed

+71
-26
lines changed

5 files changed

+71
-26
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ foreach ($result as $row) {
247247
$db->query_prepared('INSERT INTO profile( name, email, phone) VALUES( ?, ?, ? );', [$user, $address, $number]);
248248

249249
$db->query_prepared('SELECT name, email FROM profile WHERE phone = ? OR id != ?', [$number, 5]);
250-
$result = $db->queryResult(); // the last query that has results are stored in `last_result` protected property
250+
$result = $db->queryResult(); // the last query that has results are stored in `lastResult` protected property
251+
// Or for results in other formats use the global function, will use global database instance if no `$db` supplied
252+
$result = get_results(/* OBJECT|ARRAY_A|ARRAY_N|JSON */, $db); // Defaults to `OBJECT`
251253

252254
foreach ($result as $row) {
253255
echo $row->name.' '.$row->email;
@@ -271,11 +273,11 @@ use function ezsql\functions\{
271273
setInstance,
272274
getInstance,
273275
clearInstance,
274-
///
275276
getVendor,
277+
///
276278
to_string,
277-
cleanInput,
278-
createCertificate,
279+
clean_string,
280+
create_certificate,
279281
///
280282
column,
281283
primary,
@@ -323,6 +325,7 @@ use function ezsql\functions\{
323325
replacing,
324326
selecting,
325327
inserting,
328+
get_results,
326329
table_setup,
327330
set_table,
328331
set_prefix

lib/ezFunctions.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,27 @@ function to_string($arrays, $separation = ',')
153153
}
154154

155155
/**
156-
* Creates an database column,
157-
* - column, datatype, value/options with the given arguments.
156+
* Creates an database column as:
157+
* - `column`, data`type`, ...value/options `arguments`.
158158
*
159-
* // datatype are global CONSTANTS and can be written out like:
159+
* // datatype are global `CONSTANTS` and can be written out like:
160160
* - VARCHAR, 32, notNULL, PRIMARY, SEQUENCE|AUTO, ....
161161
* // SEQUENCE|AUTO constants will replaced with the proper auto sequence for the SQL driver
162162
*
163-
* @param string $column|CONSTRAINT, - column name/CONSTRAINT usage for PRIMARY|FOREIGN KEY
164-
* @param string $type|$constraintName, - data type for column/primary|foreign constraint name
165-
* @param mixed $size|...$primaryForeignKeys,
166-
* @param mixed $value, - column should be NULL or NOT NULL. If omitted, assumes NULL
167-
* @param mixed $default - Optional. It is the value to assign to the column
163+
* @param string $column | `CONSTRAINT`, - column name/CONSTRAINT usage for PRIMARY|FOREIGN KEY
164+
* @param string $type | constraintName, - data type for column/primary|foreign constraint name
165+
* @param mixed ...$arguments any remainder assignments `ordered` like:
166+
* - @param mixed $size, or/and
167+
* - @param mixed $value, - or/and column should be `NULLS`|`notNULL`. If omitted, assumes `NULLS`
168+
* - @param mixed $default, - or/and Optional. It is the value to assign to the column
169+
* - @param mixed $autoNumber, or/and `AUTO` for vendor's auto numbering
170+
* - @param mixed $primaryForeignKeys | or/and `PRIMARY`|`FOREIGN`
168171
*
169172
* @return string|bool - SQL schema string, or false for error
170173
*/
171-
function column(string $column = null, string $type = null, ...$args)
174+
function column(string $column = null, string $type = null, ...$arguments)
172175
{
173-
return ezSchema::column($column, $type, ...$args);
176+
return ezSchema::column($column, $type, ...$arguments);
174177
}
175178

176179
function primary(string $primaryName, ...$primaryKeys)
@@ -536,6 +539,42 @@ function getInstance()
536539
return ($ezInstance instanceof ezQueryInterface) ? $ezInstance : null;
537540
}
538541

542+
/**
543+
* Get multiple row result set from the database (previously cached results).
544+
* Returns a multi dimensional array.
545+
*
546+
* Each element of the array contains one row of results and can be
547+
* specified to be either an `object`, `json`, `associative array` or `numerical
548+
* array`.
549+
* - If no results are found then the function returns `false`,
550+
* enabling you to use the function within logic statements such as if.
551+
*
552+
* **OBJECT** - `Returning results as an object` is the quickest way to get and
553+
* display results. It is also useful that you are able to put
554+
* `$object->var` syntax directly inside print statements without
555+
* having to worry about causing php parsing errors.
556+
*
557+
* **ARRAY_A** - `Returning results as an associative array` is useful if you would
558+
* like dynamic access to column names.
559+
*
560+
* **ARRAY_N** - `Returning results as a numerical array` is useful if you are using
561+
* completely dynamic queries with varying column names but still need
562+
* a way to get a handle on the results.
563+
*
564+
* **JSON** - `Returning results as JSON encoded` is useful for any interactive dynamic queries.
565+
*
566+
* @param constant $output Either: `OBJECT`|`ARRAY_A`|`ARRAY_N`|`JSON`
567+
* @param object|null $instance `ez_pdo`|`ez_pgsql`|`ez_sqlsrv`|`ez_sqlite3`|`ez_mysqli`
568+
* @return bool|object|array - results as objects (default)
569+
*/
570+
function get_results($output = \OBJECT, $instance = null)
571+
{
572+
$ezQuery = empty($instance) || !is_object($instance) ? getInstance() : $instance;
573+
return ($ezQuery instanceof ezsqlModelInterface)
574+
? $ezQuery->get_results(null, $output, false)
575+
: false;
576+
}
577+
539578
/**
540579
* Clear/unset the global database class instance.
541580
*/
@@ -548,12 +587,12 @@ function clearInstance()
548587
}
549588

550589
/**
551-
* Clean input of XSS, html, javascript, etc...
590+
* Clean input string of XSS, html, javascript, etc...
552591
* @param string $string
553592
*
554593
* @return string cleaned string
555594
*/
556-
function cleanInput($string)
595+
function clean_string(string $string)
557596
{
558597
return ezQuery::clean($string);
559598
}

lib/ezsqlModelInterface.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,24 @@ public function get_col(string $query = null, int $x = 0, bool $use_prepare = fa
314314
* - if setup/active, `prepareActive()` has been called, use
315315
* prepare statements in SQL transactions.
316316
*
317-
* `Returning results as an object` is the quickest way to get and
317+
* **OBJECT** - `Returning results as an object` is the quickest way to get and
318318
* display results. It is also useful that you are able to put
319319
* `$object->var` syntax directly inside print statements without
320320
* having to worry about causing php parsing errors.
321321
*
322-
* `Returning results as an associative array` is useful if you would
322+
* **ARRAY_A** - `Returning results as an associative array` is useful if you would
323323
* like dynamic access to column names.
324324
*
325-
* `Returning results as a numerical array` is useful if you are using
325+
* **ARRAY_N** - `Returning results as a numerical array` is useful if you are using
326326
* completely dynamic queries with varying column names but still need
327327
* a way to get a handle on the results.
328328
*
329+
* **JSON** - `Returning results as JSON encoded` is useful for any interactive dynamic queries.
330+
329331
* @param string $query
330-
* @param OBJECT|ARRAY_A|ARRAY_N|JSON $output
332+
* @param constant $output Either: `OBJECT`|`ARRAY_A`|`ARRAY_N`|`JSON`
331333
* @param bool $use_prepare - has prepare statements been activated
332-
* @return bool|mixed - results as objects (default)
334+
* @return bool|object|array - results as objects (default)
333335
*/
334336
public function get_results(string $query = null, $output = \OBJECT, bool $use_prepare = false);
335337

@@ -339,7 +341,7 @@ public function get_results(string $query = null, $output = \OBJECT, bool $use_p
339341
* Returns meta information about one or all columns such as column name or type.
340342
* - If no information type is supplied then the default information type of name is used.
341343
* - If no column offset is supplied then a one dimensional array is returned with the
342-
* information type for ‘all columns’.
344+
* information type for all columns.
343345
* - For access to the full meta information for all columns you can use the cached
344346
* variable `$db->colInfo`, access by calling `$db->getCol_Info()`
345347
*

tests/ezQueryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
neq,
1111
like,
1212
in,
13-
cleanInput
13+
clean_string
1414
};
1515

1616
class ezQueryTest extends EZTestCase
@@ -27,9 +27,9 @@ protected function tearDown(): void
2727
$this->object = null;
2828
}
2929

30-
public function testCleanInput()
30+
public function testClean_string()
3131
{
32-
$this->assertEquals("' help", cleanInput("<?php echo 'foo' >' help</php?>"));
32+
$this->assertEquals("' help", clean_string("<?php echo 'foo' >' help</php?>"));
3333
}
3434

3535
public function testHaving()

tests/mysqli/ezResultsetTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use function ezsql\functions\{
99
mysqlInstance,
1010
column,
11+
get_results,
1112
selecting,
1213
inserting,
1314
set_table,
@@ -53,7 +54,7 @@ protected function setUp(): void
5354
inserting(['id' => 5, 'test_key' => 'test 5']);
5455
selecting();
5556

56-
$this->object = new ezResultset($this->database->get_results());
57+
$this->object = new ezResultset(get_results());
5758
} // setUp
5859

5960
/**

0 commit comments

Comments
 (0)