Class: AlRdbwPostgres
- Inherits:
-
AlRdbw
- Object
- AlRdbw
- AlRdbwPostgres
- Defined in:
- lib/al_rdbw_postgres.rb,
lib/al_persist_postgres.rb
Overview
リレーショナルデータベースラッパー PostgreSQL版
Instance Method Summary (collapse)
-
- (AlPersistSqlite) [](tname)
tableを指定して、Persistオブジェクトを生成 syntax sugar.
-
- (Boolean) commit
トランザクションコミット.
-
- (Hash) delete(table, where_cond)
delete文の発行ヘルパー.
-
- (Hash) insert(table, values)
insert文の発行ヘルパー.
-
- (Object) open_connection
RDBサーバとのコネクションを開始する.
-
- (Boolean) rollback
トランザクションロールバック.
-
- (PGresult) run(sql, var = [])
任意SQLの実行.
-
- (Array<Hash>) select(sql, where_cond = nil)
select文の発行ヘルパー.
-
- (AlPersistSqlite) table(tname, pkey = nil)
tableを指定して、Persistオブジェクトを生成.
-
- (Boolean) transaction
トランザクション開始.
-
- (Hash) update(table, values, where_cond)
update文の発行ヘルパー.
Methods inherited from AlRdbw
connect, connect_suitable, #get_handle, #initialize, #transaction_active?
Constructor Details
This class inherits a constructor from AlRdbw
Instance Method Details
- (AlPersistSqlite) [](tname)
tableを指定して、Persistオブジェクトを生成 syntax sugar
52 53 54 |
# File 'lib/al_persist_postgres.rb', line 52 def []( tname ) return AlPersistPostgres.new( self, tname ) end |
- (Boolean) commit
実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する
トランザクションコミット
239 240 241 242 243 244 245 |
# File 'lib/al_rdbw_postgres.rb', line 239 def commit() return false if ! @flag_transaction result = get_handle().exec( "commit transaction;" ) result.clear @flag_transaction = false return true end |
- (Hash) delete(table, where_cond)
delete文の発行ヘルパー
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/al_rdbw_postgres.rb', line 206 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 = {} ret[:cmdtuples] = result.cmdtuples result.clear return ret end |
- (Hash) insert(table, values)
insert文の発行ヘルパー
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/al_rdbw_postgres.rb', line 134 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 = {} ret[:cmdtuples] = result.cmdtuples result.clear return ret end |
- (Object) open_connection
コネクションエラー時のエラー処理をどこでするかを明確化して実装しなければならない。 下請けにするライブラリの都合などを鑑みて、全体統一を図る必要があるだろう。 それには、現在のSQLiteとPostgreSQLだけでは、サンプルが足りないように思う。
RDBサーバとのコネクションを開始する
27 28 29 30 31 32 33 |
# File 'lib/al_rdbw_postgres.rb', line 27 def open_connection() return false if ! @conn_str @handle = PGconn.connect( @conn_str ) @handle.set_client_encoding( AL_CHARSET ) @conn_str = nil end |
- (Boolean) rollback
トランザクションロールバック
253 254 255 256 257 258 259 |
# File 'lib/al_rdbw_postgres.rb', line 253 def rollback() return false if ! @flag_transaction result = get_handle().exec( "rollback transaction;" ) result.clear @flag_transaction = false return true end |
- (PGresult) run(sql, var = [])
PGresultは、呼出側でclearする必要がある。
任意SQLの実行
45 46 47 |
# File 'lib/al_rdbw_postgres.rb', line 45 def run( sql, var = [] ) return get_handle().exec( sql, var ) end |
- (Array<Hash>) select(sql, where_cond = nil)
select文の発行ヘルパー
use Hash
select( "select * from t1 _WHERE_;", { :id=>2, :age=>nil, "name like"=>"a%" } )
64 65 66 67 68 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 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/al_rdbw_postgres.rb', line 64 def select( sql, where_cond = nil ) case where_cond when NilClass result = get_handle().exec( sql ) when Array result = get_handle().exec( 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 ) result = get_handle().exec( "#{s[0]} where #{where} #{s[1]}", val ) else raise "where_cond error in select()" end if result.result_status != PGresult::PGRES_TUPLES_OK raise "SQL error. result is not TUPLES_OK." end # # データの取り出しとデータタイプの変換 # number is a PostgreSQL's Object ID. # see src/include/catalog/pg_type.h # ret = [] result.each do |r| i = 0 a = {} r.each do |k,v| if v != nil case result.ftype( i ) when 16 # bool v = (v == "t") when 20,21,23, 26 # int, OID v = v.to_i when 25,1042,1043 # text, char, varchar v.force_encoding( AL_CHARSET ) when 700,701 # float v = v.to_f when 1082,1083,1114,1184 # date,time,timestamp v = Time.parse( v ) end end a[k.to_sym] = v i += 1 end ret << a end result.clear return ret end |
- (AlPersistSqlite) table(tname, pkey = nil)
tableを指定して、Persistオブジェクトを生成
42 43 44 |
# File 'lib/al_persist_postgres.rb', line 42 def table( tname, pkey = nil ) return AlPersistPostgres.new( self, tname, pkey ) end |
- (Boolean) transaction
トランザクション開始
224 225 226 227 228 229 |
# File 'lib/al_rdbw_postgres.rb', line 224 def transaction() return false if @flag_transaction result = get_handle().exec( "begin transaction;" ) result.clear return @flag_transaction = true end |
- (Hash) update(table, values, where_cond)
update文の発行ヘルパー
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/al_rdbw_postgres.rb', line 171 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 = {} ret[:cmdtuples] = result.cmdtuples result.clear return ret end |