Class: AlRdbwSqlite

Inherits:
AlRdbw show all
Defined in:
lib/al_rdbw_sqlite.rb,
lib/al_persist_sqlite.rb

Overview

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

Instance Attribute Summary

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 Method Details

#[](tname) ⇒ AlPersistSqlite

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

Parameters:

  • tname (String)

    テーブル名

Returns:



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

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

#commitBoolean

TODO:

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

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

Returns:

  • (Boolean)

    成否



241
242
243
244
245
246
# File 'lib/al_rdbw_sqlite.rb', line 241

def commit()
  return false  if ! @flag_transaction
  get_handle().execute( "commit transaction;" )
  @flag_transaction = false
  return true
end

#delete(table, where_cond) ⇒ Hash

delete文の発行ヘルパー

Parameters:

  • table (String)

    テーブル名

  • where_cond (Hash)

    where条件

Returns:

  • (Hash)

    結果のHash



213
214
215
216
217
218
219
# File 'lib/al_rdbw_sqlite.rb', line 213

def delete( table, where_cond )
  (where, wval) = make_where_condition( where_cond )
  sql = "delete from #{table} where #{where};"
  get_handle().execute( sql, wval )

  return { :cmdtuples=>handle.changes() }
end

#execute(sql, var = []) ⇒ Hash Also known as: exec

Note:

任意SQLの実行

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

Parameters:

  • sql (String)

    SQL文

  • var (Array) (defaults to: [])

    パラメータクエリ用変数

Returns:

  • (Hash)

    結果



42
43
44
45
46
47
# File 'lib/al_rdbw_sqlite.rb', line 42

def execute( sql, var = [] )
  get_handle().execute( sql, var )
  ret = { :cmdtuples=>handle.changes(),
          :insert_id=>handle.last_insert_row_id() }
  return ret
end

#insert(table, values) ⇒ Hash

insert文の発行ヘルパー

Parameters:

  • table (String)

    テーブル名

  • values (Hash)

    insertする値のhash

Returns:

  • (Hash)

    結果のHash



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/al_rdbw_sqlite.rb', line 144

def insert( table, values )
  col = ""
  plh = ""
  val = []
  values.each do |k,v|
    col << "#{k},"
    plh << "?,"
    case v
    when Array
      val << v.join( ',' )
    when String, Integer, NilClass
      val << v
    when Time
      val << v.strftime("%Y-%m-%d %H:%M:%S")
    else
      val << v.to_s
    end
  end
  col.chop!
  plh.chop!

  sql = "insert into #{table} (#{col}) values (#{plh});"
  handle = get_handle()
  handle.execute( sql, val )

  return { :cmdtuples=>handle.changes(), :insert_id=>handle.last_insert_row_id() }
end

#open_connectionObject

RDBとのコネクションを開始する



23
24
25
26
27
28
29
30
# File 'lib/al_rdbw_sqlite.rb', line 23

def open_connection()
  return false  if ! @dsn

  @handle = SQLite3::Database.new( @dsn )
  @handle.type_translation = true
  @handle.busy_timeout( 30000 )
  @dsn = nil
end

#rollbackBoolean

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

Returns:

  • (Boolean)

    成否



254
255
256
257
258
259
# File 'lib/al_rdbw_sqlite.rb', line 254

def rollback()
  return false  if ! @flag_transaction
  get_handle().execute( "rollback transaction;" )
  @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=?;", [2] )
 use Hash
  select( "select * from t1 _WHERE_;",
    { :id=>2, :age=>nil, "name like"=>"a%" } )

Parameters:

  • sql (String)

    SQL文

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

    where条件

Returns:

  • (Array<Hash>)

    結果の配列



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
# File 'lib/al_rdbw_sqlite.rb', line 65

def select( sql, where_cond = nil )
  case where_cond
  when NilClass
    @result = get_handle().prepare( sql ).execute()

  when Array
    @result = get_handle().prepare( sql ).execute( 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().prepare( "#{s[0]} where #{where} #{s[1]}" ).execute( val )

  when String
    sql.sub!( '_WHERE_', "where #{where_cond}" )
    @result = get_handle().prepare( sql ).execute()

  else
    raise "where_cond error in AlRdbwSqlite#select()"
  end

  # get field name
  @fields = @result.columns.map {|field| field.to_sym }

  return select_next()  if @select_fetch_mode == :ROW

  # get data all
  ret = []
  case @select_data_type
  when :ARRAY         # Array<Array>で返す
    @result.each {|row| ret << row }

  else                # Array<Hash>で返す(標準)
    @result.each {|row| ret << [@fields, row].transpose.to_h }
  end
  @result.close()

  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)

    結果



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/al_rdbw_sqlite.rb', line 119

def select_next()
  return nil  if @result.closed?
  row = @result.next()
  if !row
    @result.close()
    return nil
  end

  case @select_data_type
  when :ARRAY
    return row

  else
    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_sqlite.rb', line 43

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

#transactionBoolean

トランザクション開始

Returns:

  • (Boolean)

    成否



227
228
229
230
231
# File 'lib/al_rdbw_sqlite.rb', line 227

def transaction()
  return false  if @flag_transaction
  get_handle().execute( "begin transaction;" )
  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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/al_rdbw_sqlite.rb', line 181

def update( table, values, where_cond )
  columns = ""
  val = []
  values.each do |k,v|
    columns << "#{k}=?,"
    case v
    when Array
      val << v.join( ',' )
    when String, Integer, NilClass
      val << v
    else
      val << v.to_s
    end
  end
  columns.chop!

  (where, wval) = make_where_condition( where_cond )

  sql = "update #{table} set #{columns} where #{where};"
  get_handle().execute( sql, val + wval )

  return { :cmdtuples=>handle.changes() }
end