Class: Mysql2wrapper::Client
- Inherits:
-
Object
- Object
- Mysql2wrapper::Client
- Defined in:
- lib/mysql2wrapper/client.rb
Defined Under Namespace
Classes: UpdateAllClass
Constant Summary collapse
- UPDATE_ALL =
UpdateAllClass- QUERY_BASE_COLOR =
35- QUERY_SPECIAL_COLOR =
31- MULTIPLE_INSERT_DEFAULT =
100
Instance Attribute Summary collapse
-
#affected_rows ⇒ Object
readonly
Returns the value of attribute affected_rows.
-
#client ⇒ Object
Returns the value of attribute client.
-
#config ⇒ Object
Returns the value of attribute config.
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
-
.config_from_yml(yml_path, environment, db_server_name = nil) ⇒ Object
db_server_nameはDB名そのもの(複数DB対応).
- .make_config_key_symbol(config) ⇒ Object
- .query(client, sql_query, logger = nil, color = QUERY_BASE_COLOR) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #count(table_name, key_name = '*') ⇒ Object
- #dump ⇒ Object
- #escape(str) ⇒ Object
-
#initialize(config, _logger = Logger.new(STDOUT)) ⇒ Client
constructor
A new instance of Client.
- #insert(table_name, data, multiple_insert_by = MULTIPLE_INSERT_DEFAULT) ⇒ Object
- #proceed_value(value) ⇒ Object
- #query(str, color = QUERY_BASE_COLOR) ⇒ Object
- #sanitize(str) ⇒ Object
- #stop_logging ⇒ Object
- #transaction(&proc) ⇒ Object
-
#update(table_name, hash, where) ⇒ Object
where は ‘WHERE’を付けずに指定 全行updateを使用するシチュエーションはほぼ0に 近いと思われるので、簡単には実行できなくしてます 実際に実行する際はwhereにUpdateAllを引数とします client.update ‘test01’,:v_int1=>3,Mysql2wrapper::Client::UPDATE_ALL.
- #update_all_flag ⇒ Object
Constructor Details
#initialize(config, _logger = Logger.new(STDOUT)) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mysql2wrapper/client.rb', line 19 def initialize(config,_logger=Logger.new(STDOUT)) self.logger = _logger if self.logger self.logger.info "mysql2 client created with #{config.inspect}" end self.client = Mysql2::Client.new(config) self.config = config # サーバが古いので一応問題あるけど以下の方向で # http://kennyqi.com/archives/61.html self.class.query(self.client,"SET NAMES 'utf8'",self.logger) self.class.query(self.client,"SET SQL_AUTO_IS_NULL=0",self.logger) self.class.query(self.client,"SET SQL_MODE=STRICT_ALL_TABLES",self.logger) end |
Instance Attribute Details
#affected_rows ⇒ Object (readonly)
Returns the value of attribute affected_rows.
8 9 10 |
# File 'lib/mysql2wrapper/client.rb', line 8 def affected_rows @affected_rows end |
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/mysql2wrapper/client.rb', line 7 def client @client end |
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/mysql2wrapper/client.rb', line 7 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/mysql2wrapper/client.rb', line 7 def logger @logger end |
Class Method Details
.config_from_yml(yml_path, environment, db_server_name = nil) ⇒ Object
db_server_nameはDB名そのもの(複数DB対応)
194 195 196 197 198 199 200 201 202 |
# File 'lib/mysql2wrapper/client.rb', line 194 def self.config_from_yml(yml_path,environment,db_server_name=nil) db_config = YAML.load_file(yml_path)[environment] if db_server_name db_config = self.make_config_key_symbol(db_config[db_server_name]) else db_config = self.make_config_key_symbol(db_config) end db_config end |
.make_config_key_symbol(config) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/mysql2wrapper/client.rb', line 74 def self.make_config_key_symbol(config) new_config = {} config.each do |key,value| new_config[key.to_sym] = value end config = new_config end |
.query(client, sql_query, logger = nil, color = QUERY_BASE_COLOR) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mysql2wrapper/client.rb', line 33 def self.query(client,sql_query,logger=nil,color=QUERY_BASE_COLOR) s = Time.now ret = client.query(sql_query) e = Time.now if logger # TODO too long sql shorten if sql_query.size > 1400 sql_query = sql_query[0..600] + "\n<< trimed >>\n" + sql_query[(sql_query.size - 600)..(sql_query.size - 1)] end logger.info "[QUERY] "" \e[#{color}m (#{((e-s)*1000).round(2)}ms/#{client.affected_rows}rows) #{sql_query}\e[0m" end ret end |
Instance Method Details
#close ⇒ Object
86 87 88 |
# File 'lib/mysql2wrapper/client.rb', line 86 def close self.client.close if self.client end |
#count(table_name, key_name = '*') ⇒ Object
82 83 84 |
# File 'lib/mysql2wrapper/client.rb', line 82 def count(table_name,key_name='*') query("SELECT COUNT(#{escape(key_name)}) AS cnt FROM #{escape(table_name)}").first['cnt'] end |
#dump ⇒ Object
51 52 53 |
# File 'lib/mysql2wrapper/client.rb', line 51 def dump "#{self.config.inspect}\n#{self.client.pretty_inspect}" end |
#escape(str) ⇒ Object
90 91 92 |
# File 'lib/mysql2wrapper/client.rb', line 90 def escape(str) self.client.escape(str) end |
#insert(table_name, data, multiple_insert_by = MULTIPLE_INSERT_DEFAULT) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/mysql2wrapper/client.rb', line 153 def insert(table_name,data,multiple_insert_by=MULTIPLE_INSERT_DEFAULT) @affected_rows = 0 # 一応リセットしとくか、、、 affected_rows_total = 0 query = "INSERT INTO `#{escape(table_name)}`" _datas = data.clone case _datas when Array ; when Hash _datas = [_datas] else raise ArgumentError, "data must be Array or Hash" end return nil if _datas.size == 0 # TODO affected_rows by multiple _datas.each_slice(multiple_insert_by).each do |rows| query = "INSERT INTO `\#{escape(table_name)}`\n(\#{rows.first.keys.map{|o|\"`\#{escape(o.to_s)}`\"}.join(',')})\nVALUES\n\#{\nrows.map do |row|\n\"(\#{\n row.map do |key,value|\n proceed_value(value)\n end.join(',')\n})\"\nend.join(',')\n}\n" self.query(query.chomp) affected_rows_total += self.client.affected_rows end @affected_rows = affected_rows_total end |
#proceed_value(value) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mysql2wrapper/client.rb', line 126 def proceed_value(value) case value when Proc "'#{escape(value.call.to_s)}'" when nil "NULL" when TrueClass,FalseClass if value "'1'" else "'0'" end when Time,DateTime "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'" when Date "'#{value.strftime("%Y-%m-%d")}'" else s = value s = s.to_s unless s.kind_of?(String) if s.respond_to?(:function_sql?) && s.function_sql? "#{value.to_s}" else "'#{escape(value.to_s)}'" end end end |
#query(str, color = QUERY_BASE_COLOR) ⇒ Object
55 56 57 58 59 |
# File 'lib/mysql2wrapper/client.rb', line 55 def query(str,color=QUERY_BASE_COLOR) res = self.class.query(self.client,str,self.logger,color) @affected_rows = self.client.affected_rows res end |
#sanitize(str) ⇒ Object
94 95 96 |
# File 'lib/mysql2wrapper/client.rb', line 94 def sanitize(str) escape(str) end |
#stop_logging ⇒ Object
47 48 49 |
# File 'lib/mysql2wrapper/client.rb', line 47 def stop_logging self.logger = nil end |
#transaction(&proc) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mysql2wrapper/client.rb', line 61 def transaction(&proc) raise ArgumentError, "No block was given" unless block_given? #query "SET AUTOCOMMIT=0;",QUERY_SPECIAL_COLOR query "BEGIN",QUERY_SPECIAL_COLOR begin yield query "COMMIT",QUERY_SPECIAL_COLOR rescue => e query "ROLLBACK",QUERY_SPECIAL_COLOR raise e end end |
#update(table_name, hash, where) ⇒ Object
where は ‘WHERE’を付けずに指定全行updateを使用するシチュエーションはほぼ0に近いと思われるので、簡単には実行できなくしてます実際に実行する際はwhereにUpdateAllを引数としますclient.update ‘test01’,:v_int1=>3,Mysql2wrapper::Client::UPDATE_ALL
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mysql2wrapper/client.rb', line 105 def update(table_name,hash,where) case where when String where = "WHERE #{where}" when UpdateAllClass.class where = '' else raise ArgumentError, "where must be String or UpdateAll" end query = "UPDATE `#{escape(table_name)}` SET #{ hash.map do |key,value| "`#{escape(key.to_s)}` = #{proceed_value(value)}" end.join(',') }" + where self.query(query) end |
#update_all_flag ⇒ Object
122 123 124 |
# File 'lib/mysql2wrapper/client.rb', line 122 def update_all_flag self.class::UPDATE_ALL end |