Module: Mystic::Model::ClassMethods

Defined in:
lib/mystic/model.rb

Instance Method Summary collapse

Instance Method Details

#create(params = {}, opts = {}) ⇒ Object



114
115
116
117
118
# File 'lib/mystic/model.rb', line 114

def create params={}, opts={}
  res = Mystic.execute insert_sql(params, opts)
  			return res if res.is_a? String
  			res.first rescue nil
end

#delete(params = {}, opts = {}) ⇒ Object



124
125
126
# File 'lib/mystic/model.rb', line 124

def delete params={}, opts={}
  			Mystic.execute delete_sql(params, opts)
end

#delete_sql(params = {}, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mystic/model.rb', line 92

def delete_sql params={}, opts={}
  return "" if params.empty?
			
  			sym_opts = opts.symbolize

  			wrapper_sql(
:sql => "DELETE FROM #{table_name} WHERE #{params.sqlize*' AND '}",
:return_rows => sym_opts[:return_rows],
:return_json => sym_opts[:return_json]
  			)
end

#exec_func(funcname, *params) ⇒ Object



128
129
130
# File 'lib/mystic/model.rb', line 128

def exec_func funcname, *params
	Mystic.execute function_sql(false, funcname, *params)
end

#exec_func_rows(funcname, *params) ⇒ Object



132
133
134
# File 'lib/mystic/model.rb', line 132

def exec_func_rows funcname, *params
	Mystic.execute function_sql(true, funcname, *params)
end

#fetch(params = {}, opts = {}) ⇒ Object



108
109
110
111
112
# File 'lib/mystic/model.rb', line 108

def fetch params={}, opts={}
  res = select params, opts.merge({:count => 1})
  			return res if res.is_a? String
  			res.first rescue nil
end

#function_sql(returns_rows, funcname, *params) ⇒ Object



45
46
47
# File 'lib/mystic/model.rb', line 45

def function_sql returns_rows, funcname, *params
  			"SELECT #{returns_rows ? "* FROM" : ""} #{funcname}(#{params.sqlize*','})"
end

#insert_sql(params = {}, opts = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mystic/model.rb', line 80

def insert_sql params={}, opts={}
  			return "" if params.empty?

  			sym_opts = opts.symbolize
			
  			wrapper_sql(
:sql => "INSERT INTO #{table_name} (#{params.keys*','}) VALUES (#{params.values.sqlize*','})",
:return_rows => sym_opts[:return_rows],
:return_json => sym_opts[:return_json]
  			)
end

#select(params = {}, opts = {}) ⇒ Object



104
105
106
# File 'lib/mystic/model.rb', line 104

def select params={}, opts={}
  Mystic.execute select_sql(params, opts)
end

#select_sql(params = {}, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mystic/model.rb', line 49

def select_sql params={}, opts={}
  			sym_opts = opts.symbolize
  count = sym_opts[:count] || 0
  			where = params.sqlize

  			sql = []
  			sql << "SELECT #{visible_cols*','} FROM #{table_name}"
  			sql << "WHERE #{where*' AND '}" if where.count > 0
  			sql << "LIMIT #{count.to_i}" if count > 0
			
  			wrapper_sql(
:sql => sql.join(' '),
:return_rows => true,
:return_json => sym_opts[:return_json],
:plural => count > 1
  			)
end

#table_nameObject



10
11
12
# File 'lib/mystic/model.rb', line 10

def table_name
  to_s.downcase
end

#update(where = {}, set = {}, opts = {}) ⇒ Object



120
121
122
# File 'lib/mystic/model.rb', line 120

def update where={}, set={}, opts={}
  Mystic.execute update_sql(where, set, opts.merge({ :return_rows => true }))
end

#update_sql(where = {}, set = {}, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mystic/model.rb', line 67

def update_sql where={}, set={}, opts={}
  return "" if where.empty?
  return "" if set.empty?
			
  			sym_opts = opts.symbolize
			
  			wrapper_sql(
:sql => "UPDATE #{table_name} SET #{set.sqlize*','} WHERE #{where.sqlize*' AND '}",
:return_rows => sym_opts[:return_rows],
:return_json => sym_opts[:return_json]
  			)
end

#visible_colsObject



14
15
16
# File 'lib/mystic/model.rb', line 14

def visible_cols
	["*"]
end

#wrapper_sql(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mystic/model.rb', line 18

def wrapper_sql opts={}
	sym_opts = opts.symbolize

	sql = sym_opts[:sql] || "SELECT 1"
	op = sql.split(/\s+/,2).first.upcase
	return_rows = sym_opts[:return_rows] || false
	return_json = sym_opts[:return_json] || false
	return_rows = true if return_json
	plural = opts[:plural] && op != "INSERT"
			
	sql << " RETURNING #{visible_cols*','}" if return_rows && op != "SELECT"
			
	s = []
			
	if return_json
		s << "WITH res AS (#{sql}) SELECT"
		s << "array_to_json(array_agg(res))" if plural
		s << "row_to_json(res)" unless plural
		s << "AS #{Mystic::JSON_COL}"
		s << "FROM res"
	else
		s << sql
	end
			
	s*' '
end