tiny_tds 0.6.2-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +19 -0
- data/CHANGELOG +180 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +23 -0
- data/README.md +385 -0
- data/Rakefile +114 -0
- data/compile/rake-compiler-dev-box.patch +31 -0
- data/ext/patch/Makefile.in.diff +29 -0
- data/ext/patch/dblib-30-char-username.diff +11 -0
- data/ext/patch/sspi_w_kerberos.diff +42 -0
- data/ext/tiny_tds/client.c +408 -0
- data/ext/tiny_tds/client.h +46 -0
- data/ext/tiny_tds/extconf.rb +102 -0
- data/ext/tiny_tds/result.c +599 -0
- data/ext/tiny_tds/result.h +36 -0
- data/ext/tiny_tds/tiny_tds_ext.c +12 -0
- data/ext/tiny_tds/tiny_tds_ext.h +15 -0
- data/lib/tiny_tds.rb +19 -0
- data/lib/tiny_tds/client.rb +96 -0
- data/lib/tiny_tds/error.rb +29 -0
- data/lib/tiny_tds/result.rb +8 -0
- data/lib/tiny_tds/version.rb +3 -0
- data/tasks/ports.rake +79 -0
- data/test/benchmark/query.rb +77 -0
- data/test/benchmark/query_odbc.rb +106 -0
- data/test/benchmark/query_tinytds.rb +126 -0
- data/test/client_test.rb +170 -0
- data/test/result_test.rb +732 -0
- data/test/schema/1px.gif +0 -0
- data/test/schema/sqlserver_2000.sql +138 -0
- data/test/schema/sqlserver_2005.sql +138 -0
- data/test/schema/sqlserver_2008.sql +138 -0
- data/test/schema/sqlserver_azure.sql +138 -0
- data/test/schema/sybase_ase.sql +138 -0
- data/test/schema_test.rb +305 -0
- data/test/test_helper.rb +195 -0
- data/test/thread_test.rb +95 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
N2U1OTJlMDBiNjFlYWYzMjE3NjQ1NWJiMDQ4MmEzMjExNWU1YTZhZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWE2MDAzM2Y5ZGQ0NWVkNmUwNTFkODBhMjYyNjdiNjZlYWRiYTBkYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YWQxNDcwZmQ0YTU0OGM5MjgxOTE5M2Y1MDJhNzMzZWNlMzE0YmZlMDY1ZTE3
|
10
|
+
NGNmNGQ0N2FkMGExNWVjNWNkOWNkNmVmODE4NGE5MTA1YzAwMmI0ZTZmYzI0
|
11
|
+
ZDhmZGI1NThlYmI4MTBhOWM0OTZmNjc1ZmNlZDc3MWRlNjY2ZTM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTUxYWQ3NjRjNThmNmRiY2UzNTZmMzAzNjFiOTU5OGJjYThjN2NiZmVkYjMy
|
14
|
+
ZTdmY2YxNDQwOWRjNmZhNDgzZmQ0MmRhMWE3MjA0OWEyNzNiZmZkZjE3N2U5
|
15
|
+
MDljYmY3YTlhZDZkMDI2NjY0ZGJlZTU3OTFjMjQ3MjFlZDg5ZmU=
|
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
|
2
|
+
* 0.6.2 *
|
3
|
+
|
4
|
+
* Support an optional environment variable to find FreeTDS. Fixes #128.
|
5
|
+
|
6
|
+
* Allow Support for 31+ Character Usernames/Passwords. Fixes #134. Thanks @wbond.
|
7
|
+
|
8
|
+
* Stronger Global VM Lock support for nonblocking connections. Fixes #133. Thanks @wbond.
|
9
|
+
|
10
|
+
* Timeout fix for working with Azure SQL. Fixes #138.
|
11
|
+
|
12
|
+
* Correctly handle requests that return multiple results sets via `.do`, such
|
13
|
+
as backups and restores. Fixes #150.
|
14
|
+
|
15
|
+
|
16
|
+
* 0.6.1 *
|
17
|
+
|
18
|
+
Use both dbsetversion() vs. dbsetlversion. Partially reverts #62.
|
19
|
+
|
20
|
+
|
21
|
+
* 0.6.0 *
|
22
|
+
|
23
|
+
* Use dbsetversion() vs. dbsetlversion. Fixes #62.
|
24
|
+
|
25
|
+
* Remove Ruby 1.8 support.
|
26
|
+
|
27
|
+
* Implement misc rb_thread_blocking_region support. Fixes #121. Thanks @lepfhty.
|
28
|
+
|
29
|
+
* Test FreeTDS v0.91.89 patch release.
|
30
|
+
|
31
|
+
* Fix lost connection handling. Fixes #124. Thanks @krzcho.
|
32
|
+
|
33
|
+
* Remove unused variable. Fixes #103. Thanks @jeremyevans.
|
34
|
+
|
35
|
+
* Remove need to specify username for Windows Authentication.
|
36
|
+
|
37
|
+
* Use proper SQL for returning IDENTITY with Sybase. Fixes #95.
|
38
|
+
|
39
|
+
* Compile windows with `--enable-sspi`.
|
40
|
+
|
41
|
+
* Allow MiniPortile to build any FreeTDS version we need. Fixes #76.
|
42
|
+
|
43
|
+
* Always convert password option to string. Fixes #92.
|
44
|
+
|
45
|
+
* Move test system to real MiniTest::Spec. All tests pass on Azure too.
|
46
|
+
|
47
|
+
* Raise and handle encoding errors on DB writes. Fixes #89.
|
48
|
+
|
49
|
+
|
50
|
+
* 0.5.1 *
|
51
|
+
|
52
|
+
* Change how we configure with iconv, basically it is always needed. Fixes #11 & #69.
|
53
|
+
|
54
|
+
|
55
|
+
* 0.5.0 *
|
56
|
+
|
57
|
+
* Copy mysql2s handling of Time and Datetime so 64bit systems are leveraged. Fixes #46 and #47. Thanks @lsylvester!
|
58
|
+
|
59
|
+
* Add CFLAGS='-fPIC' for libtool. Fix TDS version configs in our ports file. Document. Fixes #45
|
60
|
+
|
61
|
+
* Update our TDS version constants to reflect changed 8.0/9.0 to 7.1/7.2 DBLIB versions in FreeTDS
|
62
|
+
while making it backward compatible, again like FreeTDS. Even tho you can not configure FreeTDS with
|
63
|
+
TDS version 7.2 or technically even use it, I added tests to prove that we correctly handle both
|
64
|
+
varchar(max) and nvarchar(max) with large amounts of data.
|
65
|
+
|
66
|
+
* FreeTDS 0.91 has been released. Update our port scripts.
|
67
|
+
|
68
|
+
* Add test for 0.91 and higher to handle incorrect syntax in sp_executesql.
|
69
|
+
|
70
|
+
* Returning empty result sets with a command batch that has multiple statements is now the default. Use :empty_sets => false to override.
|
71
|
+
|
72
|
+
* Do not raise a TinyTds::Error with our message handler unless the severity is greater than 10.
|
73
|
+
|
74
|
+
|
75
|
+
* 0.4.5 *
|
76
|
+
|
77
|
+
* Includes precompiled Windows binaries for FreeTDS 0.91rc2 & LibIconv. No precompiled OpenSSL yet for Windows to SQL Azure.
|
78
|
+
|
79
|
+
* Fixed symbolized unicode column names.
|
80
|
+
|
81
|
+
* Use same bigint ruby functions to return identity. Hopefully fixes #19.
|
82
|
+
|
83
|
+
* Release static libs for Windows.
|
84
|
+
|
85
|
+
* Change how :host/:port are implemented. Now sending "host:port" to :dataserver.
|
86
|
+
|
87
|
+
|
88
|
+
* 0.4.4 *
|
89
|
+
|
90
|
+
* New :host/:port connection options. Removes need for freetds.conf file.
|
91
|
+
|
92
|
+
|
93
|
+
* 0.4.3 *
|
94
|
+
|
95
|
+
* New Client#active? method to check for good connection. Always use this abstract method.
|
96
|
+
|
97
|
+
* Better SYBEWRIT "Write to SQL Server failed." error handling. New Client#dead? check.
|
98
|
+
|
99
|
+
* Azure tested using latest FreeTDS with submitted patch. https://gist.github.com/889190
|
100
|
+
|
101
|
+
|
102
|
+
* 0.4.2 *
|
103
|
+
|
104
|
+
* Iconv is a dep only when compiling locally. However, left in the ability to configure
|
105
|
+
it for native gem installation but you must use
|
106
|
+
--enable-iconv before using --with-iconv-dir=/some/dir
|
107
|
+
|
108
|
+
* Really fix what 0.4.1 was supposed to do, force SYBDBLIB compile.
|
109
|
+
|
110
|
+
|
111
|
+
* 0.4.1 *
|
112
|
+
|
113
|
+
* Undefine MSDBLIB in case others have explicitly compiled FreeTDS with "MS db-lib source compatibility: yes".
|
114
|
+
|
115
|
+
|
116
|
+
* 0.4.0 *
|
117
|
+
|
118
|
+
* Allow SYBEICONVI errors to pass thru so that bad data is converted to ? marks.
|
119
|
+
|
120
|
+
* Build native deps using MiniPortile [Luis Lavena]
|
121
|
+
|
122
|
+
* Allow Result#fields to be called before iterating over the results.
|
123
|
+
|
124
|
+
* Two new client helper methods, #sqlsent? and #canceled?. Possible to use these to determine current
|
125
|
+
state of the client and the need to use Result#cancel to stop processing active results. It is also
|
126
|
+
safe to call Result#cancel over and over again.
|
127
|
+
|
128
|
+
* Look for the syb headers only.
|
129
|
+
|
130
|
+
|
131
|
+
* 0.3.2 *
|
132
|
+
|
133
|
+
* Small changes while testing JRuby. Using options hash for connect vs many args.
|
134
|
+
|
135
|
+
|
136
|
+
* 0.3.1 *
|
137
|
+
|
138
|
+
* Fix bad gem build.
|
139
|
+
|
140
|
+
|
141
|
+
* 0.3.0 *
|
142
|
+
|
143
|
+
* Access stored procedure return codes.
|
144
|
+
|
145
|
+
* Make sure dead or not enabled connections are handled.
|
146
|
+
|
147
|
+
* Fix bad client after timeout & read from server errors.
|
148
|
+
|
149
|
+
|
150
|
+
* 0.2.3 *
|
151
|
+
|
152
|
+
* Do not use development ruby/version, but simple memoize an eval check on init to find out, for 1.8.6 reflection.
|
153
|
+
|
154
|
+
|
155
|
+
* 0.2.2 *
|
156
|
+
|
157
|
+
* Fixed failing test in Ruby 1.8.6. DateTime doesn't support fractional seconds greater than 59.
|
158
|
+
See: http://redmine.ruby-lang.org/issues/show/1490 [Erik Bryn]
|
159
|
+
|
160
|
+
|
161
|
+
* 0.2.1 *
|
162
|
+
|
163
|
+
* Compatibility with 32-bit systems. Better cross language testing. [Klaus Gundermann]
|
164
|
+
|
165
|
+
|
166
|
+
* 0.2.0 *
|
167
|
+
|
168
|
+
* Convert GUID's in a more compatible way. [Klaus Gundermann]
|
169
|
+
|
170
|
+
* Handle multiple result sets in command buffer or stored procs. [Ken Collins]
|
171
|
+
|
172
|
+
* Fixed some compiler warnings. [Erik Bryn]
|
173
|
+
|
174
|
+
* Avoid segfault related to smalldatetime conversion. [Erik Bryn]
|
175
|
+
|
176
|
+
* Properly encode column names in 1.9. [Erik Bryn]
|
177
|
+
|
178
|
+
|
179
|
+
* 0.1.0 Initial release!
|
180
|
+
|
data/Gemfile
ADDED
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,385 @@
|
|
1
|
+
# TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library.
|
2
|
+
|
3
|
+
The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server databases from ruby. Even though it uses FreeTDS's DB-Library, it is NOT meant to serve as direct 1:1 mapping of that C API.
|
4
|
+
|
5
|
+
The benefits are speed, automatic casting to ruby primitives, and proper encoding support. It converts all SQL Server datatypes to native ruby objects 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. For further details see the special thanks section at the bottom
|
6
|
+
|
7
|
+
The API is simple and consists of these classes:
|
8
|
+
|
9
|
+
* TinyTds::Client - Your connection to the database.
|
10
|
+
* TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
|
11
|
+
* TinyTds::Error - A wrapper for all FreeTDS exceptions.
|
12
|
+
|
13
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 1.9.3 and upward. Older versions are tested with Ruby 1.8 and lower. If you are using Ruby 1.8, please use v0.6.0.rc1 or lower. The v0.6.0 release and higher are only compatible with Ruby 1.9.3 (or higher).
|
17
|
+
|
18
|
+
```
|
19
|
+
$ gem install tiny_tds
|
20
|
+
```
|
21
|
+
|
22
|
+
Although we search for FreeTDS's libraries and headers, you may have to specify include and lib directories using `--with-freetds-include=/some/local/include/freetds` and `--with-freetds-lib=/some/local/lib`
|
23
|
+
|
24
|
+
|
25
|
+
## FreeTDS Compatibility & Configuration
|
26
|
+
|
27
|
+
TinyTDS is developed against FreeTDS 0.82 & 0.91, and 0.92 currents, the latest is recommended. It is tested with SQL Server 2000, 2005, 2008 and Azure. Below are a few QA style notes about installing FreeTDS.
|
28
|
+
|
29
|
+
* **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.
|
30
|
+
|
31
|
+
* **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.
|
32
|
+
|
33
|
+
* **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, the make sure to use 7.1 for FreeTDS 0.91 and 8.0 for FreeTDS 0.82.
|
34
|
+
|
35
|
+
* **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
|
36
|
+
|
37
|
+
* **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 Sybase structures which does not mean we only work with that database vs SQL Server. 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 and this has no affect on what database you use!
|
38
|
+
|
39
|
+
|
40
|
+
## Data Types
|
41
|
+
|
42
|
+
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. Under ruby 1.9, all strings are encoded to the connection's encoding and all binary data types are associated to ruby's `ASCII-8BIT/BINARY` encoding.
|
43
|
+
|
44
|
+
Below is a list of the data types we plan to support using future versions of FreeTDS. They are associated with SQL Server 2008. All unsupported data types are returned as properly encoded strings.
|
45
|
+
|
46
|
+
* [date]
|
47
|
+
* [datetime2]
|
48
|
+
* [datetimeoffset]
|
49
|
+
* [time]
|
50
|
+
|
51
|
+
|
52
|
+
## TinyTds::Client Usage
|
53
|
+
|
54
|
+
Connect to a database.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
client = TinyTds::Client.new(:username => 'sa', :password => 'secret', :host => 'mydb.host.net')
|
58
|
+
```
|
59
|
+
|
60
|
+
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.
|
61
|
+
|
62
|
+
* :username - The database server user.
|
63
|
+
* :password - The user password.
|
64
|
+
* :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).
|
65
|
+
* :host - Used if :dataserver blank. Can be an host name or IP.
|
66
|
+
* :port - Defaults to 1433. Only used if :host is used.
|
67
|
+
* :database - The default database to use.
|
68
|
+
* :appname - Short string seen in SQL Servers process/activity window.
|
69
|
+
* :tds_version - TDS version. Defaults to "71" (7.1) and is not recommended to change!
|
70
|
+
* :login_timeout - Seconds to wait for login. Default to 60 seconds.
|
71
|
+
* :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds.
|
72
|
+
* :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
|
73
|
+
* :azure - Pass true to signal that you are connecting to azure.
|
74
|
+
|
75
|
+
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.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
client.dead? # => false
|
79
|
+
client.closed? # => false
|
80
|
+
client.active? # => true
|
81
|
+
client.execute("SQL TO A DEAD SERVER")
|
82
|
+
client.dead? # => true
|
83
|
+
client.closed? # => false
|
84
|
+
client.active? # => false
|
85
|
+
client.close
|
86
|
+
client.closed? # => true
|
87
|
+
client.active? # => false
|
88
|
+
```
|
89
|
+
|
90
|
+
Escape strings.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
client.escape("How's It Going'") # => "How''s It Going''"
|
94
|
+
```
|
95
|
+
|
96
|
+
Send a SQL string to the database and return a TinyTds::Result object.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
result = client.execute("SELECT * FROM [datatypes]")
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
## TinyTds::Result Usage
|
104
|
+
|
105
|
+
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.
|
106
|
+
|
107
|
+
Calling #each on the result will lazily load each row from the database.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
result.each do |row|
|
111
|
+
# By default each row is a hash.
|
112
|
+
# The keys are the fields, as you'd expect.
|
113
|
+
# The values are pre-built ruby primitives mapped from their corresponding types.
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
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.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
result = client.execute("USE [tinytdstest]")
|
121
|
+
result.fields # => []
|
122
|
+
result.do
|
123
|
+
|
124
|
+
result = client.execute("SELECT [id] FROM [datatypes]")
|
125
|
+
result.fields # => ["id"]
|
126
|
+
result.cancel
|
127
|
+
result = client.execute("SELECT [id] FROM [datatypes]")
|
128
|
+
result.each(:symbolize_keys => true)
|
129
|
+
result.fields # => [:id]
|
130
|
+
```
|
131
|
+
|
132
|
+
You can cancel a result object's data from being loading by the server.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
136
|
+
result.cancel
|
137
|
+
```
|
138
|
+
|
139
|
+
If the SQL executed by the client returns affected rows, you can easily find out how many.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
result.each
|
143
|
+
result.affected_rows # => 24
|
144
|
+
```
|
145
|
+
|
146
|
+
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`.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
result = client.execute("DELETE FROM [datatypes]")
|
150
|
+
result.do # => 72
|
151
|
+
```
|
152
|
+
|
153
|
+
Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
|
157
|
+
result.insert # => 420
|
158
|
+
```
|
159
|
+
|
160
|
+
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".
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
sql = ["SELECT TOP (1) [id] FROM [datatypes]",
|
164
|
+
"SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
|
165
|
+
|
166
|
+
set1, set2 = client.execute(sql).each
|
167
|
+
set1 # => [{"id"=>11}]
|
168
|
+
set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
|
169
|
+
|
170
|
+
result = client.execute(sql)
|
171
|
+
|
172
|
+
result.each do |rowset|
|
173
|
+
# First time data loading, yields each row from each set.
|
174
|
+
# 1st: {"id"=>11}
|
175
|
+
# 2nd: {"bigint"=>-9223372036854775807}
|
176
|
+
# 3rd: {"bigint"=>9223372036854775806}
|
177
|
+
end
|
178
|
+
|
179
|
+
result.each do |rowset|
|
180
|
+
# Second time over (if columns cached), yields each set.
|
181
|
+
# 1st: [{"id"=>11}]
|
182
|
+
# 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
186
|
+
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.
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
client.sqlsent? # = false
|
190
|
+
client.canceled? # = false
|
191
|
+
|
192
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
193
|
+
|
194
|
+
client.sqlsent? # = true
|
195
|
+
client.canceled? # = false
|
196
|
+
|
197
|
+
result.each do |row|
|
198
|
+
# Assume we break after 20 rows with 80 still pending.
|
199
|
+
break if row["id"] > 20
|
200
|
+
end
|
201
|
+
|
202
|
+
client.sqlsent? # = true
|
203
|
+
client.canceled? # = false
|
204
|
+
|
205
|
+
result.cancel
|
206
|
+
|
207
|
+
client.sqlsent? # = false
|
208
|
+
client.canceled? # = true
|
209
|
+
```
|
210
|
+
|
211
|
+
It is possible to get the return code after executing a stored procedure from either the result or client object.
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
client.return_code # => nil
|
215
|
+
|
216
|
+
result = client.execute("EXEC tinytds_TestReturnCodes")
|
217
|
+
result.do
|
218
|
+
result.return_code # => 420
|
219
|
+
client.return_code # => 420
|
220
|
+
```
|
221
|
+
|
222
|
+
|
223
|
+
## Query Options
|
224
|
+
|
225
|
+
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:
|
226
|
+
|
227
|
+
* :as => :hash - Object for each row yielded. Can be set to :array.
|
228
|
+
* :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
|
229
|
+
* :cache_rows => true - Successive calls to #each returns the cached rows.
|
230
|
+
* :timezone => :local - Local to the ruby client or :utc for UTC.
|
231
|
+
* :empty_sets => true - Include empty results set in queries that return multiple result sets.
|
232
|
+
|
233
|
+
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
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
result.each(:as => :array, :cache_rows => false) do |row|
|
237
|
+
# Each row is now an array of values ordered by #fields.
|
238
|
+
# Rows are yielded and forgotten about, freeing memory.
|
239
|
+
end
|
240
|
+
```
|
241
|
+
|
242
|
+
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.
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
246
|
+
result.each(:first => true) # => [{'id' => 24}]
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
## Row Caching
|
251
|
+
|
252
|
+
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.
|
253
|
+
|
254
|
+
|
255
|
+
## Encoding Error Handling
|
256
|
+
|
257
|
+
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.
|
258
|
+
|
259
|
+
|
260
|
+
## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
|
261
|
+
|
262
|
+
As of version 2.3.11 & 3.0.3 of the adapter, you can specify a `:dblib` mode in database.yml and use TinyTDS as the low level connection mode, this is the default in versions 3.1 or higher. The SQL Server adapter can be found using the link below. Also included is a direct link to the wiki article covering common questions when using TinyTDS as the low level connection mode for the adapter.
|
263
|
+
|
264
|
+
* ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
|
265
|
+
* Using TinyTDS: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-TinyTds
|
266
|
+
|
267
|
+
|
268
|
+
## Using TinyTDS with Azure
|
269
|
+
|
270
|
+
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]`. You must use the latest FreeTDS 0.91. FreeTDS must be compiled with OpenSSL too.
|
271
|
+
|
272
|
+
IMPORTANT: Do not use `username@server.database.windows.net` for the username connection option! You must use the shorter `username@server` instead!
|
273
|
+
|
274
|
+
|
275
|
+
## Using MiniPortile
|
276
|
+
|
277
|
+
MiniPortile is a minimalistic, simplistic and stupid implementation of a port/recipe system for developers. <https://github.com/luislavena/mini_portile>
|
278
|
+
|
279
|
+
The TinyTDS project uses MiniPortile so that we can easily install a local "project specific" version of FreeTDS and supporting libraries to link against when building a test version of TinyTDS. MiniPortile is a great tool that even allows us to build statically linked components that TinyTDS relies on. Hence this allows us to publish native gems for any platform. We use this feature for gems targeted at Windows.
|
280
|
+
|
281
|
+
You too can use MiniPortile to build TinyTDS and build your own gems for your own package management needs. Here is a few simple steps that assume you have cloned a fresh copy of this repository. 1) Bundling will install all the development dependencies. 2) Running `rake compile` will basically download and install a supported version of FreeTDS in our `ports.rake` file and supporting libraries. These will all be installed into the projects tmp directory. 3) The final `rake native gem` command will build a native gem for your specific platform. 4) The native gem can be found in the `pkg` directory. The last command assumes "X" is version numbers and #{platform} will be your platform. Note that if you're using Windows make sure you are running these rake tasks with a Unix-style command line tool like [MSYS](http://www.mingw.org/wiki/MSYS).
|
282
|
+
|
283
|
+
```
|
284
|
+
$ bundle install
|
285
|
+
$ rake compile
|
286
|
+
$ rake native gem
|
287
|
+
$ gem install pkg/tiny_tds-X.X.X-#{platform}.gem
|
288
|
+
```
|
289
|
+
|
290
|
+
**Important:** You must use rubygems version 1.7.2 or higher. You will almost certainly hit a *Don't know how to build task...* error when running the `rake native gem` command if you do not. Please update rubygems! Here is a link on [how to upgrade or downgrade rubygems](http://rubygems.rubyforge.org/rubygems-update/UPGRADING_rdoc.html).
|
291
|
+
|
292
|
+
It is also possible to build a specific version of FreeTDS for your own gem or development and testing using the `TINYTDS_FREETDS_VERSION` environment variable. Here are some exampbles of possible values.
|
293
|
+
|
294
|
+
```
|
295
|
+
$ rake TINYTDS_FREETDS_VERSION="0.82"
|
296
|
+
$ rake TINYTDS_FREETDS_VERSION="0.91"
|
297
|
+
$ rake TINYTDS_FREETDS_VERSION="0.91.89"
|
298
|
+
$ rake TINYTDS_FREETDS_VERSION="0.92.405"
|
299
|
+
$ rake TINYTDS_FREETDS_VERSION="current"
|
300
|
+
```
|
301
|
+
|
302
|
+
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.
|
303
|
+
|
304
|
+
|
305
|
+
## Compiling Gems for Windows
|
306
|
+
|
307
|
+
For the convenience of Windows users, TinyTDS ships pre-compiled for Ruby 1.9.3, 2.0 and 2.1 on Windows. In order to generate these gems, [rake-compiler-dev-box](https://github.com/tjschuck/rake-compiler-dev-box) is used. This project provides a [Vagrant](http://www.vagrantup.com/) box with rvm, cross-compilers and a number of different versions of ruby.
|
308
|
+
|
309
|
+
There are a handful of rake tasks that are part of TinyTDS that make cross-compiling simple. Ensure that you have VirtualBox, Vagrant and git installed, and then run the following to download the box and perform initial setup:
|
310
|
+
|
311
|
+
```
|
312
|
+
$ rake cross-compile:setup
|
313
|
+
```
|
314
|
+
|
315
|
+
Once the setup is complete, run the following rake task to compile the gem for Windows. The first time this will take a while since the `rake-compiler` gem will need to compile a few different versions of ruby:
|
316
|
+
|
317
|
+
```
|
318
|
+
$ rake cross-compile
|
319
|
+
```
|
320
|
+
|
321
|
+
The compiled gems will exist in `./rake-compile-dev-box/tiny_tds/pkg/`.
|
322
|
+
|
323
|
+
If you want to clean up the dependent ports, temp files and resulting gems, run:
|
324
|
+
|
325
|
+
```
|
326
|
+
$ rake cross-compile:clean
|
327
|
+
```
|
328
|
+
|
329
|
+
|
330
|
+
## Development & Testing
|
331
|
+
|
332
|
+
We use bundler for development. Simply run `bundle install` then `rake` to build the gem and run the unit tests. The tests assume you have created a database named `tinytdstest` accessible by a database owner named `tinytds`. 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, Azure or Sybase ASE) to use. For example:
|
333
|
+
|
334
|
+
```
|
335
|
+
$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
|
336
|
+
or
|
337
|
+
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
|
338
|
+
or
|
339
|
+
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase
|
340
|
+
```
|
341
|
+
|
342
|
+
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.
|
343
|
+
|
344
|
+
```
|
345
|
+
$ rake TINYTDS_SKIP_PORTS=true
|
346
|
+
```
|
347
|
+
|
348
|
+
|
349
|
+
## Help & Support
|
350
|
+
|
351
|
+
* Github Source: http://github.com/rails-sqlserver/tiny_tds
|
352
|
+
* Github Issues: http://github.com/rails-sqlserver/tiny_tds/issues
|
353
|
+
* Google Group: http://groups.google.com/group/rails-sqlserver-adapter
|
354
|
+
* IRC Room: #rails-sqlserver on irc.freenode.net
|
355
|
+
|
356
|
+
|
357
|
+
## TODO List
|
358
|
+
|
359
|
+
* Include OpenSSL with Windows binaries for SQL Azure.
|
360
|
+
* Install an interrupt handler.
|
361
|
+
* Allow #escape to accept all ruby primitives.
|
362
|
+
|
363
|
+
|
364
|
+
## About Me
|
365
|
+
|
366
|
+
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 http://metaskills.net and I can be found on twitter as @metaskills. Enjoy!
|
367
|
+
|
368
|
+
|
369
|
+
## Special Thanks
|
370
|
+
|
371
|
+
* Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
|
372
|
+
* To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
|
373
|
+
* 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/
|
374
|
+
* Josh Clayton of Thoughtbot for writing about ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
|
375
|
+
|
376
|
+
|
377
|
+
## Donators
|
378
|
+
|
379
|
+
I am trying to save up for a Happy Hacking pro keyboard. Help me out via GitTip! https://www.gittip.com/metaskills/
|
380
|
+
|
381
|
+
|
382
|
+
## License
|
383
|
+
|
384
|
+
TinyTDS is Copyright (c) 2010-2014 Ken Collins, <ken@metaskills.net> and Will Bond (Veracross LLC) <wbond@breuer.com>. It is distributed under the MIT license. Windows binaries contain precompiled 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>
|
385
|
+
|