Class: Makitzo::Store::MySQL
- Inherits:
-
Object
- Object
- Makitzo::Store::MySQL
- Defined in:
- lib/makitzo/store/mysql.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #applied_migrations_for_all_hosts ⇒ Object
- #applied_migrations_for_host(host) ⇒ Object
-
#initialize(config = {}) ⇒ MySQL
constructor
A new instance of MySQL.
- #mark_migration_as_applied(host, migration) ⇒ Object
- #open(&block) ⇒ Object
- #read(host, key) ⇒ Object
- #read_all(host, *keys) ⇒ Object
- #unmark_migration_as_applied(host, migration) ⇒ Object
- #write(host, key, value) ⇒ Object
- #write_all(host, hash) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ MySQL
Returns a new instance of MySQL.
7 8 9 10 |
# File 'lib/makitzo/store/mysql.rb', line 7 def initialize(config = {}) config.each { |k,v| send(:"#{k}=", v) } @mutex = Mutex.new end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def database @database end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def socket @socket end |
#username ⇒ Object
Returns the value of attribute username.
5 6 7 |
# File 'lib/makitzo/store/mysql.rb', line 5 def username @username end |
Instance Method Details
#applied_migrations_for_all_hosts ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/makitzo/store/mysql.rb', line 90 def applied_migrations_for_all_hosts sync do @client.query("SELECT * FROM #{migrations_table} ORDER BY migration_id ASC").inject({}) do |m,r| (m[r['hostname']] ||= []) << r['migration_id'] m end end end |
#applied_migrations_for_host(host) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/makitzo/store/mysql.rb', line 99 def applied_migrations_for_host(host) sync do @client.query("SELECT migration_id FROM #{migrations_table} WHERE hostname = #{qs(host)} ORDER BY migration_id ASC").inject([]) do |m,r| m << r['migration_id'] end end end |
#mark_migration_as_applied(host, migration) ⇒ Object
78 79 80 81 82 |
# File 'lib/makitzo/store/mysql.rb', line 78 def mark_migration_as_applied(host, migration) sync do @client.query("REPLACE INTO #{migrations_table} (hostname, migration_id) VALUES (#{qs(host)}, #{migration.to_i})") end end |
#open(&block) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/makitzo/store/mysql.rb', line 12 def open(&block) connect! begin yield if block_given? ensure cleanup! end end |
#read(host, key) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/makitzo/store/mysql.rb', line 21 def read(host, key) sync do @client.query("SELECT * FROM #{key_value_table} WHERE hostname = #{qs(host)} AND `key` = #{qs(key)}", :cast_booleans => true).each do |r| return row_value(r) end nil end end |
#read_all(host, *keys) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/makitzo/store/mysql.rb', line 62 def read_all(host, *keys) out = [keys].flatten.inject({}) { |hsh,k| hsh[k.to_s] = nil; hsh } sync do @client.query("SELECT * FROM #{key_value_table} WHERE hostname = #{qs(host)} AND `key` IN (#{out.keys.map { |k| qs(k) }.join(', ')})", :cast_booleans => true).each do |r| out[r['key']] = row_value(r) end end out end |
#unmark_migration_as_applied(host, migration) ⇒ Object
84 85 86 87 88 |
# File 'lib/makitzo/store/mysql.rb', line 84 def unmark_migration_as_applied(host, migration) sync do @client.query("DELETE FROM #{migrations_table} WHERE hostname = #{qs(host)} AND migration_id = #{migration.to_i}") end end |
#write(host, key, value) ⇒ Object
30 31 32 33 34 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 60 |
# File 'lib/makitzo/store/mysql.rb', line 30 def write(host, key, value) value_hash = { 'value_int' => 'NULL', 'value_float' => 'NULL', 'value_date' => 'NULL', 'value_datetime' => 'NULL', 'value_boolean' => 'NULL', 'value_string' => 'NULL' } case value when Fixnum then value_hash['value_int'] = value.to_s when Float then value_hash['value_float'] = value.to_s when DateTime, Time then value_hash['value_datetime'] = qs(value.strftime("%Y-%m-%dT%H:%M:%S")) when Date then value_hash['value_date'] = qs(value.strftime("%Y-%m-%d")) when TrueClass, FalseClass then value_hash['value_boolean'] = value ? '1' : '0' when NilClass then ; # do nothing else value_hash['value_string'] = qs(value) end sync do @client.query(" REPLACE INTO #{key_value_table} (hostname, `key`, value_int, value_float, value_date, value_datetime, value_boolean, value_string) VALUES (#{qs(host)}, #{qs(key)}, #{value_hash['value_int']}, #{value_hash['value_float']}, #{value_hash['value_date']}, #{value_hash['value_datetime']}, #{value_hash['value_boolean']}, #{value_hash['value_string']}) ") end end |
#write_all(host, hash) ⇒ Object
72 73 74 75 76 |
# File 'lib/makitzo/store/mysql.rb', line 72 def write_all(host, hash) sync do hash.each { |k,v| write(host, k, v) } end end |