Class: Norma::Database::Postgres
- Inherits:
-
Norma::Database
- Object
- Norma::Database
- Norma::Database::Postgres
- Defined in:
- lib/norma/database_adapter/postgres/adapter.rb
Overview
Postgresql 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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 16 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
62 63 64 65 66 67 68 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 62 def bootstrap sql = "SELECT id FROM obj;" res = execute(sql) res.each do |row| load(row['id']) end end |
#config ⇒ Object
Return database conguration.
34 35 36 37 38 39 40 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 34 def config { :database => @connection.db, :port => @connection.port, :host => @connection.host, :username => @connection.user } end |
#down ⇒ Object
Destroy object link tables.
56 57 58 59 60 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 56 def down file = File.join(File.dirname(__FILE__), 'down.sql') sql = File.read(file) execute sql end |
#execute(sql) ⇒ Object
Execute SQL
43 44 45 46 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 43 def execute(sql) #puts "\n#{sql}\n" if $DEBUG @connection.exec(sql) end |
#mint_id ⇒ Object
Get a new object id.
71 72 73 74 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 71 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.
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 139 140 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 102 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 77 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.
49 50 51 52 53 |
# File 'lib/norma/database_adapter/postgres/adapter.rb', line 49 def up file = File.join(File.dirname(__FILE__), 'up.sql') sql = File.read(file) execute sql end |