Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tableExists() cannot handle Database Name with dot #8647

Closed
mbnl opened this issue Mar 21, 2024 · 10 comments
Closed

tableExists() cannot handle Database Name with dot #8647

mbnl opened this issue Mar 21, 2024 · 10 comments
Labels
database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities

Comments

@mbnl
Copy link

mbnl commented Mar 21, 2024

PHP Version

8.1

CodeIgniter4 Version

4.4.5

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MySQL 8.0

What happened?

Hello, I'm having a problem

My database name;

com.sitedb.web

The operation I intend to perform is tableExists('tableName').

When I run this code, I receive a Database Exception error. Upon detailed examination of the error, I found that CodeIgniter 4 (CI4) was sending the following MySQL command:

SHOW TABLES FROM `com`.`sitedb`.`web`

Steps to Reproduce

That should have been the right command;

SHOW TABLES FROM `com.sitedb.web`

Due to the dots . in my database name, CI4 was treating it as `.`, thus causing the issue. However, since I'm using a hosting service, I cannot change the database name. Upon inspecting the source code, I found that in system/Database/MySQLi/Connection.php, line 391, the code was modified as follows:

$sql = 'SHOW TABLES FROM ' . $this->escapeIdentifiers($this->database);

I identified that the problem stems from the escapeIdentifiers method. Instead of modifying this method, I updated the line as follows:

"$sql = 'SHOW TABLES FROM `' . $this->escapeLikeStringDirect($this->database).'`';"

so that the mysql command was correctly as follows

SHOW TABLES FROM `com.sitedb.web`

This solution temporarily resolved my issue. However, when I update to a new version of CI4, the old code will return, potentially causing the same error in the future. I would appreciate it if you could address this issue and provide a permanent solution.
Thank you.

Expected Output

SHOW TABLES FROM `com.sitedb.web`

Anything else?

No response

@mbnl mbnl added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 21, 2024
@datamweb
Copy link
Contributor

CodeIgniter doesn’t support dots (.) in the database, table, and column names

@mbnl
Copy link
Author

mbnl commented Mar 21, 2024

CodeIgniter doesn’t support dots (.) in the database, table, and column names

Yes, I have already noticed this. Since it is a hosting provider, I cannot provide a change as a database, so if a permanent solution is possible, not only me but also the errors that may occur in the future will be eliminated. I have already produced my own solution, I just wanted to report it.

@ddevsr
Copy link
Collaborator

ddevsr commented Mar 21, 2024

@mbnl Whats a solution you have? Feel free to send PR

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 21, 2024
@kenjis
Copy link
Member

kenjis commented Mar 21, 2024

This is clearly documented. So this is not a bug.

Note
CodeIgniter doesn’t support dots (.) in the database, table, and column names.
https://codeigniter4.github.io/CodeIgniter4/database/examples.html

Probably we cannot fix this.
This is because it is impossible to determine whether a dot is a part of a database name or an identifier delimiter.

@mbnl mbnl closed this as completed Mar 21, 2024
@kenjis
Copy link
Member

kenjis commented Mar 21, 2024

@mbnl If you have a good idea, feel free to send PRs.

@kenjis kenjis changed the title Bug: Database Name Problem [MySQL] tableExists() cannot handle Database Name with dot Mar 21, 2024
@kenjis kenjis added the database Issues or pull requests that affect the database layer label Mar 21, 2024
@kenjis
Copy link
Member

kenjis commented Mar 21, 2024

This is not a bug, but I am reopening it for reconsideration.

@kenjis kenjis reopened this Mar 21, 2024
@kenjis kenjis added the enhancement PRs that improve existing functionalities label Mar 21, 2024
@kenjis
Copy link
Member

kenjis commented Mar 21, 2024

@codeigniter4/database-team @codeigniter4/core-team
This issue is because of $this->escapeIdentifiers($this->database).

$sql = 'SHOW TABLES FROM ' . $this->escapeIdentifiers($this->database);
if ($tableName !== null) {
return $sql . ' LIKE ' . $this->escape($tableName);
}
if ($prefixLimit !== false && $this->DBPrefix !== '') {
return $sql . " LIKE '" . $this->escapeLikeStringDirect($this->DBPrefix) . "%'";
}
return $sql;

Is there possibility that $this->database is not only a database name?
That is, may it contain a hostname or schema name?

If $this->database is just a database name only, we can add a new escape method for database names, and can handle a database name with dots.

@mbnl
Copy link
Author

mbnl commented Mar 22, 2024

@codeigniter4/database-team @codeigniter4/core-team This issue is because of $this->escapeIdentifiers($this->database).

$sql = 'SHOW TABLES FROM ' . $this->escapeIdentifiers($this->database);
if ($tableName !== null) {
return $sql . ' LIKE ' . $this->escape($tableName);
}
if ($prefixLimit !== false && $this->DBPrefix !== '') {
return $sql . " LIKE '" . $this->escapeLikeStringDirect($this->DBPrefix) . "%'";
}
return $sql;

Is there possibility that $this->database is not only a database name? That is, may it contain a hostname or schema name?

If $this->database is just a database name only, we can add a new escape method for database names, and can handle a database name with dots.

Yes, only the database name. There is no prefix or hostname. It is a mysql 8.0 database created through the hosting panel.

@kenjis
Copy link
Member

kenjis commented Mar 27, 2024

The $this->database comes from Config\Database:

'database' => '',

and it is just a database name only.
We also have hostname and schema in the config array.

So we can support database name with dots.

@mbnl I sent a PR #8664
Check it if you can.

@kenjis
Copy link
Member

kenjis commented Mar 28, 2024

Closed by #8664

@kenjis kenjis closed this as completed Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities
Projects
None yet
Development

No branches or pull requests

4 participants