Class: Slick::Database
Defined Under Namespace
Modules: AbstractRow
Classes: Column, Migration, Migrator, Row, Table, Union
Instance Method Summary
collapse
Constructor Details
#initialize(config = {}) ⇒ Database
6
7
8
9
|
# File 'lib/slick/database.rb', line 6
def initialize(config = {})
@config = config.paramify
run("use ?", @config.name.to_sym) if exist?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/slick/database.rb', line 82
def method_missing(name, *args, &block)
if args.length == 0
if Union.registered_classes[name.to_s]
out = Union.create(name, self)
else
out = Table.create(name, self)
end
if block
if block.arity > 0
block.call(out)
else
out.instance_eval(&block)
end
end
out
else
super
end
end
|
Instance Method Details
#add_table(name) ⇒ Object
62
63
64
|
# File 'lib/slick/database.rb', line 62
def add_table(name)
send(name).create
end
|
37
38
39
40
41
42
|
# File 'lib/slick/database.rb', line 37
def create
if !exist?
run("create database ?", @config.name.to_sym)
run("use ?", @config.name.to_sym)
end
end
|
#current_schema_version ⇒ Object
70
71
72
|
# File 'lib/slick/database.rb', line 70
def current_schema_version
Migrator.new(self).current_schema_version
end
|
44
45
46
47
|
# File 'lib/slick/database.rb', line 44
def drop
run("drop database ?", @config.name.to_sym) if exist?
@using = nil
end
|
#exist? ⇒ Boolean
33
34
35
|
# File 'lib/slick/database.rb', line 33
def exist?
run("show databases", :cache => "schema").map{|row| row["Database"]}.include?(@config.name)
end
|
#latest_schema_version ⇒ Object
74
75
76
|
# File 'lib/slick/database.rb', line 74
def latest_schema_version
Migrator.new(self).latest_schema_version
end
|
#migrate(*args) ⇒ Object
78
79
80
|
# File 'lib/slick/database.rb', line 78
def migrate(*args)
Migrator.new(self).migrate(*args)
end
|
#remove_table(name) ⇒ Object
66
67
68
|
# File 'lib/slick/database.rb', line 66
def remove_table(name)
send(name).drop
end
|
#run(*args) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/slick/database.rb', line 11
def run(*args)
options = (args.last.kind_of?(Hash) ? args.pop : {}).paramify
query = _prepare(args)
Slick.resource_provider["database_schema_cache"].clear if query.match(/\A\s*(create|drop|alter)/i)
Slick.resource_provider["database_session_cache"].clear if query.match(/\A\s*(create|drop|alter|insert|update)/i)
rows = if options.cache
Slick.resource_provider["database_#{options.cache}_cache"]["query:#{query}"] ||= _fetch_rows(query)
else
_fetch_rows(query)
end
rows.map do |row|
out = row.clone
if out['_type']
out = Slick::Database::Row.create(out.delete('_type'), self, out)
end
out
end
end
|
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/slick/database.rb', line 49
def tables
if exist?
out = {}
run("show tables", :cache => "schema").each do |row|
name = row.values[0]
out[name] = send(name)
end
out
else
{}
end
end
|