Module: FieldNode

Defined in:
lib/sql_munger/nodes.rb

Instance Method Summary collapse

Instance Method Details

#clausesObject

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

Returns:

  • (Boolean)


78
# File 'lib/sql_munger/nodes.rb', line 78

def constraint?; false; end

#defaultObject



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

Returns:

  • (Boolean)


100
101
102
# File 'lib/sql_munger/nodes.rb', line 100

def default?
  !default_clause.nil?
end

#default_clauseObject



104
105
106
# File 'lib/sql_munger/nodes.rb', line 104

def default_clause
  clauses.find {|x| x.clause_type == :default }
end

#field?Boolean

Returns:

  • (Boolean)


77
# File 'lib/sql_munger/nodes.rb', line 77

def field?; true; end

#formatted_rails_optsObject



140
141
142
143
144
145
146
# File 'lib/sql_munger/nodes.rb', line 140

def formatted_rails_opts
  if rails_options.empty?
    ''
  else
    ", " + rails_options.inspect[1..-2].gsub( '=>', ' => ' )
  end
end

#lengthObject



148
149
150
# File 'lib/sql_munger/nodes.rb', line 148

def length
  type_specifier.length.text_value rescue nil
end

#nameObject



80
81
82
# File 'lib/sql_munger/nodes.rb', line 80

def name
  qualified_identifier.parts.last
end

#nullObject



123
124
125
# File 'lib/sql_munger/nodes.rb', line 123

def null
  null_clause.nil?
end

#null_clauseObject



119
120
121
# File 'lib/sql_munger/nodes.rb', line 119

def null_clause
  clauses.find {|x| x.clause_type == :not_null }
end

#precisionObject



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

Returns:

  • (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_optionsObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sql_munger/nodes.rb', line 127

def rails_options
  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_typeObject



96
97
98
# File 'lib/sql_munger/nodes.rb', line 96

def rails_type
  type_specifier.rails_type rescue sql_type
end

#scaleObject



156
157
158
# File 'lib/sql_munger/nodes.rb', line 156

def scale
  type_specifier.args.scale.text_value rescue nil
end

#sql_typeObject



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