Class: Norma::Database::Mysql
- Inherits:
-
Norma::Database
- Object
- Norma::Database
- Norma::Database::Mysql
- Defined in:
- lib/norma/database_adapter/mysql/adapter.rb
Overview
Mysql Store
Constant Summary
Constants inherited from Norma::Database
Class Method Summary collapse
-
.connect(config) ⇒ Object
Create a connection to database.
Instance Method Summary collapse
- #bootstrap ⇒ Object
-
#config ⇒ Object
Return database conguration.
-
#down ⇒ Object
Destroy object link tables.
-
#execute(sql) ⇒ Object
Execute SQL.
-
#mint_id ⇒ Object
Get a new object id.
-
#read(id, object = nil) ⇒ Object
Read object from database.
-
#save(object) ⇒ Object
Save-out an object to the database.
-
#up ⇒ Object
Create object link tables.
Methods inherited from Norma::Database
#delete, #initialize, #insert_class_sql, #insert_ivar_sql, #insert_object_sql, #insert_sql, #load, #newArray, #newFixnum, #newFloat, #newHash, #newRange, #newString, #newSymbol, #newTime, #select, #sqlArray, #sqlFixnum, #sqlFloat, #sqlHash, #sqlRange, #sqlString, #sqlSymbol, #sqlTime, #update_class_sql, #update_ivar_sql, #update_object_sql, #update_sql
Constructor Details
This class inherits a constructor from Norma::Database
Class Method Details
.connect(config) ⇒ Object
Create a connection to database.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 14 def self.connect(config) host = config[:host] || "localhost" port = config[:port] || 5433 unless host.nil? username = config[:username] || "" password = config[:password] || "" if config.key?(:database) database = config[:database] else raise ArgumentError, "No database specified. Missing argument: database." end return new( PGconn.connect(host, port, "", "", database, username, password) #, logger ) end |
Instance Method Details
#bootstrap ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 60 def bootstrap sql = "SELECT id FROM obj;" res = execute(sql) res.each do |row| load(row['id']) end end |
#config ⇒ Object
Return database conguration.
32 33 34 35 36 37 38 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 32 def config { :database => @connection.db, :port => @connection.port, :host => @connection.host, :username => @connection.user } end |
#down ⇒ Object
Destroy object link tables.
54 55 56 57 58 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 54 def down file = File.join(File.dirname(__FILE__), 'down.sql') sql = File.read(file) execute sql end |
#execute(sql) ⇒ Object
Execute SQL
41 42 43 44 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 41 def execute(sql) #puts "\n#{sql}\n" if $DEBUG @connection.exec(sql) end |
#mint_id ⇒ Object
Get a new object id.
69 70 71 72 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 69 def mint_id r = execute "SELECT nextval('obj_sequence');" r[0][0].to_i end |
#read(id, object = nil) ⇒ Object
Read object from database.
TODO: Use a join query if possible and if more efficient.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 100 def read(id, object=nil) sql = "SELECT * FROM obj WHERE id=#{id};" obj = execute(sql) return nil unless obj # THINK: raise error instead? classname = obj[0][1] klass = Object.const_get(classname) immutable = (klass<=NilClass || klass<=FalseClass || klass<=TrueClass || klass<=Fixnum || klass<=Symbol) case klass when NilClass object = nil when FalseClass object = false when TrueClass object = true else if CORE.include?(klass) sql = "SELECT * FROM obj#{klass} WHERE id=#{id};" rec = execute(sql) object = __send__("new#{klass}", rec[0]) else object = klass.allocate end end unless immutable sql = "SELECT * FROM ivar WHERE obj_id=#{id};" var = execute(sql) var.each do |row| object.__send__(:instance_variable_set, "@#{row['name']}", load(row['val_id'])) end end object end |
#save(object) ⇒ Object
Save-out an object to the database.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 75 def save(object) #puts "Saving a #{object.class} id(#{object.record_id})" sql = [] if object.as_record.persisted? sql << update_sql(object) object.state.each do |field, value| value.as_record.save unless value.as_record.persisted? sql << update_ivar_sql(object, field, value) end else sql << insert_sql(object) object.state.each do |field, value| value.as_record.save unless value.as_record.persisted? sql << insert_ivar_sql(object, field, value) end end sql = sql.join("\n") execute(sql) object.as_record.persisted! end |
#up ⇒ Object
Create object link tables.
47 48 49 50 51 |
# File 'lib/norma/database_adapter/mysql/adapter.rb', line 47 def up file = File.join(File.dirname(__FILE__), 'up.sql') sql = File.read(file) execute sql end |