Skip to content

Commit 86ebfbf

Browse files
authored
Merge pull request #758 from kbrock/adapter-module
Conditional SQL based upon connector (mysql/pg/sqlite)
2 parents 4e532d2 + d893f6a commit 86ebfbf

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

lib/ancestry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'ancestry/version'
4+
require_relative 'ancestry/adapter'
45
require_relative 'ancestry/class_methods'
56
require_relative 'ancestry/instance_methods'
67
require_relative 'ancestry/exceptions'

lib/ancestry/adapter.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module Ancestry
4+
# Adapter detection helpers — small wrappers around the connection's
5+
# adapter_name. Centralizes the magic strings so format modules and
6+
# other call sites stop duplicating the matcher arrays.
7+
module Adapter
8+
extend self
9+
10+
PG = %w(pg postgresql postgis).freeze
11+
MYSQL = %w(mysql mysql2 trilogy).freeze
12+
SQLITE = %w(sqlite sqlite3).freeze
13+
14+
def pg?(adapter = current)
15+
PG.include?(adapter.to_s.downcase)
16+
end
17+
18+
def mysql?(adapter = current)
19+
MYSQL.include?(adapter.to_s.downcase)
20+
end
21+
22+
def sqlite?(adapter = current)
23+
SQLITE.include?(adapter.to_s.downcase)
24+
end
25+
26+
def current
27+
ActiveRecord::Base.connection.adapter_name
28+
end
29+
30+
# Cross-DB string concatenation. SQLite uses ||; PG/MySQL use CONCAT().
31+
# Returns a SQL fragment as a String — not an Arel node.
32+
def concat(adapter, *args)
33+
sqlite?(adapter) ? args.join('||') : "CONCAT(#{args.join(', ')})"
34+
end
35+
end
36+
end

lib/ancestry/materialized_path.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Ancestry
55
# root a=nil,id=1 children=id,id/% == 1, 1/%
66
# 3: a=1/2,id=3 children=a/id,a/id/% == 1/2/3, 1/2/3/%
77
class MaterializedPath
8+
extend Ancestry::Adapter
9+
810
def self.root
911
nil
1012
end
@@ -71,14 +73,6 @@ def self.indirects_condition(attr, child_ancestry)
7173
attr.matches("#{child_ancestry}/%", nil, true)
7274
end
7375

74-
def self.concat(adapter, *args)
75-
if %w(sqlite sqlite3).include?(adapter)
76-
args.join('||')
77-
else
78-
%{CONCAT(#{args.join(', ')})}
79-
end
80-
end
81-
8276
# SQL to replace old ancestry prefix with new ancestry prefix in descendants
8377
def self.replace_ancestry_sql(column, old_ancestry, new_ancestry, klass)
8478
adapter = klass.connection.adapter_name.downcase

lib/ancestry/materialized_path_array.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Ancestry
55
# root: a=[],id=1 children=a||[id] == [1]
66
# 3: a=[1,2],id=3 children=a||[id] == [1,2,3]
77
class MaterializedPathArray
8+
extend Ancestry::Adapter
9+
810
def self.root
911
[]
1012
end

0 commit comments

Comments
 (0)