tiny_tds_vagas 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/CHANGELOG +198 -0
  4. data/CODE_OF_CONDUCT.md +31 -0
  5. data/Gemfile +6 -0
  6. data/ISSUE_TEMPLATE.md +5 -0
  7. data/MIT-LICENSE +23 -0
  8. data/README.md +427 -0
  9. data/Rakefile +110 -0
  10. data/VERSION +1 -0
  11. data/appveyor.yml +52 -0
  12. data/bin/defncopy +3 -0
  13. data/bin/tsql +3 -0
  14. data/ext/tiny_tds/client.c +410 -0
  15. data/ext/tiny_tds/client.h +49 -0
  16. data/ext/tiny_tds/extconf.rb +329 -0
  17. data/ext/tiny_tds/extconsts.rb +15 -0
  18. data/ext/tiny_tds/result.c +608 -0
  19. data/ext/tiny_tds/result.h +32 -0
  20. data/ext/tiny_tds/tiny_tds_ext.c +12 -0
  21. data/ext/tiny_tds/tiny_tds_ext.h +17 -0
  22. data/lib/tiny_tds.rb +37 -0
  23. data/lib/tiny_tds/bin.rb +86 -0
  24. data/lib/tiny_tds/client.rb +124 -0
  25. data/lib/tiny_tds/error.rb +15 -0
  26. data/lib/tiny_tds/result.rb +8 -0
  27. data/lib/tiny_tds/version.rb +3 -0
  28. data/ports/patches/freetds/1.00/0001-mingw_missing_inet_pton.diff +34 -0
  29. data/test/appveyor/dbsetup.ps1 +27 -0
  30. data/test/appveyor/dbsetup.sql +9 -0
  31. data/test/benchmark/query.rb +77 -0
  32. data/test/benchmark/query_odbc.rb +106 -0
  33. data/test/benchmark/query_tinytds.rb +126 -0
  34. data/test/client_test.rb +217 -0
  35. data/test/result_test.rb +728 -0
  36. data/test/schema/1px.gif +0 -0
  37. data/test/schema/sqlserver_2000.sql +140 -0
  38. data/test/schema/sqlserver_2005.sql +140 -0
  39. data/test/schema/sqlserver_2008.sql +140 -0
  40. data/test/schema/sqlserver_2014.sql +140 -0
  41. data/test/schema/sqlserver_azure.sql +140 -0
  42. data/test/schema/sybase_ase.sql +138 -0
  43. data/test/schema_test.rb +443 -0
  44. data/test/test_helper.rb +213 -0
  45. data/test/thread_test.rb +98 -0
  46. data/tiny_tds.gemspec +28 -0
  47. metadata +201 -0
@@ -0,0 +1,213 @@
1
+ # encoding: UTF-8
2
+ require 'bundler' ; Bundler.require :development, :test
3
+ require 'tiny_tds'
4
+ require 'minitest/autorun'
5
+
6
+ TINYTDS_SCHEMAS = ['sqlserver_2000', 'sqlserver_2005', 'sqlserver_2008', 'sqlserver_2014', 'sqlserver_azure', 'sybase_ase'].freeze
7
+
8
+ module TinyTds
9
+ class TestCase < MiniTest::Spec
10
+
11
+ class << self
12
+
13
+ def current_schema
14
+ ENV['TINYTDS_SCHEMA'] || 'sqlserver_2014'
15
+ end
16
+
17
+ TINYTDS_SCHEMAS.each do |schema|
18
+ define_method "#{schema}?" do
19
+ schema == self.current_schema
20
+ end
21
+ end
22
+
23
+ def sqlserver?
24
+ current_schema =~ /sqlserver/
25
+ end
26
+
27
+ end
28
+
29
+ after { close_client }
30
+
31
+ protected
32
+
33
+ TINYTDS_SCHEMAS.each do |schema|
34
+ define_method "#{schema}?" do
35
+ schema == self.class.current_schema
36
+ end
37
+ end
38
+
39
+ def current_schema
40
+ self.class.current_schema
41
+ end
42
+
43
+ def sqlserver?
44
+ self.class.sqlserver?
45
+ end
46
+
47
+ def close_client(client=@client)
48
+ client.close if defined?(client) && client.is_a?(TinyTds::Client)
49
+ end
50
+
51
+ def new_connection(options={})
52
+ client = TinyTds::Client.new(connection_options(options))
53
+ if sybase_ase?
54
+ client.execute("SET ANSINULL ON").do
55
+ return client
56
+ elsif sqlserver_azure?
57
+ client.execute('SET ANSI_NULLS ON').do
58
+ client.execute('SET CURSOR_CLOSE_ON_COMMIT OFF').do
59
+ client.execute('SET ANSI_NULL_DFLT_ON ON').do
60
+ client.execute('SET IMPLICIT_TRANSACTIONS OFF').do
61
+ client.execute('SET ANSI_PADDING ON').do
62
+ client.execute('SET QUOTED_IDENTIFIER ON').do
63
+ client.execute('SET ANSI_WARNINGS ON').do
64
+ else
65
+ client.execute('SET ANSI_DEFAULTS ON').do
66
+ client.execute('SET CURSOR_CLOSE_ON_COMMIT OFF').do
67
+ client.execute('SET IMPLICIT_TRANSACTIONS OFF').do
68
+ end
69
+ client.execute('SET TEXTSIZE 2147483647').do
70
+ client.execute('SET CONCAT_NULL_YIELDS_NULL ON').do
71
+ client
72
+ end
73
+
74
+ def connection_options(options={})
75
+ username = (sqlserver_azure? ? ENV['TINYTDS_UNIT_AZURE_USER'] : ENV['TINYTDS_UNIT_USER']) || 'tinytds'
76
+ password = (sqlserver_azure? ? ENV['TINYTDS_UNIT_AZURE_PASS'] : ENV['TINYTDS_UNIT_PASS']) || ''
77
+ { :dataserver => sqlserver_azure? ? nil : ENV['TINYTDS_UNIT_DATASERVER'],
78
+ :host => ENV['TINYTDS_UNIT_HOST'],
79
+ :port => ENV['TINYTDS_UNIT_PORT'],
80
+ :tds_version => ENV['TINYTDS_UNIT_VERSION'],
81
+ :username => username,
82
+ :password => password,
83
+ :database => ENV['TINYTDS_UNIT_DATABASE'] || 'tinytdstest',
84
+ :appname => 'TinyTds Dev',
85
+ :login_timeout => 5,
86
+ :timeout => connection_timeout,
87
+ :azure => sqlserver_azure?
88
+ }.merge(options)
89
+ end
90
+
91
+ def connection_timeout
92
+ sqlserver_azure? ? 20 : 5
93
+ end
94
+
95
+ def assert_client_works(client)
96
+ client.execute("SELECT 'client_works' as [client_works]").each.must_equal [{'client_works' => 'client_works'}]
97
+ end
98
+
99
+ def assert_new_connections_work
100
+ client = new_connection
101
+ client.execute("SELECT 'new_connections_work' as [new_connections_work]").each
102
+ client.close
103
+ end
104
+
105
+ def assert_raise_tinytds_error(action)
106
+ result = nil
107
+ error_raised = false
108
+ begin
109
+ result = action.call
110
+ rescue TinyTds::Error => e
111
+ error_raised = true
112
+ end
113
+ assert error_raised, 'expected a TinyTds::Error but none happened'
114
+ yield e
115
+ ensure
116
+ close_client(result)
117
+ end
118
+
119
+ def inspect_tinytds_exception
120
+ begin
121
+ yield
122
+ rescue TinyTds::Error => e
123
+ props = { :source => e.source, :message => e.message, :severity => e.severity,
124
+ :db_error_number => e.db_error_number, :os_error_number => e.os_error_number }
125
+ raise "TinyTds::Error - #{props.inspect}"
126
+ end
127
+ end
128
+
129
+ def assert_binary_encoding(value)
130
+ assert_equal Encoding.find('BINARY'), value.encoding
131
+ end
132
+
133
+ def assert_utf8_encoding(value)
134
+ assert_equal Encoding.find('UTF-8'), value.encoding
135
+ end
136
+
137
+ def rubyRbx?
138
+ RUBY_DESCRIPTION =~ /rubinius/i
139
+ end
140
+
141
+ def ruby_windows?
142
+ RbConfig::CONFIG['host_os'] =~ /ming/
143
+ end
144
+
145
+ def load_current_schema
146
+ loader = new_connection
147
+ schema_file = File.expand_path File.join(File.dirname(__FILE__), 'schema', "#{current_schema}.sql")
148
+ schema_sql = File.open(schema_file,"rb:UTF-8") { |f|f.read }
149
+ loader.execute(drop_sql).do
150
+ loader.execute(schema_sql).do
151
+ loader.execute(sp_sql).do
152
+ loader.close
153
+ true
154
+ end
155
+
156
+ def drop_sql
157
+ sybase_ase? ? drop_sql_sybase : drop_sql_microsoft
158
+ end
159
+
160
+ def drop_sql_sybase
161
+ %|IF EXISTS(
162
+ SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'datatypes'
163
+ ) DROP TABLE datatypes
164
+ IF EXISTS(
165
+ SELECT 1 FROM sysobjects WHERE type = 'P' AND name = 'tinytds_TestReturnCodes'
166
+ ) DROP PROCEDURE tinytds_TestReturnCodes|
167
+ end
168
+
169
+ def drop_sql_microsoft
170
+ %|IF EXISTS (
171
+ SELECT TABLE_NAME
172
+ FROM INFORMATION_SCHEMA.TABLES
173
+ WHERE TABLE_CATALOG = 'tinytdstest'
174
+ AND TABLE_TYPE = 'BASE TABLE'
175
+ AND TABLE_NAME = 'datatypes'
176
+ ) DROP TABLE [datatypes]
177
+ IF EXISTS (
178
+ SELECT name FROM sysobjects
179
+ WHERE name = 'tinytds_TestReturnCodes' AND type = 'P'
180
+ ) DROP PROCEDURE tinytds_TestReturnCodes|
181
+ end
182
+
183
+ def sp_sql
184
+ %|CREATE PROCEDURE tinytds_TestReturnCodes
185
+ AS
186
+ SELECT 1 as [one]
187
+ RETURN(420) |
188
+ end
189
+
190
+ def find_value(id, column, query_options={})
191
+ query_options[:timezone] ||= :utc
192
+ sql = "SELECT [#{column}] FROM [datatypes] WHERE [id] = #{id}"
193
+ @client.execute(sql).each(query_options).first[column.to_s]
194
+ end
195
+
196
+ def local_offset
197
+ TinyTds::Client.local_offset
198
+ end
199
+
200
+ def utc_offset
201
+ ::Time.local(2010).utc_offset
202
+ end
203
+
204
+ def rollback_transaction(client)
205
+ client.execute("BEGIN TRANSACTION").do
206
+ yield
207
+ ensure
208
+ client.execute("ROLLBACK TRANSACTION").do
209
+ end
210
+
211
+ end
212
+ end
213
+
@@ -0,0 +1,98 @@
1
+ require 'test_helper'
2
+ require 'logger'
3
+ require 'benchmark'
4
+
5
+ class ThreadTest < TinyTds::TestCase
6
+
7
+ describe 'Threaded SELECT queries' do
8
+
9
+ before do
10
+ @logger = Logger.new $stdout
11
+ @logger.level = Logger::WARN
12
+ @poolsize = 4
13
+ @numthreads = 10
14
+ @query = "waitfor delay '00:00:01'"
15
+ @pool = ConnectionPool.new(:size => @poolsize, :timeout => 5) { new_connection }
16
+ end
17
+
18
+ after do
19
+ @pool.shutdown { |c| c.close }
20
+ end
21
+
22
+ it 'should finish faster in parallel' do
23
+ skip if sqlserver_azure?
24
+ x = Benchmark.realtime do
25
+ threads = []
26
+ @numthreads.times do |i|
27
+ start = Time.new
28
+ threads << Thread.new do
29
+ ts = Time.new
30
+ @pool.with { |c| c.execute(@query).do }
31
+ te = Time.new
32
+ @logger.info "Thread #{i} finished in #{te - ts} thread seconds, #{te - start} real seconds"
33
+ end
34
+ end
35
+ threads.each { |t| t.join }
36
+ end
37
+ assert x < @numthreads, "#{x} is not faster than #{@numthreads} seconds"
38
+ mintime = (1.0*@numthreads/@poolsize).ceil
39
+ @logger.info "#{@numthreads} queries on #{@poolsize} threads: #{x} sec. Minimum time: #{mintime} sec."
40
+ assert x > mintime, "#{x} is not slower than #{mintime} seconds"
41
+ end
42
+
43
+ it 'should not crash on error in parallel' do
44
+ skip if sqlserver_azure?
45
+ threads = []
46
+ @numthreads.times do |i|
47
+ threads << Thread.new do
48
+ @pool.with do |client|
49
+ begin
50
+ result = client.execute "select dbname()"
51
+ result.each { |r| puts r }
52
+ rescue Exception => e
53
+ # We are throwing an error on purpose here since 0.6.1 would
54
+ # segfault on errors thrown in threads
55
+ end
56
+ end
57
+ end
58
+ end
59
+ threads.each { |t| t.join }
60
+ assert true
61
+ end
62
+
63
+ it 'should cancel when hitting timeout in thread' do
64
+ exception = false
65
+
66
+ thread = Thread.new do
67
+ @pool.with do |client|
68
+ begin
69
+ delay = ('0' + (connection_timeout + 2).to_s)[-2,2] # Two seconds longer than default.
70
+ result = client.execute "waitfor delay '00:00:#{delay}'; select db_name()"
71
+ result.each { |r| puts r }
72
+ rescue TinyTds::Error => e
73
+ if e.message == 'Adaptive Server connection timed out'
74
+ exception = true
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ timer_thread = Thread.new do
81
+ # Sleep until after the timeout should have been reached
82
+ sleep(connection_timeout+2)
83
+ if not exception
84
+ thread.kill
85
+ raise "Timeout passed without query timing out"
86
+ end
87
+ end
88
+
89
+ thread.join
90
+ timer_thread.join
91
+
92
+ assert exception
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
data/tiny_tds.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tiny_tds/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'tiny_tds_vagas'
7
+ s.version = TinyTds::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Equipe P&D Vagas', 'Ken Collins', 'Erik Bryn', 'Will Bond']
10
+ s.email = ['equipepdp@vagas.com.br', 'ken@metaskills.net', 'will@wbond.net']
11
+ s.homepage = 'http://github.com/VAGAScom/tiny_tds_vagas'
12
+ s.summary = 'TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library.'
13
+ s.description = 'TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library. Developed for the ActiveRecord SQL Server adapter.'
14
+ s.files = `git ls-files`.split("\n") + Dir.glob('exe/*')
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+ s.rdoc_options = ['--charset=UTF-8']
19
+ s.extensions = ['ext/tiny_tds/extconf.rb']
20
+ s.license = 'MIT'
21
+ s.required_ruby_version = '>= 2.0.0'
22
+ s.add_runtime_dependency 'mini_portile2', '~> 2.0' # Keep this version in sync with the one in extconf.rb !
23
+ s.add_development_dependency 'rake', '~> 10.4'
24
+ s.add_development_dependency 'rake-compiler', '0.9.5'
25
+ s.add_development_dependency 'rake-compiler-dock', '~> 0.5.1'
26
+ s.add_development_dependency 'minitest', '~> 5.6'
27
+ s.add_development_dependency 'connection_pool', '~> 2.2'
28
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_tds_vagas
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Equipe P&D Vagas
8
+ - Ken Collins
9
+ - Erik Bryn
10
+ - Will Bond
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2016-07-19 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: mini_portile2
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '10.4'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '10.4'
44
+ - !ruby/object:Gem::Dependency
45
+ name: rake-compiler
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '='
49
+ - !ruby/object:Gem::Version
50
+ version: 0.9.5
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '='
56
+ - !ruby/object:Gem::Version
57
+ version: 0.9.5
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake-compiler-dock
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: 0.5.1
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: 0.5.1
72
+ - !ruby/object:Gem::Dependency
73
+ name: minitest
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '5.6'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '5.6'
86
+ - !ruby/object:Gem::Dependency
87
+ name: connection_pool
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '2.2'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '2.2'
100
+ description: TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library.
101
+ Developed for the ActiveRecord SQL Server adapter.
102
+ email:
103
+ - equipepdp@vagas.com.br
104
+ - ken@metaskills.net
105
+ - will@wbond.net
106
+ executables:
107
+ - defncopy
108
+ - tsql
109
+ extensions:
110
+ - ext/tiny_tds/extconf.rb
111
+ extra_rdoc_files: []
112
+ files:
113
+ - ".gitignore"
114
+ - CHANGELOG
115
+ - CODE_OF_CONDUCT.md
116
+ - Gemfile
117
+ - ISSUE_TEMPLATE.md
118
+ - MIT-LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - VERSION
122
+ - appveyor.yml
123
+ - bin/defncopy
124
+ - bin/tsql
125
+ - exe/.keep
126
+ - ext/tiny_tds/client.c
127
+ - ext/tiny_tds/client.h
128
+ - ext/tiny_tds/extconf.rb
129
+ - ext/tiny_tds/extconsts.rb
130
+ - ext/tiny_tds/result.c
131
+ - ext/tiny_tds/result.h
132
+ - ext/tiny_tds/tiny_tds_ext.c
133
+ - ext/tiny_tds/tiny_tds_ext.h
134
+ - lib/tiny_tds.rb
135
+ - lib/tiny_tds/bin.rb
136
+ - lib/tiny_tds/client.rb
137
+ - lib/tiny_tds/error.rb
138
+ - lib/tiny_tds/result.rb
139
+ - lib/tiny_tds/version.rb
140
+ - ports/patches/freetds/1.00/0001-mingw_missing_inet_pton.diff
141
+ - test/appveyor/dbsetup.ps1
142
+ - test/appveyor/dbsetup.sql
143
+ - test/benchmark/query.rb
144
+ - test/benchmark/query_odbc.rb
145
+ - test/benchmark/query_tinytds.rb
146
+ - test/client_test.rb
147
+ - test/result_test.rb
148
+ - test/schema/1px.gif
149
+ - test/schema/sqlserver_2000.sql
150
+ - test/schema/sqlserver_2005.sql
151
+ - test/schema/sqlserver_2008.sql
152
+ - test/schema/sqlserver_2014.sql
153
+ - test/schema/sqlserver_azure.sql
154
+ - test/schema/sybase_ase.sql
155
+ - test/schema_test.rb
156
+ - test/test_helper.rb
157
+ - test/thread_test.rb
158
+ - tiny_tds.gemspec
159
+ homepage: http://github.com/VAGAScom/tiny_tds_vagas
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options:
165
+ - "--charset=UTF-8"
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 2.0.0
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.6.3
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library.
184
+ test_files:
185
+ - test/appveyor/dbsetup.ps1
186
+ - test/appveyor/dbsetup.sql
187
+ - test/benchmark/query.rb
188
+ - test/benchmark/query_odbc.rb
189
+ - test/benchmark/query_tinytds.rb
190
+ - test/client_test.rb
191
+ - test/result_test.rb
192
+ - test/schema/1px.gif
193
+ - test/schema/sqlserver_2000.sql
194
+ - test/schema/sqlserver_2005.sql
195
+ - test/schema/sqlserver_2008.sql
196
+ - test/schema/sqlserver_2014.sql
197
+ - test/schema/sqlserver_azure.sql
198
+ - test/schema/sybase_ase.sql
199
+ - test/schema_test.rb
200
+ - test/test_helper.rb
201
+ - test/thread_test.rb