# = Database Utilities # # A collection of database utilities. # # code: # George Moschovitis # # (c) 2004 Navel, all rights reserved. # $Id$ module N; require "n/properties" require "n/utils/array" require "n/utils/time" # = DbUtils # # A collection of Database utilities. # Additional backend specific utilities are defined in # the backend implementations. # module DbUtils # Escape an sql string # def self.escape(str) # defined in the backend end # Escape bytes # def self.escape_bytes(bytes) # defined in the backend end # The name of the SQL table where entities of this class are stored. # The Module separators are replaced with _ and NOT stripped out so # that we can convert back to the original notation if needed. # def self.sql_table(klass) return "#{klass}".gsub(/::/, "_").downcase end # Convert a ruby time to an sql timestamp. # def self.sql_timestamp(time = Time.now) # defined in the backend end # Output YYY-mm-dd # def self.sql_date(date) # defined in the backend end # Parse sql datetime # def self.parse_sql_timestamp(str) # defined in the backend end # Input YYYY-mm-dd # def self.parse_sql_date(str) # defined in the backend end # Return an sql string evaluator for the property. # No need to optimize this, used only to precalculate code. # # FIXME: add extra handling for float. # def self.write_prop(p) if String == p.klass return "'#\{DbUtils.escape(@#{p.symbol})\}'" elsif Time == p.klass return %|#\{@#{p.symbol} ? "'#\{DbUtils.sql_timestamp(@#{p.symbol})\}'" : 'NULL'\}| elsif Date == p.klass return %|#\{@#{p.symbol} ? "'#\{DbUtils.sql_date(@#{p.symbol})\}'" : 'NULL'\}| elsif Object == p.klass or Array == p.klass or Hash == p.klass #return "'#\{DbUtils.escape_bytes(Marshal.dump(@#{p.symbol}))\}'" return "'#\{DbUtils.escape_bytes(Marshal.dump(@#{p.symbol}))\}'" else # Fixnum, TrueClass return "#\{@#{p.symbol} || 'NULL'\}" end end # Return an evaluator for reading the property # No need to optimize this, used only to precalculate code. # def self.read_prop(p) # defined in the backend end end end # namespace