Class: AlRdbwSqlite
- 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
-
#[](tname) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成 syntax sugar.
-
#commit ⇒ Boolean
トランザクションコミット.
-
#delete(table, where_cond) ⇒ Hash
delete文の発行ヘルパー.
-
#execute(sql, var = []) ⇒ 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 Method Details
#[](tname) ⇒ AlPersistSqlite
tableを指定して、Persistオブジェクトを生成 syntax sugar
53 54 55 |
# File 'lib/al_persist_sqlite.rb', line 53 def []( tname ) return AlPersistSqlite.new( self, tname ) end |
#commit ⇒ Boolean
トランザクションコミット
実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する
240 241 242 243 244 245 |
# File 'lib/al_rdbw_sqlite.rb', line 240 def commit() return false if ! @flag_transaction get_handle().execute( "commit transaction;" ) @flag_transaction = false return true end |
#delete(table, where_cond) ⇒ Hash
delete文の発行ヘルパー
212 213 214 215 216 217 218 |
# File 'lib/al_rdbw_sqlite.rb', line 212 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
任意SQLの実行
アクションクエリの実行用。selectは、select()メソッドを使う。
41 42 43 44 45 46 |
# File 'lib/al_rdbw_sqlite.rb', line 41 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文の発行ヘルパー
143 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 |
# File 'lib/al_rdbw_sqlite.rb', line 143 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_connection ⇒ Object
RDBとのコネクションを開始する
23 24 25 26 27 28 29 |
# File 'lib/al_rdbw_sqlite.rb', line 23 def open_connection() return false if ! @dsn @handle = SQLite3::Database.new( @dsn ) @handle.busy_timeout( 30000 ) @dsn = nil end |
#rollback ⇒ Boolean
トランザクションロールバック
253 254 255 256 257 258 |
# File 'lib/al_rdbw_sqlite.rb', line 253 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文の発行ヘルパー
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 |
# File 'lib/al_rdbw_sqlite.rb', line 64 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_next ⇒ Array, ...
シングル行モード(select_fetch_mode = :ROW)の場合の次行取得
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/al_rdbw_sqlite.rb', line 118 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オブジェクトを生成
43 44 45 |
# File 'lib/al_persist_sqlite.rb', line 43 def table( tname, pkey = nil ) return AlPersistSqlite.new( self, tname, pkey ) end |
#transaction ⇒ Boolean
トランザクション開始
226 227 228 229 230 |
# File 'lib/al_rdbw_sqlite.rb', line 226 def transaction() return false if @flag_transaction get_handle().execute( "begin transaction;" ) return @flag_transaction = true end |
#update(table, values, where_cond) ⇒ Hash
update文の発行ヘルパー
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/al_rdbw_sqlite.rb', line 180 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 |