sequel-collation 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +6 -0
- data/Gemfile +11 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +21 -0
- data/lib/sequel/extensions/collation.rb +19 -0
- data/lib/sequel/extensions/collation/adapters/h2.rb +25 -0
- data/lib/sequel/extensions/collation/adapters/mysql.rb +25 -0
- data/lib/sequel/extensions/collation/version.rb +5 -0
- data/sequel-collation.gemspec +19 -0
- data/test/config.yml +14 -0
- data/test/helper.rb +45 -0
- data/test/unit/test_h2.rb +21 -0
- data/test/unit/test_mysql.rb +20 -0
- metadata +88 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Vanderbilt University
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= sequel-collation
|
2
|
+
|
3
|
+
Sequel extension to access collation information.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
require 'sequel'
|
8
|
+
Sequel.extension :collation
|
9
|
+
db = Sequel.connect("mysql2://localhost/foo?user=foo&password=bar")
|
10
|
+
db.extend(Sequel::Collation)
|
11
|
+
db.schema(:my_table)
|
12
|
+
|
13
|
+
== Contributing to sequel-collation
|
14
|
+
|
15
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
16
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
17
|
+
* Fork the project.
|
18
|
+
* Start a feature/bugfix branch.
|
19
|
+
* Commit and push until you are happy with your contribution.
|
20
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
21
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
22
|
+
|
23
|
+
== Copyright
|
24
|
+
|
25
|
+
Copyright (c) 2012 Vanderbilt University. See LICENSE.txt for
|
26
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'rake'
|
12
|
+
require 'bundler/gem_tasks'
|
13
|
+
|
14
|
+
require 'rake/testtask'
|
15
|
+
Rake::TestTask.new(:test) do |test|
|
16
|
+
test.libs << 'lib' << 'test'
|
17
|
+
test.pattern = 'test/**/test_*.rb'
|
18
|
+
test.verbose = true
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Collation
|
3
|
+
ADAPTER_MAP = {}
|
4
|
+
|
5
|
+
def self.extended(base)
|
6
|
+
db_type = base.database_type
|
7
|
+
unless ADAPTER_MAP.has_key?(db_type)
|
8
|
+
# attempt to load the adapter file
|
9
|
+
begin
|
10
|
+
Sequel.tsk_require("sequel/extensions/collation/adapters/#{db_type}")
|
11
|
+
rescue LoadError => e
|
12
|
+
warn "Sequel::Collation does not support the database type '#{db_type}'"
|
13
|
+
return
|
14
|
+
end
|
15
|
+
end
|
16
|
+
base.extend(ADAPTER_MAP[db_type])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Collation
|
3
|
+
module H2
|
4
|
+
private
|
5
|
+
def schema_parse_table(table_name, opts)
|
6
|
+
result = super(table_name, opts)
|
7
|
+
ds = metadata_dataset.
|
8
|
+
select(:COLLATION_NAME).
|
9
|
+
from(:INFORMATION_SCHEMA__COLUMNS).
|
10
|
+
filter(:LOWER.sql_function(:TABLE_NAME) => :LOWER.sql_function(table_name))
|
11
|
+
|
12
|
+
result.each do |(name, info)|
|
13
|
+
row = ds.filter(:LOWER.sql_function(:COLUMN_NAME) => :LOWER.sql_function(name.to_s)).first
|
14
|
+
if row[:collation_name] == "OFF"
|
15
|
+
info[:collate] = nil
|
16
|
+
else
|
17
|
+
info[:collate] = row[:collation_name]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
ADAPTER_MAP[:h2] = H2
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Collation
|
3
|
+
module MySQL
|
4
|
+
private
|
5
|
+
def schema_parse_table(table_name, opts)
|
6
|
+
m = output_identifier_meth(opts[:dataset])
|
7
|
+
im = input_identifier_meth(opts[:dataset])
|
8
|
+
table = SQL::Identifier.new(im.call(table_name))
|
9
|
+
table = SQL::QualifiedIdentifier.new(im.call(opts[:schema]), table) if opts[:schema]
|
10
|
+
metadata_dataset.with_sql("SHOW FULL COLUMNS FROM ?", table).map do |row|
|
11
|
+
row[:auto_increment] = true if row.delete(:Extra).to_s =~ /auto_increment/io
|
12
|
+
row[:allow_null] = row.delete(:Null) == 'YES'
|
13
|
+
row[:default] = row.delete(:Default)
|
14
|
+
row[:primary_key] = row.delete(:Key) == 'PRI'
|
15
|
+
row[:default] = nil if blank_object?(row[:default])
|
16
|
+
row[:db_type] = row.delete(:Type)
|
17
|
+
row[:type] = schema_column_type(row[:db_type])
|
18
|
+
row[:collate] = row.delete(:Collation)
|
19
|
+
[m.call(row.delete(:Field)), row]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
ADAPTER_MAP[:mysql] = MySQL
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sequel/extensions/collation/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jeremy Stephens"]
|
6
|
+
gem.email = ["jeremy.f.stephens@vanderbilt.edu"]
|
7
|
+
gem.summary = %q{Sequel extension to access collation information}
|
8
|
+
gem.description = %q{Adds collation information to data returned by a Sequel database's schema method}
|
9
|
+
gem.homepage = "http://github.com/coupler/sequel-collation"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "sequel-collation"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Sequel::Collation::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency("sequel")
|
19
|
+
end
|
data/test/config.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
mysql:
|
2
|
+
<% if RUBY_PLATFORM == "java" %>
|
3
|
+
"jdbc:mysql://localhost/test?user=test"
|
4
|
+
<% else %>
|
5
|
+
adapter: mysql2
|
6
|
+
host: localhost
|
7
|
+
port: 3306
|
8
|
+
database: test
|
9
|
+
user: test
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<% if RUBY_PLATFORM == "java" %>
|
13
|
+
h2: "jdbc:h2:mem:"
|
14
|
+
<% end %>
|
data/test/helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'sequel'
|
12
|
+
require 'erb'
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
+
Sequel.extension :collation
|
17
|
+
|
18
|
+
class Test::Unit::TestCase
|
19
|
+
@@database_config = nil
|
20
|
+
|
21
|
+
def self.database_config
|
22
|
+
if @@database_config.nil?
|
23
|
+
template = File.read(File.join(File.dirname(__FILE__), "config.yml"))
|
24
|
+
@@database_config = YAML.load(ERB.new(template).result(binding))
|
25
|
+
end
|
26
|
+
@@database_config
|
27
|
+
end
|
28
|
+
|
29
|
+
def database_config
|
30
|
+
self.class.database_config
|
31
|
+
end
|
32
|
+
|
33
|
+
def database_for(adapter, &block)
|
34
|
+
config = database_config[adapter]
|
35
|
+
if config
|
36
|
+
if block
|
37
|
+
Sequel.connect(config, &block)
|
38
|
+
else
|
39
|
+
Sequel.connect(config)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
omit("Couldn't find configuration for adapter '#{adapter}'")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestH2 < Test::Unit::TestCase
|
4
|
+
test "extending module" do
|
5
|
+
database_for("h2") do |db|
|
6
|
+
db.extend(Sequel::Collation)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
test "schema shows collation" do
|
11
|
+
database_for("h2") do |db|
|
12
|
+
db.extend(Sequel::Collation)
|
13
|
+
db.run("SET COLLATION ENGLISH")
|
14
|
+
db.create_table!(:foo) do
|
15
|
+
String :bar
|
16
|
+
end
|
17
|
+
schema = db.schema(:foo)
|
18
|
+
assert_equal "ENGLISH", schema.assoc(:bar)[1][:collate]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMysql < Test::Unit::TestCase
|
4
|
+
test "extending module" do
|
5
|
+
database_for("mysql") do |db|
|
6
|
+
db.extend(Sequel::Collation)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
test "schema shows collation" do
|
11
|
+
database_for("mysql") do |db|
|
12
|
+
db.extend(Sequel::Collation)
|
13
|
+
db.create_table!(:foo) do
|
14
|
+
String :bar, :collate => "latin1_swedish_ci"
|
15
|
+
end
|
16
|
+
schema = db.schema(:foo)
|
17
|
+
assert_equal "latin1_swedish_ci", schema.assoc(:bar)[1][:collate]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-collation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Stephens
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sequel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Adds collation information to data returned by a Sequel database's schema
|
31
|
+
method
|
32
|
+
email:
|
33
|
+
- jeremy.f.stephens@vanderbilt.edu
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .document
|
39
|
+
- .gitignore
|
40
|
+
- Gemfile
|
41
|
+
- Guardfile
|
42
|
+
- LICENSE.txt
|
43
|
+
- README.rdoc
|
44
|
+
- Rakefile
|
45
|
+
- lib/sequel/extensions/collation.rb
|
46
|
+
- lib/sequel/extensions/collation/adapters/h2.rb
|
47
|
+
- lib/sequel/extensions/collation/adapters/mysql.rb
|
48
|
+
- lib/sequel/extensions/collation/version.rb
|
49
|
+
- sequel-collation.gemspec
|
50
|
+
- test/config.yml
|
51
|
+
- test/helper.rb
|
52
|
+
- test/unit/test_h2.rb
|
53
|
+
- test/unit/test_mysql.rb
|
54
|
+
homepage: http://github.com/coupler/sequel-collation
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
hash: 868813292461605924
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: 868813292461605924
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.24
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Sequel extension to access collation information
|
84
|
+
test_files:
|
85
|
+
- test/config.yml
|
86
|
+
- test/helper.rb
|
87
|
+
- test/unit/test_h2.rb
|
88
|
+
- test/unit/test_mysql.rb
|