jeremyevans / sequel

Sequel: The Database Toolkit for Ruby
http://sequel.jeremyevans.net
Other
5k stars 1.07k forks source link

DB.literal :symbol should not quote "symbol" #2119

Closed felipeAH closed 10 months ago

felipeAH commented 10 months ago

Complete Description of Issue

Hi, trying to create a table with this code into postgres database

DB.create_table! :test do
  DateTime :date, default: :CURRENT_DATETIME
end

results in an error because CURRENT_DATETIME gets quoted, so digging into the source code I come to

Proxy the literal call to the dataset.

#
#   DB.literal(1)   # 1
#   DB.literal(:a)  # a
#   DB.literal('a') # 'a'
def literal(v)
  schema_utility_dataset.literal(v)
end

and with my poor ruby skills is the farthest I could get. Could't find where #literal gets implemented. ( can you please enlighten me? ) Sorry for my bad english.

Simplest Possible Self-Contained Example Showing the Bug

DB.literal :symbol => "\"symbol\"" (should be "symbol")

Full Backtrace of Exception (if any)

No response

SQL Log (if any)

No response

Ruby Version

ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu]

Sequel Version

Tested with 5.63.0 and 5.76.0

jeremyevans commented 10 months ago

This is expected behavior, as Sequel quotes identifiers by default (to handle symbols that are also SQL keywords, among other reasons). Use Sequel.lit('CURRENT_DATETIME'). If you don't want identifiers quoted by default, you can use the identifier_mangling extension and set DB.quote_identifiers = false.

felipeAH commented 10 months ago

Thank you.

Anyway the comments in

#
#   DB.literal(1)   # 1
#   DB.literal(:a)  # a
#   DB.literal('a') # 'a'
def literal(v)
  schema_utility_dataset.literal(v)
end

are missleading ... wrong IMHO.

Regards.

jeremyevans commented 10 months ago

Thank you.

Anyway the comments in

#
#   DB.literal(1)   # 1
#   DB.literal(:a)  # a
#   DB.literal('a') # 'a'
def literal(v)
  schema_utility_dataset.literal(v)
end

are missleading ... wrong IMHO.

Well, they aren't necessarily wrong. Some adapters disable identifier quoting, and the actual literal value depends on the adapter. In most example SQL examples in the Sequel documentation, identifiers are shown unquoted. However, I don't have an issue expanding this example.

jeremyevans commented 10 months ago

Updated the documentation in 150e7547e9fc28909dbf771d544bbb446ac974b3