Skip to content

Commit

Permalink
4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Jul 1, 2023
1 parent 90184bb commit a8a387f
Show file tree
Hide file tree
Showing 5 changed files with 1,274 additions and 1,165 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ ProductRepo // this class was generated with echo $pdoOne()->generateCodeClass([
* [End of the chain](#end-of-the-chain)
* [runGen($returnArray=true)](#rungenreturnarraytrue)
* [toList($pdoMode)](#tolistpdomode)
* [toPdoStatement($pdoMode)](#topdostatementpdomode)
* [fetchLoop($callable,$pdoMode)](#fetchloopcallablepdomode)
* [toMeta()](#tometa)
* [toListSimple()](#tolistsimple)
* [toListKeyValue()](#tolistkeyvalue)
Expand Down Expand Up @@ -300,7 +302,7 @@ $allRows=$rows->fetch_all(PDO::FETCH_ASSOC);
### 3. Running using the query builder

You can use the query builder to build your command. You could check the chapter
about [Query Builder (DQL)](#query-builder--dql-) for more information.
about [Query Builder (DQL)](#query-builder-dql) for more information.

```php
// query builder
Expand Down Expand Up @@ -842,6 +844,32 @@ $results = $pdoOne->select("*")
->toList();
```

# toPdoStatement($pdoMode)

It returns a PdoStatement from the current query
> Note: if you want to loop the statement, then you can use fetchLoop()
**Example**:
```php
$stmt = $pdoOne->select("*")
->from('table')
->toPdoStatement();
while ($row = $stmt->fetch()) {
// do something
}
```

# fetchLoop($callable,$pdoMode)
It fetches a query for every row.
This method could be used when we don't want to read all the information at once,
so you can read and process each line separately
**Example**:
```php
$this->select('select id,name from table')
->fetchLoop(static function($row) {return($row);},\PDO::FETCH_ASSOC)
```


#### toMeta()

It returns a **metacode** (definitions) of each column of a query.
Expand Down Expand Up @@ -1834,10 +1862,16 @@ In a nutshell:
> Every minor version means that it adds a new functionality i.e. 1.5 -> 1.6 (new methods)
>
> Every decimal version means that it patches/fixes/refactoring a previous functionality i.e. 1.5.0 -> 1.5.1 (fix)
* 4.3 2023-07-01
* [PdoOneQuery] Update to 4.1
* _toList() added argument $returnArray
* toPdoStatement() new method
* fetchLoop() new method.
* 4.2 2023-04-07
* [PdoOne] the constructor allows to set the key-value table.
* [PdoOne] new methods getTableKV() getDefaultTableKV(),existKVTable()
* [PdoOneCli] Update to 2.3.1. Now you can enter more values. Also, load and save works in PHP format (it is more flexible)
* [PdoOneCli] Update to 2.3.1. Now you can enter more values. Also, load and save works in PHP format (it is more flexible4)
* 4.1.2 2023-03-21
* str_start_with() is not defined in PHP older than PHP 8.0, so I replaced.
* 4.1.1 2023-03-21
Expand Down
6 changes: 3 additions & 3 deletions lib/PdoOne.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpUnused */
/** @noinspection SqlDialectInspection */
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection PhpConditionAlreadyCheckedInspection */
Expand All @@ -25,11 +25,11 @@
* @package eftec
* @author Jorge Castro Castillo
* @copyright (c) Jorge Castro C. Dual Licence: MIT and Commercial License https://github.com/EFTEC/PdoOne
* @version 4.2
* @version 4.3
*/
class PdoOne
{
public const VERSION = '4.2';
public const VERSION = '4.3';
/** @var int We need this value because null and false could be a valid value. */
public const NULL = PHP_INT_MAX;
/** @var string Prefix of the related columns. It is used for ORM */
Expand Down
86 changes: 66 additions & 20 deletions lib/PdoOneQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
use PDOStatement;
use RuntimeException;


/**
* Class PdoOneQuery
*
* @version 4.00
* @version 4.1
* @package eftec
* @author Jorge Castro Castillo
* @copyright (c) Jorge Castro C. Dual Licence: MIT and Commercial License https://github.com/EFTEC/PdoOne
Expand Down Expand Up @@ -64,7 +63,6 @@ class PdoOneQuery
protected $distinct = '';
/** @var array parameters for the where. [paramvar,value,type,size] */
private $whereParamAssoc = [];

//</editor-fold>
//<editor-fold desc="Query Builder DQL functions" defaultstate="collapsed" >
/**
Expand All @@ -83,7 +81,6 @@ public function __construct(PdoOne $parent, ?string $repo = null)
$this->ormClass = $repo;
}


/**
* It returns an array with the metadata of each column (i.e. name, type,
* size, etc.) or false if error.
Expand Down Expand Up @@ -410,7 +407,6 @@ private function endTry(): bool
return true;
}


/**
* It adds a having to the query builder.
* <br><b>Example</b>:<br>
Expand Down Expand Up @@ -857,7 +853,6 @@ public function from(?string $sqlOrTableName, ?string $schema = null): PdoOneQue
$sqlOrTableName = $this->parent->prefixTable . $sqlOrTableName;
}
$this->from = ($sqlOrTableName) ? $sqlOrTableName . $this->from : $this->from;

$this->parent->tables[] = explode(' ', $sqlOrTableName)[0];
return $this;
}
Expand All @@ -871,7 +866,7 @@ public function _exist($conditions)
$cls = $this->ormClass;
$this->ormClass = null; // to avoid recursivity
$cls::setPdoOneQuery($this);
$r=$cls::exist($conditions);
$r = $cls::exist($conditions);
$cls::reset();
return $r;
}
Expand Down Expand Up @@ -973,13 +968,14 @@ public function toListKeyValue(?string $extraValueSeparator = null): ?array

/**
* It runs a toList() without transformation. It is used internally.
* bool $returnArray = true
* @throws Exception
*/
public function _toList($pdoMode = PDO::FETCH_ASSOC)
public function _toList($pdoMode = PDO::FETCH_ASSOC, bool $returnArray = true)
{
$useCache = $this->useCache; // because builderReset cleans this value
$this->beginTry();
$rows = $this->runGen(true, $pdoMode, 'tolist', false);
$rows = $this->runGen($returnArray, $pdoMode, 'tolist', false);
if ($this->endtry() === false) {
return false;
}
Expand All @@ -1003,23 +999,74 @@ public function _toList($pdoMode = PDO::FETCH_ASSOC)
* ->toList();
* </pre>
*
* @param int $pdoMode (optional) By default is PDO::FETCH_ASSOC
*
* @param int $pdoMode (optional) By default is PDO::FETCH_ASSOC
* @param bool $returnArray
* @return array|bool
* @throws Exception
*/
public function toList(int $pdoMode = PDO::FETCH_ASSOC)
public function toList(int $pdoMode = PDO::FETCH_ASSOC, bool $returnArray = true)
{
if ($this->ormClass !== null) {
$cls = $this->ormClass;
/** @see _BasePdoOneRepo::executePlan0 */
return $cls::executePlan0($this);
}
return $this->_toList($pdoMode);
return $this->_toList($pdoMode, $returnArray);
}

/**
* It returns a PdoStatement from a query.<br>
* <b>Example</b>:<br>
* <pre>
* $stmt=$this->select('select id,name from table')->toPdoStatement();
* while ($row = $stmt->fetch()) {
* // do something
* }
* </pre>
*
* @param int $pdoMode (optional) By default is PDO::FETCH_ASSOC
* @return PDOStatement|null It returns a PdoStatement or null if error.
* @throws Exception
*/
public function toPdoStatement(int $pdoMode = PDO::FETCH_ASSOC): ?PDOStatement
{
return $this->_toList($pdoMode, false);
}

/**
* It fetches a query for every row.<br>
* This method could be used when we don't want to read all the information at once, so you can read and process
* each line separately<br>
* <b>Example</b>:<br>
* <pre>
* $this->select('select id,name from table')
* ->fetchLoop(static function($row) {return($row);},\PDO::FETCH_ASSOC)
* </pre>
*
* @param callable $callable the function to call. This function could have an argument with the value of
* the row<br>
* This function could return a value. If returns a value, then the value is returned in
* an array.
* @param int $pdoMode (default PDO::FETCH_ASSOC)
* @return array|null If nothing is found then it returns an empty array. If error, then it returns null
* @throws Exception
*/
public function fetchLoop(callable $callable, int $pdoMode = PDO::FETCH_ASSOC): ?array
{
$result = [];
$stmt = $this->toPdoStatement($pdoMode);
if ($stmt === null) {
return null;
}
while ($row = $stmt->fetch($pdoMode)) {
$result[] = $callable($row);
}
$stmt = null;
return $result;
}

/**
* It returns a PDOStatement.<br>
* It returns a PDOStatement of the current query.<br>
* <b>Note:</b> The result is not cached.
*
* @return PDOStatement
Expand Down Expand Up @@ -1891,7 +1938,7 @@ public function insert(
$cls = $this->ormClass;
$this->ormClass = null; // to avoid recursivity
$cls::setPdoOneQuery($this);
$r=$cls::insert($tableNameOrValues);
$r = $cls::insert($tableNameOrValues);
$cls::reset();
return $r;
}
Expand Down Expand Up @@ -1928,15 +1975,14 @@ public function delete(
if ($this->ormClass !== null) {
$cls = $this->ormClass;
$this->ormClass = null; // to avoid recursivity
if(count($this->where)>0) {
if (count($this->where) > 0) {
// it is processed as non-orm, using the table of the orm.
$cls::reset();
return $this->delete($cls::TABLE);
}
/** @see _BasePdoOneRepo::_delete */
$cls::setPdoOneQuery($this);
/** @var ChamberRepo::delete $r */
$r=$cls::delete($tableOrObject);
$r = $cls::delete($tableOrObject);
// it goes to the repo class.
// If the method is not override, then it goes to the abstract class
// repo->abstract->_delete method.
Expand Down Expand Up @@ -1996,7 +2042,7 @@ public function deleteById($pks, bool $transaction = true)
$this->ormClass = null; // to avoid recursivity
/** @see _BasePdoOneRepo::deleteById */
$cls::setPdoOneQuery($this);
$r=$cls::deleteById($pks, $transaction);
$r = $cls::deleteById($pks, $transaction);
$cls::reset();
return $r;
}
Expand Down Expand Up @@ -2074,7 +2120,7 @@ public function update(
$this->ormClass = null; // toavoid recursivity
/** @see _BasePdoOneRepo::_update */
$cls::setPdoOneQuery($this);
$r=$cls::update($tableOrObject);
$r = $cls::update($tableOrObject);
$cls::reset();
return $r;
}
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="PdoOne Test Suite">
<directory suffix="Test.php">./tests</directory>
Expand Down
Loading

0 comments on commit a8a387f

Please sign in to comment.