Class: Microframe::ORM::Base
- Inherits:
-
Object
- Object
- Microframe::ORM::Base
- Extended by:
- Relationships
- Defined in:
- lib/microframe/orm/base.rb,
lib/microframe/orm/class_queries.rb
Constant Summary collapse
- @@create_table_query =
[]
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
- .create(options = {}) ⇒ Object
- .create_table ⇒ Object
- .define_attributes ⇒ Object
- .destroy(id) ⇒ Object
- .destroy_all(xtra = "; VACUUM") ⇒ Object
- .find(id) ⇒ Object
- .find_by(options) ⇒ Object
- .first ⇒ Object
- .get_create_table_query ⇒ Object
- .inherited(base) ⇒ Object
- .init_queryset(mtd, option) ⇒ Object
- .last ⇒ Object
- .limit(val) ⇒ Object
- .order(val) ⇒ Object
- .property(col_name, options = {}) ⇒ Object
- .select(val) ⇒ Object
- .table_name ⇒ Object
- .where(options) ⇒ Object
Instance Method Summary collapse
Methods included from Relationships
Class Method Details
.all ⇒ Object
66 67 68 |
# File 'lib/microframe/orm/class_queries.rb', line 66 def all init_queryset(:select, "*").fetch end |
.count ⇒ Object
86 87 88 |
# File 'lib/microframe/orm/class_queries.rb', line 86 def count all.size end |
.create(options = {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/microframe/orm/class_queries.rb', line 54 def create(={}) keys = .keys.join(", ") values = .values placeholders = Array.new(values.size, "?").join(", ") Connection.connect.execute("INSERT INTO #{table_name} (#{keys}) VALUES (#{placeholders})", values) self.last end |
.create_table ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/microframe/orm/class_queries.rb', line 26 def create_table query = "CREATE TABLE IF NOT EXISTS #{table_name} (#{@@create_table_query.join(", ")})" if Connection.execute(query) @@create_table_query = [] define_attributes end end |
.define_attributes ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/microframe/orm/class_queries.rb', line 34 def define_attributes save_data = [] Connection.retrieve_columns(table_name).each do |column| save_data << column define_method("#{column}=") do |val| instance_var = "@#{column}" instance_variable_set(instance_var, val) end define_method(column) do instance_var = "@#{column}" instance_variable_get(instance_var) end end define_method("models_columns") do save_data end end |
.destroy(id) ⇒ Object
106 107 108 |
# File 'lib/microframe/orm/class_queries.rb', line 106 def destroy(id) destroy_all("WHERE id = #{id}") end |
.destroy_all(xtra = "; VACUUM") ⇒ Object
110 111 112 113 114 |
# File 'lib/microframe/orm/class_queries.rb', line 110 def destroy_all(xtra = "; VACUUM") query = "DELETE FROM #{table_name} #{xtra}" Connection.connect.execute(query) self end |
.find(id) ⇒ Object
70 71 72 |
# File 'lib/microframe/orm/class_queries.rb', line 70 def find(id) find_by(id: id) end |
.find_by(options) ⇒ Object
74 75 76 |
# File 'lib/microframe/orm/class_queries.rb', line 74 def find_by() init_queryset(:find_by, ).fetch.first end |
.first ⇒ Object
90 91 92 |
# File 'lib/microframe/orm/class_queries.rb', line 90 def first limit(1).fetch.first end |
.get_create_table_query ⇒ Object
22 23 24 |
# File 'lib/microframe/orm/class_queries.rb', line 22 def get_create_table_query @@create_table_query end |
.inherited(base) ⇒ Object
11 12 13 |
# File 'lib/microframe/orm/class_queries.rb', line 11 def inherited(base) base.include InstanceQueries end |
.init_queryset(mtd, option) ⇒ Object
116 117 118 |
# File 'lib/microframe/orm/class_queries.rb', line 116 def init_queryset(mtd, option) Queryset.new(self).send(mtd, option) end |
.last ⇒ Object
94 95 96 |
# File 'lib/microframe/orm/class_queries.rb', line 94 def last limit(1).order("id DESC").fetch.first end |
.limit(val) ⇒ Object
98 99 100 |
# File 'lib/microframe/orm/class_queries.rb', line 98 def limit(val) init_queryset(:limit, val) end |
.order(val) ⇒ Object
102 103 104 |
# File 'lib/microframe/orm/class_queries.rb', line 102 def order(val) init_queryset(:order, val) end |
.property(col_name, options = {}) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/microframe/orm/class_queries.rb', line 15 def property(col_name, = {}) [:type] = [:type].to_s.upcase [:nullable] = [:nullable] ? "NULL" : "NOT NULL" [:primary_key] = [:primary_key] ? "PRIMARY KEY AUTOINCREMENT" : "" @@create_table_query << (col_name.to_s + " " + .values.join(" ")) end |
.select(val) ⇒ Object
82 83 84 |
# File 'lib/microframe/orm/class_queries.rb', line 82 def select(val) init_queryset(:select, val) end |
.table_name ⇒ Object
62 63 64 |
# File 'lib/microframe/orm/class_queries.rb', line 62 def table_name self.to_s.downcase + "s" end |
.where(options) ⇒ Object
78 79 80 |
# File 'lib/microframe/orm/class_queries.rb', line 78 def where() init_queryset(:where, ) end |
Instance Method Details
#table_name ⇒ Object
9 10 11 |
# File 'lib/microframe/orm/base.rb', line 9 def table_name self.class.table_name end |