Skip to content

Instantly share code, notes, and snippets.

@maxlapshin
Created March 31, 2009 11:49
Show Gist options
  • Save maxlapshin/88159 to your computer and use it in GitHub Desktop.
Save maxlapshin/88159 to your computer and use it in GitHub Desktop.
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
def quote_table_name(name)
schema, name_part = extract_pg_identifier_from_name(name.to_s)
return quote_column_name(schema) unless name_part
table_name, name_part = extract_pg_identifier_from_name(name_part)
"#{quote_column_name(schema)}.#{quote_column_name(table_name)}"
end
private
def extract_pg_identifier_from_name(name)
if name[0,1] == '"'
match_data = name.match(/\"([^\"]+)\"/)
else
match_data = name.match(/([^\.]+)/)
end
if match_data
rest = name[match_data[0].length..-1]
rest = rest[1..-1] if rest[0,1] == "."
return match_data[1], (rest.length > 0 ? rest : nil)
end
return nil, nil
end
public
# Quotes column names for use in SQL queries.
def quote_column_name(name) #:nodoc:
PGconn.quote_ident(name.to_s)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment