Skip to content

Commit

Permalink
Merge pull request #92 from WebFiori/dev
Browse files Browse the repository at this point in the history
feat: Handel Query Preparation Error
  • Loading branch information
usernane authored Oct 28, 2024
2 parents ffa11a1 + 6924c94 commit fda0e18
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tests/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
define('SQL_SERVER_HOST', 'localhost');
$stderr = fopen('php://stderr', 'w');
$testsDirName = 'tests';
$rootDir = substr(__DIR__, 0, strlen(__DIR__) - strlen($testsDirName));
Expand Down Expand Up @@ -104,7 +105,7 @@
} else {
echo "Dropping test tables from MSSQL Server...\n";
try{
$mssqlConnInfo = new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', 'localhost');
$mssqlConnInfo = new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', SQL_SERVER_HOST);
$mssqlConn = new MSSQLConnection($mssqlConnInfo);
$mssqlSchema = new MSSQLTestSchema();
$mssqlSchema->setConnection($mssqlConn);
Expand Down
28 changes: 24 additions & 4 deletions tests/webfiori/database/tests/mssql/MSSQLQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function testTransaction00() {
'details' => 'This task is about testing if transactions work as intended.',
]
], $tasks);

return intval($userId);
}
/**
Expand Down Expand Up @@ -284,7 +285,7 @@ public function testTransaction02(int $userId) {
});
}, [$userId]);
} catch (DatabaseException $ex) {
$this->assertEquals("515 - [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column 'age', table 'testing_db.dbo.users'; column does not allow nulls. INSERT fails.", $ex->getMessage());
$this->assertEquals("515 - The statement has been terminated due to following: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column 'age', table 'testing_db.dbo.users'; column does not allow nulls. INSERT fails.", $ex->getMessage());
$this->assertEquals(515, $ex->getCode());
$user = $schema->table('users')->select()->where('id', $userId)->execute()->getRows()[0];
$this->assertEquals([
Expand Down Expand Up @@ -548,9 +549,10 @@ public function testDelete01() {
/**
* @test
* @param MSSQLTestSchema $schema
* @depends testCreateTable
*/
public function testInsert03(MSSQLTestSchema $schema) {
public function testInsert03() {
$schema = new MSSQLTestSchema();
$schema->createTables()->execute();
$schema->table('users')->insert([
'first-name' => 'Ibrahim',
'last-name' => 'BinAlshikh',
Expand Down Expand Up @@ -710,6 +712,24 @@ public function testInsert05() {
. "([user_id], [details], [created_on], [is_finished]) "
. "values (?, ?, ?, ?);", $schema->getLastQuery());
}
/**
* @test
*/
public function testInsert06() {
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage("207 - Statement(s) could not be prepared due to the following: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'not_exist'.");
$schema = new MSSQLTestSchema();
$q = $schema->table('users_tasks');
$q->insert([
'user-id' => null,
'details' => 'OK task',
'not-exist' => 7
]);
$this->assertEquals("insert into [users_tasks] "
. "([user_id], [details], [not_exist], [created_on], [is_finished]) "
. "values (?, ?, ?, ?, ?);", $schema->getLastQuery());
$q->execute();
}
/**
* @test
*/
Expand Down Expand Up @@ -1144,7 +1164,7 @@ public function testSetConnection00() {
if (PHP_MAJOR_VERSION == 5) {
$this->markTestSkipped('PHP 5 has no MSSQL driver in selected setup.');
} else {
$connInfo = new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', 'localhost');
$connInfo = new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', SQL_SERVER_HOST);
$conn = new MSSQLConnection($connInfo);
$schema = new MSSQLTestSchema();
$schema->setConnection($conn);
Expand Down
2 changes: 1 addition & 1 deletion tests/webfiori/database/tests/mssql/MSSQLTestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class MSSQLTestSchema extends Database {
public function __construct() {
parent::__construct(new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', 'localhost'));
parent::__construct(new ConnectionInfo('mssql','sa', '1234567890@Eu', 'testing_db', SQL_SERVER_HOST));

$table00 = new MSSQLTable('users');
$table00->setComment('This table is used to hold users info.');
Expand Down
12 changes: 9 additions & 3 deletions webfiori/database/mssql/MSSQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,19 @@ private function setSqlErr() {
$allErrs = sqlsrv_errors(SQLSRV_ERR_ERRORS);
$lastErr = $allErrs[count($allErrs) - 1];

if (strpos($lastErr['message'], 'The statement has been terminated') === false) {
if ($lastErr['SQLSTATE'] == '01000' && $lastErr['code'] == 3621) {
$lastErr = $allErrs[count($allErrs) - 2];
$this->sqlState = $lastErr['SQLSTATE'];
$this->setErrMessage($lastErr['message']);
$this->setErrMessage('The statement has been terminated due to following: '.$lastErr['message']);
$this->setErrCode($lastErr['code']);
} else {
} else if ($lastErr['SQLSTATE'] == '42000' && $lastErr['code'] == 8180) {
$lastErr = $allErrs[count($allErrs) - 2];
$this->sqlState = $lastErr['SQLSTATE'];
$this->setErrMessage('Statement(s) could not be prepared due to the following: '.$lastErr['message']);
$this->setErrCode($lastErr['code']);
} else {
$this->setErrCode($lastErr['code']);
$this->sqlState = $lastErr['SQLSTATE'];
$this->setErrMessage($lastErr['message']);
$this->setErrCode($lastErr['code']);
}
Expand Down

0 comments on commit fda0e18

Please sign in to comment.