activerecord-cubrid2-adapter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/LICENSE +21 -0
  4. data/README.md +63 -0
  5. data/Rakefile +11 -0
  6. data/VERSION +1 -0
  7. data/activerecord-cubrid2-adapter.gemspec +26 -0
  8. data/lib/active_record/connection_adapters/abstract_cubrid2_adapter.rb +750 -0
  9. data/lib/active_record/connection_adapters/cubrid2/column.rb +28 -0
  10. data/lib/active_record/connection_adapters/cubrid2/database_statements.rb +192 -0
  11. data/lib/active_record/connection_adapters/cubrid2/explain_pretty_printer.rb +71 -0
  12. data/lib/active_record/connection_adapters/cubrid2/quoting.rb +118 -0
  13. data/lib/active_record/connection_adapters/cubrid2/schema_creation.rb +98 -0
  14. data/lib/active_record/connection_adapters/cubrid2/schema_definitions.rb +81 -0
  15. data/lib/active_record/connection_adapters/cubrid2/schema_dumper.rb +31 -0
  16. data/lib/active_record/connection_adapters/cubrid2/schema_statements.rb +276 -0
  17. data/lib/active_record/connection_adapters/cubrid2/type_metadata.rb +31 -0
  18. data/lib/active_record/connection_adapters/cubrid2/version.rb +7 -0
  19. data/lib/active_record/connection_adapters/cubrid2_adapter.rb +169 -0
  20. data/lib/activerecord-cubrid2-adapter.rb +4 -0
  21. data/lib/arel/visitors/cubrid.rb +67 -0
  22. data/lib/cubrid2/client.rb +93 -0
  23. data/lib/cubrid2/console.rb +5 -0
  24. data/lib/cubrid2/error.rb +86 -0
  25. data/lib/cubrid2/field.rb +3 -0
  26. data/lib/cubrid2/result.rb +7 -0
  27. data/lib/cubrid2/statement.rb +11 -0
  28. data/lib/cubrid2/version.rb +3 -0
  29. data/lib/cubrid2.rb +76 -0
  30. data/tests/Gemfile +10 -0
  31. data/tests/test_activerecord.rb +109 -0
  32. metadata +102 -0
@@ -0,0 +1,86 @@
1
+ module Cubrid2
2
+ class Error < StandardError
3
+ ENCODE_OPTS = {
4
+ undef: :replace,
5
+ invalid: :replace,
6
+ replace: '?'.freeze
7
+ }.freeze
8
+
9
+ ConnectionError = Class.new(Error)
10
+ TimeoutError = Class.new(Error)
11
+
12
+ attr_reader :error_number, :sql_state
13
+
14
+ ######################################
15
+ # ### CUBRID Error codes
16
+ # from ext/error.c
17
+
18
+ # {-1, "CUBRID database error"},
19
+ # {-2, "Invalid connection handle"},
20
+ # {-3, "Memory allocation error"},
21
+ # {-4, "Communication error"},
22
+ # {-5, "No more data"},
23
+ # {-6, "Unknown transaction type"},
24
+ # {-7, "Invalid string parameter"},
25
+ # {-8, "Type conversion error"},
26
+ # {-9, "Parameter binding error"},
27
+ # {-10, "Invalid type"},
28
+ # {-11, "Parameter binding error"},
29
+ # {-12, "Invalid database parameter name"},
30
+ # {-13, "Invalid column index"},
31
+ # {-14, "Invalid schema type"},
32
+ # {-15, "File open error"},
33
+ # {-16, "Connection error"},
34
+ # {-17, "Connection handle creation error"},
35
+ # {-18, "Invalid request handle"},
36
+ # {-19, "Invalid cursor position"},
37
+ # {-20, "Object is not valid"},
38
+ # {-21, "CAS error"},
39
+ # {-22, "Unknown host name"},
40
+ # {-99, "Not implemented"},
41
+ # {-1000, "Database connection error"},
42
+ # {-1002, "Memory allocation error"},
43
+ # {-1003, "Communication error"},
44
+ # {-1004, "Invalid argument"},
45
+ # {-1005, "Unknown transaction type"},
46
+ # {-1007, "Parameter binding error"},
47
+ # {-1008, "Parameter binding error"},
48
+ # {-1009, "Cannot make DB_VALUE"},
49
+ # {-1010, "Type conversion error"},
50
+ # {-1011, "Invalid database parameter name"},
51
+ # {-1012, "No more data"},
52
+ # {-1013, "Object is not valid"},
53
+ # {-1014, "File open error"},
54
+ # {-1015, "Invalid schema type"},
55
+ # {-1016, "Version mismatch"},
56
+ # {-1017, "Cannot process the request. Try again later."},
57
+ # {-1018, "Authorization error"},
58
+ # {-1020, "The attribute domain must be the set type."},
59
+ # {-1021, "The domain of a set must be the same data type."},
60
+ # {-2001, "Memory allocation error"},
61
+ # {-2002, "Invalid API call"},
62
+ # {-2003, "Cannot get column info"},
63
+ # {-2004, "Array initializing error"},
64
+ # {-2005, "Unknown column type"},
65
+ # {-2006, "Invalid parameter"},
66
+ # {-2007, "Invalid array type"},
67
+ # {-2008, "Invalid type"},
68
+ # {-2009, "File open error"},
69
+ # {-2010, "Temporary file open error"},
70
+ # {-2011, "Glo transfering error"},
71
+ # {0, "Unknown Error"}
72
+ ######################################
73
+
74
+ # cubrid gem compatibility
75
+ alias err_code error_number
76
+ alias err_msg message
77
+
78
+ def initialize(msg, server_version = nil, error_number = nil, sql_state = nil)
79
+ @server_version = server_version
80
+ @error_number = error_number
81
+ @sql_state = sql_state ? sql_state.encode(**ENCODE_OPTS) : nil
82
+
83
+ super msg
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Cubrid2
2
+ Field = Struct.new(:name, :type)
3
+ end
@@ -0,0 +1,7 @@
1
+ module Cubrid2
2
+ class Result
3
+ #attr_reader :server_flags
4
+
5
+ include Enumerable
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Cubrid2
2
+ class Statement
3
+ include Enumerable
4
+
5
+ def execute(*args, **kwargs)
6
+ Thread.handle_interrupt(::Cubrid2::Util::TIMEOUT_ERROR_CLASS => :never) do
7
+ _execute(*args, **kwargs)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Cubrid2
2
+ VERSION = File.read(File.expand_path('../../VERSION', __dir__)).freeze
3
+ end
data/lib/cubrid2.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'date'
2
+ require 'bigdecimal'
3
+
4
+ # Load libcubrid.dll before requiring cubrid/cubrid.so
5
+ # This gives a chance to be flexible about the load path
6
+ # Or to bomb out with a clear error message instead of a linker crash
7
+ if RUBY_PLATFORM =~ /mswin|mingw/
8
+ dll_path = if ENV['RUBY_CUBRID_LIBCUBRID_DLL']
9
+ # If this environment variable is set, it overrides any other paths
10
+ # The user is advised to use backslashes not forward slashes
11
+ ENV['RUBY_CUBRID_LIBCUBRID_DLL']
12
+ elsif File.exist?(File.expand_path('../vendor/libcubrid.dll', File.dirname(__FILE__)))
13
+ # Use vendor/libcubrid.dll if it exists, convert slashes for Win32 LoadLibrary
14
+ File.expand_path('../vendor/libcubrid.dll', File.dirname(__FILE__))
15
+ # elsif defined?(RubyInstaller)
16
+ # RubyInstaller-2.4+ native build doesn't need DLL preloading
17
+ # else
18
+ # This will use default / system library paths
19
+ '../vendor/libcubrid.dll'
20
+ end
21
+
22
+ if dll_path
23
+ require 'fiddle'
24
+ kernel32 = Fiddle.dlopen 'kernel32'
25
+ load_library = Fiddle::Function.new(
26
+ kernel32['LoadLibraryW'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT
27
+ )
28
+ abort "Failed to load libcubrid.dll from #{dll_path}" if load_library.call(dll_path.encode('utf-16le')).zero?
29
+ end
30
+ end
31
+
32
+ # load c extension
33
+ gem 'cubrid', '>= 10.0'
34
+ require 'cubrid'
35
+
36
+ require 'cubrid2/version' unless defined? Cubrid2::VERSION
37
+ require 'cubrid2/error'
38
+ require 'cubrid2/result'
39
+ require 'cubrid2/client'
40
+ require 'cubrid2/field'
41
+ require 'cubrid2/statement'
42
+
43
+ # = cubrid
44
+ #
45
+ # A modern, simple and very fast Cubrid library for Ruby - binding to libcubrid
46
+ module Cubrid2
47
+ end
48
+
49
+ # For holding utility methods
50
+ module Cubrid2
51
+ module Util
52
+ #
53
+ # Rekey a string-keyed hash with equivalent symbols.
54
+ #
55
+ def self.key_hash_as_symbols(hash)
56
+ return nil unless hash
57
+
58
+ Hash[hash.map { |k, v| [k.to_sym, v] }]
59
+ end
60
+
61
+ #
62
+ # In Cubrid2::Client#query and Cubrid2::Statement#execute,
63
+ # Thread#handle_interrupt is used to prevent Timeout#timeout
64
+ # from interrupting query execution.
65
+ #
66
+ # Timeout::ExitException was removed in Ruby 2.3.0, 2.2.3, and 2.1.8,
67
+ # but is present in earlier 2.1.x and 2.2.x, so we provide a shim.
68
+ #
69
+ require 'timeout'
70
+ TIMEOUT_ERROR_CLASS = if defined?(::Timeout::ExitException)
71
+ ::Timeout::ExitException
72
+ else
73
+ ::Timeout::Error
74
+ end
75
+ end
76
+ end
data/tests/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # I cloned from original cubrid-ruby git repository to test
4
+ # git clone https://github.com/CUBRID/cubrid-ruby.git
5
+ gem 'cubrid', path: '/opt/cubrid/cubrid-ruby'
6
+
7
+ gem 'activerecord', '~> 6.0'
8
+ gem 'activerecord-cubrid2-adapter', path: '../'
9
+ gem 'test-unit'
10
+
@@ -0,0 +1,109 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'active_record'
5
+ require 'activerecord-cubrid2-adapter'
6
+
7
+ require 'benchmark'
8
+ require 'test/unit'
9
+
10
+ TABLE_NAME = 'cubrid_tests'
11
+
12
+ class CubridTest < ActiveRecord::Base
13
+ self.table_name = TABLE_NAME
14
+ end
15
+
16
+ class CUBRID_ActiveRecordTest < Test::Unit::TestCase
17
+ def setup
18
+ puts '### setup '
19
+ puts "-- activerecord-cubrid2-adapter version: #{ActiveRecord::ConnectionAdapters::Cubrid2::VERSION}"
20
+
21
+ adapter = ActiveRecord::Base.establish_connection(
22
+ adapter: 'cubrid2',
23
+ host: 'localhost',
24
+ username: 'dba',
25
+ password: '',
26
+ database: 'testdb'
27
+ )
28
+
29
+ @con = adapter.connection
30
+
31
+ puts "-- cubrid server version: #{@con.server_version}"
32
+ puts "-- charset: #{@con.charset}"
33
+ puts "-- collation: #{@con.collation}"
34
+
35
+ ActiveRecord::Base.connection.drop_table TABLE_NAME if ActiveRecord::Base.connection.table_exists?(TABLE_NAME)
36
+
37
+ ActiveRecord::Base.connection.create_table TABLE_NAME do |t|
38
+ t.string :name
39
+ t.text :body
40
+ t.timestamps
41
+ end
42
+
43
+ exists = ActiveRecord::Base.connection.table_exists?(TABLE_NAME)
44
+ assert(exists, 'Table not found')
45
+ end
46
+
47
+ def teardown
48
+ puts '### teardown '
49
+
50
+ @con.close
51
+ end
52
+
53
+ def test_insert
54
+ puts '### test_insert'
55
+
56
+ cnt = CubridTest.count
57
+
58
+ p = CubridTest.new
59
+ p.name = 'test11'
60
+ p.save!
61
+
62
+ test1 = CubridTest.create!(name: 'test1', body: 'test1')
63
+ puts "inserted id: #{test1.id}"
64
+
65
+ test2 = CubridTest.create!(name: 'test2', body: '한글2')
66
+ puts "inserted id: #{test2.id}"
67
+
68
+ test3 = CubridTest.create!(name: 'test3', body: '中文3')
69
+ puts "inserted id: #{test3.id}"
70
+
71
+ test2_1 = CubridTest.where("name = 'test2'").first
72
+ pp test2_1
73
+
74
+ test3_1 = CubridTest.where("name = 'test3'").first
75
+ pp test3_1
76
+
77
+ assert(CubridTest.count == (cnt + 4), 'Table row count mismatch')
78
+
79
+ CubridTest.destroy_all
80
+
81
+ assert(CubridTest.count == 0, 'Table row count mismatch')
82
+ end
83
+
84
+ def test_benchmark_insert
85
+ puts '### test_benchmark_insert'
86
+
87
+ @max_insert = 10
88
+
89
+ count1 = CubridTest.count
90
+
91
+ Benchmark.bm do |x|
92
+ x.report do
93
+ (1..@max_insert).each do |i|
94
+ puts "#{i}th test"
95
+ p = CubridTest.new
96
+ p.name = 'test11'
97
+ p.save!
98
+ puts "inserted id: #{p.id}"
99
+ end
100
+ end
101
+ end
102
+
103
+ count2 = CubridTest.count
104
+ inserted_count = (count2 - count1)
105
+
106
+ puts "### #{inserted_count} rows inserted"
107
+ assert(inserted_count == @max_insert, 'inserted rows mismatch!')
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-cubrid2-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eui-Taik Na
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cubrid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ActiveRecord Cubrid Adapter. Cubrid 9 and upward. Based on cubrid gem.
42
+ email:
43
+ - damulhan@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - VERSION
53
+ - activerecord-cubrid2-adapter.gemspec
54
+ - lib/active_record/connection_adapters/abstract_cubrid2_adapter.rb
55
+ - lib/active_record/connection_adapters/cubrid2/column.rb
56
+ - lib/active_record/connection_adapters/cubrid2/database_statements.rb
57
+ - lib/active_record/connection_adapters/cubrid2/explain_pretty_printer.rb
58
+ - lib/active_record/connection_adapters/cubrid2/quoting.rb
59
+ - lib/active_record/connection_adapters/cubrid2/schema_creation.rb
60
+ - lib/active_record/connection_adapters/cubrid2/schema_definitions.rb
61
+ - lib/active_record/connection_adapters/cubrid2/schema_dumper.rb
62
+ - lib/active_record/connection_adapters/cubrid2/schema_statements.rb
63
+ - lib/active_record/connection_adapters/cubrid2/type_metadata.rb
64
+ - lib/active_record/connection_adapters/cubrid2/version.rb
65
+ - lib/active_record/connection_adapters/cubrid2_adapter.rb
66
+ - lib/activerecord-cubrid2-adapter.rb
67
+ - lib/arel/visitors/cubrid.rb
68
+ - lib/cubrid2.rb
69
+ - lib/cubrid2/client.rb
70
+ - lib/cubrid2/console.rb
71
+ - lib/cubrid2/error.rb
72
+ - lib/cubrid2/field.rb
73
+ - lib/cubrid2/result.rb
74
+ - lib/cubrid2/statement.rb
75
+ - lib/cubrid2/version.rb
76
+ - tests/Gemfile
77
+ - tests/test_activerecord.rb
78
+ homepage: https://github.com/damulhan/activerecord-cubrid2-adapter
79
+ licenses:
80
+ - MIT
81
+ - GPL-2.0
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.5.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.3.7
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: ActiveRecord Cubrid Adapter.
102
+ test_files: []