Class: AlRdbwPostgres
- Defined in:
- lib/al_rdbw_postgres.rb,
lib/al_persist_postgres.rb
Overview
リレーショナルデータベースラッパー PostgreSQL版
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
クエリ実行結果.
Attributes inherited from AlRdbw
#fields, #flag_transaction, #handle, #select_data_type, #select_fetch_mode
Instance Method Summary collapse
-
#[](tname) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成 syntax sugar.
-
#commit ⇒ Boolean
トランザクションコミット.
-
#delete(table, where_cond) ⇒ Hash
delete文の発行ヘルパー.
-
#execute(sql, var = nil) ⇒ Hash
(also: #exec)
任意SQLの実行.
-
#insert(table, values) ⇒ Hash
insert文の発行ヘルパー.
-
#open_connection ⇒ Object
RDBサーバとのコネクションを開始する.
-
#rollback ⇒ Boolean
トランザクションロールバック.
-
#select(sql, where_cond = nil) ⇒ Array<Hash>
select文の発行ヘルパー.
-
#select_next ⇒ Array, ...
シングル行モード(select_fetch_mode = :ROW)の場合の次行取得.
-
#table(tname, pkey = nil) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成.
-
#transaction ⇒ Boolean
トランザクション開始.
-
#update(table, values, where_cond) ⇒ Hash
update文の発行ヘルパー.
Methods inherited from AlRdbw
#close, connect, #get_handle, inherited, #initialize, #transaction_active?
Constructor Details
This class inherits a constructor from AlRdbw
Instance Attribute Details
#result ⇒ Object (readonly)
Returns クエリ実行結果.
21 22 23 |
# File 'lib/al_rdbw_postgres.rb', line 21 def result @result end |
Instance Method Details
#[](tname) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成 syntax sugar
53 54 55 |
# File 'lib/al_persist_postgres.rb', line 53 def []( tname ) return AlPersistPostgres.new( self, tname ) end |
#commit ⇒ Boolean
トランザクションコミット
実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する
250 251 252 253 254 255 256 |
# File 'lib/al_rdbw_postgres.rb', line 250 def commit() return false if ! @flag_transaction @result = get_handle().exec( "commit transaction;" ) @result.clear() @flag_transaction = false return true end |
#delete(table, where_cond) ⇒ Hash
delete文の発行ヘルパー
219 220 221 222 223 224 225 226 227 |
# File 'lib/al_rdbw_postgres.rb', line 219 def delete( table, where_cond ) (where, wval) = make_where_condition( where_cond ) sql = "delete from #{table} where #{where};" @result = get_handle().exec( sql, wval ) ret = { :cmdtuples=>@result.cmdtuples } @result.clear() return ret end |
#execute(sql, var = nil) ⇒ Hash Also known as: exec
任意SQLの実行
アクションクエリの実行用。selectは、select()メソッドを使う。
46 47 48 49 50 51 |
# File 'lib/al_rdbw_postgres.rb', line 46 def execute( sql, var = nil ) @result = get_handle().exec( sql, var ) ret = { :cmdtuples=>@result.cmdtuples } @result.clear() return ret end |
#insert(table, values) ⇒ Hash
insert文の発行ヘルパー
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/al_rdbw_postgres.rb', line 151 def insert( table, values ) col = "" plh = "" val = [] cnt = 1 values.each do |k,v| col << "#{k}," plh << "$#{cnt}," cnt += 1 if v.class == Array val << v.join( ',' ) else val << v end end col.chop! plh.chop! sql = "insert into #{table} (#{col}) values (#{plh});" @result = get_handle().exec( sql, val ) ret = { :cmdtuples=>@result.cmdtuples } @result.clear() return ret end |
#open_connection ⇒ Object
RDBサーバとのコネクションを開始する
27 28 29 30 31 32 33 34 |
# File 'lib/al_rdbw_postgres.rb', line 27 def open_connection() return false if ! @dsn @handle = PG::Connection.new( @dsn ) @handle.set_client_encoding( AL_CHARSET.to_s ) @handle.type_map_for_results = PG::BasicTypeMapForResults.new(@handle) @dsn = nil end |
#rollback ⇒ Boolean
トランザクションロールバック
264 265 266 267 268 269 270 |
# File 'lib/al_rdbw_postgres.rb', line 264 def rollback() return false if ! @flag_transaction @result = get_handle().exec( "rollback transaction;" ) @result.clear() @flag_transaction = false return true end |
#select(sql, where_cond = nil) ⇒ Array<Hash>
select文の発行ヘルパー
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/al_rdbw_postgres.rb', line 69 def select( sql, where_cond = nil ) conn = get_handle() case where_cond when NilClass conn.send_query( sql ) when Array conn.send_query_params( sql, where_cond ); when Hash s = sql.split( '_WHERE_' ) raise "SQL error in select()" if s.size != 2 (where, val) = make_where_condition( where_cond ) conn.send_query_params( "#{s[0]} where #{where} #{s[1]}", val ) when String conn.send_query( sql.sub( '_WHERE_', "where #{where_cond}" )) else raise "where_cond error in AlRdbwPostgres#select()" end conn.set_single_row_mode if @select_fetch_mode == :ROW # get field name @result = conn.get_result() or return @select_fetch_mode == :ROW ? nil : [] @result.check_result @fields = @result.fields().map {|field| field.to_sym } # get data ret = [] case @select_data_type when :ARRAY # Array<Array>で返す @result.each_row {|row| ret << row } else # Array<Hash>で返す(標準) @result.each_row {|row| ret << [@fields, row].transpose.to_h } end return ret[0] if @select_fetch_mode == :ROW @result.clear() conn.get_result() # libqp needs this operation. return ret end |
#select_next ⇒ Array, ...
シングル行モード(select_fetch_mode = :ROW)の場合の次行取得
res = @db.select( sql ) p @db.fields while res
p res
res = db.select_next()
end
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/al_rdbw_postgres.rb', line 129 def select_next() @result = get_handle().get_result() or return nil @result.check_result case @select_data_type when :ARRAY @result.each_row {|row| return row } else @result.each_row {|row| return [@fields, row].transpose.to_h } end end |
#table(tname, pkey = nil) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成
43 44 45 |
# File 'lib/al_persist_postgres.rb', line 43 def table( tname, pkey = nil ) return AlPersistPostgres.new( self, tname, pkey ) end |
#transaction ⇒ Boolean
トランザクション開始
235 236 237 238 239 240 |
# File 'lib/al_rdbw_postgres.rb', line 235 def transaction() return false if @flag_transaction @result = get_handle().exec( "begin transaction;" ) @result.clear() return @flag_transaction = true end |
#update(table, values, where_cond) ⇒ Hash
update文の発行ヘルパー
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/al_rdbw_postgres.rb', line 186 def update( table, values, where_cond ) columns = "" cnt = 1 val = [] values.each do |k,v| columns << "#{k}=$#{cnt}," cnt += 1 if v.class == Array val << v.join( ',' ) else val << v end end columns.chop! (where, wval) = make_where_condition( where_cond, cnt ) sql = "update #{table} set #{columns} where #{where};" @result = get_handle().exec( sql, val + wval ) ret = { :cmdtuples=>@result.cmdtuples } @result.clear() return ret end |