Class: AlRdbwMysql
- Inherits:
-
AlRdbw
- Object
- AlRdbw
- AlRdbwMysql
- Defined in:
- lib/al_rdbw_mysql.rb,
lib/al_persist_mysql.rb
Overview
リレーショナルデータベースラッパー MySQL版
Instance Method Summary (collapse)
-
- (AlPersistMysql) [](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文の発行ヘルパー.
-
- (AlPersistMysql) 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
- (AlPersistMysql) [](tname)
tableを指定して、Persistオブジェクトを生成 syntax sugar
52 53 54 |
# File 'lib/al_persist_mysql.rb', line 52 def []( tname ) return AlPersistMysql.new( self, tname ) end |
- (Boolean) commit
実装中。トランザクションがSQLレベルで失敗する条件をテストして返り値に反映する
トランザクションコミット
249 250 251 252 253 254 |
# File 'lib/al_rdbw_mysql.rb', line 249 def commit() return false if ! @flag_transaction get_handle().query( "commit;" ) @flag_transaction = false return true end |
- (Hash) delete(table, where_cond)
delete文の発行ヘルパー
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/al_rdbw_mysql.rb', line 215 def delete( table, where_cond ) (where, wval) = make_where_condition( where_cond ) sql = "delete from #{table} where #{where};" stmt = get_handle().prepare( sql ) stmt.execute( *wval ) ret = {} ret[:cmdtuples] = stmt.affected_rows() stmt.close() return ret end |
- (Hash) insert(table, values)
insert文の発行ヘルパー
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_mysql.rb', line 144 def insert( table, values ) col = "" plh = "" val = [] values.each do |k,v| col << "#{k}," plh << "?," if v.class == Array val << v.join( ',' ) else val << v end end col.chop! plh.chop! sql = "insert into #{table} (#{col}) values (#{plh});" stmt = get_handle().prepare( sql ) stmt.execute( *val ) ret = {} ret[:cmdtuples] = stmt.affected_rows() stmt.close() return ret end |
- (Object) open_connection
conn_strは、他では文字列だが、MySQLではHashである。
RDBサーバとのコネクションを開始する
26 27 28 29 30 31 |
# File 'lib/al_rdbw_mysql.rb', line 26 def open_connection() return false if ! @conn_str @handle = Mysql::connect( @conn_str[:host], @conn_str[:user], @conn_str[:passwd], @conn_str[:db], @conn_str[:port], @conn_str[:sock], @conn_str[:flag] ) @conn_str = nil end |
- (Boolean) rollback
トランザクションロールバック
262 263 264 265 266 267 |
# File 'lib/al_rdbw_mysql.rb', line 262 def rollback() return false if ! @flag_transaction get_handle().query( "rollback;" ) @flag_transaction = false return true end |
- (Array<Hash>) run(sql, var = [])
任意SQLの実行
41 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 67 68 69 |
# File 'lib/al_rdbw_mysql.rb', line 41 def run( sql, var = [] ) ret = [] if var.empty? return get_handle().query( sql ) end stmt = get_handle().prepare( sql ) stmt.execute( *var ) fields = stmt.().fetch_fields() stmt.each do |datas| idx = 0 r = {} datas.each do |d| if d.class == Mysql::Time r[ fields[idx].name.to_sym ] = Time.local( d.year, d.month, d.day, d.hour, d.minute, d.second ) else r[ fields[idx].name.to_sym ] = d end idx += 1 end ret << r end stmt.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%" } )
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 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/al_rdbw_mysql.rb', line 86 def select( sql, where_cond = nil ) case where_cond when NilClass stmt = get_handle().prepare( sql ) stmt.execute() when Array stmt = get_handle().prepare( sql ) stmt.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 ) stmt = get_handle().prepare( "#{s[0]} where #{where} #{s[1]}" ) stmt.execute( *val ) else raise "where_cond error in select()" end # # データの取り出しとデータタイプの変換 # TODO: if type is bool? # ret = [] fields = stmt.().fetch_fields() stmt.each do |datas| idx = 0 r = {} datas.each do |d| case d when Mysql::Time r[ fields[idx].name.to_sym ] = Time.local( d.year, d.month, d.day, d.hour, d.minute, d.second ) when String d.force_encoding( AL_CHARSET ) r[ fields[idx].name.to_sym ] = d else r[ fields[idx].name.to_sym ] = d end idx += 1 end ret << r end stmt.close() return ret end |
- (AlPersistMysql) table(tname, pkey = nil)
tableを指定して、Persistオブジェクトを生成
42 43 44 |
# File 'lib/al_persist_mysql.rb', line 42 def table( tname, pkey = nil ) return AlPersistMysql.new( self, tname, pkey ) end |
- (Boolean) transaction
トランザクション開始
235 236 237 238 239 |
# File 'lib/al_rdbw_mysql.rb', line 235 def transaction() return false if @flag_transaction get_handle().query( "begin;" ) return @flag_transaction = true end |
- (Hash) update(table, values, where_cond)
update文の発行ヘルパー
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/al_rdbw_mysql.rb', line 180 def update( table, values, where_cond ) columns = "" val = [] values.each do |k,v| columns << "#{k}=?," if v.class == Array val << v.join( ',' ) else val << v end end columns.chop! (where, wval) = make_where_condition( where_cond ) sql = "update #{table} set #{columns} where #{where};" val += wval stmt = get_handle().prepare( sql ) stmt.execute( *val ) ret = {} ret[:cmdtuples] = stmt.affected_rows() stmt.close() return ret end |