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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 771f174207a3137c6536523031adfb8a044ab936
4
+ data.tar.gz: 93793554ae4869ee5aab3030c3d872e82d65f49b
5
+ SHA512:
6
+ metadata.gz: 491c5aedba709c990af5964e3a23c31dedf2fece9a3d38f3965119a1acb20c0146e621cf3bbd9f597d1645a12189f78259a57ada93c2072695859c6e41f74743
7
+ data.tar.gz: 9ff9daca9f965dd2c32a245523ce60f541a67cc47c65603aad9a7ea4bdd292ba7c5524d1548debb53ca5d7e2973db0e2aec27267cfa07865e28a2d1dfb5fd110
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ Makefile
2
+ *.dSYM
3
+ *.o
4
+ *.bundle
5
+ *.so
6
+ *.a
7
+ *.rbc
8
+ mkmf.log
9
+ pkg/
10
+ tmp
11
+ vendor
12
+ lib/tiny_tds/tiny_tds.rb
13
+ .rvmrc
14
+ .rbenv-version
15
+ Gemfile.lock
16
+ misc
17
+ *.gem
18
+ /exe/*
19
+ /ports/*
20
+ !/ports/patches/
data/CHANGELOG ADDED
@@ -0,0 +1,198 @@
1
+ * 1.0.4 *
2
+
3
+ * Use FreeTDS 1.0 final
4
+
5
+
6
+ * 1.0.3 *
7
+
8
+ * Use FreeTDS 1.0rc5 for cross compile windows gems.
9
+ * Ensure we only work with latest FreeTDS v0.95.x or higher.
10
+
11
+
12
+ * 1.0.2 *
13
+
14
+ * Cross compile w/2.3.0 using rake-compiler-dock ~> 0.5.1. Fixes #268 #270.
15
+ * Use FreeTDS 1.0rc4 for cross compile windows gems.
16
+
17
+
18
+ * 1.0.1 *
19
+
20
+ * Fix ruby exe's in non-platform gem.
21
+
22
+
23
+ * 1.0.0 *
24
+
25
+ * Tested with FreeTDS 1.0.
26
+ * Add emoji support by default using FreeTDS v1.0 in docs.
27
+
28
+
29
+ * 0.9.5 * (release candidates only)
30
+
31
+ * Binstub wrappers for `tsql`. Fixes #227 #251
32
+ * Add support for 2008 data types. Must use TDSVER 7.3 or higher. Fixes #244 #251
33
+ - [date]
34
+ - [datetime2]
35
+ - [datetimeoffset]
36
+ - [time]
37
+ * Default FreeTDS to 0.95. Support 0.91 Alternate Fixes #233
38
+ - Allow our `tds_version` to mirror TDSVER env var. Ex '7.3' vs '73'.
39
+ - Change error handler for `SYBEICONVO` to hard return INT_CANCEL.
40
+ * Made sure Azure logins are user@short vs. long domain. Fixes #229
41
+ * Removed Ruby 1.9.3 from CI builds.
42
+ * CI now tests Azure too.
43
+ * Fixed compiler warnings on all platforms. Fixed #241
44
+ * FreeTDS - Remove support for bad iconv.
45
+
46
+
47
+ * 0.7.0 *
48
+
49
+ * Refactor build of FreeTDS & Iconv recipes. Add OpenSSL. Merged #207.
50
+ * Ensure zero terminated strings, where C-str pointers are expected. Use StringValueCStr() Fixes #208.
51
+ * Revert 999fa571 so timeouts do not kill the client. Fixes #179.
52
+ * Remove `sspi_w_kerberos.diff` patch. Not needed anymore.
53
+ * Tested again on Azure. Added notes to README on recommended settings.
54
+ * Replace `rb_thread_blocking_region` (removed in Ruby 2.2.0) w/`rb_thread_call_without_gvl`. Fixes #182.
55
+ * Remove 30 char password warning. Fixes #172.
56
+ * Remove Ruby 1.8.6 support. We always use Time vs edge case DateTime.
57
+
58
+
59
+ * 0.6.2 *
60
+
61
+ * Support an optional environment variable to find FreeTDS. Fixes #128.
62
+ * Allow Support for 31+ Character Usernames/Passwords. Fixes #134. Thanks @wbond.
63
+ * Stronger Global VM Lock support for nonblocking connections. Fixes #133. Thanks @wbond.
64
+ * Timeout fix for working with Azure SQL. Fixes #138.
65
+ * Correctly handle requests that return multiple results sets via `.do`, such
66
+ as backups and restores. Fixes #150.
67
+
68
+
69
+ * 0.6.1 *
70
+
71
+ Use both dbsetversion() vs. dbsetlversion. Partially reverts #62.
72
+
73
+
74
+ * 0.6.0 *
75
+
76
+ * Use dbsetversion() vs. dbsetlversion. Fixes #62.
77
+ * Remove Ruby 1.8 support.
78
+ * Implement misc rb_thread_blocking_region support. Fixes #121. Thanks @lepfhty.
79
+ * Test FreeTDS v0.91.89 patch release.
80
+ * Fix lost connection handling. Fixes #124. Thanks @krzcho.
81
+ * Remove unused variable. Fixes #103. Thanks @jeremyevans.
82
+ * Remove need to specify username for Windows Authentication.
83
+ * Use proper SQL for returning IDENTITY with Sybase. Fixes #95.
84
+ * Compile windows with `--enable-sspi`.
85
+ * Allow MiniPortile to build any FreeTDS version we need. Fixes #76.
86
+ * Always convert password option to string. Fixes #92.
87
+ * Move test system to real MiniTest::Spec. All tests pass on Azure too.
88
+ * Raise and handle encoding errors on DB writes. Fixes #89.
89
+
90
+
91
+ * 0.5.1 *
92
+
93
+ * Change how we configure with iconv, basically it is always needed. Fixes #11 & #69.
94
+
95
+
96
+ * 0.5.0 *
97
+
98
+ * Copy mysql2s handling of Time and Datetime so 64bit systems are leveraged. Fixes #46 and #47. Thanks @lsylvester!
99
+ * Add CFLAGS='-fPIC' for libtool. Fix TDS version configs in our ports file. Document. Fixes #45
100
+ * Update our TDS version constants to reflect changed 8.0/9.0 to 7.1/7.2 DBLIB versions in FreeTDS
101
+ while making it backward compatible, again like FreeTDS. Even tho you can not configure FreeTDS with
102
+ TDS version 7.2 or technically even use it, I added tests to prove that we correctly handle both
103
+ varchar(max) and nvarchar(max) with large amounts of data.
104
+ * FreeTDS 0.91 has been released. Update our port scripts.
105
+ * Add test for 0.91 and higher to handle incorrect syntax in sp_executesql.
106
+ * Returning empty result sets with a command batch that has multiple statements is now the default. Use :empty_sets => false to override.
107
+ * Do not raise a TinyTds::Error with our message handler unless the severity is greater than 10.
108
+
109
+
110
+ * 0.4.5 *
111
+
112
+ * Includes precompiled Windows binaries for FreeTDS 0.91rc2 & LibIconv. No precompiled OpenSSL yet for Windows to SQL Azure.
113
+ * Fixed symbolized unicode column names.
114
+ * Use same bigint ruby functions to return identity. Hopefully fixes #19.
115
+ * Release static libs for Windows.
116
+ * Change how :host/:port are implemented. Now sending "host:port" to :dataserver.
117
+
118
+
119
+ * 0.4.4 *
120
+
121
+ * New :host/:port connection options. Removes need for freetds.conf file.
122
+
123
+
124
+ * 0.4.3 *
125
+
126
+ * New Client#active? method to check for good connection. Always use this abstract method.
127
+ * Better SYBEWRIT "Write to SQL Server failed." error handling. New Client#dead? check.
128
+ * Azure tested using latest FreeTDS with submitted patch. https://gist.github.com/889190
129
+
130
+
131
+ * 0.4.2 *
132
+
133
+ * Iconv is a dep only when compiling locally. However, left in the ability to configure
134
+ it for native gem installation but you must use
135
+ --enable-iconv before using --with-iconv-dir=/some/dir
136
+ * Really fix what 0.4.1 was supposed to do, force SYBDBLIB compile.
137
+
138
+
139
+ * 0.4.1 *
140
+
141
+ * Undefine MSDBLIB in case others have explicitly compiled FreeTDS with "MS db-lib source compatibility: yes".
142
+
143
+
144
+ * 0.4.0 *
145
+
146
+ * Allow SYBEICONVI errors to pass thru so that bad data is converted to ? marks.
147
+ * Build native deps using MiniPortile [Luis Lavena]
148
+ * Allow Result#fields to be called before iterating over the results.
149
+ * Two new client helper methods, #sqlsent? and #canceled?. Possible to use these to determine current
150
+ state of the client and the need to use Result#cancel to stop processing active results. It is also
151
+ safe to call Result#cancel over and over again.
152
+ * Look for the syb headers only.
153
+
154
+
155
+ * 0.3.2 *
156
+
157
+ * Small changes while testing JRuby. Using options hash for connect vs many args.
158
+
159
+
160
+ * 0.3.1 *
161
+
162
+ * Fix bad gem build.
163
+
164
+
165
+ * 0.3.0 *
166
+
167
+ * Access stored procedure return codes.
168
+ * Make sure dead or not enabled connections are handled.
169
+ * Fix bad client after timeout & read from server errors.
170
+
171
+
172
+ * 0.2.3 *
173
+
174
+ * Do not use development ruby/version, but simple memoize an eval check on init to find out, for 1.8.6 reflection.
175
+
176
+
177
+ * 0.2.2 *
178
+
179
+ * Fixed failing test in Ruby 1.8.6. DateTime doesn't support fractional seconds greater than 59.
180
+ See: http://redmine.ruby-lang.org/issues/show/1490 [Erik Bryn]
181
+
182
+
183
+ * 0.2.1 *
184
+
185
+ * Compatibility with 32-bit systems. Better cross language testing. [Klaus Gundermann]
186
+
187
+
188
+ * 0.2.0 *
189
+
190
+ * Convert GUID's in a more compatible way. [Klaus Gundermann]
191
+ * Handle multiple result sets in command buffer or stored procs. [Ken Collins]
192
+ * Fixed some compiler warnings. [Erik Bryn]
193
+ * Avoid segfault related to smalldatetime conversion. [Erik Bryn]
194
+ * Properly encode column names in 1.9. [Erik Bryn]
195
+
196
+
197
+ * 0.1.0 Initial release!
198
+
@@ -0,0 +1,31 @@
1
+ Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all
4
+ people who contribute through reporting issues, posting feature requests,
5
+ updating documentation, submitting pull requests or patches, and other
6
+ activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, or religion.
12
+
13
+ Examples of unacceptable behavior by participants include the use of sexual
14
+ language or imagery, derogatory comments or personal attacks, trolling, public
15
+ or private harassment, insults, or other unprofessional conduct.
16
+
17
+ Project maintainers have the right and responsibility to remove, edit, or
18
+ reject comments, commits, code, wiki edits, issues, and other contributions
19
+ that are not aligned to this Code of Conduct. Project maintainers who do not
20
+ follow the Code of Conduct may be removed from the project team.
21
+
22
+ This code of conduct applies both within project spaces and in public spaces
23
+ when an individual is representing the project or its community.
24
+
25
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
26
+ reported by opening an issue or contacting one or more of the project
27
+ maintainers.
28
+
29
+ This Code of Conduct is adapted from the Contributor Covenant
30
+ (http://contributor-covenant.org), version 1.1.0, available at
31
+ http://contributor-covenant.org/version/1/1/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'byebug'
6
+ end
data/ISSUE_TEMPLATE.md ADDED
@@ -0,0 +1,5 @@
1
+ Having problems? Have you checked these first:
2
+
3
+ * Are you using FreeTDS 0.95.80 or later? Check `$ tsql -C` to find out.
4
+ * If not, please update then uninstall the TinyTDS gem and re-install it.
5
+ * Are you using Ubuntu? If so, you may have forgotten to install FreeTDS first.
data/MIT-LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2010-2014 Ken Collins <ken@metaskills.net>,
2
+ Will Bond (Veracross LLC) <wbond@breuer.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person
5
+ obtaining a copy of this software and associated documentation
6
+ files (the "Software"), to deal in the Software without
7
+ restriction, including without limitation the rights to use,
8
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following
11
+ conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,427 @@
1
+ # TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
2
+
3
+ This is a forked version of [TinyTDS](https://github.com/rails-sqlserver/tiny_tds), with the changes suggested in this [PR](https://github.com/rails-sqlserver/tiny_tds/pull/293).
4
+
5
+ The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server or Sybase databases from Ruby using the FreeTDS's DB-Library API.
6
+
7
+ TinyTDS offers automatic casting to Ruby primitives along with proper encoding support. It converts all SQL Server datatypes to native Ruby primitives while supporting :utc or :local time zones for time-like types. To date it is the only Ruby client library that allows client encoding options, defaulting to UTF-8, while connecting to SQL Server. It also properly encodes all string and binary data. The motivation for TinyTDS is to become the de-facto low level connection mode for the SQL Server Adapter for ActiveRecord.
8
+
9
+ The API is simple and consists of these classes:
10
+
11
+ * TinyTds::Client - Your connection to the database.
12
+ * TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
13
+ * TinyTds::Error - A wrapper for all FreeTDS exceptions.
14
+
15
+
16
+ ## Install
17
+
18
+ Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 2.0.0 and upward.
19
+
20
+ ```
21
+ $ gem install tiny_tds_vagas
22
+ ```
23
+
24
+ If you use Windows, we pre-compile TinyTDS with static versions of FreeTDS, libiconv, and OpenSSL. On all other platforms, we will find these dependencies. If none exist, our native extension will use MiniPortile to install any missing dependencies listed above for your specific platform. These dependencies will be built and linked within the installed TinyTDS gem. Please read the MiniPortile and/or Windows sections at the end of this file for advanced configuration options past the following:
25
+
26
+ ```
27
+ --enable-system-freetds / --disable-system-freetds
28
+ --enable-system-iconv / --disable-system-iconv
29
+ --enable-system-openssl / --disable-system-openssl
30
+ Force use of system or builtin freetds/iconv/openssl library.
31
+ Default is to prefer system libraries and fallback to builtin.
32
+
33
+ --with-freetds-dir=DIR
34
+ Use the freetds library placed under DIR.
35
+
36
+ --enable-lookup
37
+ Search for freetds through all paths in the PATH environment variable.
38
+
39
+ --enable-cross-build
40
+ Do cross-build.
41
+ ```
42
+
43
+
44
+ ## FreeTDS Compatibility & Configuration
45
+
46
+ TinyTDS is developed against FreeTDS 0.95, 0.99, and 1.0 current. Our default and recommended is 1.0. We also test with SQL Server 2008, 2014, and Azure. However, usage of TinyTDS with SQL Server 2000 or 2005 should be just fine. Below are a few QA style notes about installing FreeTDS.
47
+
48
+ **NOTE:** Windows users of our pre-compiled native gems need not worry about installing FreeTDS and its dependencies.
49
+
50
+ * **Do I need to install FreeTDS?** Yes! Somehow, someway, you are going to need FreeTDS for TinyTDS to compile against. You can avoid installing FreeTDS on your system by using our projects usage of rake-compiler and mini_portile to compile and package a native gem just for you. See the "Using MiniPortile" section below.
51
+
52
+ * **OK, I am installing FreeTDS, how do I configure it?** Contrary to what most people think, you do not need to specially configure FreeTDS in any way for client libraries like TinyTDS to use it. About the only requirement is that you compile it with libiconv for proper encoding support. FreeTDS must also be compiled with OpenSSL (or the like) to use it with Azure. See the "Using TinyTDS with Azure" section below for more info.
53
+
54
+ * **Do I need to configure `--with-tdsver` equal to anything?** Most likely! Technically you should not have too. This is only a default for clients/configs that do not specify what TDS version they want to use. We are currently having issues with passing down a TDS version with the login bit. Till we get that fixed, if you are not using a freetds.conf or a TDSVER environment variable, then make sure to use 7.1.
55
+
56
+ * **But I want to use TDS version 7.2 for SQL Server 2005 and up!** TinyTDS uses TDS version 7.1 (previously named 8.0) and fully supports all the data types supported by FreeTDS, this includes `varchar(max)` and `nvarchar(max)`. Technically compiling and using TDS version 7.2 with FreeTDS is not supported. But this does not mean those data types will not work. I know, it's confusing If you want to learn more, read this thread. http://lists.ibiblio.org/pipermail/freetds/2011q3/027306.html
57
+
58
+ * **I want to configure FreeTDS using `--enable-msdblib` and/or `--enable-sybase-compat` so it works for my database. Cool?** It's a waste of time and totally moot! Client libraries like TinyTDS define their own C structure names where they diverge from Sybase to SQL Server. Technically we use the MSDBLIB structures which does not mean we only work with that database vs Sybase. These configs are just a low level default for C libraries that do not define what they want. So I repeat, you do not NEED to use any of these, nor will they hurt anything since we control what C structure names we use internally!
59
+
60
+
61
+ ## Data Types
62
+
63
+ Our goal is to support every SQL Server data type and covert it to a logical Ruby object. When dates or times are returned, they are instantiated to either `:utc` or `:local` time depending on the query options. Only [datetimeoffset] types are excluded. All strings are associated the to the connection's encoding and all binary data types are associated to Ruby's `ASCII-8BIT/BINARY` encoding.
64
+
65
+ Below is a list of the data types we support when using the 7.3 TDS protocol version. Using a lower protocol version will result in these types being returned as strings.
66
+
67
+ * [date]
68
+ * [datetime2]
69
+ * [datetimeoffset]
70
+ * [time]
71
+
72
+
73
+ ## TinyTds::Client Usage
74
+
75
+ Connect to a database.
76
+
77
+ ```ruby
78
+ client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'
79
+ ```
80
+
81
+ Creating a new client takes a hash of options. For valid iconv encoding options, see the output of `iconv -l`. Only a few have been tested and highly recommended to leave blank for the UTF-8 default.
82
+
83
+ * :username - The database server user.
84
+ * :password - The user password.
85
+ * :dataserver - Can be the name for your data server as defined in freetds.conf. Raw hostname or hostname:port will work here too. FreeTDS says that named instance like 'localhost\SQLEXPRESS' work too, but I highly suggest that you use the :host and :port options below. [Google how to find your host port if you are using named instances](http://bit.ly/xAf2jm) or [go here](http://msdn.microsoft.com/en-us/library/ms181087.aspx).
86
+ * :host - Used if :dataserver blank. Can be an host name or IP.
87
+ * :port - Defaults to 1433. Only used if :host is used.
88
+ * :database - The default database to use.
89
+ * :appname - Short string seen in SQL Servers process/activity window.
90
+ * :tds_version - TDS version. Defaults to "7.3".
91
+ * :login_timeout - Seconds to wait for login. Default to 60 seconds.
92
+ * :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds.
93
+ * :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
94
+ * :azure - Pass true to signal that you are connecting to azure.
95
+
96
+ Use the `#active?` method to determine if a connection is good. The implementation of this method may change but it should always guarantee that a connection is good. Current it checks for either a closed or dead connection.
97
+
98
+ ```ruby
99
+ client.dead? # => false
100
+ client.closed? # => false
101
+ client.active? # => true
102
+ client.execute("SQL TO A DEAD SERVER")
103
+ client.dead? # => true
104
+ client.closed? # => false
105
+ client.active? # => false
106
+ client.close
107
+ client.closed? # => true
108
+ client.active? # => false
109
+ ```
110
+
111
+ Escape strings.
112
+
113
+ ```ruby
114
+ client.escape("How's It Going'") # => "How''s It Going''"
115
+ ```
116
+
117
+ Send a SQL string to the database and return a TinyTds::Result object.
118
+
119
+ ```ruby
120
+ result = client.execute("SELECT * FROM [datatypes]")
121
+ ```
122
+
123
+
124
+ ## TinyTds::Result Usage
125
+
126
+ A result object is returned by the client's execute command. It is important that you either return the data from the query, most likely with the #each method, or that you cancel the results before asking the client to execute another SQL batch. Failing to do so will yield an error.
127
+
128
+ Calling #each on the result will lazily load each row from the database.
129
+
130
+ ```ruby
131
+ result.each do |row|
132
+ # By default each row is a hash.
133
+ # The keys are the fields, as you'd expect.
134
+ # The values are pre-built Ruby primitives mapped from their corresponding types.
135
+ end
136
+ ```
137
+
138
+ A result object has a `#fields` accessor. It can be called before the result rows are iterated over. Even if no rows are returned, #fields will still return the column names you expected. Any SQL that does not return columned data will always return an empty array for `#fields`. It is important to remember that if you access the `#fields` before iterating over the results, the columns will always follow the default query option's `:symbolize_keys` setting at the client's level and will ignore the query options passed to each.
139
+
140
+ ```ruby
141
+ result = client.execute("USE [tinytdstest]")
142
+ result.fields # => []
143
+ result.do
144
+
145
+ result = client.execute("SELECT [id] FROM [datatypes]")
146
+ result.fields # => ["id"]
147
+ result.cancel
148
+ result = client.execute("SELECT [id] FROM [datatypes]")
149
+ result.each(:symbolize_keys => true)
150
+ result.fields # => [:id]
151
+ ```
152
+
153
+ You can cancel a result object's data from being loading by the server.
154
+
155
+ ```ruby
156
+ result = client.execute("SELECT * FROM [super_big_table]")
157
+ result.cancel
158
+ ```
159
+
160
+ You can use results cancelation in conjunction with results lazy loading, no problem.
161
+
162
+ ```ruby
163
+ result = client.execute("SELECT * FROM [super_big_table]")
164
+ result.each_with_index do |row, i|
165
+ break if row > 10
166
+ end
167
+ result.cancel
168
+ ```
169
+
170
+ If the SQL executed by the client returns affected rows, you can easily find out how many.
171
+
172
+ ```ruby
173
+ result.each
174
+ result.affected_rows # => 24
175
+ ```
176
+
177
+ This pattern is so common for UPDATE and DELETE statements that the #do method cancels any need for loading the result data and returns the `#affected_rows`.
178
+
179
+ ```ruby
180
+ result = client.execute("DELETE FROM [datatypes]")
181
+ result.do # => 72
182
+ ```
183
+
184
+ Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.
185
+
186
+ ```ruby
187
+ result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
188
+ result.insert # => 420
189
+ ```
190
+
191
+ The result object can handle multiple result sets form batched SQL or stored procedures. It is critical to remember that when calling each with a block for the first time will return each "row" of each result set. Calling each a second time with a block will yield each "set".
192
+
193
+ ```ruby
194
+ sql = ["SELECT TOP (1) [id] FROM [datatypes]",
195
+ "SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
196
+
197
+ set1, set2 = client.execute(sql).each
198
+ set1 # => [{"id"=>11}]
199
+ set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
200
+
201
+ result = client.execute(sql)
202
+
203
+ result.each do |rowset|
204
+ # First time data loading, yields each row from each set.
205
+ # 1st: {"id"=>11}
206
+ # 2nd: {"bigint"=>-9223372036854775807}
207
+ # 3rd: {"bigint"=>9223372036854775806}
208
+ end
209
+
210
+ result.each do |rowset|
211
+ # Second time over (if columns cached), yields each set.
212
+ # 1st: [{"id"=>11}]
213
+ # 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
214
+ end
215
+ ```
216
+
217
+ Use the `#sqlsent?` and `#canceled?` query methods on the client to determine if an active SQL batch still needs to be processed and or if data results were canceled from the last result object. These values reset to true and false respectively for the client at the start of each `#execute` and new result object. Or if all rows are processed normally, `#sqlsent?` will return false. To demonstrate, lets assume we have 100 rows in the result object.
218
+
219
+ ```ruby
220
+ client.sqlsent? # = false
221
+ client.canceled? # = false
222
+
223
+ result = client.execute("SELECT * FROM [super_big_table]")
224
+
225
+ client.sqlsent? # = true
226
+ client.canceled? # = false
227
+
228
+ result.each do |row|
229
+ # Assume we break after 20 rows with 80 still pending.
230
+ break if row["id"] > 20
231
+ end
232
+
233
+ client.sqlsent? # = true
234
+ client.canceled? # = false
235
+
236
+ result.cancel
237
+
238
+ client.sqlsent? # = false
239
+ client.canceled? # = true
240
+ ```
241
+
242
+ It is possible to get the return code after executing a stored procedure from either the result or client object.
243
+
244
+ ```ruby
245
+ client.return_code # => nil
246
+
247
+ result = client.execute("EXEC tinytds_TestReturnCodes")
248
+ result.do
249
+ result.return_code # => 420
250
+ client.return_code # => 420
251
+ ```
252
+
253
+
254
+ ## Query Options
255
+
256
+ Every `TinyTds::Result` object can pass query options to the #each method. The defaults are defined and configurable by setting options in the `TinyTds::Client.default_query_options` hash. The default values are:
257
+
258
+ * :as => :hash - Object for each row yielded. Can be set to :array.
259
+ * :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
260
+ * :cache_rows => true - Successive calls to #each returns the cached rows.
261
+ * :timezone => :local - Local to the Ruby client or :utc for UTC.
262
+ * :empty_sets => true - Include empty results set in queries that return multiple result sets.
263
+
264
+ Each result gets a copy of the default options you specify at the client level and can be overridden by passing an options hash to the #each method. For example
265
+
266
+ ```ruby
267
+ result.each(:as => :array, :cache_rows => false) do |row|
268
+ # Each row is now an array of values ordered by #fields.
269
+ # Rows are yielded and forgotten about, freeing memory.
270
+ end
271
+ ```
272
+
273
+ Besides the standard query options, the result object can take one additional option. Using `:first => true` will only load the first row of data and cancel all remaining results.
274
+
275
+ ```ruby
276
+ result = client.execute("SELECT * FROM [super_big_table]")
277
+ result.each(:first => true) # => [{'id' => 24}]
278
+ ```
279
+
280
+
281
+ ## Row Caching
282
+
283
+ By default row caching is turned on because the SQL Server adapter for ActiveRecord would not work without it. I hope to find some time to create some performance patches for ActiveRecord that would allow it to take advantages of lazily created yielded rows from result objects. Currently only TinyTDS and the Mysql2 gem allow such a performance gain.
284
+
285
+
286
+ ## Encoding Error Handling
287
+
288
+ TinyTDS takes an opinionated stance on how we handle encoding errors. First, we treat errors differently on reads vs. writes. Our opinion is that if you are reading bad data due to your client's encoding option, you would rather just find `?` marks in your strings vs being blocked with exceptions. This is how things wold work via ODBC or SMS. On the other hand, writes will raise an exception. In this case we raise the SYBEICONVO/2402 error message which has a description of `Error converting characters into server's character set. Some character(s) could not be converted.`. Even though the severity of this message is only a `4` and TinyTDS will automatically strip/ignore unknown characters, we feel you should know that you are inserting bad encodings. In this way, a transaction can be rolled back, etc. Remember, any database write that has bad characters due to the client encoding will still be written to the database, but it is up to you rollback said write if needed. Most ORMs like ActiveRecord handle this scenario just fine.
289
+
290
+
291
+ ## Binstubs
292
+
293
+ The TinyTDS gem uses binstub wrappers which mirror compiled [FreeTDS Utilities](http://www.freetds.org/userguide/usefreetds.htm) binaries. These native executables are usually installed at the system level when installing FreeTDS. However, when using MiniPortile to install TinyTDS as we do with Windows binaries, these binstubs will find and prefer local gem `exe` directory executables. These are the following binstubs we wrap.
294
+
295
+ * tsql - Used to test connections and debug compile time settings.
296
+ * defncopy - Used to dump schema structures.
297
+
298
+
299
+ ## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
300
+
301
+ TinyTDS is the default connection mode for the SQL Server adapter in versions 3.1 or higher. The SQL Server adapter can be found using the links below.
302
+
303
+ * ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
304
+
305
+
306
+ ## Using TinyTDS with Azure
307
+
308
+ TinyTDS is fully tested with the Azure platform. You must set the `azure: true` connection option when connecting. This is needed to specify the default database name in the login packet since Azure has no notion of `USE [database]`. FreeTDS must be compiled with OpenSSL too.
309
+
310
+ **IMPORTANT**: Do not use `username@server.database.windows.net` for the username connection option! You must use the shorter `username@server` instead!
311
+
312
+ We recommend the following settings when using TinyTDS with Azure. These are the same settings used in the ActiveRecord SQL Server adapter.
313
+
314
+ ```sql
315
+ SET ANSI_NULLS ON
316
+ SET CURSOR_CLOSE_ON_COMMIT OFF
317
+ SET ANSI_NULL_DFLT_ON ON
318
+ SET IMPLICIT_TRANSACTIONS OFF
319
+ SET ANSI_PADDING ON
320
+ SET QUOTED_IDENTIFIER ON
321
+ SET ANSI_WARNINGS ON
322
+ ```
323
+
324
+ Also, please read the [Azure SQL Database General Guidelines and Limitations](https://msdn.microsoft.com/en-us/library/ee336245.aspx) MSDN article to understand the differences. Specifically, the connection constraints section!
325
+
326
+
327
+ ## Emoji Support 😍
328
+
329
+ This is possible using FreeTDS version 0.95 or higher. You must add the following config to your `freetds.conf` in either the global section or a specfic dataserver. If you are on Windows, the default location for your conf file will be in `C:\Sites`.
330
+
331
+ ```ini
332
+ [global]
333
+ use utf-16 = true
334
+ ```
335
+
336
+ Good news! If you are using FreeTDS v1.0 or later, then `use utf-16` is set true by default!
337
+
338
+
339
+ ## Using MiniPortile
340
+
341
+ MiniPortile is a minimalistic implementation of a port/recipe system. <https://github.com/luislavena/mini_portile>
342
+
343
+ The TinyTDS project uses MiniPortile so that we can easily install a local version of FreeTDS and supporting libraries to link against when building a test version of TinyTDS. This same system is also used when installing TinyTDS with Rubygems and building native extensions. It is possible to build TinyTDS with a specific version of FreeTDS using the `TINYTDS_FREETDS_VERSION` environment variable. Here are some exampbles of possible values.
344
+
345
+ ```
346
+ $ rake TDSVER='7.1' TINYTDS_FREETDS_VERSION='0.95' -- --disable-system-freetds --disable-system-iconv
347
+ ```
348
+
349
+ To find out more about the FreeTDS release system [visit this thread](http://lists.ibiblio.org/pipermail/freetds/2012q1/027756.html) on their mailing list. You can also browse thier FTP server [ftp://ftp.astron.com/pub/freetds/](ftp://ftp.astron.com/pub/freetds/) for version number strings.
350
+
351
+
352
+ ## Compiling Gems for Windows
353
+
354
+ For the convenience of Windows users, TinyTDS ships pre-compiled gems for Ruby 2.0, 2.1, 2.2, and 2.3 on Windows. In order to generate these gems, [rake-compiler-dock](https://github.com/rake-compiler/rake-compiler-dock) is used. This project provides a [Docker image](https://registry.hub.docker.com/u/larskanis/rake-compiler-dock/) with rvm, cross-compilers and a number of different target versions of Ruby.
355
+
356
+ Run the following rake task to compile the gems for Windows. This will check the availability of [Docker](https://www.docker.com/) (and boot2docker on Windows or OS-X) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build:
357
+
358
+ ```
359
+ $ rake gem:windows
360
+ ```
361
+
362
+ The compiled gems will exist in `./pkg` directory.
363
+
364
+
365
+ ## Development & Testing
366
+
367
+ First make sure your local database has a `[tinytdstest]` database with a owner login named `[tinytds]` having no password. The following SQL run via the `sa` account should set that up for you.
368
+
369
+ ```bash
370
+ $ tsql -H <host> -p 1433 -U sa -P <pass>
371
+ $ sqsh -H <host> -p 1433 -U sa -P <pass>
372
+ ```
373
+
374
+ ```sql
375
+ CREATE DATABASE [tinytdstest];
376
+ GO
377
+ CREATE LOGIN [tinytds] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [tinytdstest];
378
+ GO
379
+ USE [tinytdstest];
380
+ CREATE USER [tinytds] FOR LOGIN [tinytds];
381
+ GO
382
+ EXEC sp_addrolemember N'db_owner', N'tinytds';
383
+ GO
384
+ ```
385
+
386
+ We use bundler for development. Simply run `bundle install` then `rake` to build the gem and run the unit tests. Before running the test rake task, you may need to define a pair of environment variables that help the client connect to your specific FreeTDS database server name and which schema (2000, 2005, 2008, 2014, Azure or Sybase ASE) to use. For example:
387
+
388
+ ```
389
+ $ rake TINYTDS_UNIT_DATASERVER=mydbserver
390
+ or
391
+ $ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
392
+ or
393
+ $ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
394
+ or
395
+ $ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase
396
+ ```
397
+
398
+ If you do not want to use MiniPortile to compile a local project version of FreeTDS and instead use your local system version, use the `TINYTDS_SKIP_PORTS` environment variable. This will ignore any port tasks and will instead build and link to your system's FreeTDS installation as a normal gem install would.
399
+
400
+ ```
401
+ $ rake TINYTDS_SKIP_PORTS=1
402
+ ```
403
+
404
+
405
+ ## Help & Support
406
+
407
+ * Github Source: https://github.com/VAGAScom/tiny_tds_vagas
408
+ * Github Issues: https://github.com/VAGAScom/tiny_tds_vagas/issues
409
+
410
+
411
+ ## About Me
412
+
413
+ My name is Ken Collins and I currently maintain the SQL Server adapter for ActiveRecord and wrote this library as my first cut into learning Ruby C extensions. Hopefully it will help promote the power of Ruby and the Rails framework to those that have not yet discovered it. My blog is [metaskills.net](http://metaskills.net/) and I can be found on twitter as @metaskills. Enjoy!
414
+
415
+
416
+ ## Special Thanks
417
+
418
+ * Lars Kanis for all his help getting the Windows builds working again with rake-compiler-dock.
419
+ * Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
420
+ * To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
421
+ * Yehuda Katz for articulating Ruby's need for proper encoding support. Especially in database drivers - http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/
422
+ * Josh Clayton of Thoughtbot for writing about Ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
423
+
424
+
425
+ ## License
426
+
427
+ TinyTDS is Copyright (c) 2010-2015 Ken Collins, <ken@metaskills.net> and Will Bond (Veracross LLC) <wbond@breuer.com>. It is distributed under the MIT license. Windows binaries contain pre-compiled versions of FreeTDS <http://www.freetds.org/> which is licensed under the GNU LGPL license at <http://www.gnu.org/licenses/lgpl-2.0.html>