Skip to content

Fix: escape MySQL identifiers using reserved words #42

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

helix
Copy link

@helix helix commented Apr 13, 2017

@@ -145,7 +145,7 @@ def write(chunk)
data = format_proc.call(tag, time, data, max_lengths)
values << Mysql2::Client.pseudo_bind(values_template, data)
end
sql = "INSERT INTO #{table} (#{@column_names.join(',')}) VALUES #{values.join(',')}"
sql = "INSERT INTO `#{table}` (#{@column_names.map { |colname| "`#{colname}`" }.join(',')}) VALUES #{values.join(',')}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Column names may legally include a backtick character themselves.

Identifier quote characters can be included within an identifier if you quote the identifier. If the character to be included within the identifier is the same as that used to quote the identifier itself, then you need to double the character. The following statement creates a table named a`b that contains a column named c"d:

CREATE TABLE `a``b` (`c"d` INT);

So in addition to wrapping names in backticks, you will also have to replace backticks inside the names with double backticks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants