Skip to content

Commit

Permalink
Merge pull request #72 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Insert Prepared Queries
  • Loading branch information
usernane authored Jul 26, 2023
2 parents b062d56 + f202387 commit 127d46a
Show file tree
Hide file tree
Showing 23 changed files with 921 additions and 548 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ tests/webfiori/database/tests/common/UserClass.php
composer.lock
php-cs-fixer-v2.phar
.idea/*
tests/webfiori/database/tests/mysql/UserEntity.php
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="tests/boot.php" stderr="true">
<phpunit colors="true" bootstrap="tests/boot.php">
<php>
</php>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./webfiori/database/mysql</directory>
<directory suffix=".php">./webfiori/database/mssql</directory>
<directory suffix=".php">./webfiori/database/AbstractQuery.php</directory>
<directory suffix=".php">./webfiori/database/Column.php</directory>
<directory suffix=".php">./webfiori/database/Condition.php</directory>
Expand All @@ -19,6 +20,7 @@
<directory suffix=".php">./webfiori/database/Table.php</directory>
<directory suffix=".php">./webfiori/database/WhereExpression.php</directory>
<directory suffix=".php">./webfiori/database/RecordMapper.php</directory>
<directory suffix=".php">./webfiori/database/InsertBuilder.php</directory>
</whitelist>
</filter>
<logging>
Expand All @@ -33,8 +35,6 @@
</testsuite>
<testsuite name="MSSQL Tests">
<directory>./tests/webfiori/database/tests/mssql</directory>
<exclude>./tests/webfiori/database/tests/mssql/MSSQLQueryBuilderTest.php</exclude>
<exclude>./tests/webfiori/database/tests/mssql/MSSQLTestSchema.php</exclude>
</testsuite>
</testsuites>
</phpunit>
2 changes: 0 additions & 2 deletions samples/connect-to-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

use webfiori\database\ConnectionInfo;
use webfiori\database\Database;
use webfiori\database\DatabaseException;

//This assumes that MySQL is installed on locahost
//and root password is set to '123456'
//and there is a schema with name 'testing_db'
$connection = new ConnectionInfo('mysql', 'root', '123456', 'testing_db');
$database = new Database($connection);

3 changes: 2 additions & 1 deletion samples/createEntity/using-entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
->select()
->execute();

$mappedSet = $resultSet->map(function (array $record) {
$mappedSet = $resultSet->map(function (array $record)
{
return UserInformation::map($record);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
} else {
echo "Dropping test tables from MSSQL Server...\n";
try{
$mssqlConnInfo = new ConnectionInfo('mssql','sa', '1234567890', 'testing_db', 'localhost');
$mssqlConnInfo = new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', 'localhost');
$mssqlConn = new MSSQLConnection($mssqlConnInfo);
$mssqlSchema = new MSSQLTestSchema();
$mssqlSchema->setConnection($mssqlConn);
Expand Down
222 changes: 222 additions & 0 deletions tests/webfiori/database/tests/common/InsertBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

namespace webfiori\database\tests\common;

use PHPUnit\Framework\TestCase;
use webfiori\database\mssql\MSSQLInsertBuilder;
use webfiori\database\mssql\MSSQLTable;
use webfiori\database\mysql\MySQLInsertBuilder;
use webfiori\database\mysql\MySQLTable;

/**
* Description of InsertBuilderTest
*
*/
class InsertBuilderTest extends TestCase {
/**
* @test
*/
public function test00() {
$table = new MySQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'auto-inc' => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
'is-unique' => true
],
'username' => [
'type' => 'varchar',
'size' => 20,
'is-unique' => true
],
'password' => [
'type' => 'varchar',
'size' => 256
],
'age' => [
'type' => 'decimal'
],
'created-on' => [
'type' => 'timestamp',
'default' => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
]
]);

$helper = new MySQLInsertBuilder($table, [
'user-id' => 1
]);

$this->assertEquals('insert into `users` (`user_id`, `created_on`, `is_active`) values (?, ?, ?);', $helper->getQuery());
$this->assertEquals([
'bind' => 'isi',
'values' => [
[1,
date('Y-m-d H:i:s'),
1]
]
], $helper->getQueryParams());
$helper->insert([
'user-id' => 1,
'email' => '[email protected]',
'username' => 'warrior',
'password' => 'abcd',
'age' => 33.3,
]);

$this->assertEquals('insert into `users` (`user_id`, `email`, `username`, `password`, `age`, `created_on`, `is_active`) values (?, ?, ?, ?, ?, ?, ?);', $helper->getQuery());
$this->assertEquals([
'bind' => 'isssdsi',
'values' => [
[1,
'[email protected]',
'warrior',
'abcd',
33.3,
date('Y-m-d H:i:s'),
1]
]
], $helper->getQueryParams());
}
/**
* @test
*/
public function test01() {
$table = new MySQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'auto-inc' => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
'is-unique' => true
],
'username' => [
'type' => 'varchar',
'size' => 20,
'is-unique' => true
],
'password' => [
'type' => 'varchar',
'size' => 256
],
'age' => [
'type' => 'decimal'
],
'created-on' => [
'type' => 'timestamp',
'default' => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
]
]);

$helper = new MySQLInsertBuilder($table, [
'cols' => [
'user-id'
],
'values' => [
[1],[3],[4]
]
]);

$this->assertEquals("insert into `users` (`user_id`, `created_on`, `is_active`)\nvalues\n(?),\n(?),\n(?);", $helper->getQuery());
$this->assertEquals([
'bind' => 'isiisiisi',
'values' => [
[1,
date('Y-m-d H:i:s'),
1],
[3,
date('Y-m-d H:i:s'),
1],
[4,
date('Y-m-d H:i:s'),
1]
]
], $helper->getQueryParams());

}
/**
* @test
*/
public function test03() {
$table = new MSSQLTable('users');
$table->addColumns([
'user-id' => [
'type' => 'int',
'size' => 11,
'primary' => true,
'identity' => true
],
'email' => [
'type' => 'varchar',
'size' => 256,
],
'username' => [
'type' => 'varchar',
'size' => 20,
],
'password' => [
'type' => 'varchar',
'size' => 256
],
'age' => [
'type' => 'decimal'
],
'created-on' => [
'type' => 'datetime2',
'default' => 'now()',
],
'is-active' => [
'type' => 'bool',
'default' => true
]
]);

$helper = new MSSQLInsertBuilder($table, [
'user-id' => 1
]);

$this->assertEquals('insert into [users] ([user_id], [created_on], [is_active]) values (?, ?, ?);', $helper->getQuery());
$this->assertEquals([
[1, 1, 2, 4],
[date('Y-m-d H:i:s'), 1, 5, 58734173],
[1, 1, 2, -7]
], $helper->getQueryParams());
$helper->insert([
'user-id' => 1,
'email' => '[email protected]',
'username' => 'warrior',
'password' => 'abcd',
'age' => 33.3,
]);

$this->assertEquals('insert into [users] ([user_id], [email], [username], [password], [age], [created_on], [is_active]) values (?, ?, ?, ?, ?, ?, ?);', $helper->getQuery());
$encoded = $helper->getQueryParams()[1][2];
$this->assertEquals([
[1, 1, 2, 4],
['[email protected]', 1, $encoded, 12 ],
['warrior', 1, $encoded, 12],
['abcd', 1, $encoded, 12],
[33.3, 1, 3, 3],
[date('Y-m-d H:i:s'), 1, 5, 58734173],
[1, 1, 2, -7],
], $helper->getQueryParams());
}
}
6 changes: 3 additions & 3 deletions tests/webfiori/database/tests/common/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test00() {
'pass' => 'rand_pass'
]);
$this->assertEquals('insert into '.$table->getName().' (`user_id`, `username`, `password`) '
. "values (33, 'Ibrahim', 'rand_pass');", $s->getLastQuery());
. "values (?, ?, ?);", $s->getLastQuery());
$s->table('hello')->select();
$this->assertEquals('select * from `hello`',$s->getLastQuery());
$s->where('user-id', 66);
Expand Down Expand Up @@ -91,7 +91,7 @@ public function test01() {
'pass' => 'rand_pass'
]);
$this->assertEquals('insert into `hello` (`user_id`, `username`, `password`) '
. "values (33, 'Ibrahim', 'rand_pass');", $s->getLastQuery());
. "values (?, ?, ?);", $s->getLastQuery());
$s->table('hello')->select();
$this->assertEquals('select * from `hello`',$s->getLastQuery());
$s->where('user-id', 66);
Expand Down Expand Up @@ -153,7 +153,7 @@ public function test04() {
'pass' => 'rand_pass'
]);
$this->assertEquals('insert into `hello` (`user_id`, `username`, `password`) '
. "values (33, 'Ibrahim', 'rand_pass');", $s->getLastQuery());
. "values (?, ?, ?);", $s->getLastQuery());
}
/**
* @test
Expand Down
14 changes: 7 additions & 7 deletions tests/webfiori/database/tests/mssql/MSSQLColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ public function testGetPHPType06() {
public function testSetDefault00() {
$col = new MSSQLColumn('date', 'datetime2');
$col->setDefault('2019-11-09');
$this->assertEquals('2019-11-09 00:00:00',$col->getDefault());
$this->assertEquals('[date] [datetime2] not null default \'2019-11-09 00:00:00\'',$col.'');
$this->assertEquals('2019-11-09',$col->getDefault());
$this->assertEquals('[date] [datetime2] not null default \'2019-11-09\'',$col.'');
}
/**
* @test
Expand Down Expand Up @@ -400,7 +400,7 @@ public function testSetDefault08() {
public function testSetDefault09() {
$col = new MSSQLColumn('mix', 'mixed');
$col->setDefault('2019-11-09');
$this->assertEquals("N'2019-11-09'",$col->getDefault());
$this->assertEquals("2019-11-09",$col->getDefault());
$this->assertEquals("[mix] [nvarchar](256) not null default N'2019-11-09'",$col.'');
}
/**
Expand All @@ -409,7 +409,7 @@ public function testSetDefault09() {
public function testSetDefault10() {
$col = new MSSQLColumn('mix', 'mixed');
$col->setDefault(1);
$this->assertEquals("N'1'",$col->getDefault());
$this->assertEquals(1,$col->getDefault());
$this->assertEquals("[mix] [nvarchar](256) not null default N'1'",$col.'');
}
/**
Expand Down Expand Up @@ -476,7 +476,7 @@ public function testSetType03() {
$this->assertEquals('datetime2',$col->getDatatype());
$this->assertEquals(1,$col->getSize());
$col->setDefault('2019-01-11');
$this->assertSame('2019-01-11 00:00:00',$col->getDefault());
$this->assertSame('2019-01-11',$col->getDefault());
}
/**
* @test
Expand All @@ -488,9 +488,9 @@ public function testSetType04() {
$this->assertEquals('datetime2',$col->getDatatype());
$this->assertEquals(1,$col->getSize());
$this->assertNull($col->getDefault());
$col->setDefault('2019-13-11 00:00:00');
$col->setDefault('2019-13-11');
$this->assertNull($col->getDefault());
$col->setDefault('2019-04-44 00:00:00');
$col->setDefault('2019-04-44');
$this->assertNull($col->getDefault());

}
Expand Down
Loading

0 comments on commit 127d46a

Please sign in to comment.