Class: AlRdbwSqlite
- Inherits:
-
AlRdbw
- Object
- AlRdbw
- AlRdbwSqlite
- Defined in:
- lib/al_rdbw_sqlite.rb,
lib/al_persist_sqlite.rb
Overview
リレーショナルデータベースラッパー SQLite版
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
トランザクションロールバック.
-
- (Array<Hash>) 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_sqlite.rb', line 52 def []( tname ) return AlPersistSqlite.new( self, tname ) end |
- (Boolean) commit
実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する
トランザクションコミット
232 233 234 235 236 237 |
# File 'lib/al_rdbw_sqlite.rb', line 232 def commit() return false if ! @flag_transaction get_handle().execute( "commit transaction;" ) @flag_transaction = false return true end |
- (Hash) delete(table, where_cond)
delete文の発行ヘルパー
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/al_rdbw_sqlite.rb', line 201 def delete( table, where_cond ) (where, wval) = make_where_condition( where_cond ) sql = "delete from #{table} where #{where};" get_handle().execute( sql, wval ) ret = {} ret[:cmdtuples] = get_handle().changes() return ret end |
- (Hash) insert(table, values)
insert文の発行ヘルパー
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/al_rdbw_sqlite.rb', line 129 def insert( table, values ) col = "" plh = "" val = [] values.each do |k,v| col << "#{k}," plh << "?," case v when Array val << v.join( ',' ) when String, Fixnum, NilClass val << v else val << v.to_s end end col.chop! plh.chop! sql = "insert into #{table} (#{col}) values (#{plh});" get_handle().execute( sql, val ) ret = {} ret[:cmdtuples] = get_handle().changes() return ret end |
- (Object) open_connection
コネクションエラー時のエラー処理をどこでするかを明確化して実装しなければならない。 下請けにするライブラリの都合などを鑑みて、全体統一を図る必要があるだろう。 それには、現在のSQLiteとPostgreSQLだけでは、サンプルが足りないように思う。
RDBとのコネクションを開始する
26 27 28 29 30 31 32 |
# File 'lib/al_rdbw_sqlite.rb', line 26 def open_connection() return false if ! @conn_str @handle = SQLite3::Database.new( @conn_str ) @handle.type_translation = true @conn_str = nil end |
- (Boolean) rollback
トランザクションロールバック
245 246 247 248 249 250 |
# File 'lib/al_rdbw_sqlite.rb', line 245 def rollback() return false if ! @flag_transaction get_handle().execute( "rollback transaction;" ) @flag_transaction = false return true end |
- (Array<Hash>) run(sql, *var)
任意SQLの実行
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/al_rdbw_sqlite.rb', line 42 def run( sql, *var ) ret = [] # SQLをプリペアドクエリで実行 stmt = get_handle().prepare( sql ) result = stmt.execute( var ) # アトリビュート用キーの準備 keys = [] result.columns.each { |k| keys << k.to_sym } # 戻り値用Hashの生成 result.each do |row| h = {} keys.each do |k| v = row.shift v.force_encoding( AL_CHARSET ) if v.respond_to?('force_encoding') h[k] = v end ret << h end result.close return ret end |
- (Array<Hash>) select(sql, where_cond = nil)
select文の発行ヘルパー
use Hash
select( "select * from t1 _WHERE_;", { :id=>2, :age=>nil, "name like"=>"a%" } )
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 |
# File 'lib/al_rdbw_sqlite.rb', line 83 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 ) else raise "where_cond error in select()" end # アトリビュート用キーの準備 keys = [] result.columns.each { |k| keys << k.to_sym } # 戻り値用Hashの生成 ret = [] result.each do |r| a = {} keys.each do |k| v = r.shift v.force_encoding( AL_CHARSET ) if v.respond_to?('force_encoding') a[k] = v end ret << a end result.close return ret end |
- (AlPersistSqlite) table(tname, pkey = nil)
tableを指定して、Persistオブジェクトを生成
42 43 44 |
# File 'lib/al_persist_sqlite.rb', line 42 def table( tname, pkey = nil ) return AlPersistSqlite.new( self, tname, pkey ) end |
- (Boolean) transaction
トランザクション開始
218 219 220 221 222 |
# File 'lib/al_rdbw_sqlite.rb', line 218 def transaction() return false if @flag_transaction get_handle().execute( "begin transaction;" ) return @flag_transaction = true end |
- (Hash) update(table, values, where_cond)
update文の発行ヘルパー
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/al_rdbw_sqlite.rb', line 166 def update( table, values, where_cond ) columns = "" val = [] values.each do |k,v| columns << "#{k}=?," case v when Array val << v.join( ',' ) when String, Fixnum, 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 ) ret = {} ret[:cmdtuples] = get_handle().changes() return ret end |