Class: Json2sql::WhereRelation
- Inherits:
-
Object
- Object
- Json2sql::WhereRelation
- Defined in:
- lib/json2sql/where_relation.rb
Constant Summary collapse
- NONE =
:none- CHILD =
:child- PARENT =
:parent
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
-
.child(table) ⇒ Object
Factory: foreign key is on the child table pointing to the parent.
-
.none(table) ⇒ Object
Factory: no relationship (top-level query).
-
.parent(table) ⇒ Object
Factory: foreign key is on the current/parent table pointing to the child.
Instance Method Summary collapse
-
#build_table_id(tbl) ⇒ Object
Converts a (possibly plural) table name to its foreign-key column name wrapped in backticks.
-
#build_table_relation(sql, current) ⇒ Object
Appends the JOIN condition for this relationship into sql.
-
#initialize(table, kind) ⇒ WhereRelation
constructor
A new instance of WhereRelation.
Constructor Details
#initialize(table, kind) ⇒ WhereRelation
Returns a new instance of WhereRelation.
11 12 13 14 15 16 |
# File 'lib/json2sql/where_relation.rb', line 11 def initialize(table, kind) @table = table.to_s @kind = kind end |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
9 10 11 |
# File 'lib/json2sql/where_relation.rb', line 9 def kind @kind end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'lib/json2sql/where_relation.rb', line 9 def table @table end |
Class Method Details
.child(table) ⇒ Object
Factory: foreign key is on the child table pointing to the parent. Produces: ‘parent`.`child_id` = `current`.`id`
28 29 30 31 |
# File 'lib/json2sql/where_relation.rb', line 28 def self.child(table) new(table, CHILD) end |
.none(table) ⇒ Object
Factory: no relationship (top-level query).
20 21 22 23 |
# File 'lib/json2sql/where_relation.rb', line 20 def self.none(table) new(table, NONE) end |
.parent(table) ⇒ Object
Factory: foreign key is on the current/parent table pointing to the child. Produces: ‘current`.`parent_id` = `parent`.`id`
36 37 38 39 |
# File 'lib/json2sql/where_relation.rb', line 36 def self.parent(table) new(table, PARENT) end |
Instance Method Details
#build_table_id(tbl) ⇒ Object
Converts a (possibly plural) table name to its foreign-key column name wrapped in backticks.
"users" → "`user_id`"
"categories" → "`category_id`"
"admins" → "`admin_id`"
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/json2sql/where_relation.rb', line 87 def build_table_id(tbl) base = Sanitizer.keyword(tbl.to_s) return "`#{base[0..-4]}y_id`" if base.end_with?("ies") return "`#{base[0..-2]}_id`" if base.end_with?("s") "`#{base}_id`" end |
#build_table_relation(sql, current) ⇒ Object
Appends the JOIN condition for this relationship into sql. current is the name of the table being queried.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/json2sql/where_relation.rb', line 44 def build_table_relation(sql, current) current = current.to_s if kind == CHILD sql << Sanitizer.keyword_wrap(table) sql << "." sql << build_table_id(current) sql << " = " sql << Sanitizer.keyword_wrap(current) sql << ".`id`" return end if kind == PARENT sql << Sanitizer.keyword_wrap(current) sql << "." sql << build_table_id(table) sql << " = " sql << Sanitizer.keyword_wrap(table) sql << ".`id`" end end |