codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.28k stars 1.88k forks source link

[SQLSRV] Session DatabaseHandler #5693

Open websignsistemas opened 2 years ago

websignsistemas commented 2 years ago

PHP Version

7.4

CodeIgniter4 Version

4.1.8

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cgi-fcgi

Database

SQLSRV 11.0.5058

What happened?

Exception 'now' is not a recognized built-in function name [SQLSRV].

I'm running Codeigniter in the configuration parameters mentioned above and when I set the application settings to store the sessions in the database (public $sessionDriver = 'CodeIgniter\Session\Handlers\DatabaseHandler';) and I created the table "ci_sessions" in the SQL Server database but when I refresh any page I come across with one exception: Informing that the now() function does not exist.

Steps to Reproduce

Just put it in the App.php settings to store the sessions in the SQL Server database, create the session storage table in the database and that's it.

Expected Output

Shouldn't give this exception, I believe this now() function should be replaced by getdate() if $this->platform is "sqlsrv"

Anything else?

Searching a little more for this problem in the file SYSTEMPATH/Session/Handlers/DatabaseHandler.php I verified that within the methods of the database queries there are database queries like this (line 177):

...
if (! $this->db->table($this->table)->set('timestamp', 'now()', false)->insert($insertData)) {
    return $this->fail();
}
...

inside the set() method, the now() function does not work in the case of SQL Server, when I changed this code snippet to:

...
if (! $this->db->table($this->table)->set('timestamp', 'getdate()', false)->insert($insertData)) {
    return $this->fail();
}
...

completely resolved the errors and the code works fine. I believe that to solve this bug, I can insert a code snippet where I can identify through the variable $this->platform which type of database is and separate which method is proper for each corresponding platform.

kenjis commented 2 years ago

Sorry, this is not a bug. We don't support SQLSRV as a session database now.

Only MySQL and PostgreSQL databases are officially supported, due to lack of advisory locking mechanisms on other platforms. https://codeigniter4.github.io/CodeIgniter4/libraries/sessions.html#databasehandler-driver

iRedds commented 2 years ago

Sorry, this is not a bug. We don't support SQLSRV as a session database now.

Maybe then throw an exception that the DBMS is not supported?

kenjis commented 2 years ago

It seems SQL Server has advisory locking mechanisms. https://dba.stackexchange.com/questions/176424/does-microsoft-sql-server-offer-an-advisory-locks-feature-like-postgres

If someone implements DatabaseHandler...