From 40307b5a1686ff8b297ada2a6e9db92f2a0494b0 Mon Sep 17 00:00:00 2001 From: techno-express Date: Fri, 19 Feb 2021 19:20:56 -0500 Subject: [PATCH] corrections/updates, additional function, and renaming --- README.md | 25 +++++++++++++++++++------ composer.json | 7 ++++--- lib/ezFunctions.php | 12 +++++++++--- lib/ezQuery.php | 4 ++-- lib/ezSchema.php | 7 ++++--- lib/ezsqlModel.php | 4 ++-- tests/ezFunctionsTest.php | 21 +++++++++++++++++++-- tests/ezSchemaTest.php | 14 +++++++------- 8 files changed, 66 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index fb0d83c..1de2310 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ [![Total Downloads](https://poser.pugx.org/ezsql/ezsql/downloads)](https://packagist.org/packages/ezsql/ezsql) ***A class to make it very easy to deal with database connections.*** +*An universal interchangeable **CRUD** system.* This is [__Version 5__](https://github.com/ezSQL/ezsql/tree/v5) which will break users of **version 4**. @@ -34,6 +35,9 @@ Mainly by: - The table *name* with *prefix*, can be preset/stored with methods `tableSetup(name, prefix), or setTable(name), setPrefix(append)`, if called without presetting, `false` is returned. - This **feature** will be added to **all** database *CRUD* access methods , each method name will have an `ing` ending added. - Removed global functions where `table` name passed in, use functions using preset table names ending with `ing`. +- renamed cleanInput to clean_string +- renamed createCertificate to create_certificate +- added global get_results to return result sets in different formats [__Version 4__](https://github.com/ezSQL/ezsql/tree/v4) has many modern programming practices in which will break users of version 3. @@ -83,7 +87,7 @@ ___General Methods___ string $path = '.'._DS ); secureReset(); - createCertificate(string $privatekeyFile = certificate.key, + create_certificate(string $privatekeyFile = certificate.key, string $certificateFile = certificate.crt, string $signingFile = certificate.csr, string $ssl_path = null, array $details = [commonName => localhost] @@ -96,6 +100,7 @@ ___Shortcut Table Methods___ primary(string $primaryName, ...$primaryKeys); index(string $indexName, ...$indexKeys); drop(string $table); + Example ```php @@ -147,6 +152,9 @@ prepareOff(); // When off shortcut SQL Methods calls will use vendors escape rou * `delete(string $table = null, ...$whereConditions);` * `replace(string $table = null, $keyAndValue);` * `insert(string $table = null, $keyAndValue);` +* `create(string $table = null, ...$schemas);` +* `drop(string $table = null);` +* `alter(string $table = null, ...$alteringSchema);` * `insert_select(string $toTable = null, $toColumns = '*', $fromTable = null, $fromColumns = '*', ...$conditions);` ```php @@ -239,6 +247,9 @@ $result = $db->select('profile', 'name, email', foreach ($result as $row) { echo $row->name.' '.$row->email; } + +// To get results in `JSON` format +$json = get_results(JSON, $db); ``` #### Example for using prepare statements directly, no shortcut SQL methods used @@ -273,7 +284,7 @@ use function ezsql\functions\{ setInstance, getInstance, clearInstance, - getVendor, + get_vendor, /// to_string, clean_string, @@ -286,6 +297,7 @@ use function ezsql\functions\{ index, addColumn, dropColumn, + changingColumn, /// eq, neq, @@ -303,9 +315,6 @@ use function ezsql\functions\{ between, notBetween, /// - select_into, - insert_select, - create_select, where, grouping, groupBy, @@ -325,10 +334,14 @@ use function ezsql\functions\{ replacing, selecting, inserting, + altering, get_results, table_setup, set_table, - set_prefix + set_prefix, + select_into, + insert_select, + create_select, }; ``` diff --git a/composer.json b/composer.json index 29f7ef4..912fedd 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,9 @@ { "name": "ezsql/ezsql", - "description": "Advance database access library. Make interacting with a database ridiculously easy.", + "description": "Advance database access library. Make interacting with a database ridiculously easy. An universal interchangeable CRUD system.", "keywords": [ + "crud", + "dba", "mysql", "mysqli", "postgresql", @@ -13,8 +15,7 @@ "sqlite3", "database", "abstraction", - "sql", - "dba" + "sql" ], "license": [ "LGPL-3.0-or-later", diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index c2a59fe..56c2d7f 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -133,13 +133,14 @@ function sqliteInstance(array $databaseSetting = null, string $instanceTag = nul } /** - * Returns the current global database vendor being used. + * Returns database vendor string, either the global instance, or provided class instance. + * @param \ezsql\DatabaseInterface|null $instance * * @return string|null `mysqli`|`pgsql`|`sqlite3`|`sqlsrv` */ - function getVendor() + function get_vendor(DatabaseInterface $instance = null) { - return ezSchema::vendor(); + return ezSchema::vendor($instance); } /** @@ -209,6 +210,11 @@ function dropColumn(string $columnName, ...$data) return column(\DROP, $columnName, ...$data); } + function changingColumn(string $columnName, ...$datatype) + { + return column(\CHANGER, $columnName, ...$datatype); + } + /** * Creates self signed certificate * diff --git a/lib/ezQuery.php b/lib/ezQuery.php index a1a9421..51bba4b 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -4,7 +4,7 @@ use ezsql\ezSchema; use ezsql\ezQueryInterface; -use function ezsql\functions\{column, getVendor}; +use function ezsql\functions\{column, get_vendor}; class ezQuery implements ezQueryInterface { @@ -896,7 +896,7 @@ private function create_schema(array ...$columnDataOptions) public function create(string $table = null, ...$schemas) { - $vendor = getVendor(); + $vendor = get_vendor(); if (empty($table) || empty($schemas) || empty($vendor)) return false; diff --git a/lib/ezSchema.php b/lib/ezSchema.php index 2a70a83..6ffafb2 100644 --- a/lib/ezSchema.php +++ b/lib/ezSchema.php @@ -158,14 +158,15 @@ public function __call($type, $args) } /** - * Returns the current global database vendor being used. + * Returns database vendor string, either the global instance, or provided database class. + * @param \ezsql\DatabaseInterface|null $db * * @return string|null `mysqli`|`pgsql`|`sqlite3`|`sqlsrv` */ - public static function vendor() + public static function vendor(DatabaseInterface $db = null) { $type = null; - $instance = getInstance(); + $instance = empty($db) || !is_object($db) ? getInstance() : $db; if ($instance instanceof DatabaseInterface) { $type = $instance->settings()->getDriver(); if ($type === \Pdo) { diff --git a/lib/ezsqlModel.php b/lib/ezsqlModel.php index 8978708..897c968 100644 --- a/lib/ezsqlModel.php +++ b/lib/ezsqlModel.php @@ -4,7 +4,7 @@ use ezsql\ezQuery; use ezsql\ezsqlModelInterface; -use function ezsql\functions\{getVendor, create_certificate}; +use function ezsql\functions\{get_vendor, create_certificate}; /** * Core class containing common functions to manipulate **query** `result sets` once returned. @@ -855,7 +855,7 @@ public function secureSetup( string $path = '.' . \_DS ) { if (!\file_exists($path . $cert) || !\file_exists($path . $key)) { - $vendor = getVendor(); + $vendor = get_vendor(); if (($vendor != \SQLITE) || ($vendor != \MSSQL)) $path = create_certificate(); } elseif ($path == '.' . \_DS) { diff --git a/tests/ezFunctionsTest.php b/tests/ezFunctionsTest.php index 3910b87..32c3b27 100644 --- a/tests/ezFunctionsTest.php +++ b/tests/ezFunctionsTest.php @@ -8,7 +8,7 @@ setInstance, getInstance, clearInstance, - getVendor, + get_vendor, column, primary, foreign, @@ -16,6 +16,7 @@ index, addColumn, dropColumn, + changingColumn, eq, neq, ne, @@ -45,6 +46,8 @@ replacing, selecting, inserting, + altering, + get_results, table_setup, set_table, set_prefix @@ -64,7 +67,7 @@ public function testGetInstance() public function testGetVendor() { - $this->assertNull(getVendor()); + $this->assertNull(get_vendor()); } public function testColumn() @@ -101,6 +104,10 @@ public function testDropColumn() { $this->assertFalse(dropColumn('column', 'column')); } + public function testChangingColumn() + { + $this->assertFalse(changingColumn('column', 'column')); + } public function testEq() { @@ -269,6 +276,16 @@ public function testReplacing() $this->assertFalse(replacing(['data' => 'data2'])); } + public function testAltering() + { + $this->assertFalse(altering([])); + } + + public function testGet_results() + { + $this->assertFalse(get_results()); + } + public function testDropping() { $this->assertFalse(dropping()); diff --git a/tests/ezSchemaTest.php b/tests/ezSchemaTest.php index 1f72075..185be60 100644 --- a/tests/ezSchemaTest.php +++ b/tests/ezSchemaTest.php @@ -11,7 +11,7 @@ mssqlInstance, sqliteInstance, clearInstance, - getVendor, + get_vendor, column, primary, index @@ -22,7 +22,7 @@ class ezSchemaTest extends EZTestCase public function testVendor() { clearInstance(); - $this->assertEquals(null, getVendor()); + $this->assertEquals(null, get_vendor()); $this->assertEquals(false, ezSchema::datatype(BLOB, NULLS)); $this->assertFalse(column('id', INTR, 32, AUTO, PRIMARY)); } @@ -36,7 +36,7 @@ public function testVendor_mysqli() } mysqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]); - $this->assertEquals(MYSQLI, getVendor()); + $this->assertEquals(MYSQLI, get_vendor()); $this->assertEquals('BLOB NULL', ezSchema::datatype(BLOB, NULLS)); $this->assertEquals('VARCHAR(256) NOT NULL', ezSchema::datatype(VARCHAR, 256, notNULL)); $this->assertEquals('id INT(32) AUTO_INCREMENT PRIMARY KEY, ', column('id', INTR, 32, AUTO, PRIMARY)); @@ -129,7 +129,7 @@ public function testVendor_Pgsql() } pgsqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT]); - $this->assertEquals(PGSQL, getVendor()); + $this->assertEquals(PGSQL, get_vendor()); $this->assertEquals('TIMESTAMP NOT NULL', ezSchema::datatype(TIMESTAMP, notNULL)); $this->assertEquals('price NUMERIC(6,2) NULL, ', column('price', NUMERIC, 6, 2, NULLS)); $this->assertEquals('id SERIAL PRIMARY KEY, ', column('id', AUTO, PRIMARY)); @@ -144,7 +144,7 @@ public function testVendor_Sqlite3() } sqliteInstance([self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB]); - $this->assertEquals(SQLITE3, getVendor()); + $this->assertEquals(SQLITE3, get_vendor()); } public function testVendor_Sqlsrv() @@ -156,7 +156,7 @@ public function testVendor_Sqlsrv() } mssqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME]); - $this->assertEquals(MSSQL, getVendor()); + $this->assertEquals(MSSQL, get_vendor()); } public function testVendor_Pdo() @@ -169,7 +169,7 @@ public function testVendor_Pdo() $pdo_mysql = pdoInstance(['mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=3306', self::TEST_DB_USER, self::TEST_DB_PASSWORD]); $pdo_mysql->connect(); - $this->assertEquals(MYSQLI, getVendor()); + $this->assertEquals(MYSQLI, get_vendor()); } public function test__construct()