Skip to content

Commit

Permalink
corrections/updates, additional function, and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Feb 20, 2021
1 parent d5668fd commit 40307b5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 28 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.

Expand All @@ -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.

Expand Down Expand Up @@ -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]
Expand All @@ -96,6 +100,7 @@ ___Shortcut Table Methods___
primary(string $primaryName, ...$primaryKeys);
index(string $indexName, ...$indexKeys);
drop(string $table);

Example

```php
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -273,7 +284,7 @@ use function ezsql\functions\{
setInstance,
getInstance,
clearInstance,
getVendor,
get_vendor,
///
to_string,
clean_string,
Expand All @@ -286,6 +297,7 @@ use function ezsql\functions\{
index,
addColumn,
dropColumn,
changingColumn,
///
eq,
neq,
Expand All @@ -303,9 +315,6 @@ use function ezsql\functions\{
between,
notBetween,
///
select_into,
insert_select,
create_select,
where,
grouping,
groupBy,
Expand All @@ -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,
};
```

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -13,8 +15,7 @@
"sqlite3",
"database",
"abstraction",
"sql",
"dba"
"sql"
],
"license": [
"LGPL-3.0-or-later",
Expand Down
12 changes: 9 additions & 3 deletions lib/ezFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions lib/ezQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions lib/ezSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ezsqlModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 19 additions & 2 deletions tests/ezFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
setInstance,
getInstance,
clearInstance,
getVendor,
get_vendor,
column,
primary,
foreign,
unique,
index,
addColumn,
dropColumn,
changingColumn,
eq,
neq,
ne,
Expand Down Expand Up @@ -45,6 +46,8 @@
replacing,
selecting,
inserting,
altering,
get_results,
table_setup,
set_table,
set_prefix
Expand All @@ -64,7 +67,7 @@ public function testGetInstance()

public function testGetVendor()
{
$this->assertNull(getVendor());
$this->assertNull(get_vendor());
}

public function testColumn()
Expand Down Expand Up @@ -101,6 +104,10 @@ public function testDropColumn()
{
$this->assertFalse(dropColumn('column', 'column'));
}
public function testChangingColumn()
{
$this->assertFalse(changingColumn('column', 'column'));
}

public function testEq()
{
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 7 additions & 7 deletions tests/ezSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
mssqlInstance,
sqliteInstance,
clearInstance,
getVendor,
get_vendor,
column,
primary,
index
Expand All @@ -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));
}
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 40307b5

Please sign in to comment.