Class: AlRdbwPostgres

Inherits:
AlRdbw
  • Object
show all
Defined in:
lib/al_rdbw_postgres.rb,
lib/al_persist_postgres.rb

Overview

リレーショナルデータベースラッパー PostgreSQL版

Instance Attribute Summary collapse

Attributes inherited from AlRdbw

#fields, #flag_transaction, #handle, #select_data_type, #select_fetch_mode

Instance Method Summary collapse

Methods inherited from AlRdbw

#close, connect, #get_handle, inherited, #initialize, #transaction_active?

Constructor Details

This class inherits a constructor from AlRdbw

Instance Attribute Details

#resultObject (readonly)

Returns クエリ実行結果.

Returns:

  • (Object)

    クエリ実行結果



21
22
23
# File 'lib/al_rdbw_postgres.rb', line 21

def result
  @result
end

Instance Method Details

#[](tname) ⇒ AlPersistSqlite

tableを指定して、Persistオブジェクトを生成 syntax sugar

Parameters:

  • tname (String)

    テーブル名

Returns:



53
54
55
# File 'lib/al_persist_postgres.rb', line 53

def []( tname )
  return AlPersistPostgres.new( self, tname )
end

#commitBoolean

TODO:

トランザクションコミット

実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する

Returns:

  • (Boolean)

    成否



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文の発行ヘルパー

Parameters:

  • table (String)

    テーブル名

  • where_cond (Hash)

    where条件

Returns:

  • (Hash)

    結果のHash



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

Note:

任意SQLの実行

アクションクエリの実行用。selectは、select()メソッドを使う。

Parameters:

  • sql (String)

    SQL文

  • var (Array) (defaults to: nil)

    パラメータクエリ用変数

Returns:

  • (Hash)

    結果



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文の発行ヘルパー

Parameters:

  • table (String)

    テーブル名

  • values (Hash)

    insertする値のhash

Returns:

  • (Hash)

    結果のHash



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_connectionObject

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

#rollbackBoolean

トランザクションロールバック

Returns:

  • (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文の発行ヘルパー

Examples:

where condition
 use Array
  select( "select * from t1 where id=$1;", [2] )
 use Hash
  select( "select * from t1 _WHERE_;",
    { :id=>2, :age=>nil, "name like"=>"a%" } )

Parameters:

  • sql (String)

    SQL文

  • where_cond (Array, Hash, String) (defaults to: nil)

    where条件

Returns:

  • (Array<Hash>)

    結果の配列



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_nextArray, ...

シングル行モード(select_fetch_mode = :ROW)の場合の次行取得

res = @db.select( sql ) p @db.fields while res

p res
res = db.select_next()

end

Returns:

  • (Array, Hash, Nil)

    結果



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オブジェクトを生成

Parameters:

  • tname (String)

    テーブル名

  • pkey (Array<String,Symbol>, String, Symbol) (defaults to: nil)

    プライマリキー

Returns:



43
44
45
# File 'lib/al_persist_postgres.rb', line 43

def table( tname, pkey = nil )
  return AlPersistPostgres.new( self, tname, pkey )
end

#transactionBoolean

トランザクション開始

Returns:

  • (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文の発行ヘルパー

Parameters:

  • table (String)

    テーブル名

  • values (Hash)

    updateする値のhash

  • where_cond (Hash)

    where条件

Returns:

  • (Hash)

    結果のHash



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