Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tim Morgan http://timmorgan.org

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Rails MySQL/SQLite convenience methods

Usage:

   1  
   2    Person.find :all, :conditions => ["#{sql_year 'birthday'} >= ?", year]


   1  
   2  SQLITE = true # or false
   3  
   4  def sql_concat(*args)
   5    SQLITE ? args.join(' || ') : "CONCAT(#{args.join(', ')})"
   6  end
   7  
   8  def sql_lcase(expr)
   9    SQLITE ? "LOWER(#{expr})" : "LCASE(#{expr})"
  10  end
  11  
  12  def sql_year(expr)
  13    SQLITE ? "CAST(STRFTIME('%y', #{expr}) as 'INTEGER')" : "YEAR(#{expr})"
  14  end
  15  
  16  def sql_month(expr)
  17    SQLITE ? "CAST(STRFTIME('%m', #{expr}) as 'INTEGER')" : "MONTH(#{expr})"
  18  end
  19  
  20  def sql_day(expr)
  21    SQLITE ? "CAST(STRFTIME('%d', #{expr}) as 'INTEGER')" : "DAY(#{expr})"
  22  end
  23  
  24  def sql_now
  25    SQLITE ? "CURRENT_TIMESTAMP" : "NOW()"
  26  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS