diff --git a/composer.json b/composer.json index b25882f..3351aac 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "require": { "php": ">=7.2.5", "ext-json": "*", + "eftec/clione": "^1.5", "ext-pdo": "*", "ext-readline": "*", "eftec/messagecontainer": "^2.4" diff --git a/examples/clitest/cli2.php b/examples/clitest/cli2.php new file mode 100644 index 0000000..09d57fa --- /dev/null +++ b/examples/clitest/cli2.php @@ -0,0 +1,17 @@ +"; +} +*/ +$txt='12345678901234567890'; +echo "
";
+$color=$cli->replaceColor($txt);
+echo $color."\n";
+echo $cli->colorLess($color)."\n";
+echo $cli->colorMask($color)."\n";
+echo $cli->removechar($color,3)."\n";
+echo "
"; diff --git a/examples/repomysql/generated/AbstractTableChildRepo.php b/examples/repomysql/generated/AbstractTableChildRepo.php index 74234e9..15b5e96 100644 --- a/examples/repomysql/generated/AbstractTableChildRepo.php +++ b/examples/repomysql/generated/AbstractTableChildRepo.php @@ -1,478 +1,499 @@ + * Generated by PdoOne Version 2.24 Date generated Sun, 06 Feb 2022 16:39:24 -0300. + * DO NOT EDIT THIS CODE. Use instead the Repo Class. + * @copyright (c) Jorge Castro C. MIT License https://github.com/EFTEC/PdoOne + * Class AbstractTableChildRepo + *
  * $code=$pdoOne->generateCodeClass('TableChild','repomysql',array(),array('TableParent'=>'TableParentRepo','TableChild'=>'TableChildRepo','TableGrandChild'=>'TableGrandChildRepo','TableGrandChildTag'=>'TableGrandChildTagRepo','TableParentxCategory'=>'TableParentxCategoryRepo','TableCategory'=>'TableCategoryRepo','TableParentExt'=>'TableParentExtRepo',),array(),'','','TestDb','mysql\repomodel\TableChildModel',array(),array());
  * 
-*/ + */ abstract class AbstractTableChildRepo extends TestDb { -const TABLE = 'TableChild'; -const IDENTITY = 'idtablachildPK'; -const PK = [ - 'idtablachildPK' - ]; -const ME=__CLASS__; -const EXTRACOLS=''; -/** @var string|null $schema you can set the current schema/database used by this class. [Default is null] */ -public static $schema; + const TABLE = 'TableChild'; + const IDENTITY = 'idtablachildPK'; + const PK = [ + 'idtablachildPK' + ]; + const ME = __CLASS__; + const EXTRACOLS = ''; + /** @var string|null $schema you can set the current schema/database used by this class. [Default is null] */ + public static $schema; -/** -* It returns the definitions of the columns
-* Example:
-*
+    /**
+     * It returns the definitions of the columns
+ * Example:
+ *
      * self::getDef(); // ['colName'=>[php type,php conversion type,type,size,nullable,extra,sql],'colName2'=>..]
      * self::getDef('sql'); // ['colName'=>'sql','colname2'=>'sql2']
      * self::getDef('identity',true); // it returns the columns that are identities ['col1','col2']
      * 
-* PHP Types: binary, date, datetime, decimal/float,int, string,time, timestamp
-* PHP Conversions: datetime (datetime class), datetime2 (iso),datetime3 (human string) -* , datetime4 (sql no conversion!), timestamp (int), bool, int, float
-* Param Types: PDO::PARAM_LOB, PDO::PARAM_STR, PDO::PARAM_INT
-* -* @param string|null $column =['phptype','conversion','type','size','null','identity','sql'][$i] -* if not null then it only returns the column specified. -* @param string|null $filter If filter is not null, then it uses the column to filter the result. -* -* @return array|array[] -*/ -public static function getDef($column=null,$filter=null) { -$r = [ - 'idtablachildPK' => [ - 'phptype' => 'int', - 'conversion' => 'int', - 'type' => 'int', - 'size' => NULL, - 'null' => FALSE, - 'identity' => TRUE, - 'sql' => 'int not null auto_increment' - ], - 'NameChild' => [ - 'phptype' => 'string', - 'conversion' => NULL, - 'type' => 'varchar', - 'size' => '50', - 'null' => TRUE, - 'identity' => FALSE, - 'sql' => 'varchar(50)' - ], - 'idgrandchildFK' => [ - 'phptype' => 'int', - 'conversion' => 'int', - 'type' => 'int', - 'size' => NULL, - 'null' => TRUE, - 'identity' => FALSE, - 'sql' => 'int' - ] - ]; -if($column!==null) { -if($filter===null) { -foreach($r as $k=>$v) { -$r[$k]=$v[$column]; -} -} else { -$new=[]; -foreach($r as $k=>$v) { -if($v[$column]===$filter) { -$new[]=$k; -} -} -return $new; -} -} -return $r; -} + * PHP Types: binary, date, datetime, decimal/float,int, string,time, timestamp
+ * PHP Conversions: datetime (datetime class), datetime2 (iso),datetime3 (human string) + * , datetime4 (sql no conversion!), timestamp (int), bool, int, float
+ * Param Types: PDO::PARAM_LOB, PDO::PARAM_STR, PDO::PARAM_INT
+ * + * @param string|null $column =['phptype','conversion','type','size','null','identity','sql'][$i] + * if not null then it only returns the column specified. + * @param string|null $filter If filter is not null, then it uses the column to filter the result. + * + * @return array|array[] + */ + public static function getDef($column = null, $filter = null) + { + $r = [ + 'idtablachildPK' => [ + 'phptype' => 'int', + 'conversion' => 'int', + 'type' => 'int', + 'size' => NULL, + 'null' => FALSE, + 'identity' => TRUE, + 'sql' => 'int not null auto_increment' + ], + 'NameChild' => [ + 'phptype' => 'string', + 'conversion' => NULL, + 'type' => 'varchar', + 'size' => '50', + 'null' => TRUE, + 'identity' => FALSE, + 'sql' => 'varchar(50)' + ], + 'idgrandchildFK' => [ + 'phptype' => 'int', + 'conversion' => 'int', + 'type' => 'int', + 'size' => NULL, + 'null' => TRUE, + 'identity' => FALSE, + 'sql' => 'int' + ] + ]; + if ($column !== null) { + if ($filter === null) { + foreach ($r as $k => $v) { + $r[$k] = $v[$column]; + } + } else { + $new = []; + foreach ($r as $k => $v) { + if ($v[$column] === $filter) { + $new[] = $k; + } + } + return $new; + } + } + return $r; + } -/** -* It converts a row returned from the database.
-* If the column is missing then it sets the field as null. -* -* @param array $row [ref] -*/ -public static function convertOutputVal(&$row) { -if($row===false || $row===null) { -return; -} - $row['idtablachildPK']=isset($row['idtablachildPK']) ? (int)$row['idtablachildPK'] : null; - !isset($row['NameChild']) and $row['NameChild']=null; // varchar - $row['idgrandchildFK']=isset($row['idgrandchildFK']) ? (int)$row['idgrandchildFK'] : null; - isset($row['_idgrandchildFK']) - and $row['_idgrandchildFK']['idgrandchildPK']=&$row['idgrandchildFK']; // linked MANYTOONE + /** + * It converts a row returned from the database.
+ * If the column is missing then it sets the field as null. + * + * @param array $row [ref] + */ + public static function convertOutputVal(&$row) + { + if ($row === false || $row === null) { + return; + } + $row['idtablachildPK'] = isset($row['idtablachildPK']) ? (int)$row['idtablachildPK'] : null; + !isset($row['NameChild']) and $row['NameChild'] = null; // varchar + $row['idgrandchildFK'] = isset($row['idgrandchildFK']) ? (int)$row['idgrandchildFK'] : null; + isset($row['_idgrandchildFK']) + and $row['_idgrandchildFK']['idgrandchildPK'] =& $row['idgrandchildFK']; // linked MANYTOONE -} + } -/** -* It converts a row to be inserted or updated into the database.
-* If the column is missing then it is ignored and not converted. -* -* @param array $row [ref] -*/ -public static function convertInputVal(&$row) { - isset($row['idtablachildPK']) and $row['idtablachildPK']=(int)$row['idtablachildPK']; - isset($row['idgrandchildFK']) and $row['idgrandchildFK']=(int)$row['idgrandchildFK']; -} + /** + * It converts a row to be inserted or updated into the database.
+ * If the column is missing then it is ignored and not converted. + * + * @param array $row [ref] + */ + public static function convertInputVal(&$row) + { + isset($row['idtablachildPK']) and $row['idtablachildPK'] = (int)$row['idtablachildPK']; + isset($row['idgrandchildFK']) and $row['idgrandchildFK'] = (int)$row['idgrandchildFK']; + } -/** -* It gets all the name of the columns. -* -* @return string[] -*/ -public static function getDefName() { -return [ - 'idtablachildPK', - 'NameChild', - 'idgrandchildFK' - ]; -} + /** + * It gets all the name of the columns. + * + * @return string[] + */ + public static function getDefName() + { + return [ + 'idtablachildPK', + 'NameChild', + 'idgrandchildFK' + ]; + } -/** -* It returns an associative array (colname=>key type) with all the keys/indexes (if any) -* -* @return string[] -*/ -public static function getDefKey() { -return [ - 'idtablachildPK' => 'PRIMARY KEY', - 'idgrandchildFK' => 'KEY' - ]; -} + /** + * It returns an associative array (colname=>key type) with all the keys/indexes (if any) + * + * @return string[] + */ + public static function getDefKey() + { + return [ + 'idtablachildPK' => 'PRIMARY KEY', + 'idgrandchildFK' => 'KEY' + ]; + } -/** -* It returns a string array with the name of the columns that are skipped when insert -* @return string[] -*/ -public static function getDefNoInsert() { -return [ - 'idtablachildPK' - ]; -} + /** + * It returns a string array with the name of the columns that are skipped when insert + * @return string[] + */ + public static function getDefNoInsert() + { + return [ + 'idtablachildPK' + ]; + } -/** -* It returns a string array with the name of the columns that are skipped when update -* @return string[] -*/ -public static function getDefNoUpdate() { -return [ - 'idtablachildPK' - ]; -} + /** + * It returns a string array with the name of the columns that are skipped when update + * @return string[] + */ + public static function getDefNoUpdate() + { + return [ + 'idtablachildPK' + ]; + } -/** -* It adds a where to the query pipeline. It could be stacked with many where() -* Example:
-*
+    /**
+     * It adds a where to the query pipeline. It could be stacked with many where()
+     * Example:
+ *
      * self::where(['col'=>'value'])::toList();
      * self::where(['col']=>['value'])::toList(); // s= string/double/date, i=integer, b=bool
      * self::where(['col=?']=>['value'])::toList(); // s= string/double/date, i=integer, b=bool
      * 
-* -* @param array|string $sql =self::factory() -* @param null|array|int $param -* -* @return PdoOneQuery -*/ -public static function where($sql, $param = PdoOne::NULL) -{ -return static::newQuery()->where($sql, $param,false,TableChildRepo::TABLE); -} + * + * @param array|string $sql =self::factory() + * @param null|array|int $param + * + * @return PdoOneQuery + */ + public static function where($sql, $param = PdoOne::NULL) + { + return static::newQuery()->where($sql, $param, false, TableChildRepo::TABLE); + } -public static function getDefFK($structure=false) { -if ($structure) { -return [ - 'idgrandchildFK' => 'FOREIGN KEY REFERENCES`TableGrandChild`(`idgrandchildPK`)' - ]; -} -/* key,refcol,reftable,extra */ -return [ - 'idgrandchildFK' => [ - 'key' => 'FOREIGN KEY', - 'refcol' => 'idgrandchildPK', - 'reftable' => 'TableGrandChild', - 'extra' => '', - 'name' => 'fk_TableChild_idgrandchildFK' - ], - '_idgrandchildFK' => [ - 'key' => 'MANYTOONE', - 'refcol' => 'idgrandchildPK', - 'reftable' => 'TableGrandChild', - 'extra' => '', - 'name' => 'fk_TableChild_idgrandchildFK' - ], - '_TableParent' => [ - 'key' => 'ONETOMANY', - 'col' => 'idtablachildPK', - 'reftable' => 'TableParent', - 'refcol' => '_idchildFK' - ] - ]; -} + public static function getDefFK($structure = false) + { + if ($structure) { + return [ + 'idgrandchildFK' => 'FOREIGN KEY REFERENCES`TableGrandChild`(`idgrandchildPK`)' + ]; + } + /* key,refcol,reftable,extra */ + return [ + 'idgrandchildFK' => [ + 'key' => 'FOREIGN KEY', + 'refcol' => 'idgrandchildPK', + 'reftable' => 'TableGrandChild', + 'extra' => '', + 'name' => 'fk_TableChild_idgrandchildFK' + ], + '_idgrandchildFK' => [ + 'key' => 'MANYTOONE', + 'refcol' => 'idgrandchildPK', + 'reftable' => 'TableGrandChild', + 'extra' => '', + 'name' => 'fk_TableChild_idgrandchildFK' + ], + '_TableParent' => [ + 'key' => 'ONETOMANY', + 'col' => 'idtablachildPK', + 'reftable' => 'TableParent', + 'refcol' => '_idchildFK' + ] + ]; + } -/** -* It returns all the relational fields by type. '*' returns all types.
-* It doesn't return normal columns. -* -* @param string $type=['*','MANYTOONE','ONETOMANY','ONETOONE','MANYTOMANY'][$i] -* -* @return string[] -* @noinspection SlowArrayOperationsInLoopInspection -*/ -public static function getRelations($type='all') { -$r= [ - 'MANYTOONE' => [ - '_idgrandchildFK' - ], - 'ONETOMANY' => [ - '_TableParent' - ] - ]; -if($type==='*') { -$result=[]; -foreach($r as $arr) { -$result = array_merge($result,$arr); -} -return $result; -} -return $r[$type] ?? []; -} + /** + * It returns all the relational fields by type. '*' returns all types.
+ * It doesn't return normal columns. + * + * @param string $type =['*','MANYTOONE','ONETOMANY','ONETOONE','MANYTOMANY'][$i] + * + * @return string[] + * @noinspection SlowArrayOperationsInLoopInspection + */ + public static function getRelations($type = 'all') + { + $r = [ + 'MANYTOONE' => [ + '_idgrandchildFK' + ], + 'ONETOMANY' => [ + '_TableParent' + ] + ]; + if ($type === '*') { + $result = []; + foreach ($r as $arr) { + $result = array_merge($result, $arr); + } + return $result; + } + return $r[$type] ?? []; + } -/** -* @param array|int $filter (optional) if we want to filter the results. -* @param array|null $filterValue (optional) the values of the filter -* @return array|bool|null -* @throws Exception -*/ -public static function toList($filter=PdoOne::NULL,$filterValue=null) { -if(self::$useModel) { -return TableChildModel::fromArrayMultiple( self::_toList($filter, $filterValue)); -} -return self::_toList($filter, $filterValue); -} + /** + * @param array|int $filter (optional) if we want to filter the results. + * @param array|null $filterValue (optional) the values of the filter + * @return array|bool|null + * @throws Exception + */ + public static function toList($filter = PdoOne::NULL, $filterValue = null) + { + if (self::$useModel) { + return TableChildModel::fromArrayMultiple(self::_toList($filter, $filterValue)); + } + return self::_toList($filter, $filterValue); + } -/** -* It sets the recursivity. By default, if we query or modify a value, it operates with the fields of the entity. -* With recursivity, we could use the recursivity of the fields, for example, loading a MANYTOONE relation
-* Example:
-*
+    /**
+     * It sets the recursivity. By default, if we query or modify a value, it operates with the fields of the entity.
+     * With recursivity, we could use the recursivity of the fields, for example, loading a MANYTOONE relation
+ * Example:
+ *
      * self::setRecursive([]); // (default) no use recursivity.
      * self::setRecursive('*'); // recursive every MANYTOONE,ONETOONE,MANYTOONE and ONETOONE relations (first level)
      * self::setRecursive('MANYTOONE'); // recursive all relations of the type MANYTOONE (first level)
      * self::setRecursive(['_relation1','_relation2']); // recursive only the relations of the first level
      * self::setRecursive(['_relation1','_relation1/_subrelation1']); //recursive the relations (first and second level)
      * 
-* If array then it uses the values to set the recursivity.
-* If string then the values allowed are '*', 'MANYTOONE','ONETOMANY','MANYTOMANY','ONETOONE' (first level only)
-* -* @param string|array $recursive=self::factory(); -* -* @return PdoOneQuery -*/ -public static function setRecursive($recursive=[]) -{ -if(is_string($recursive)) { -$recursive=TableChildRepo::getRelations($recursive); -} -return parent::_setRecursive($recursive); -} + * If array then it uses the values to set the recursivity.
+ * If string then the values allowed are '*', 'MANYTOONE','ONETOMANY','MANYTOMANY','ONETOONE' (first level only)
+ * + * @param string|array $recursive =self::factory(); + * + * @return PdoOneQuery + */ + public static function setRecursive($recursive = []) + { + if (is_string($recursive)) { + $recursive = TableChildRepo::getRelations($recursive); + } + return parent::_setRecursive($recursive); + } -/** -* It adds an "limit" in a query. It depends on the type of database
-* Example:
-*
+    /**
+     * It adds an "limit" in a query. It depends on the type of database
+ * Example:
+ *
      *      ->select("")->limit("10,20")->toList();
      * 
-* -* @param string $sql Input SQL query -* -* @return PdoOneQuery -* @throws Exception -* @test InstanceOf PdoOne::class,this('1,10') -*/ -public static function limit($sql) -{ -return static::newQuery()->limit($sql); -} + * + * @param string $sql Input SQL query + * + * @return PdoOneQuery + * @throws Exception + * @test InstanceOf PdoOne::class,this('1,10') + */ + public static function limit($sql) + { + return static::newQuery()->limit($sql); + } -/** -* It returns the first row of a query.
-* Example:
-*
+    /**
+     * It returns the first row of a query.
+ * Example:
+ *
      * Repo::first(); // it returns the first value encountered.
      * Repo::first(2); // it returns the first value where the primary key is equals to 2 (simple primary key)
      * Repo::first([2,3]); // it returns the first value where the primary key is equals to 2 (multiple primary keys)
      * Repo::first(['id'=>2,'id2'=>3]); // it returns the first value where id=2 and id2=3 (multiple primary keys)
      * 
-* @param array|mixed|null $pk [optional] Specify the value of the primary key. -* -* @return array|bool It returns false if not file is found. -* @throws Exception -*/ -public static function first($pk = PdoOne::NULL) { -if(self::$useModel) { -return TableChildModel::fromArray(self::_first($pk)); -} -return self::_first($pk); -} + * @param array|mixed|null $pk [optional] Specify the value of the primary key. + * + * @return array|bool It returns false if not file is found. + * @throws Exception + */ + public static function first($pk = PdoOne::NULL) + { + if (self::$useModel) { + return TableChildModel::fromArray(self::_first($pk)); + } + return self::_first($pk); + } -/** -* It returns true if the entity exists, otherwise false.
-* Example:
-*
+    /**
+     *  It returns true if the entity exists, otherwise false.
+ * Example:
+ *
      *  $this->exist(['id'=>'a1','name'=>'name']); // using an array
      *  $this->exist('a1'); // using the primary key. The table needs a pks and it only works with the first pk.
      *  
-* -* @param array|mixed $entity =self::factory() -* -* @return bool true if the pks exists -* @throws Exception -*/ -public static function exist($entity) { -return self::_exist($entity); -} + * + * @param array|mixed $entity =self::factory() + * + * @return bool true if the pks exists + * @throws Exception + */ + public static function exist($entity) + { + return self::_exist($entity); + } -/** -* It inserts a new entity(row) into the database
-* @param array|object $entity =self::factory() -* @param bool $transactional If true (default) then the operation is transactional -* -* @return array|false=self::factory() -* @throws Exception -*/ -public static function insert(&$entity,$transactional=true) { -return self::_insert($entity,$transactional); -} + /** + * It inserts a new entity(row) into the database
+ * @param array|object $entity =self::factory() + * @param bool $transactional If true (default) then the operation is transactional + * + * @return array|false=self::factory() + * @throws Exception + */ + public static function insert(&$entity, $transactional = true) + { + return self::_insert($entity, $transactional); + } -/** -* It merge a new entity(row) into the database. If the entity exists then it is updated, otherwise the entity is -* inserted
-* @param array|object $entity =self::factory() -* @param bool $transactional If true (default) then the operation is transactional -* -* @return array|false=self::factory() -* @throws Exception -*/ -public static function merge(&$entity,$transactional=true) { -return self::_merge($entity,$transactional); -} + /** + * It merge a new entity(row) into the database. If the entity exists then it is updated, otherwise the entity is + * inserted
+ * @param array|object $entity =self::factory() + * @param bool $transactional If true (default) then the operation is transactional + * + * @return array|false=self::factory() + * @throws Exception + */ + public static function merge(&$entity, $transactional = true) + { + return self::_merge($entity, $transactional); + } -/** -* @param array|object $entity =self::factory() -* @param bool $transactional If true (default) then the operation is transactional -* -* @return array|false=self::factory() -* @throws Exception -*/ -public static function update($entity,$transactional=true) { -return self::_update($entity,$transactional); -} + /** + * @param array|object $entity =self::factory() + * @param bool $transactional If true (default) then the operation is transactional + * + * @return array|false=self::factory() + * @throws Exception + */ + public static function update($entity, $transactional = true) + { + return self::_update($entity, $transactional); + } -/** -* It deletes an entity by the primary key -* -* @param array|object $entity =self::factory() -* @param bool $transactional If true (default) then the operation is transactional -* -* @return false|int -* @throws Exception -*/ -public static function delete($entity,$transactional=true) { -return self::_delete($entity,$transactional); -} + /** + * It deletes an entity by the primary key + * + * @param array|object $entity =self::factory() + * @param bool $transactional If true (default) then the operation is transactional + * + * @return false|int + * @throws Exception + */ + public static function delete($entity, $transactional = true) + { + return self::_delete($entity, $transactional); + } -/** -* It deletes an entity by the primary key. -* -* @param array|mixed $pk =self::factory() -* @param bool $transactional If true (default) then the operation is transactional -* -* @return int|false -* @throws Exception -*/ -public static function deleteById($pk,$transactional=true) { -return self::_deleteById($pk,$transactional); -} + /** + * It deletes an entity by the primary key. + * + * @param array|mixed $pk =self::factory() + * @param bool $transactional If true (default) then the operation is transactional + * + * @return int|false + * @throws Exception + */ + public static function deleteById($pk, $transactional = true) + { + return self::_deleteById($pk, $transactional); + } -/** -* Returns an array with the default values (0 for numbers, empty for string, and array|null if recursive) -* -* @param array|null $values =self::factory() -* @param string $recursivePrefix It is the prefix of the recursivity. -* -* @return array -*/ -public static function factory($values = null, $recursivePrefix = '') { -$recursive=static::getRecursive(); -static::setRecursive(); // reset the recursivity. -$row= [ - 'idtablachildPK'=>0, - '_TableParent'=>(in_array($recursivePrefix.'_TableParent',$recursive,true)) - ? [] - : null, /* ONETOMANY! */ - 'NameChild'=>'', - 'idgrandchildFK'=>0, - '_idgrandchildFK'=>(in_array($recursivePrefix.'_idgrandchildFK',$recursive,true)) - ? TableGrandChildRepo::factory(null,$recursivePrefix.'_idgrandchildFK') - : null, /* MANYTOONE!! */ - ]; - isset($row['_idgrandchildFK']) - and $row['_idgrandchildFK']['idgrandchildPK']=&$row['idgrandchildFK']; // linked MANYTOONE + /** + * Returns an array with the default values (0 for numbers, empty for string, and array|null if recursive) + * + * @param array|null $values =self::factory() + * @param string $recursivePrefix It is the prefix of the recursivity. + * + * @return array + */ + public static function factory($values = null, $recursivePrefix = '') + { + $recursive = static::getRecursive(); + static::setRecursive(); // reset the recursivity. + $row = [ + 'idtablachildPK' => 0, + '_TableParent' => (in_array($recursivePrefix . '_TableParent', $recursive, true)) + ? [] + : null, /* ONETOMANY! */ + 'NameChild' => '', + 'idgrandchildFK' => 0, + '_idgrandchildFK' => (in_array($recursivePrefix . '_idgrandchildFK', $recursive, true)) + ? TableGrandChildRepo::factory(null, $recursivePrefix . '_idgrandchildFK') + : null, /* MANYTOONE!! */ + ]; + isset($row['_idgrandchildFK']) + and $row['_idgrandchildFK']['idgrandchildPK'] =& $row['idgrandchildFK']; // linked MANYTOONE -if ($values !== null) { -$row = array_merge($row, $values); -} -return $row; -} + if ($values !== null) { + $row = array_merge($row, $values); + } + return $row; + } -/** -* It returns an empty array with null values and no recursivity. -* @param array|null $values=self::factoryNull() -* -* @return array -*/ -public static function factoryNull($values=null) { -$row= [ - 'idtablachildPK'=>null, - '_TableParent'=>null, /* ONETOMANY! */ - 'NameChild'=>null, - 'idgrandchildFK'=>null, - '_idgrandchildFK'=>null, /* MANYTOONE!! */ - ]; -if ($values !== null) { -$row = array_merge($row, $values); -} -return $row; -} + /** + * It returns an empty array with null values and no recursivity. + * @param array|null $values =self::factoryNull() + * + * @return array + */ + public static function factoryNull($values = null) + { + $row = [ + 'idtablachildPK' => null, + '_TableParent' => null, /* ONETOMANY! */ + 'NameChild' => null, + 'idgrandchildFK' => null, + '_idgrandchildFK' => null, /* MANYTOONE!! */ + ]; + if ($values !== null) { + $row = array_merge($row, $values); + } + return $row; + } } diff --git a/lib/PdoOne.php b/lib/PdoOne.php index 8b0b09d..4501d5f 100644 --- a/lib/PdoOne.php +++ b/lib/PdoOne.php @@ -1,4 +1,5 @@ - + /** @var CliOne */ + private $cli; /** * PdoOne constructor. It doesn't open the connection to the database. @@ -242,14 +246,12 @@ public function __construct( $this->construct($database, $server, $user, $pwd, $db, $logFile, $charset, $nodeId); if (!class_exists('eftec\MessageContainer')) { throw new RuntimeException('MessageContainer class does not exist'); - } // autowire MessageContainer if the method exists. - $this->messageContainer=MessageContainer::instance(); - if(self::$instance===null) { - self::$instance=$this; + $this->messageContainer = MessageContainer::instance(); + if (self::$instance === null) { + self::$instance = $this; } - } /** @@ -257,12 +259,12 @@ public function __construct( * @param bool $throwIfNull * @return PdoOne|null */ - public static function instance($throwIfNull=true): ?PdoOne + public static function instance($throwIfNull = true): ?PdoOne { - if(self::$instance===null && $throwIfNull) { - throw new RuntimeException('instance not created for PdoOne'); - } - return self::$instance; + if (self::$instance === null && $throwIfNull) { + throw new RuntimeException('instance not created for PdoOne'); + } + return self::$instance; } protected function construct( @@ -292,13 +294,15 @@ protected function construct( case 'test': $this->service = new PdoOne_TestMockup($this); break; + default: + throw new RuntimeException('no value selected'); } $charset = $this->service->construct($charset, []); $this->server = $server; $this->user = $user; $this->pwd = $pwd; $this->db = $db; - $this->lockerId='Pdo::'.$this->db; + $this->lockerId = 'Pdo::' . $this->db; $this->tableDependencyArray = null; $this->tableDependencyArrayCol = null; $this->logFile = $logFile; @@ -364,7 +368,6 @@ public static function unixtime2Sql($dateNum): ?string if ($dateNum === null) { return self::$dateEpoch; } - return date(self::$isoDateTimeMs, $dateNum); } @@ -408,10 +411,8 @@ public static function dateTimeSql2PHP($sqlField, &$hasTime = false) if (self::$dateEpoch === null) { return null; } - return DateTime::createFromFormat(self::$isoDateTimeMs, self::$dateEpoch); } - if (strpos($sqlField, '.')) { // with date with time and microseconds //2018-02-06 05:06:07.123 @@ -420,7 +421,6 @@ public static function dateTimeSql2PHP($sqlField, &$hasTime = false) //$x = DateTime::createFromFormat("Y-m-d H:i:s.u", "2018-02-06 05:06:07.1234"); return DateTime::createFromFormat(self::$isoDateTimeMs, $sqlField); } - if (strpos($sqlField, ':')) { // date with time $hasTime = true; @@ -428,7 +428,6 @@ public static function dateTimeSql2PHP($sqlField, &$hasTime = false) } // only date $hasTime = false; - return DateTime::createFromFormat(self::$isoDate, $sqlField); } @@ -493,7 +492,6 @@ public static function dateConvert($sqlField, $inputFormat, $outputFormat, $forc if ($time) { return $tmpDate->format(self::$dateTimeHumanFormat); } - return $tmpDate->format(self::$dateHumanFormat); case 'sql': if ($ms) { @@ -502,7 +500,6 @@ public static function dateConvert($sqlField, $inputFormat, $outputFormat, $forc if ($time) { return $tmpDate->format(self::$isoDateInputTime); } - return $tmpDate->format(self::$isoDateInput); case 'class': return $tmpDate; @@ -559,7 +556,6 @@ public static function dateConvertInput($inputValue, $inputFormat, &$ms, &$time) $tmpDate = DateTime::createFromFormat(self::$dateTimeHumanFormat, $inputValue); } else { $tmpDate = DateTime::createFromFormat(self::$dateHumanFormat, $inputValue); - if ($tmpDate === false) { return false; } @@ -620,12 +616,10 @@ public static function dateText2Sql($textDate, $hasTime = true): ?string } else { $tmpFormat = self::$dateFormat; } - $tmpDate = DateTime::createFromFormat($tmpFormat, $textDate); if (!$hasTime && $tmpDate) { $tmpDate->setTime(0, 0); } - return self::dateTimePHP2Sql($tmpDate); // it always returns a date with time. Mysql Ignores it. } @@ -647,7 +641,6 @@ public static function dateTimePHP2Sql($date): ?string if ($date->format('u') !== '000000') { return $date->format(self::$isoDateTimeMs); } - return $date->format(self::$isoDateTime); } @@ -710,7 +703,6 @@ public static function dateSqlNow($hasTime = true, $hasMicroseconds = false): st if ($hasTime) { return $tmpDate->format(($hasMicroseconds !== false) ? self::$isoDateTimeMs : self::$isoDateTime); } - return $tmpDate->format(self::$isoDate); } @@ -794,7 +786,6 @@ public static function replaceBetween( $len = $p1 + strlen($endNeedle) - $ini; $offset = $ini + $len; return substr_replace($haystack, $replaceText, $ini, $len); - } $len = $p1 - $ini2; $offset = $ini2 + $len; @@ -833,7 +824,6 @@ public static function tableCase($txt) } if (strpos($txt, '_') !== false || strpos($txt, ' ') !== false) { $txt = strtolower($txt); - $result = ''; $l = strlen($txt); for ($i = 0; $i < $l; $i++) { @@ -849,7 +839,6 @@ public static function tableCase($txt) $result .= $c; } } - return self::singularTable(ucfirst($result)); } // the text is simple. @@ -857,7 +846,8 @@ public static function tableCase($txt) } /** - * It converts a name to singular. This method is used automatically for the generation of the repository classes
+ * It converts a name to singular. This method is used automatically for the generation of the repository + * classes
* Example:
*
      * self::singularTable('categories'); // category
@@ -871,13 +861,13 @@ public static function tableCase($txt)
     public static function singularTable($tableName)
     {
         $l = strlen($tableName);
-        if ($l>=3 && substr($tableName,-3)==='ies') {
+        if ($l >= 3 && substr($tableName, -3) === 'ies') {
             // categories => category
-            $tableName = substr($tableName, 0, $l - 3).'y';
-        } else if ($l>=2 && substr($tableName,-2)==='es') {
+            $tableName = substr($tableName, 0, $l - 3) . 'y';
+        } else if ($l >= 2 && substr($tableName, -2) === 'es') {
             // churches => church (however it fails with prices => pric)
             $tableName = substr($tableName, 0, $l - 2);
-        } else if($l>=1 && substr($tableName,-1)==='s') {
+        } else if ($l >= 1 && substr($tableName, -1) === 's') {
             // users => user
             $tableName = substr($tableName, 0, $l - 1);
         }
@@ -955,7 +945,6 @@ public function validateDefTable($table, $defArray, $defKeys, $defFK): array
                 $error[$k] = "fk: $dc , $defFK[$k] are different";
             }
         }
-
         return $error;
     }
 
@@ -996,12 +985,12 @@ public function getDefTable($table, $specialConversion = null): array
             $type = $t[0];
             $conversion = $specialConversion[$k] ?? null;
             $extra = (count($t) > 1) ? $t[1] : null;
-            if ($extra!==null && stripos($extra, 'not null') !== false) {
+            if ($extra !== null && stripos($extra, 'not null') !== false) {
                 $null = false;
             } else {
                 $null = true;
             }
-            if ($extra!==null && stripos($extra, $this->database_identityName) !== false) {
+            if ($extra !== null && stripos($extra, $this->database_identityName) !== false) {
                 $identity = true;
             } else {
                 $identity = false;
@@ -1023,7 +1012,6 @@ public function getDefTable($table, $specialConversion = null): array
                 'sql' => $v
             ];
         }
-
         return $r;
     }
 
@@ -1130,7 +1118,7 @@ public function dbTypeToPHP($type): array
      *
      * @param string $table            The name of the table to analize.
      * @param bool   $returnSimple     true= returns as a simple associative
-     *                                 array
example:['id'=>'PRIMARY + * array
Example:['id'=>'PRIMARY * KEY','name'=>'FOREIGN KEY...']
false= * returns as an associative array separated * by parts
@@ -1149,7 +1137,7 @@ public function getDefTableKeys($table, $returnSimple = true, $filter = null): a /** * @param string $table The name of the table to analize. * @param bool $returnSimple true= returns as a simple associative - * array
example:['id'=>'PRIMARY + * array
Example:['id'=>'PRIMARY * KEY','name'=>'FOREIGN KEY...']
false= * returns as an associative array separated * by parts
@@ -1202,16 +1190,143 @@ public function getDefTableExtended($table, $onlyDescription = false) */ public function cliEngine(): void { - $database = self::getParameterCli('database'); - $server = self::getParameterCli('server'); - $user = self::getParameterCli('user'); - $pwd = self::getParameterCli('pwd'); - $db = self::getParameterCli('db'); - $input = self::getParameterCli('input'); - $output = self::getParameterCli('output'); - $namespace = self::getParameterCli('namespace'); + $this->cli = new CliOne(__FILE__); + $this->cli->createParam('database') + ->setRequired(false) + ->setDescription('The type of database', 'Select the type of database', [ + 'Values allowed: