Class: Og::KirbyStore
- Inherits:
-
SqlStore
show all
- Defined in:
- lib/og/store/kirby.rb,
lib/og/store/alpha/kirby.rb
Overview
A Store that persists objects into an KirbyBase database. KirbyBase is a pure-ruby database implementation. To read documentation about the methods, consult the documentation for SqlStore and Store.
Instance Attribute Summary
Attributes inherited from SqlStore
#conn
Attributes inherited from Store
#options, #transaction_nesting
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from SqlStore
#aggregate, #delete_all, #enable_logging, #managed_tables, #select, #select_one, #unmanaged_tables, #update, #update_by_sql, #update_properties
Methods included from SqlUtils
#blob, #build_join_name, #date, #escape, #join_class_ordering, #join_object_ordering, #join_table, #join_table_index, #join_table_info, #join_table_key, #join_table_keys, #ordered_join_table_keys, #parse_blob, #parse_boolean, #parse_date, #parse_float, #parse_int, #parse_timestamp, #quote, #table, #tableize, #timestamp
Methods inherited from Store
create, #delete, for_name, #force_save!, #insert, #save, #transaction, #update, #update_properties
Constructor Details
#initialize(options) ⇒ KirbyStore
Returns a new instance of KirbyStore.
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/og/store/kirby.rb', line 35
def initialize(options)
super
@typemap = {
:Fixnum => :Integer,
:TrueClass => :Boolean
}
mode = options[:mode] || :local
if mode == :client
@conn = KirbyBase.new(:client, options[:address], options[:port])
else
base_dir = self.class.base_dir(options)
FileUtils.mkdir_p(base_dir) unless File.exist?(base_dir)
@conn = KirbyBase.new(
:local,
nil, nil,
base_dir,
options[:ext] || '.tbl'
)
end
end
|
Class Method Details
.base_dir(options) ⇒ Object
22
23
24
|
# File 'lib/og/store/kirby.rb', line 22
def self.base_dir(options)
options[:base_dir] || './kirbydb'
end
|
.db_dir(options) ⇒ Object
19
20
21
|
# File 'lib/og/store/alpha/kirby.rb', line 19
def self.db_dir(options)
"#{options[:name]}_db"
end
|
.destroy(options) ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'lib/og/store/kirby.rb', line 26
def self.destroy(options)
begin
FileUtils.rm_rf(base_dir(options))
super
rescue Object
Logger.info 'Cannot drop database!'
end
end
|
Instance Method Details
#close ⇒ Object
61
62
63
64
|
# File 'lib/og/store/kirby.rb', line 61
def close
super
end
|
#commit ⇒ Object
153
154
155
|
# File 'lib/og/store/kirby.rb', line 153
def commit
end
|
#count(options) ⇒ Object
114
115
116
|
# File 'lib/og/store/kirby.rb', line 114
def count(options)
find(options).size
end
|
#enchant(klass, manager) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/og/store/kirby.rb', line 66
def enchant(klass, manager)
klass.send :attr_accessor, :oid
klass.send :alias_method, :recno, :oid
klass.send :alias_method, :recno=, :oid=
symbols = klass.properties.keys
klass.module_eval %{
def self.kb_create(recno, #{symbols.join(', ')})
obj = self.allocate
obj.recno = recno
#{ symbols.map { |s| "obj.#{s} = #{s}; "} }
return obj
end
}
super
end
|
#exec(sql) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/og/store/alpha/kirby.rb', line 60
def exec(sql)
Logger.debug sql if $DBG
@conn.query(sql).close
rescue => ex
handle_sql_exception(ex, sql)
end
|
#find(options) ⇒ Object
102
103
104
|
# File 'lib/og/store/kirby.rb', line 102
def find(options)
query(options)
end
|
#find_one(options) ⇒ Object
106
107
108
|
# File 'lib/og/store/kirby.rb', line 106
def find_one(options)
query(options).first
end
|
#get_table(klass) ⇒ Object
85
86
87
|
# File 'lib/og/store/kirby.rb', line 85
def get_table(klass)
@conn.get_table(klass.table.to_sym)
end
|
#join(obj1, obj2, table, options = nil) ⇒ Object
167
168
169
170
|
# File 'lib/og/store/kirby.rb', line 167
def join(obj1, obj2, table, options = nil)
first, second = join_object_ordering(obj1, obj2)
@conn.get_table(table.to_sym).insert(first.pk, second.pk)
end
|
#load(pk, klass) ⇒ Object
Also known as:
exist?
:section: Lifecycle methods.
91
92
93
|
# File 'lib/og/store/kirby.rb', line 91
def load(pk, klass)
get_table(klass)[pk.to_i]
end
|
#query(sql) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/og/store/kirby.rb', line 118
def query(options)
Logger.debug "Querying with #{options.inspect}." if $DBG
klass = options[:class]
table = get_table(klass)
objects = []
if condition = options[:condition] || options[:where]
condition.gsub!(/=/, '==')
condition.gsub!(/LIKE '(.*?)'/, '=~ /\1/')
condition.gsub!(/\%/, '')
condition.gsub!(/(\w*?)\s?=(.)/, 'o.\1 =\2')
objects = eval("table.select { |o| #{condition} }")
else
objects = table.select
end
if order = options[:order]
order = order.to_s
desc = (order =~ /DESC/)
order = order.gsub(/DESC/, '').gsub(/ASC/, '')
eval "objects.sort { |x, y| x.#{order} <=> y.#{order} }"
objects.reverse! if desc
end
return objects
end
|
#reload(obj, pk) ⇒ Object
96
97
98
99
100
|
# File 'lib/og/store/kirby.rb', line 96
def reload(obj, pk)
raise 'Cannot reload unmanaged object' unless obj.saved?
new_obj = load(pk, obj.class)
obj.clone(new_obj)
end
|
#rollback ⇒ Object
159
160
161
|
# File 'lib/og/store/kirby.rb', line 159
def rollback
end
|
#sql_update(sql) ⇒ Object
163
164
165
|
# File 'lib/og/store/kirby.rb', line 163
def sql_update(sql)
end
|
#start ⇒ Object
147
148
149
|
# File 'lib/og/store/kirby.rb', line 147
def start
end
|
#unjoin(obj1, obj2, table) ⇒ Object
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/og/store/kirby.rb', line 172
def unjoin(obj1, obj2, table)
first, second = join_object_ordering(obj1, obj2)
@conn.get_table(table.to_sym).delete do |r|
require 'dev-utils/debug'
breakpoint
r.send(:first_key) == first.pk and
r.send(:second_key) == second.pk
end
end
|