DBcache 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/lib/DBcache.rb +158 -0
- data/test/test_helper.rb +10 -0
- metadata +67 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Miguel Vazquez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= DBcache
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Miguel Vazquez. See LICENSE for details.
|
data/lib/DBcache.rb
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'mysql'
|
2
|
+
module DBcache
|
3
|
+
@@info = nil
|
4
|
+
|
5
|
+
def self.config(info)
|
6
|
+
@@info=info
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.info
|
10
|
+
@@info
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.driver
|
14
|
+
@@driver ||= Mysql::new(info[:dbhost], info[:dbuser], info[:dbpass], info[:dbname] )
|
15
|
+
@@driver
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.has_table?(table)
|
19
|
+
driver.query("SHOW TABLES").each{|row|
|
20
|
+
return true if row.include? table
|
21
|
+
}
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.has_id?(table, id)
|
26
|
+
driver.query("SELECT id FROM #{ table } WHERE id = #{ process(id) }").num_rows == 1
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.delete(table, id)
|
30
|
+
driver.query("DELETE FROM #{ table } WHERE id = #{ process(id) }")
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.fast_add(table, id, values)
|
34
|
+
driver.query("INSERT INTO #{ table } VALUE(#{process(id)}, #{values.collect{|v| process(v)}.join(", ")})")
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.add(table, id, values)
|
38
|
+
values = values.collect{|v| process(v)}
|
39
|
+
create(table, field_type(id), values.collect{|v| field_type(v)}) unless has_table?(table)
|
40
|
+
delete(table, id) if has_id?(table, id)
|
41
|
+
driver.query("INSERT INTO #{ table } VALUE(#{process(id)}, #{values.join(", ")})")
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.field_type(value)
|
45
|
+
case
|
46
|
+
when Symbol === value
|
47
|
+
"CHAR(50)"
|
48
|
+
when String === value
|
49
|
+
"VARCHAR(255)"
|
50
|
+
when Integer === value
|
51
|
+
"INT"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.num_rows(table, field = '*')
|
56
|
+
driver.query("SELECT COUNT(#{ field }) FROM #{ table }").fetch_row.first.to_i
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.matches(table, ids)
|
60
|
+
return [] if ids.empty?
|
61
|
+
matches = []
|
62
|
+
driver.query("SELECT id FROM #{ table } WHERE id IN (#{ ids.collect{|id| process(id) }.join(", ")}) ").each{|row| matches << row.first}
|
63
|
+
matches
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.process(value)
|
67
|
+
case
|
68
|
+
when value.nil?
|
69
|
+
"NULL"
|
70
|
+
when Symbol === value
|
71
|
+
return "'" + Mysql.escape_string(value.to_s) + "'"
|
72
|
+
when String === value
|
73
|
+
if value.length == 0
|
74
|
+
"NULL"
|
75
|
+
elsif value.length > 256
|
76
|
+
return "'" + Mysql.escape_string(value.scan(/^.{253}/).first) + '...' + "'"
|
77
|
+
else
|
78
|
+
return "'" + Mysql.escape_string(value) + "'"
|
79
|
+
end
|
80
|
+
else
|
81
|
+
value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def self.drop(table)
|
87
|
+
db = driver
|
88
|
+
begin
|
89
|
+
db.query("DROP TABLE #{ table }")
|
90
|
+
rescue
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.create(table, id_type, value_types)
|
95
|
+
db = driver
|
96
|
+
db.query("CREATE TABLE #{ table } ( id #{ id_type }, #{
|
97
|
+
i = -1
|
98
|
+
value_types.collect{|type|
|
99
|
+
i += 1
|
100
|
+
"C#{i} #{type}"
|
101
|
+
}.join(", ")
|
102
|
+
}, PRIMARY KEY(id) )" )
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.save(table, info, value_types = nil)
|
107
|
+
drop(table)
|
108
|
+
|
109
|
+
if Array === info
|
110
|
+
hash = {}
|
111
|
+
info.each_with_index{|v,i| hash[i] = v}
|
112
|
+
info = hash
|
113
|
+
end
|
114
|
+
|
115
|
+
if value_types.nil?
|
116
|
+
template = info.values.select{|list|
|
117
|
+
if Array === list
|
118
|
+
list.select{|v| v.nil?}.empty?
|
119
|
+
else
|
120
|
+
list != nil
|
121
|
+
end
|
122
|
+
}.first
|
123
|
+
|
124
|
+
template = [template] unless Array === template
|
125
|
+
value_types = template.collect{|f| field_type(f)}
|
126
|
+
end
|
127
|
+
|
128
|
+
create(table, field_type(info.keys.first), value_types)
|
129
|
+
|
130
|
+
db = driver
|
131
|
+
info.keys.each{|k|
|
132
|
+
values = info[k].collect{|v| process(v)}
|
133
|
+
db.query("INSERT INTO #{ table } VALUE(#{process(k)}, #{values.join(", ")})")
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
def self.load(table, ids = nil)
|
139
|
+
db = driver
|
140
|
+
data = {}
|
141
|
+
|
142
|
+
if ids.nil?
|
143
|
+
db.query("SELECT * FROM #{ table }").each{|row|
|
144
|
+
data[row.shift] = row
|
145
|
+
}
|
146
|
+
else
|
147
|
+
ids = [ids] unless Array === ids
|
148
|
+
return data if ids.empty?
|
149
|
+
|
150
|
+
db.query("SELECT * FROM #{ table } WHERE id IN (#{ids.collect{|v| process(v)}.join(", ")})").each{|row|
|
151
|
+
data[row.shift] = row
|
152
|
+
}
|
153
|
+
end
|
154
|
+
|
155
|
+
data
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: DBcache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Miguel Vazquez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-29 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mysql
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - <
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "2.8"
|
24
|
+
version:
|
25
|
+
description: Creates a table and stores and retrieves de data on the hash. It can guess the best field type.
|
26
|
+
email: miguel.vazquez@fdi.ucm.es
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- lib/DBcache.rb
|
36
|
+
- LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/mikisvaz/DBcache
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Cache Hashes into Mysql
|
66
|
+
test_files:
|
67
|
+
- test/test_helper.rb
|