Class: OpalORM::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/opal_orm/query_builder.rb

Constant Summary collapse

DATATYPES =
{
string: "VARCHAR(255)",
integer: "INTEGER",
float: "REAL",
text: "TEXT"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



21
22
23
24
25
# File 'lib/opal_orm/query_builder.rb', line 21

def initialize(name)
  @table_name = name
  @columns = []
  @foreign_keys = []
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



19
20
21
# File 'lib/opal_orm/query_builder.rb', line 19

def query
  @query
end

Class Method Details

.create_table_query(name, &prc) ⇒ Object



12
13
14
15
16
17
# File 'lib/opal_orm/query_builder.rb', line 12

def self.create_table_query(name, &prc)
  manager = new(name)
  prc.call(manager)

  manager.build!
end

Instance Method Details

#build!Object

def foreign_key(ref_name)

@foreign_keys << ref_name

end



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/opal_orm/query_builder.rb', line 47

def build!
  query_start = "CREATE TABLE #{@table_name} ("
  
  column_queries = @columns.map do |col_info|
    result = ["#{col_info[:name]}"]
    result << get_datatype_string(col_info[:type])

    unless col_info[:options].nil?
      col_info[:options].each do |key|
        case col_info[:options]
        when :null
          result << "NOT NULL" unless col_info[:options][:null]
        end
      end
    end

    result.join(" ")
  end
  column_queries.unshift("id INTEGER PRIMARY KEY")

  foreign_keys = @foreign_keys.map do |ref_name|
    if foreign_key_valid?(ref_name)
      "FOREIGN KEY(#{ref_name}) REFERENCES #{@table_name.singularize}(id)"
    else
      raise ForeignKeyMissingError, "Error adding foreign key constraint for #{ref_name}: couldn't find column named #{ref_name}."
    end
  end

  query_end = ");"

  @query = query_start + column_queries.concat(foreign_keys).join(",\n") + query_end
  @query
end

#float(name, *options) ⇒ Object



35
36
37
# File 'lib/opal_orm/query_builder.rb', line 35

def float(name, *options)
  @columns << {type: :float, name: name, options: options}
end

#foreign_key_valid?(key) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/opal_orm/query_builder.rb', line 85

def foreign_key_valid?(key)
  @columns.any? {|col| key == col[:name]}
end

#get_datatype_string(type) ⇒ Object



81
82
83
# File 'lib/opal_orm/query_builder.rb', line 81

def get_datatype_string(type)
  DATATYPES[type]
end

#integer(name, *options) ⇒ Object



31
32
33
# File 'lib/opal_orm/query_builder.rb', line 31

def integer(name, *options)
  @columns << {type: :integer, name: name, options: options}
end

#string(name, *options) ⇒ Object



27
28
29
# File 'lib/opal_orm/query_builder.rb', line 27

def string(name, *options)
  @columns << {type: :string, name: name, options: options}
end

#text(name, *options) ⇒ Object



39
40
41
# File 'lib/opal_orm/query_builder.rb', line 39

def text(name, *options)
  @columns << {type: :text, name: name, options: options}
end