Skip to content

Commit d5668fd

Browse files
committed
add/test alter/altering table methods, corrections
1 parent 525e1de commit d5668fd

File tree

5 files changed

+151
-29
lines changed

5 files changed

+151
-29
lines changed

lib/ezFunctions.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,37 @@ function dropping()
805805
}
806806

807807
/**
808-
* Set table `name` and `prefix` for global usage on calls to database
808+
* Preforms a `alter` method call on a already preset `table name`, and optional `prefix`
809+
*
810+
* This method **expects** either `table_setup(name, prefix)`, `set_table(name)`, or `set_prefix(append)`
811+
* to have been called **before usage**, otherwise will return `false`, if no `table name` previous stored.
812+
*
813+
* Modify columns in an existing database table, by either:
814+
*```js
815+
* - array( column_name, datatype, ...value/options arguments ) // calls create_schema()
816+
* - addColumn( column_name, datatype, ...value/options arguments ) // returns string
817+
* - dropColumn( column_name ) // returns string
818+
* - changingColumn( column_name, datatype, ...value/options arguments ) // returns string
819+
*```
820+
* @param array ...$alteringSchema An array of:
821+
*
822+
* - @param string `$name,` - column name
823+
* - @param string `$type,` - data type for the column
824+
* - @param mixed `$size,` | `$value,`
825+
* - @param mixed `...$anyOtherArgs`
826+
*
827+
* @return mixed results of query() call
828+
*/
829+
function altering(...$alteringSchema)
830+
{
831+
$ezQuery = getInstance();
832+
return ($ezQuery instanceof DatabaseInterface)
833+
? $ezQuery->altering(...$alteringSchema)
834+
: false;
835+
}
836+
837+
/**
838+
* Set table `name` and `prefix` for usage on calls to database `CRUD`
809839
* **method/function** *names* ending with `ing`.
810840
*
811841
* @param string $name
@@ -821,7 +851,7 @@ function table_setup(string $name = '', string $prefix = '')
821851
}
822852

823853
/**
824-
* Set table `name` to use on calls to database **method/function** *names* ending with `ing`.
854+
* Set table `name` to use on calls to database `CRUD` **method/function** *names* ending with `ing`.
825855
*
826856
* @param string $name
827857
*/
@@ -835,7 +865,7 @@ function set_table(string $name = '')
835865
}
836866

837867
/**
838-
* Add a `prefix` to **append** to `table` name on calls to database
868+
* Add a `prefix` to **append** to `table` name on calls to database `CRUD`
839869
* **method/function** *names* ending with `ing`.
840870
*
841871
* @param string $append

lib/ezQuery.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use ezsql\ezSchema;
66
use ezsql\ezQueryInterface;
7-
use function ezsql\functions\column;
7+
use function ezsql\functions\{column, getVendor};
88

99
class ezQuery implements ezQueryInterface
1010
{
@@ -19,14 +19,16 @@ class ezQuery implements ezQueryInterface
1919
protected $insertId = null;
2020

2121
/**
22-
* The table `name` to use on calls to `selecting` method.
22+
* The table `name` to use on calls to `ing` ending
23+
* `CRUD` methods/functions.
2324
*
2425
* @var string
2526
*/
2627
protected $table = '';
2728

2829
/**
29-
* A `prefix` to append to `table` on calls to `selecting` method.
30+
* A `prefix` to append to `table` on calls to `ing` ending
31+
* `CRUD` methods/functions.
3032
*
3133
* @var string
3234
*/
@@ -894,7 +896,7 @@ private function create_schema(array ...$columnDataOptions)
894896

895897
public function create(string $table = null, ...$schemas)
896898
{
897-
$vendor = ezSchema::vendor();
899+
$vendor = getVendor();
898900
if (empty($table) || empty($schemas) || empty($vendor))
899901
return false;
900902

@@ -955,24 +957,6 @@ public function create(string $table = null, ...$schemas)
955957
return false;
956958
}
957959

958-
/**
959-
* Modify columns in an existing database table, by either:
960-
*```js
961-
* - array( column_name, datatype, ...value/options arguments ) // calls create_schema()
962-
* - addColumn( column_name, datatype, ...value/options arguments ) // returns string
963-
* - dropColumn( column_name ) // returns string
964-
* - changingColumn( column_name, datatype, ...value/options arguments ) // returns string
965-
*```
966-
* @param string $table The name of the db table that you wish to alter
967-
* @param array ...$alteringSchema An array of:
968-
*
969-
* - @param string `$name,` - column name
970-
* - @param string `$type,` - data type for the column
971-
* - @param mixed `$size,` | `$value,`
972-
* - @param mixed `...$anyOtherArgs`
973-
*
974-
* @return mixed results of query() call
975-
*/
976960
public function alter(string $table = null, ...$alteringSchema)
977961
{
978962
if (empty($table) || empty($alteringSchema))
@@ -1056,8 +1040,14 @@ public function dropping()
10561040
return ($table === false) ? false : $this->drop($table);
10571041
}
10581042

1043+
public function altering(...$alteringSchema)
1044+
{
1045+
$table = $this->table_prefix();
1046+
return ($table === false) ? false : $this->alter($table, ...$alteringSchema);
1047+
}
1048+
10591049
/**
1060-
* Check and return the stored **global** database `table` preset with any `prefix`.
1050+
* Check and return the stored database `table` preset with any `prefix`.
10611051
*
10621052
* @return boolean|string `false` if no preset.
10631053
*/
@@ -1066,7 +1056,7 @@ protected function table_prefix()
10661056
if (empty($this->table) || !\is_string($this->table))
10671057
return $this->clearPrepare();
10681058

1069-
$table = (!empty($this->prefix) || \is_string($this->prefix))
1059+
$table = (!empty($this->prefix) && \is_string($this->prefix))
10701060
? $this->prefix . $this->table
10711061
: $this->table;
10721062

lib/ezQueryInterface.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,48 @@ public function drop(string $table = null);
749749
* @return bool|int
750750
*/
751751
public function dropping();
752+
753+
/**
754+
* Modify columns in an existing database table, by either:
755+
*```js
756+
* - array( column_name, datatype, ...value/options arguments ) // calls create_schema()
757+
* - addColumn( column_name, datatype, ...value/options arguments ) // returns string
758+
* - dropColumn( column_name ) // returns string
759+
* - changingColumn( column_name, datatype, ...value/options arguments ) // returns string
760+
*```
761+
* @param string $table The name of the db table that you wish to alter
762+
* @param array ...$alteringSchema An array of:
763+
*
764+
* - @param string `$name,` - column name
765+
* - @param string `$type,` - data type for the column
766+
* - @param mixed `$size,` | `$value,`
767+
* - @param mixed `...$anyOtherArgs`
768+
*
769+
* @return mixed results of query() call
770+
*/
771+
public function alter(string $table = null, ...$alteringSchema);
772+
773+
/**
774+
* Preforms a `alter` method call on a already preset `table name`, and optional `prefix`
775+
*
776+
* This method **expects** either `tableSetup(name, prefix)`, `setTable(name)`, or `setPrefix(append)`
777+
* to have been called **before usage**, otherwise will return `false`, if no `table name` previous stored.
778+
*
779+
* Modify columns in an existing database table, by either:
780+
*```js
781+
* - array( column_name, datatype, ...value/options arguments ) // calls create_schema()
782+
* - addColumn( column_name, datatype, ...value/options arguments ) // returns string
783+
* - dropColumn( column_name ) // returns string
784+
* - changingColumn( column_name, datatype, ...value/options arguments ) // returns string
785+
*```
786+
* @param array ...$alteringSchema An array of:
787+
*
788+
* - @param string `$name,` - column name
789+
* - @param string `$type,` - data type for the column
790+
* - @param mixed `$size,` | `$value,`
791+
* - @param mixed `...$anyOtherArgs`
792+
*
793+
* @return mixed results of query() call
794+
*/
795+
public function altering(...$alteringSchema);
752796
}

lib/ezsqlModel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,16 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
306306
protected $lastQuery = null;
307307

308308
/**
309-
* The table `name` to use on calls to `selecting` method.
309+
* The table `name` to use on calls to `ing` ending m
310+
* `CRUD` methods/functions.
310311
*
311312
* @var string
312313
*/
313314
protected $table = '';
314315

315316
/**
316-
* A `prefix` to append to `table` name on calls to `selecting` method.
317+
* A `prefix` to append to `table` name on calls to `ing` ending
318+
* `CRUD` methods/functions.
317319
*
318320
* @var string
319321
*/

tests/mysqli/mysqliTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use function ezsql\functions\{
1111
mysqlInstance,
1212
column,
13+
addColumn,
14+
dropColumn,
1315
primary,
1416
eq,
1517
like,
@@ -18,7 +20,11 @@
1820
selecting,
1921
inserting,
2022
set_table,
23+
set_prefix,
2124
creating,
25+
dropping,
26+
altering,
27+
get_results,
2228
replacing,
2329
table_setup,
2430
where
@@ -109,6 +115,7 @@ public function testDbSelect()
109115
$this->errors = array();
110116
$this->assertTrue($this->object->dbSelect(''));
111117
$this->object->disconnect();
118+
set_error_handler(array($this, 'errorHandler'));
112119
$this->assertFalse($this->object->dbSelect('notest'));
113120
$this->object->connect();
114121
$this->object->reset();
@@ -431,6 +438,55 @@ public function testSelectingInserting()
431438
}
432439
}
433440

441+
public function testAltering()
442+
{
443+
$this->object->connect();
444+
set_table('test');
445+
set_prefix('unit_');
446+
creating(
447+
column('id', INTR, 11, PRIMARY),
448+
column('test_key', VARCHAR, 50)
449+
);
450+
451+
$results = null;
452+
$results = altering(
453+
addColumn('add_key', VARCHAR, 50)
454+
);
455+
$this->assertEquals(0, $results);
456+
457+
inserting(array('id' => 1, 'test_key' => 'testing 1', 'add_key' => 'adding 1'));
458+
inserting(array('id' => 2, 'test_key' => 'testing 2', 'add_key' => 'adding 2'));
459+
inserting(array('id' => 3, 'test_key' => 'testing 3', 'add_key' => 'adding 3'));
460+
461+
$result = selecting();
462+
463+
$i = 1;
464+
foreach ($result as $row) {
465+
$this->assertEquals($i, $row->id);
466+
$this->assertEquals('testing ' . $i, $row->test_key);
467+
$this->assertEquals('adding ' . $i, $row->add_key);
468+
++$i;
469+
}
470+
471+
$results = null;
472+
$results = altering(
473+
dropColumn('test_key')
474+
);
475+
$this->assertEquals(0, $results);
476+
477+
selecting();
478+
479+
$i = 1;
480+
foreach (get_results() as $row) {
481+
$this->assertEquals($i, $row->id);
482+
$this->assertNotEquals('testing ' . $i, $row->test_key);
483+
$this->assertEquals('adding ' . $i, $row->add_key);
484+
++$i;
485+
}
486+
487+
dropping();
488+
}
489+
434490
public function testBeginTransactionCommit()
435491
{
436492
$this->object->connect();

0 commit comments

Comments
 (0)