?
avatar dawe78
dawe78
8 Jan 2020

Steps to reproduce the issue

I was trying to connnect to a MSSQL database using the JDatabase-Class but got the "Could not connect to SQL Server database." error. After lots of hours checking SQL Server, Firewall, PHP... I checked the Joomla library and found the issue. The database name contains dots - and renaming database is not possible.

Expected result

Connect to database and return connection id.

Actual result

Error message "Could not connect to SQL Server database."

System information (as much as possible)

Joomla 3.9.14 on IIS 8.5, PHP 7.2

Additional comments

I found the solution in libraries/joomla/database/sqlsrv.php, line 775. In the select-method, which auto-selects the choosen database, the database name is not quoted, so if there is a dot (or maybe other characters) inside the database name, the USE-Statement fails and throws this error.

Original code:
if (!sqlsrv_query($this->connection, 'USE ' . $database, null, array('scrollable' => SQLSRV_CURSOR_STATIC)))

In my case, I just put the database name inside brackets and all works fine:
if (!sqlsrv_query($this->connection, 'USE [' . $database .'], null, array('scrollable' => SQLSRV_CURSOR_STATIC)))

Mabe the $this->quoteName()-method can be used for adding brackets.

avatar dawe78 dawe78 - open - 8 Jan 2020
avatar joomla-cms-bot joomla-cms-bot - change - 8 Jan 2020
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 8 Jan 2020
avatar alikon
alikon - comment - 8 Jan 2020

i didn't have a sqlsrv instance for test right now ....
but if you can test...

if (!sqlsrv_query($this->connection, 'USE ' . $this->quoteName($database), null, array('scrollable' => SQLSRV_CURSOR_STATIC)))

probably brackets are uneeded.....iirc
please report back if you have the time to test...

avatar dawe78
dawe78 - comment - 9 Jan 2020

I tried to add square brackets using $this->quoteName() method now. The result is not as I expected; example: My database name is database2.0.3. Result should be [database2.0.3]; but I got [database2].[0].[3] and the connection fails.

avatar alikon
alikon - comment - 9 Jan 2020

an interesting reading on the subject https://dba.stackexchange.com/questions/1166/is-it-okay-to-put-dots-in-sql-server-database-names

Do not use period in database names, publication names, user names. And I strongly recommend never to use a period in name (column, table, view, database, etc).

avatar dawe78
dawe78 - comment - 9 Jan 2020

Yes, I agree, but it wasn't my decision. This database is a external database of a key management system which I need to manage via joomla. The database still exists and I'm not able to change the database name. So, unfortunally, I do have to use it as it is... for my own databases I would't use dots or characters like this...

avatar richard67
richard67 - comment - 9 Jan 2020

Result should be [database2.0.3]; but I got [database2].[0].[3] and the connection fails.

That's by design: The quoteName has a special treatment of dots. It assumes that they are separator between a table alias and a column name, e.g. a.id with abeing the table alias, which has to be quoted with squared brackets as [a].[id] in MS-SQL.

avatar HLeithner HLeithner - change - 9 Jan 2020
Status New Closed
Closed_Date 0000-00-00 00:00:00 2020-01-09 11:32:49
Closed_By HLeithner
avatar HLeithner
HLeithner - comment - 9 Jan 2020

looking at the Stack Exchange example [MAIN-SQL\EXPRESS].[MyApp.Sales].sch_HR.[Products From.Our-Competitors]seams to be unfixable for quoteName.

I would suggest to not include the database name in the query if possible or to quote it by hand in your database queries.

I'm closing this. Feel free to reopen it.

avatar HLeithner HLeithner - close - 9 Jan 2020
avatar dawe78
dawe78 - comment - 10 Jan 2020

looking at the Stack Exchange example [MAIN-SQL\EXPRESS].[MyApp.Sales].sch_HR.[Products From.Our-Competitors]seams to be unfixable for quoteName.

Yes, that's right, but as I wrote in my first post, the only thing that needs to be fixed is the select-method in sqlsrv.php on line 775 by adding square brackets for the database name. Its not necessary to use the quoteName-method in this case. The select-method is defined in sqlsrv.php, so square brackets are always the right choice for quoting the database name inside this file.

Code: 'USE [' . $database .'], null, array('scrollable' => SQLSRV_CURSOR_STATIC)

Although dots shouldn't be used for database name, this simple solution would fix it...

If this fix will not be merged, I will have to change it by hand after each joomla update. So could you please check if this fix can be merged?

avatar HLeithner
HLeithner - comment - 10 Jan 2020

If someone write a PR for it would merge it, in J4 we don't support sqlsrv any longer but you can still use the joomla framework to access sqlsrv.

avatar dawe78
dawe78 - comment - 10 Jan 2020

I wrote a pull request #27462 but I think I did something wrong... ;-(

avatar HLeithner
HLeithner - comment - 10 Jan 2020

your pull request is based on 3.10-dev but have to be based on staging

avatar brianteeman
brianteeman - comment - 10 Jan 2020
avatar HLeithner
HLeithner - comment - 10 Jan 2020

The problem is not the target branch, he used 3.10-dev as source branch. That can't be changed.

avatar brianteeman
brianteeman - comment - 10 Jan 2020

Ah - oops

avatar richard67
richard67 - comment - 11 Jan 2020

@HLeithner Am confused now because to me it looks vice versa:

pr-27462

Into 3.10-dev from staging ... or am I getting something wrong?

avatar richard67
richard67 - comment - 11 Jan 2020

@HLeithner I just see it is even worse: The PR wants to merge the CMS' staging branch into the CMS' 3.10-dev branch!!!
pr-27462_2

pr-27462_3

avatar richard67
richard67 - comment - 11 Jan 2020

@HLeithner I've posted a link to instructions in the PR.

@dawe78 Please check #27462 (comment) and let us know if you need further advise.

avatar richard67
richard67 - comment - 11 Jan 2020

Reopened because PR #27462 was closed due to reasons stated there.

avatar richard67 richard67 - change - 11 Jan 2020
Status Closed New
Closed_Date 2020-01-09 11:32:49
Closed_By HLeithner
avatar richard67 richard67 - reopen - 11 Jan 2020
avatar richard67
richard67 - comment - 11 Jan 2020

@dawe78 If you want to try it again with making a Pull Request (PR): Feel welcome.

You can find instructions here: https://docs.joomla.org/Using_the_Github_UI_to_Make_Pull_Requests.

But if you don't want let me know (i.e. post back here) then I can make one.

avatar alikon
alikon - comment - 11 Jan 2020

a more quick way ...
please test #27484

avatar alikon alikon - change - 11 Jan 2020
Status New Closed
Closed_Date 0000-00-00 00:00:00 2020-01-11 18:20:15
Closed_By alikon
avatar alikon alikon - close - 11 Jan 2020

Add a Comment

Login with GitHub to post a comment