Quote table names starting by number

pull/10510/head
Javier Torres 8 years ago
parent 1a2516f8da
commit 7a1efccb34

@ -19,10 +19,10 @@ module Carto
private
ALREADY_QUOTED = /\A".*"\Z/
NON_VALID_CHARACTERS = /[^[a-z][A-Z][0-9]_$]/
VALID_IDENTIFIER = /^[a-zA-Z_][a-zA-Z0-9_$]*$/
def dashes_quoting(name)
name && !name.match(ALREADY_QUOTED) && name =~ NON_VALID_CHARACTERS ? "\"#{name}\"" : name
name && !name.match(ALREADY_QUOTED) && !name.match(VALID_IDENTIFIER) ? "\"#{name}\"" : name
end
def can_be_quoted?(name)

@ -49,6 +49,11 @@ describe Carto::TableUtils do
table_utils.safe_schema_and_table_quoting('public', 'my""ta-ble').should eq 'public."my""ta-ble"'
end
it 'quotes table names starting with numbers' do
table_utils.safe_schema_and_table_quoting('public', '42').should eq 'public."42"'
table_utils.safe_schema_and_table_quoting('public', 'a42').should eq 'public.a42'
end
it 'does not quote already quoted strings' do
table_utils.safe_schema_and_table_quoting('public', '"my-table"').should eq 'public."my-table"'
table_utils.safe_schema_and_table_quoting('public', '"my""ta-ble"').should eq 'public."my""ta-ble"'

Loading…
Cancel
Save