Class: ArelExtensions::CommonSqlFunctions

Inherits:
Object
  • Object
show all
Defined in:
lib/arel_extensions/common_sql_functions.rb

Instance Method Summary collapse

Constructor Details

#initialize(cnx) ⇒ CommonSqlFunctions

Returns a new instance of CommonSqlFunctions.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/arel_extensions/common_sql_functions.rb', line 3

def initialize(cnx)
  @cnx = cnx
  if cnx && cnx.adapter_name =~ /sqlite/i && !$load_extension_disabled
    begin
      db = cnx.raw_connection
      db.enable_load_extension(1)
      db.load_extension("/usr/lib/sqlite3/pcre.so")
      db.load_extension("/usr/lib/sqlite3/extension-functions.so")
      db.enable_load_extension(0)
    rescue => e
      $load_extension_disabled = true
      puts "cannot load extensions #{e.inspect}"
    end
  end
end

Instance Method Details

#add_sql_functions(env_db = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/arel_extensions/common_sql_functions.rb', line 39

def add_sql_functions(env_db = nil)
  env_db ||= @cnx.adapter_name
  env_db = 'mysql' if env_db =~ /mysql/i
  if env_db =~ /sqlite/i
    begin
      add_sqlite_functions
    rescue => e
      puts "cannot add sqlite functions #{e.inspect}"
    end
  end
  if File.exist?("init/#{env_db}.sql")
    sql = File.read("init/#{env_db}.sql")
    if env_db == 'mssql'
      sql.split(/^GO\s*$/).each {|str|
        @cnx.execute(str.strip) unless str.blank?
      }
    elsif env_db =='mysql'
      sql.split("$$")[1..-2].each { |str|
        @cnx.execute(str.strip) unless str.strip.blank?
      }
    else
      @cnx.execute(sql) unless sql.blank?
    end
  end
end

#add_sqlite_functionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/arel_extensions/common_sql_functions.rb', line 19

def add_sqlite_functions
  db = @cnx.raw_connection
  db.create_function("find_in_set", 1) do |func, val, list|
    case list
    when String
      i = list.split(',').index(val.to_s)
      func.result = i ? (i+1) : 0
    when NilClass
      func.result = nil
    else
      i = list.to_s.split(',').index(val.to_s)
      func.result = i ? (i+1) : 0
    end
  end
  db.create_function("instr", 1) do |func, value1, value2|
    i = value1.to_s.index(value2.to_s)
    func.result = i ? (i+1) : 0
  end rescue "function instr already here (>= 3.8.5)"
end