public
Rubygem
Description: An ActiveRecord to the DBSlayer lightweight proxy/pooling layer (http://code.nytimes.com/projects/dbslayer)
Clone URL: git://github.com/harrisj/activerecord-dbslayer-adapter.git
Search Repo:
Initial version of the Dbslayer ActiveRecord adapter

git-svn-id: 
svn://newsprojects.nytimes.com/newsdev/gems/activerecord_dbslayer_adapter@
4379 ae54da50-7589-804d-96e1-6ee4b2538e53
harrisj (author)
Wed Apr 30 14:57:46 -0700 2008
commit  6d31b094f14ec212ef068aa0347500620959d2b4
tree    3c529544593a4a0123463316f5eb1bd98b97d678
...
 
 
 
 
...
1
2
3
4
0
@@ -0,0 +1,4 @@
0
+== 0.0.1 2008-04-15
0
+
0
+* 1 major enhancement:
0
+ * Initial release
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2008 FIXME full name
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -0,0 +1,25 @@
0
+History.txt
0
+License.txt
0
+Manifest.txt
0
+README.txt
0
+Rakefile
0
+config/hoe.rb
0
+config/requirements.rb
0
+lib/activerecord_dbslayer_adapter.rb
0
+lib/activerecord_dbslayer_adapter/version.rb
0
+log/debug.log
0
+script/console
0
+script/destroy
0
+script/generate
0
+script/txt2html
0
+setup.rb
0
+tasks/deployment.rake
0
+tasks/environment.rake
0
+tasks/website.rake
0
+test/test_activerecord_dbslayer_adapter.rb
0
+test/test_helper.rb
0
+website/index.html
0
+website/index.txt
0
+website/javascripts/rounded_corners_lite.inc.js
0
+website/stylesheets/screen.css
0
+website/template.html.erb
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
0
@@ -0,0 +1,48 @@
0
+= activerecord_dbslayer_adapter
0
+
0
+* FIX (url)
0
+
0
+== DESCRIPTION:
0
+
0
+FIX (describe your package)
0
+
0
+== FEATURES/PROBLEMS:
0
+
0
+* FIX (list of features or problems)
0
+
0
+== SYNOPSIS:
0
+
0
+ FIX (code sample of usage)
0
+
0
+== REQUIREMENTS:
0
+
0
+* FIX (list of requirements)
0
+
0
+== INSTALL:
0
+
0
+* FIX (sudo gem install, anything else)
0
+
0
+== LICENSE:
0
+
0
+(The MIT License)
0
+
0
+Copyright (c) 2008 FIX
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+'Software'), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+require 'config/requirements'
0
+require 'config/hoe' # setup Hoe + all gem configuration
0
+
0
+Dir['tasks/**/*.rake'].each { |rake| load rake }
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
70
71
0
@@ -0,0 +1,70 @@
0
+require 'active_record/connection_adapters/dbslayer_adapter'
0
+
0
+AUTHOR = 'Jacob Harris' # can also be an array of Authors
0
+EMAIL = "jharris@nytimes.com"
0
+DESCRIPTION = "An ActiveRecord adapter to DBSlayer"
0
+GEM_NAME = 'activerecord_dbslayer_adapter' # what ppl will type to install your gem
0
+RUBYFORGE_PROJECT = 'activerecord_dbslayer_adapter' # The unix name for your project
0
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
0
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
0
+
0
+@config_file = "~/.rubyforge/user-config.yml"
0
+@config = nil
0
+RUBYFORGE_USERNAME = "unknown"
0
+def rubyforge_username
0
+ unless @config
0
+ begin
0
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
0
+ rescue
0
+ puts <<-EOS
0
+ERROR: No rubyforge config file found: #{@config_file}
0
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
0
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
0
+ EOS
0
+ exit
0
+ end
0
+ end
0
+ RUBYFORGE_USERNAME.replace @config["username"]
0
+end
0
+
0
+
0
+REV = nil
0
+# UNCOMMENT IF REQUIRED:
0
+# REV = YAML.load(`svn info`)['Revision']
0
+VERS = ActiveRecord::ConnectionAdapters::DbslayerAdapter::VERSION + (REV ? ".#{REV}" : "")
0
+RDOC_OPTS = ['--quiet', '--title', 'activerecord_dbslayer_adapter documentation',
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--main", "README",
0
+ "--inline-source"]
0
+
0
+class Hoe
0
+ def extra_deps
0
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
0
+ @extra_deps
0
+ end
0
+end
0
+
0
+# Generate all the Rake tasks
0
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
0
+$hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
+ p.developer(AUTHOR, EMAIL)
0
+ p.description = DESCRIPTION
0
+ p.summary = DESCRIPTION
0
+ p.url = HOMEPATH
0
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
0
+ p.test_globs = ["test/**/test_*.rb"]
0
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
0
+
0
+ # == Optional
0
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
0
+
0
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
0
+
0
+end
0
+
0
+CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
0
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
0
+$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
0
+$hoe.rsync_args = '-av --delete --ignore-errors'
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -0,0 +1,15 @@
0
+require 'fileutils'
0
+include FileUtils
0
+
0
+require 'rubygems'
0
+%w[rake hoe newgem rubigen mocha active_support active_record].each do |req_gem|
0
+ begin
0
+ require req_gem
0
+ rescue LoadError
0
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
0
+ puts "Installation: gem install #{req_gem} -y"
0
+ exit
0
+ end
0
+end
0
+
0
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
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
135
136
137
138
139
140
141
142
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
170
171
172
173
174
175
176
177
178
179
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
0
@@ -0,0 +1,379 @@
0
+require 'active_record/connection_adapters/abstract_adapter'
0
+require 'active_record/connection_adapters/dbslayer_connection'
0
+require 'active_record/connection_adapters/mysql_adapter'
0
+
0
+module ActiveRecord
0
+ class Base
0
+ # Establishes a connection to the database that's used by all Active Record objects.
0
+ def self.dbslayer_connection(config) # :nodoc:
0
+ config = config.symbolize_keys
0
+ host = config[:host]
0
+ port = config[:port]
0
+
0
+ connection = ConnectionAdapters::DbslayerConnection.new(host, port)
0
+ ConnectionAdapters::DbslayerAdapter.new(connection, logger, [host, port], config)
0
+ end
0
+ end
0
+
0
+ module ConnectionAdapters
0
+ class DbslayerColumn < MysqlColumn #:nodoc:
0
+ # def extract_default(default)
0
+ # if type == :binary || type == :text
0
+ # if default.blank?
0
+ # default
0
+ # else
0
+ # raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
0
+ # end
0
+ # elsif missing_default_forged_as_empty_string?(default)
0
+ # nil
0
+ # else
0
+ # super
0
+ # end
0
+ # end
0
+
0
+ private
0
+ def simplified_type(field_type)
0
+ return :boolean if DbslayerAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)")
0
+ return :string if field_type =~ /enum/i
0
+ super
0
+ end
0
+
0
+ # MySQL misreports NOT NULL column default when none is given.
0
+ # We can't detect this for columns which may have a legitimate ''
0
+ # default (string) but we can for others (integer, datetime, boolean,
0
+ # and the rest).
0
+ #
0
+ # Test whether the column has default '', is not null, and is not
0
+ # a type allowing default ''.
0
+ # def missing_default_forged_as_empty_string?(default)
0
+ # type != :string && !null && default == ''
0
+ # end
0
+ end
0
+
0
+ # The DbslayerAdapter is an adapter to use Rails with the DBSlayer
0
+ #
0
+ # Options:
0
+ #
0
+ # * <tt>:host</tt> -- Defaults to localhost
0
+ # * <tt>:port</tt> -- Defaults to 3306
0
+ #
0
+ # Like the MySQL adapter: by default, the MysqlAdapter will consider all columns of type tinyint(1)
0
+ # as boolean. If you wish to disable this emulation (which was the default
0
+ # behavior in versions 0.13.1 and earlier) you can add the following line
0
+ # to your environment.rb file:
0
+ #
0
+ # ActiveRecord::ConnectionAdapters::DbslayerAdapter.emulate_booleans = false
0
+ class DbslayerAdapter < MysqlAdapter
0
+ VERSION = '0.2.0'
0
+
0
+ def initialize(connection, logger, connection_options, config)
0
+ super(connection, logger, connection_options, config)
0
+ ActiveRecord::Base.allow_concurrency = true
0
+ end
0
+
0
+ def adapter_name #:nodoc:
0
+ 'DBSlayer (MySQL)'
0
+ end
0
+
0
+ # def supports_migrations? #:nodoc:
0
+ # true
0
+ # end
0
+ #
0
+ # def native_database_types #:nodoc:
0
+ # {
0
+ # :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
0
+ # :string => { :name => "varchar", :limit => 255 },
0
+ # :text => { :name => "text" },
0
+ # :integer => { :name => "int", :limit => 11 },
0
+ # :float => { :name => "float" },
0
+ # :decimal => { :name => "decimal" },
0
+ # :datetime => { :name => "datetime" },
0
+ # :timestamp => { :name => "datetime" },
0
+ # :time => { :name => "time" },
0
+ # :date => { :name => "date" },
0
+ # :binary => { :name => "blob" },
0
+ # :boolean => { :name => "tinyint", :limit => 1 }
0
+ # }
0
+ # end
0
+ #
0
+ #
0
+ # # QUOTING ==================================================
0
+ #
0
+ # def quote(value, column = nil)
0
+ # if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
0
+ # s = column.class.string_to_binary(value).unpack("H*")[0]
0
+ # "x'#{s}'"
0
+ # elsif value.kind_of?(BigDecimal)
0
+ # "'#{value.to_s("F")}'"
0
+ # else
0
+ # super
0
+ # end
0
+ # end
0
+ #
0
+ # def quote_column_name(name) #:nodoc:
0
+ # "`#{name}`"
0
+ # end
0
+ #
0
+ # def quote_table_name(name) #:nodoc:
0
+ # quote_column_name(name).gsub('.', '`.`')
0
+ # end
0
+ #
0
+ # def quote_string(string) #:nodoc:
0
+ # @connection.quote(string)
0
+ # end
0
+ #
0
+ # def quoted_true
0
+ # "1"
0
+ # end
0
+ #
0
+ # def quoted_false
0
+ # "0"
0
+ # end
0
+
0
+ # REFERENTIAL INTEGRITY ====================================
0
+
0
+ def disable_referential_integrity(&block) #:nodoc:
0
+ #FIXME: I CAN'T LET YOU DO THIS
0
+ # old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
0
+ #
0
+ # begin
0
+ # update("SET FOREIGN_KEY_CHECKS = 0")
0
+ # yield
0
+ # ensure
0
+ # update("SET FOREIGN_KEY_CHECKS = #{old}")
0
+ # end
0
+ end
0
+
0
+ # CONNECTION MANAGEMENT ====================================
0
+
0
+ def active?
0
+ stats = @connection.mysql_stats
0
+ !stats.nil? && !stats.empty?
0
+ rescue
0
+ false
0
+ end
0
+
0
+ def reconnect!
0
+ # DO NOTHING, we connect on the request
0
+ end
0
+
0
+ def disconnect!
0
+ # DO NOTHING, we connect on the request
0
+ end
0
+
0
+
0
+ # DATABASE STATEMENTS ======================================
0
+
0
+ def select_rows(sql, name = nil)
0
+ result = execute(sql, name)
0
+ result.rows
0
+ end
0
+
0
+ def execute(sql, name = nil) #:nodoc:
0
+ log(sql, name) {
0
+ @connection.execute(sql)
0
+ }
0
+ end
0
+
0
+ # def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
0
+ # super sql, name
0
+ # id_value || @connection.insert_id
0
+ # end
0
+ #
0
+ # def update_sql(sql, name = nil) #:nodoc:
0
+ # super
0
+ # @connection.affected_rows
0
+ # end
0
+
0
+ def begin_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+ # execute "BEGIN"
0
+ # rescue Exception
0
+ # Transactions aren't supported
0
+ end
0
+
0
+ def commit_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+
0
+ # execute "COMMIT"
0
+ # rescue Exception
0
+ # Transactions aren't supported
0
+ end
0
+
0
+ def rollback_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+
0
+ # execute "ROLLBACK"
0
+ # rescue Exception
0
+ # Transactions aren't supported
0
+ end
0
+
0
+ # SCHEMA STATEMENTS ========================================
0
+
0
+ # def structure_dump #:nodoc:
0
+ # if supports_views?
0
+ # sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
0
+ # else
0
+ # sql = "SHOW TABLES"
0
+ # end
0
+ #
0
+ # select_all(sql).inject("") do |structure, table|
0
+ # table.delete('Table_type')
0
+ # structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
0
+ # end
0
+ # end
0
+ #
0
+ # def recreate_database(name) #:nodoc:
0
+ # drop_database(name)
0
+ # create_database(name)
0
+ # end
0
+
0
+ # Create a new MySQL database with optional :charset and :collation.
0
+ # Charset defaults to utf8.
0
+ #
0
+ # Example:
0
+ # create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
0
+ # create_database 'matt_development'
0
+ # create_database 'matt_development', :charset => :big5
0
+ # def create_database(name, options = {})
0
+ # if options[:collation]
0
+ # execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
0
+ # else
0
+ # execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
0
+ # end
0
+ # end
0
+ #
0
+ # def drop_database(name) #:nodoc:
0
+ # execute "DROP DATABASE IF EXISTS `#{name}`"
0
+ # end
0
+ #
0
+ # def current_database
0
+ # select_value 'SELECT DATABASE() as db'
0
+ # end
0
+
0
+ # Returns the database character set.
0
+ # def charset
0
+ # if @charset.nil?
0
+ # @charset = show_variable 'character_set_database'
0
+ # end
0
+ #
0
+ # @charset
0
+ # end
0
+ #
0
+ # # Returns the database collation strategy.
0
+ # def collation
0
+ # if @collation.nil?
0
+ # @collation = show_variable 'collation_database'
0
+ # end
0
+ # @collation
0
+ # end
0
+
0
+ def tables(name = nil) #:nodoc:
0
+ tables = []
0
+ execute("SHOW TABLES", name).rows.each { |row| tables << row[0] }
0
+ tables
0
+ end
0
+
0
+ # def drop_table(table_name, options = {})
0
+ # super(table_name, options)
0
+ # end
0
+
0
+ def indexes(table_name, name = nil)#:nodoc:
0
+ indexes = []
0
+ current_index = nil
0
+ execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name).rows.each do |row|
0
+ if current_index != row[2]
0
+ next if row[2] == "PRIMARY" # skip the primary key
0
+ current_index = row[2]
0
+ indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
0
+ end
0
+
0
+ indexes.last.columns << row[4]
0
+ end
0
+ indexes
0
+ end
0
+
0
+ def columns(table_name, name = nil)#:nodoc:
0
+ sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
0
+ columns = []
0
+ execute(sql, name).rows.each { |row| columns << DbslayerColumn.new(row[0], row[4], row[1], row[2] == "YES") }
0
+ columns
0
+ end
0
+
0
+ # def create_table(table_name, options = {}) #:nodoc:
0
+ # super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
0
+ # end
0
+ #
0
+ # def rename_table(table_name, new_name)
0
+ # execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
0
+ # end
0
+ #
0
+ # def change_column_default(table_name, column_name, default) #:nodoc:
0
+ # current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
0
+ #
0
+ # execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
0
+ # end
0
+ #
0
+ # def change_column(table_name, column_name, type, options = {}) #:nodoc:
0
+ # unless options_include_default?(options)
0
+ # if column = columns(table_name).find { |c| c.name == column_name.to_s }
0
+ # options[:default] = column.default
0
+ # else
0
+ # raise "No such column: #{table_name}.#{column_name}"
0
+ # end
0
+ # end
0
+ #
0
+ # change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
0
+ # add_column_options!(change_column_sql, options)
0
+ # execute(change_column_sql)
0
+ # end
0
+ #
0
+ # def rename_column(table_name, column_name, new_column_name) #:nodoc:
0
+ # current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
0
+ # execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
0
+ # end
0
+
0
+ # SHOW VARIABLES LIKE 'name'
0
+ def show_variable(name)
0
+ variables = select_all("SHOW VARIABLES LIKE '#{name}'")
0
+ variables.first['Value'] unless variables.empty?
0
+ end
0
+
0
+ # Returns a table's primary key and belonging sequence.
0
+ def pk_and_sequence_for(table) #:nodoc:
0
+ keys = []
0
+ execute("describe #{quote_table_name(table)}").each_hash do |h|
0
+ keys << h["Field"]if h["Key"] == "PRI"
0
+ end
0
+ keys.length == 1 ? [keys.first, nil] : nil
0
+ end
0
+
0
+ private
0
+ def connect
0
+ # By default, MySQL 'where id is null' selects the last inserted id.
0
+ # Turn this off. http://dev.rubyonrails.org/ticket/6778
0
+ ##FIXME !!! execute("SET SQL_AUTO_IS_NULL=0")
0
+ end
0
+
0
+ def select(sql, name = nil)
0
+ result = execute(sql, name)
0
+ rows = result.hash_rows
0
+ rows
0
+ end
0
+
0
+ # Executes the update statement and returns the number of rows affected.
0
+ def update_sql(sql, name = nil)
0
+ execute(sql, name).rows[0][0]
0
+ end
0
+
0
+ def supports_views?
0
+ ## get mysql version
0
+ version[0] >= 5
0
+ end
0
+
0
+ def version
0
+ @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
0
+ end
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
0
@@ -0,0 +1,125 @@
0
+require 'rubygems'
0
+require 'net/http'
0
+require 'open-uri'
0
+require 'json'
0
+
0
+module ActiveRecord
0
+ module ConnectionAdapters
0
+
0
+ class DbslayerResult
0
+ def initialize(results_hash)
0
+ @hash = results_hash
0
+ end
0
+
0
+ def rows
0
+ @hash['ROWS']
0
+ end
0
+
0
+ def types
0
+ @hash['TYPES']
0
+ end
0
+
0
+ def header
0
+ @hash['HEADER']
0
+ end
0
+
0
+ ## Compatibility to the MySQL ones
0
+ def num_rows
0
+ return 0 if rows.nil?
0
+ rows.size
0
+ end
0
+
0
+ def each
0
+ return if rows.nil?
0
+ rows.each {|r| yield r }
0
+ end
0
+
0
+ def hash_rows
0
+ return [] if rows.nil?
0
+ rows.map do |row|
0
+ hash = {}
0
+ header.each_with_index do |head, i|
0
+ hash[head] = row[i]
0
+ end
0
+
0
+ hash
0
+ end
0
+ end
0
+
0
+ def each_hash
0
+ return if rows.nil?
0
+ hash_rows.each do |row|
0
+ yield row
0
+ end
0
+ end
0
+ end
0
+
0
+ class DbslayerConnection
0
+ def initialize(host='localhost', port=9090)
0
+ @host = host
0
+ @port = port
0
+ end
0
+
0
+ ##
0
+ # Executes a SQL query
0
+ def sql_query(sql)
0
+ dbslay_results = cmd_execute(:db, 'SQL' => sql)
0
+
0
+ case dbslay_results
0
+ when Hash
0
+ DbslayerResult.new(dbslay_results['RESULT'])
0
+ when Array
0
+ dbslay_results.map { |r| DbslayerResult.new(r['RESULT']) }
0
+ else
0
+ end
0
+ end
0
+
0
+ alias execute sql_query
0
+ alias query sql_query
0
+
0
+ def mysql_stats
0
+ results = cmd_execute(:db, 'STAT' => true)
0
+ results['STAT']
0
+ end
0
+
0
+ alias stat mysql_stats
0
+
0
+ def client_info
0
+ if @client_info.nil?
0
+ @client_info = cmd_execute(:db, 'CLIENT_INFO' => true)["CLIENT_INFO"]
0
+ end
0
+ @client_info
0
+ end
0
+
0
+ alias server_info client_info
0
+
0
+ def client_version_num
0
+ if @client_version.nil?
0
+ @client_version = cmd_execute(:db, 'CLIENT_VERSION' => true)["CLIENT_VERSION"]
0
+ end
0
+ @client_version
0
+ end
0
+
0
+ def server_version_num
0
+ if @server_version.nil?
0
+ @server_version = cmd_execute(:db, 'SERVER_VERSION' => true)["SERVER_VERSION"]
0
+ end
0
+ @server_version
0
+ end
0
+
0
+ private
0
+ def query_string(commands)
0
+ URI.encode