Module: FieldNode
- Defined in:
- lib/sql_munger/nodes.rb
Instance Method Summary collapse
-
#clauses ⇒ Object
return text values of each clause.
- #constraint? ⇒ Boolean
- #default ⇒ Object
- #default? ⇒ Boolean
- #default_clause ⇒ Object
- #field? ⇒ Boolean
- #formatted_rails_opts ⇒ Object
- #length ⇒ Object
- #name ⇒ Object
- #null ⇒ Object
- #null_clause ⇒ Object
- #precision ⇒ Object
- #primary_key? ⇒ Boolean
- #rails_options ⇒ Object
- #rails_type ⇒ Object
- #scale ⇒ Object
- #sql_type ⇒ Object
Instance Method Details
#clauses ⇒ Object
return text values of each clause
161 162 163 164 165 166 167 |
# File 'lib/sql_munger/nodes.rb', line 161 def clauses if field_clauses.empty? [] else field_clauses.elements.map{|node| node.field_clause} end end |
#constraint? ⇒ Boolean
78 |
# File 'lib/sql_munger/nodes.rb', line 78 def constraint?; false; end |
#default ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sql_munger/nodes.rb', line 108 def default # do we have a default clause? if default? # fetch the default value retval = default_clause.parenthesised.value # don't return a default of "null" because it's redundant retval unless retval == "null" end end |
#default? ⇒ Boolean
100 101 102 |
# File 'lib/sql_munger/nodes.rb', line 100 def default? !default_clause.nil? end |
#default_clause ⇒ Object
104 105 106 |
# File 'lib/sql_munger/nodes.rb', line 104 def default_clause clauses.find {|x| x.clause_type == :default } end |
#field? ⇒ Boolean
77 |
# File 'lib/sql_munger/nodes.rb', line 77 def field?; true; end |
#formatted_rails_opts ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/sql_munger/nodes.rb', line 140 def formatted_rails_opts if .empty? '' else ", " + .inspect[1..-2].gsub( '=>', ' => ' ) end end |
#length ⇒ Object
148 149 150 |
# File 'lib/sql_munger/nodes.rb', line 148 def length type_specifier.length.text_value rescue nil end |
#name ⇒ Object
80 81 82 |
# File 'lib/sql_munger/nodes.rb', line 80 def name qualified_identifier.parts.last end |
#null ⇒ Object
123 124 125 |
# File 'lib/sql_munger/nodes.rb', line 123 def null null_clause.nil? end |
#null_clause ⇒ Object
119 120 121 |
# File 'lib/sql_munger/nodes.rb', line 119 def null_clause clauses.find {|x| x.clause_type == :not_null } end |
#precision ⇒ Object
152 153 154 |
# File 'lib/sql_munger/nodes.rb', line 152 def precision type_specifier.args.precision.text_value rescue nil end |
#primary_key? ⇒ Boolean
92 93 94 |
# File 'lib/sql_munger/nodes.rb', line 92 def primary_key? clauses.any? {|x| x.clause_type == :primary_key } end |
#rails_options ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sql_munger/nodes.rb', line 127 def if @rails_options.nil? @rails_options ||= {} @rails_options[:limit] = length.to_i unless length.nil? @rails_options[:precision] = precision.to_i unless precision.nil? @rails_options[:scale] = scale.to_i unless scale.nil? # only include a null specifier if it's false, ie we need a "not null" SQL clause @rails_options[:null] = null unless ( null.nil? || null ) @rails_options[:default] = default unless default.nil? end @rails_options end |
#rails_type ⇒ Object
96 97 98 |
# File 'lib/sql_munger/nodes.rb', line 96 def rails_type type_specifier.rails_type rescue sql_type end |
#scale ⇒ Object
156 157 158 |
# File 'lib/sql_munger/nodes.rb', line 156 def scale type_specifier.args.scale.text_value rescue nil end |
#sql_type ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/sql_munger/nodes.rb', line 84 def sql_type if type_specifier.terminal? type_specifier.text_value else type_specifier.sql_type.text_value end end |