vm_tiny_tds 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +20 -0
- data/.gitattributes +1 -0
- data/.gitignore +20 -0
- data/.rubocop.yml +31 -0
- data/.travis.yml +24 -0
- data/BACKERS.md +32 -0
- data/CHANGELOG.md +255 -0
- data/CODE_OF_CONDUCT.md +31 -0
- data/Gemfile +9 -0
- data/ISSUE_TEMPLATE.md +38 -0
- data/MIT-LICENSE +23 -0
- data/README.md +504 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/appveyor.yml +51 -0
- data/bin/defncopy-ttds +3 -0
- data/bin/tsql-ttds +3 -0
- data/exe/.keep +0 -0
- data/ext/tiny_tds/client.c +451 -0
- data/ext/tiny_tds/client.h +51 -0
- data/ext/tiny_tds/extconf.rb +69 -0
- data/ext/tiny_tds/extconsts.rb +15 -0
- data/ext/tiny_tds/result.c +619 -0
- data/ext/tiny_tds/result.h +32 -0
- data/ext/tiny_tds/tiny_tds_ext.c +12 -0
- data/ext/tiny_tds/tiny_tds_ext.h +17 -0
- data/lib/tiny_tds/bin.rb +104 -0
- data/lib/tiny_tds/client.rb +136 -0
- data/lib/tiny_tds/error.rb +14 -0
- data/lib/tiny_tds/gem.rb +32 -0
- data/lib/tiny_tds/result.rb +7 -0
- data/lib/tiny_tds/version.rb +3 -0
- data/lib/tiny_tds.rb +61 -0
- data/patches/freetds/1.00.27/0001-mingw_missing_inet_pton.diff +34 -0
- data/patches/freetds/1.00.27/0002-Don-t-use-MSYS2-file-libws2_32.diff +28 -0
- data/patches/libiconv/1.14/1-avoid-gets-error.patch +17 -0
- data/tasks/native_gem.rake +14 -0
- data/tasks/package.rake +8 -0
- data/tasks/ports/freetds.rb +37 -0
- data/tasks/ports/libiconv.rb +43 -0
- data/tasks/ports/openssl.rb +78 -0
- data/tasks/ports/recipe.rb +52 -0
- data/tasks/ports.rake +87 -0
- data/tasks/test.rake +9 -0
- data/test/appveyor/dbsetup.ps1 +27 -0
- data/test/appveyor/dbsetup.sql +9 -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/bin/install-freetds.sh +20 -0
- data/test/bin/install-openssl.sh +18 -0
- data/test/bin/setup.sh +19 -0
- data/test/client_test.rb +230 -0
- data/test/gem_test.rb +179 -0
- data/test/result_test.rb +773 -0
- data/test/schema/1px.gif +0 -0
- data/test/schema/sqlserver_2000.sql +140 -0
- data/test/schema/sqlserver_2005.sql +140 -0
- data/test/schema/sqlserver_2008.sql +140 -0
- data/test/schema/sqlserver_2014.sql +140 -0
- data/test/schema/sqlserver_2016.sql +140 -0
- data/test/schema/sqlserver_azure.sql +140 -0
- data/test/schema/sybase_ase.sql +138 -0
- data/test/schema_test.rb +443 -0
- data/test/test_helper.rb +217 -0
- data/test/thread_test.rb +98 -0
- data/tiny_tds.gemspec +29 -0
- metadata +225 -0
data/README.md
ADDED
@@ -0,0 +1,504 @@
|
|
1
|
+
# TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
|
2
|
+
|
3
|
+
* [![TravisCI](https://travis-ci.org/rails-sqlserver/tiny_tds.svg?branch=master)](https://travis-ci.org/rails-sqlserver/tiny_tds) - TravisCI
|
4
|
+
* [![Build Status](https://ci.appveyor.com/api/projects/status/g2bhhbsdkx0mal55/branch/master?svg=true)](https://ci.appveyor.com/project/rails-sqlserver/tiny-tds/branch/master) - Appveyor
|
5
|
+
* [![Gem Version](https://img.shields.io/gem/v/tiny_tds.svg)](https://rubygems.org/gems/tiny_tds) - Gem Version
|
6
|
+
* [![Dependency Status](https://dependencyci.com/github/rails-sqlserver/tiny_tds/badge)](https://dependencyci.com/github/rails-sqlserver/tiny_tds) - Dependency Status
|
7
|
+
* [![Gitter chat](https://img.shields.io/badge/%E2%8A%AA%20GITTER%20-JOIN%20CHAT%20%E2%86%92-brightgreen.svg?style=flat)](https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter) - Community
|
8
|
+
|
9
|
+
## Supporting TinyTDS/Adapter
|
10
|
+
|
11
|
+
Both TinyTDS and the Rails SQL Server Adapter are MIT-licensed open source projects. Its ongoing development is made possible thanks to the support by these awesome [backers](https://github.com/rails-sqlserver/tiny_tds/blob/master/BACKERS.md). If you'd like to join them, check out our [Patreon Campaign](https://www.patreon.com/metaskills).
|
12
|
+
|
13
|
+
|
14
|
+
## About TinyTDS
|
15
|
+
|
16
|
+
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.
|
17
|
+
|
18
|
+
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.
|
19
|
+
|
20
|
+
The API is simple and consists of these classes:
|
21
|
+
|
22
|
+
* TinyTds::Client - Your connection to the database.
|
23
|
+
* TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
|
24
|
+
* TinyTds::Error - A wrapper for all FreeTDS exceptions.
|
25
|
+
|
26
|
+
|
27
|
+
## Install
|
28
|
+
|
29
|
+
Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 2.0.0 and upward.
|
30
|
+
|
31
|
+
```
|
32
|
+
$ gem install tiny_tds
|
33
|
+
```
|
34
|
+
|
35
|
+
If you use Windows, we pre-compile TinyTDS with static versions of FreeTDS and supporting libraries.
|
36
|
+
If you're using RubyInstaller the binary gem will require that devkit is installed and in your path to operate properly.
|
37
|
+
|
38
|
+
On all other platforms, we will find these dependencies. It is recommended that you install the latest FreeTDS via your method of choice. For example, here is how to install FreeTDS on Ubuntu. You might also need the `build-essential` and possibly the `libc6-dev` packages.
|
39
|
+
|
40
|
+
```shell
|
41
|
+
$ apt-get install wget
|
42
|
+
$ apt-get install build-essential
|
43
|
+
$ apt-get install libc6-dev
|
44
|
+
|
45
|
+
$ wget http://www.freetds.org/files/stable/freetds-1.00.21.tar.gz
|
46
|
+
$ tar -xzf freetds-1.00.21.tar.gz
|
47
|
+
$ cd freetds-1.00.21
|
48
|
+
$ ./configure --prefix=/usr/local --with-tdsver=7.3
|
49
|
+
$ make
|
50
|
+
$ make install
|
51
|
+
```
|
52
|
+
|
53
|
+
Please read the MiniPortile and/or Windows sections at the end of this file for advanced configuration options past the following:
|
54
|
+
|
55
|
+
```
|
56
|
+
--with-freetds-dir=DIR
|
57
|
+
Use the freetds library placed under DIR.
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
## Getting Started
|
62
|
+
|
63
|
+
Optionally, Microsoft has done a great job writing some articles on how to get started with SQL Server and Ruby using TinyTDS. Please checkout one of the following posts that match your platform.
|
64
|
+
|
65
|
+
* [SQL Server on a Mac](https://www.microsoft.com/en-us/sql-server/developer-get-started/ruby/mac)
|
66
|
+
* [SQL Server on RHEL](https://www.microsoft.com/en-us/sql-server/developer-get-started/ruby/rhel)
|
67
|
+
* [SQL Server on Ubuntu](https://www.microsoft.com/en-us/sql-server/developer-get-started/ruby/ubuntu)
|
68
|
+
|
69
|
+
|
70
|
+
## FreeTDS Compatibility & Configuration
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
**NOTE:** Windows users of our pre-compiled native gems need not worry about installing FreeTDS and its dependencies.
|
75
|
+
|
76
|
+
* **Do I need to install FreeTDS?** Yes! Somehow, someway, you are going to need FreeTDS for TinyTDS to compile against.
|
77
|
+
|
78
|
+
* **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.
|
79
|
+
|
80
|
+
* **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.
|
81
|
+
|
82
|
+
* **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
|
83
|
+
|
84
|
+
* **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!
|
85
|
+
|
86
|
+
|
87
|
+
## Data Types
|
88
|
+
|
89
|
+
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.
|
90
|
+
|
91
|
+
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.
|
92
|
+
|
93
|
+
* [date]
|
94
|
+
* [datetime2]
|
95
|
+
* [datetimeoffset]
|
96
|
+
* [time]
|
97
|
+
|
98
|
+
|
99
|
+
## TinyTds::Client Usage
|
100
|
+
|
101
|
+
Connect to a database.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'
|
105
|
+
```
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
* :username - The database server user.
|
110
|
+
* :password - The user password.
|
111
|
+
* :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).
|
112
|
+
* :host - Used if :dataserver blank. Can be an host name or IP.
|
113
|
+
* :port - Defaults to 1433. Only used if :host is used.
|
114
|
+
* :database - The default database to use.
|
115
|
+
* :appname - Short string seen in SQL Servers process/activity window.
|
116
|
+
* :tds_version - TDS version. Defaults to "7.3".
|
117
|
+
* :login_timeout - Seconds to wait for login. Default to 60 seconds.
|
118
|
+
* :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds. Prior to 1.0rc5, FreeTDS was unable to set the timeout on a per-client basis, permitting only a global timeout value. This means that if you're using an older version, the timeout values for all clients will be overwritten each time you instantiate a new `TinyTds::Client` object. If you are using 1.0rc5 or later, all clients will have an independent timeout setting as you'd expect.
|
119
|
+
* :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
|
120
|
+
* :azure - Pass true to signal that you are connecting to azure.
|
121
|
+
* :contained - Pass true to signal that you are connecting with a contained database user.
|
122
|
+
* :use_utf16 - Instead of using UCS-2 for database wide character encoding use UTF-16. Newer Windows versions use this encoding instead of UCS-2. Default true.
|
123
|
+
* :message_handler - Pass in a `call`-able object such as a `Proc` or a method to receive info messages from the database. It should have a single parameter, which will be a `TinyTds::Error` object representing the message. For example:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
opts = ... # host, username, password, etc
|
127
|
+
opts[:message_handler] = Proc.new { |m| puts m.message }
|
128
|
+
client = TinyTds::Client.new opts
|
129
|
+
# => Changed database context to 'master'.
|
130
|
+
# => Changed language setting to us_english.
|
131
|
+
client.execute("print 'hello world!'").do
|
132
|
+
# => hello world!
|
133
|
+
```
|
134
|
+
|
135
|
+
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.
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
client.dead? # => false
|
139
|
+
client.closed? # => false
|
140
|
+
client.active? # => true
|
141
|
+
client.execute("SQL TO A DEAD SERVER")
|
142
|
+
client.dead? # => true
|
143
|
+
client.closed? # => false
|
144
|
+
client.active? # => false
|
145
|
+
client.close
|
146
|
+
client.closed? # => true
|
147
|
+
client.active? # => false
|
148
|
+
```
|
149
|
+
|
150
|
+
Escape strings.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
client.escape("How's It Going'") # => "How''s It Going''"
|
154
|
+
```
|
155
|
+
|
156
|
+
Send a SQL string to the database and return a TinyTds::Result object.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
result = client.execute("SELECT * FROM [datatypes]")
|
160
|
+
```
|
161
|
+
|
162
|
+
|
163
|
+
## TinyTds::Result Usage
|
164
|
+
|
165
|
+
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.
|
166
|
+
|
167
|
+
Calling #each on the result will lazily load each row from the database.
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
result.each do |row|
|
171
|
+
# By default each row is a hash.
|
172
|
+
# The keys are the fields, as you'd expect.
|
173
|
+
# The values are pre-built Ruby primitives mapped from their corresponding types.
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
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.
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
result = client.execute("USE [tinytdstest]")
|
181
|
+
result.fields # => []
|
182
|
+
result.do
|
183
|
+
|
184
|
+
result = client.execute("SELECT [id] FROM [datatypes]")
|
185
|
+
result.fields # => ["id"]
|
186
|
+
result.cancel
|
187
|
+
result = client.execute("SELECT [id] FROM [datatypes]")
|
188
|
+
result.each(:symbolize_keys => true)
|
189
|
+
result.fields # => [:id]
|
190
|
+
```
|
191
|
+
|
192
|
+
You can cancel a result object's data from being loading by the server.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
196
|
+
result.cancel
|
197
|
+
```
|
198
|
+
|
199
|
+
You can use results cancelation in conjunction with results lazy loading, no problem.
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
203
|
+
result.each_with_index do |row, i|
|
204
|
+
break if row > 10
|
205
|
+
end
|
206
|
+
result.cancel
|
207
|
+
```
|
208
|
+
|
209
|
+
If the SQL executed by the client returns affected rows, you can easily find out how many.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
result.each
|
213
|
+
result.affected_rows # => 24
|
214
|
+
```
|
215
|
+
|
216
|
+
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`.
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
result = client.execute("DELETE FROM [datatypes]")
|
220
|
+
result.do # => 72
|
221
|
+
```
|
222
|
+
|
223
|
+
Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
|
227
|
+
result.insert # => 420
|
228
|
+
```
|
229
|
+
|
230
|
+
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".
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
sql = ["SELECT TOP (1) [id] FROM [datatypes]",
|
234
|
+
"SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
|
235
|
+
|
236
|
+
set1, set2 = client.execute(sql).each
|
237
|
+
set1 # => [{"id"=>11}]
|
238
|
+
set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
|
239
|
+
|
240
|
+
result = client.execute(sql)
|
241
|
+
|
242
|
+
result.each do |rowset|
|
243
|
+
# First time data loading, yields each row from each set.
|
244
|
+
# 1st: {"id"=>11}
|
245
|
+
# 2nd: {"bigint"=>-9223372036854775807}
|
246
|
+
# 3rd: {"bigint"=>9223372036854775806}
|
247
|
+
end
|
248
|
+
|
249
|
+
result.each do |rowset|
|
250
|
+
# Second time over (if columns cached), yields each set.
|
251
|
+
# 1st: [{"id"=>11}]
|
252
|
+
# 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
|
253
|
+
end
|
254
|
+
```
|
255
|
+
|
256
|
+
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.
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
client.sqlsent? # = false
|
260
|
+
client.canceled? # = false
|
261
|
+
|
262
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
263
|
+
|
264
|
+
client.sqlsent? # = true
|
265
|
+
client.canceled? # = false
|
266
|
+
|
267
|
+
result.each do |row|
|
268
|
+
# Assume we break after 20 rows with 80 still pending.
|
269
|
+
break if row["id"] > 20
|
270
|
+
end
|
271
|
+
|
272
|
+
client.sqlsent? # = true
|
273
|
+
client.canceled? # = false
|
274
|
+
|
275
|
+
result.cancel
|
276
|
+
|
277
|
+
client.sqlsent? # = false
|
278
|
+
client.canceled? # = true
|
279
|
+
```
|
280
|
+
|
281
|
+
It is possible to get the return code after executing a stored procedure from either the result or client object.
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
client.return_code # => nil
|
285
|
+
|
286
|
+
result = client.execute("EXEC tinytds_TestReturnCodes")
|
287
|
+
result.do
|
288
|
+
result.return_code # => 420
|
289
|
+
client.return_code # => 420
|
290
|
+
```
|
291
|
+
|
292
|
+
|
293
|
+
## Query Options
|
294
|
+
|
295
|
+
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:
|
296
|
+
|
297
|
+
* :as => :hash - Object for each row yielded. Can be set to :array.
|
298
|
+
* :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
|
299
|
+
* :cache_rows => true - Successive calls to #each returns the cached rows.
|
300
|
+
* :timezone => :local - Local to the Ruby client or :utc for UTC.
|
301
|
+
* :empty_sets => true - Include empty results set in queries that return multiple result sets.
|
302
|
+
|
303
|
+
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
|
304
|
+
|
305
|
+
```ruby
|
306
|
+
result.each(:as => :array, :cache_rows => false) do |row|
|
307
|
+
# Each row is now an array of values ordered by #fields.
|
308
|
+
# Rows are yielded and forgotten about, freeing memory.
|
309
|
+
end
|
310
|
+
```
|
311
|
+
|
312
|
+
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.
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
result = client.execute("SELECT * FROM [super_big_table]")
|
316
|
+
result.each(:first => true) # => [{'id' => 24}]
|
317
|
+
```
|
318
|
+
|
319
|
+
|
320
|
+
## Row Caching
|
321
|
+
|
322
|
+
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.
|
323
|
+
|
324
|
+
|
325
|
+
## Encoding Error Handling
|
326
|
+
|
327
|
+
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.
|
328
|
+
|
329
|
+
|
330
|
+
## Binstubs
|
331
|
+
|
332
|
+
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.
|
333
|
+
|
334
|
+
* tsql - Used to test connections and debug compile time settings.
|
335
|
+
* defncopy - Used to dump schema structures.
|
336
|
+
|
337
|
+
|
338
|
+
## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
|
339
|
+
|
340
|
+
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.
|
341
|
+
|
342
|
+
* ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
|
343
|
+
|
344
|
+
|
345
|
+
## Using TinyTDS with Azure
|
346
|
+
|
347
|
+
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.
|
348
|
+
|
349
|
+
**IMPORTANT**: Do not use `username@server.database.windows.net` for the username connection option! You must use the shorter `username@server` instead!
|
350
|
+
|
351
|
+
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!
|
352
|
+
|
353
|
+
## Connection Settings
|
354
|
+
|
355
|
+
A DBLIB connection does not have the same default SET options for a standard SMS SQL Server connection. Hence, we recommend the following options post establishing your connection.
|
356
|
+
|
357
|
+
#### SQL Server
|
358
|
+
|
359
|
+
```sql
|
360
|
+
SET ANSI_DEFAULTS ON
|
361
|
+
|
362
|
+
SET QUOTED_IDENTIFIER ON
|
363
|
+
SET CURSOR_CLOSE_ON_COMMIT OFF
|
364
|
+
SET IMPLICIT_TRANSACTIONS OFF
|
365
|
+
SET TEXTSIZE 2147483647
|
366
|
+
SET CONCAT_NULL_YIELDS_NULL ON
|
367
|
+
```
|
368
|
+
|
369
|
+
#### Azure
|
370
|
+
|
371
|
+
```sql
|
372
|
+
SET ANSI_NULLS ON
|
373
|
+
SET ANSI_NULL_DFLT_ON ON
|
374
|
+
SET ANSI_PADDING ON
|
375
|
+
SET ANSI_WARNINGS ON
|
376
|
+
|
377
|
+
SET QUOTED_IDENTIFIER ON
|
378
|
+
SET CURSOR_CLOSE_ON_COMMIT OFF
|
379
|
+
SET IMPLICIT_TRANSACTIONS OFF
|
380
|
+
SET TEXTSIZE 2147483647
|
381
|
+
SET CONCAT_NULL_YIELDS_NULL ON
|
382
|
+
```
|
383
|
+
|
384
|
+
|
385
|
+
## Thread Safety
|
386
|
+
|
387
|
+
TinyTDS must be used with a connection pool for thread safety. If you use ActiveRecord or the [Sequel](https://github.com/jeremyevans/sequel) gem this is done for you. However, if you are using TinyTDS on your own, we recommend using the ConnectionPool gem when using threads:
|
388
|
+
|
389
|
+
* ConnectionPool Gem - https://github.com/mperham/connection_pool
|
390
|
+
|
391
|
+
Please read our [thread_test.rb](https://github.com/rails-sqlserver/tiny_tds/blob/master/test/thread_test.rb) file for details on how we test its usage.
|
392
|
+
|
393
|
+
|
394
|
+
## Emoji Support 😍
|
395
|
+
|
396
|
+
This is possible using FreeTDS version 0.95 or higher. You must use the `use_utf16` login option or 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`.
|
397
|
+
|
398
|
+
```ini
|
399
|
+
[global]
|
400
|
+
use utf-16 = true
|
401
|
+
```
|
402
|
+
|
403
|
+
The default is true and since FreeTDS v1.0 would do this as well.
|
404
|
+
|
405
|
+
|
406
|
+
## Compiling Gems for Windows
|
407
|
+
|
408
|
+
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.
|
409
|
+
|
410
|
+
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:
|
411
|
+
|
412
|
+
```
|
413
|
+
$ rake gem:windows
|
414
|
+
```
|
415
|
+
|
416
|
+
The compiled gems will exist in `./pkg` directory.
|
417
|
+
|
418
|
+
|
419
|
+
## Development & Testing
|
420
|
+
|
421
|
+
First, clone the repo using the command line or your Git GUI of choice.
|
422
|
+
|
423
|
+
```shell
|
424
|
+
$ git clone git@github.com:rails-sqlserver/tiny_tds.git
|
425
|
+
```
|
426
|
+
|
427
|
+
After that, the quickest way to get setup for development is to use [Docker](https://www.docker.com/). Assuming you have [downloaded docker](https://www.docker.com/products/docker) for your platform and you have , you can run our test setup script.
|
428
|
+
|
429
|
+
```shell
|
430
|
+
$ ./test/bin/setup.sh
|
431
|
+
```
|
432
|
+
|
433
|
+
This will download our SQL Server for Linux Docker image based from [microsoft/mssql-server-linux/](https://hub.docker.com/r/microsoft/mssql-server-linux/). Our image already has the `[tinytdstest]` DB and `tinytds` users created. Basically, it does the following.
|
434
|
+
|
435
|
+
```shell
|
436
|
+
$ docker pull metaskills/mssql-server-linux-tinytds
|
437
|
+
$ docker run -p 1433:1433 -d metaskills/mssql-server-linux-tinytds
|
438
|
+
```
|
439
|
+
|
440
|
+
If you are using your own database. Make sure to run these SQL commands as SA to get the test database and user installed.
|
441
|
+
|
442
|
+
```sql
|
443
|
+
CREATE DATABASE [tinytdstest];
|
444
|
+
```
|
445
|
+
|
446
|
+
```sql
|
447
|
+
CREATE LOGIN [tinytds] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [tinytdstest];
|
448
|
+
USE [tinytdstest];
|
449
|
+
CREATE USER [tinytds] FOR LOGIN [tinytds];
|
450
|
+
EXEC sp_addrolemember N'db_owner', N'tinytds';
|
451
|
+
```
|
452
|
+
|
453
|
+
From here you can build and run tests against an installed version of FreeTDS.
|
454
|
+
|
455
|
+
```shell
|
456
|
+
$ bundle install
|
457
|
+
$ bundle exec rake
|
458
|
+
```
|
459
|
+
|
460
|
+
Examples us using enviornment variables to customize the test task.
|
461
|
+
|
462
|
+
```
|
463
|
+
$ rake TINYTDS_UNIT_DATASERVER=mydbserver
|
464
|
+
$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
|
465
|
+
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
|
466
|
+
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase
|
467
|
+
```
|
468
|
+
|
469
|
+
## Docker Builds
|
470
|
+
|
471
|
+
If you use a [multi stage](https://docs.docker.com/develop/develop-images/multistage-build/) Docker build to assemble your gems in one phase and then copy your app and gems
|
472
|
+
into another, lighter, container without build tools you will need to make sure you tell the OS how to find dependencies for TinyTDS.
|
473
|
+
|
474
|
+
After you have built and installed FreeTDS it will normally place library files in `/usr/local/lib`. When TinyTDS builds native extensions,
|
475
|
+
it [already knows to look here](https://github.com/rails-sqlserver/tiny_tds/blob/master/ext/tiny_tds/extconf.rb#L31) but if you copy your app to a new container that link will be broken.
|
476
|
+
|
477
|
+
Set the LD_LIBRARY_PATH environment variable `export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}` and run `ldconfig`. If you run `ldd tiny_tds.so` you should not see any broken links. Make
|
478
|
+
sure you also copied in the library dependencies from your build container with a command like `COPY --from=builder /usr/local/lib /usr/local/lib`.
|
479
|
+
|
480
|
+
## Help & Support
|
481
|
+
|
482
|
+
* Github Source: http://github.com/rails-sqlserver/tiny_tds
|
483
|
+
* Github Issues: http://github.com/rails-sqlserver/tiny_tds/issues
|
484
|
+
* Gitter Chat: https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter
|
485
|
+
* IRC Room: #rails-sqlserver on irc.freenode.net
|
486
|
+
|
487
|
+
|
488
|
+
## About Me
|
489
|
+
|
490
|
+
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!
|
491
|
+
|
492
|
+
|
493
|
+
## Special Thanks
|
494
|
+
|
495
|
+
* Lars Kanis for all his help getting the Windows builds working again with rake-compiler-dock.
|
496
|
+
* Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
|
497
|
+
* To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
|
498
|
+
* 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/
|
499
|
+
* Josh Clayton of Thoughtbot for writing about Ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
|
500
|
+
|
501
|
+
|
502
|
+
## License
|
503
|
+
|
504
|
+
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>
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rbconfig'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/clean'
|
5
|
+
require 'rake/extensiontask'
|
6
|
+
require_relative './ext/tiny_tds/extconsts'
|
7
|
+
|
8
|
+
SPEC = Gem::Specification.load(File.expand_path('../tiny_tds.gemspec', __FILE__))
|
9
|
+
GEM_PLATFORM_HOSTS = {
|
10
|
+
'x86-mingw32' => 'i686-w64-mingw32',
|
11
|
+
'x64-mingw32' => 'x86_64-w64-mingw32'
|
12
|
+
}
|
13
|
+
|
14
|
+
# Add our project specific files to clean for a rebuild
|
15
|
+
CLEAN.include FileList["{ext,lib}/**/*.{so,#{RbConfig::CONFIG['DLEXT']},o}"],
|
16
|
+
FileList["exe/*"]
|
17
|
+
|
18
|
+
# Clobber all our temp files and ports files including .install files
|
19
|
+
# and archives
|
20
|
+
CLOBBER.include FileList["tmp/**/*"],
|
21
|
+
FileList["ports/**/*"].exclude(%r{^ports/archives})
|
22
|
+
|
23
|
+
Dir['tasks/*.rake'].sort.each { |f| load f }
|
24
|
+
|
25
|
+
Rake::ExtensionTask.new('tiny_tds', SPEC) do |ext|
|
26
|
+
ext.lib_dir = 'lib/tiny_tds'
|
27
|
+
ext.cross_compile = true
|
28
|
+
ext.cross_platform = GEM_PLATFORM_HOSTS.keys
|
29
|
+
|
30
|
+
# Add dependent DLLs to the cross gems
|
31
|
+
ext.cross_compiling do |spec|
|
32
|
+
# The fat binary gem doesn't depend on the freetds package, since it bundles the library.
|
33
|
+
spec.metadata.delete('msys2_mingw_dependencies')
|
34
|
+
|
35
|
+
platform_host_map = GEM_PLATFORM_HOSTS
|
36
|
+
gemplat = spec.platform.to_s
|
37
|
+
host = platform_host_map[gemplat]
|
38
|
+
|
39
|
+
# We don't need the sources in a fat binary gem
|
40
|
+
spec.files = spec.files.reject { |f| f =~ %r{^ports\/archives/} }
|
41
|
+
|
42
|
+
# Make sure to include the ports binaries and libraries
|
43
|
+
spec.files += FileList["ports/#{host}/**/**/{bin,lib}/*"].exclude do |f|
|
44
|
+
File.directory? f
|
45
|
+
end
|
46
|
+
|
47
|
+
spec.files += Dir.glob('exe/*')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
task build: [:clean, :compile]
|
52
|
+
task default: [:build, :test]
|
53
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/appveyor.yml
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
init:
|
2
|
+
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
|
3
|
+
- SET PATH=C:\MinGW\msys\1.0\bin;%PATH%
|
4
|
+
- SET RAKEOPT=-rdevkit
|
5
|
+
- SET TESTOPTS='-v'
|
6
|
+
- SET MAKE=make V=1
|
7
|
+
clone_depth: 5
|
8
|
+
skip_tags: true
|
9
|
+
skip_branch_with_pr: true
|
10
|
+
matrix:
|
11
|
+
fast_finish: true
|
12
|
+
install:
|
13
|
+
# Output debugging info
|
14
|
+
- ps: Update-AppveyorBuild -Version "$(Get-Content $env:appveyor_build_folder\VERSION).$env:appveyor_build_number"
|
15
|
+
- perl --version
|
16
|
+
- ruby --version
|
17
|
+
- gem --version
|
18
|
+
|
19
|
+
# Set up project prerequisits
|
20
|
+
- bundle install
|
21
|
+
- bundle exec rake ports
|
22
|
+
build: off
|
23
|
+
branches:
|
24
|
+
except:
|
25
|
+
- /dev.*/
|
26
|
+
test_script:
|
27
|
+
- timeout /t 4 /nobreak > NUL
|
28
|
+
- powershell -File "%APPVEYOR_BUILD_FOLDER%\test\appveyor\dbsetup.ps1"
|
29
|
+
- timeout /t 4 /nobreak > NUL
|
30
|
+
- ps: Start-Service 'MSSQL$SQL2014'
|
31
|
+
- timeout /t 4 /nobreak > NUL
|
32
|
+
- sqlcmd -S ".\SQL2014" -U sa -P Password12! -i %APPVEYOR_BUILD_FOLDER%\test\appveyor\dbsetup.sql
|
33
|
+
- bundle exec rake ports build test TINYTDS_UNIT_HOST_TEST=localhost TINYTDS_UNIT_DATASERVER="localhost\SQL2014" TINYTDS_SCHEMA=sqlserver_2014 TDSVER=7.1
|
34
|
+
- bundle exec rake ports build test TINYTDS_UNIT_HOST_TEST=localhost TINYTDS_UNIT_DATASERVER="localhost\SQL2014" TINYTDS_SCHEMA=sqlserver_2014
|
35
|
+
- ps: Stop-Service 'MSSQL$SQL2014'
|
36
|
+
- ps: Start-Service 'MSSQL$SQL2012SP1'
|
37
|
+
- timeout /t 4 /nobreak > NUL
|
38
|
+
- sqlcmd -S ".\SQL2012SP1" -U sa -P Password12! -i %APPVEYOR_BUILD_FOLDER%\test\appveyor\dbsetup.sql
|
39
|
+
- bundle exec rake ports build test TINYTDS_UNIT_HOST_TEST=localhost TINYTDS_UNIT_DATASERVER="localhost\SQL2012SP1" TINYTDS_SCHEMA=sqlserver_2014
|
40
|
+
environment:
|
41
|
+
CI_AZURE_HOST:
|
42
|
+
secure: 8ydpYysZYKEBKvp6plKlYfepH98/zAuT27FFCaJ9Sss=
|
43
|
+
TINYTDS_UNIT_AZURE_PASS:
|
44
|
+
secure: fYKSKV4v+36OFQp2nZdX4DfUpgmy5cm0wuR73cgdmEk=
|
45
|
+
matrix:
|
46
|
+
- ruby_version: "24-x64"
|
47
|
+
- ruby_version: "24"
|
48
|
+
- ruby_version: "22-x64"
|
49
|
+
- ruby_version: "22"
|
50
|
+
on_failure:
|
51
|
+
- find -name compile.log | xargs cat
|
data/bin/defncopy-ttds
ADDED
data/bin/tsql-ttds
ADDED
data/exe/.keep
ADDED
File without changes
|