tiny_tds 0.5.0.rc1-x86-mingw32 → 0.5.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,340 @@
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
+ ## New & Noteworthy
15
+
16
+ * Works with FreeTDS 0.91
17
+ * Tested on Windows using MiniPortile & RailsInstaller.
18
+ * New :host/:port connection options. Removes need for freetds.conf file.
19
+
20
+
21
+ ## Install
22
+
23
+ Installing with rubygems should just work. TinyTDS is tested on ruby version 1.8.6, 1.8.7, 1.9.1, 1.9.2, 1.9.3 as well as REE & JRuby.
24
+
25
+ ```
26
+ $ gem install tiny_tds
27
+ ```
28
+
29
+ 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`
30
+
31
+
32
+ ## FreeTDS Compatibility & Configuration
33
+
34
+ TinyTDS is developed against FreeTDS 0.82 & 0.91, 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.
35
+
36
+ * **Do I need to install FreeTDS?** Yes! Somehow, someway, your 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.
37
+
38
+ * **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.
39
+
40
+ * **Do I need to configure `--with-tdsver` equal to anything?** Maybe! 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.
41
+
42
+ * **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
43
+
44
+ * **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!
45
+
46
+
47
+ ## Data Types
48
+
49
+ 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.
50
+
51
+ 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.
52
+
53
+ * [date]
54
+ * [datetime2]
55
+ * [datetimeoffset]
56
+ * [time]
57
+
58
+
59
+ ## TinyTds::Client Usage
60
+
61
+ Connect to a database.
62
+
63
+ ```ruby
64
+ client = TinyTds::Client.new(:username => 'sa', :password => 'secret', :host => 'mydb.host.net')
65
+ ```
66
+
67
+ 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.
68
+
69
+ * :username - The database server user.
70
+ * :password - The user password.
71
+ * :dataserver - Many options. Can be the name for your data server as defined in freetds.conf. Raw host name or IP will work here too. You can also used a named instance like 'localhost\SQLEXPRESS' too.
72
+ * :host - Used if :dataserver blank. Can be an host name or IP.
73
+ * :port - Defaults to 1433. Only used if :host is used.
74
+ * :database - The default database to use.
75
+ * :appname - Short string seen in SQL Servers process/activity window.
76
+ * :tds_version - TDS version. Defaults to 71 (7.1) and is not recommended to change!
77
+ * :login_timeout - Seconds to wait for login. Default to 60 seconds.
78
+ * :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds.
79
+ * :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
80
+ * :azure - Pass true to signal that you are connecting to azure.
81
+
82
+ 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.
83
+
84
+ ```ruby
85
+ client.dead? # => false
86
+ client.closed? # => false
87
+ client.active? # => true
88
+ client.execute("SQL TO A DEAD SERVER")
89
+ client.dead? # => true
90
+ client.closed? # => false
91
+ client.active? # => false
92
+ client.close
93
+ client.closed? # => true
94
+ client.active? # => false
95
+ ```
96
+
97
+ Escape strings.
98
+
99
+ ```ruby
100
+ client.escape("How's It Going'") # => "How''s It Going''"
101
+ ```
102
+
103
+ Send a SQL string to the database and return a TinyTds::Result object.
104
+
105
+ ```ruby
106
+ result = client.execute("SELECT * FROM [datatypes]")
107
+ ```
108
+
109
+
110
+ ## TinyTds::Result Usage
111
+
112
+ 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.
113
+
114
+ Calling #each on the result will lazily load each row from the database.
115
+
116
+ ```ruby
117
+ result.each do |row|
118
+ # By default each row is a hash.
119
+ # The keys are the fields, as you'd expect.
120
+ # The values are pre-built ruby primitives mapped from their corresponding types.
121
+ # Here's an leemer: http://is.gd/g61xo
122
+ end
123
+ ```
124
+
125
+ 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.
126
+
127
+ ```ruby
128
+ result = client.execute("USE [tinytdstest]")
129
+ result.fields # => []
130
+ result.do
131
+
132
+ result = client.execute("SELECT [id] FROM [datatypes]")
133
+ result.fields # => ["id"]
134
+ result.cancel
135
+ result = client.execute("SELECT [id] FROM [datatypes]")
136
+ result.each(:symbolize_keys => true)
137
+ result.fields # => [:id]
138
+ ```
139
+
140
+ You can cancel a result object's data from being loading by the server.
141
+
142
+ ```ruby
143
+ result = client.execute("SELECT * FROM [super_big_table]")
144
+ result.cancel
145
+ ```
146
+
147
+ If the SQL executed by the client returns affected rows, you can easily find out how many.
148
+
149
+ ```ruby
150
+ result.each
151
+ result.affected_rows # => 24
152
+ ```
153
+
154
+ 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`.
155
+
156
+ ```ruby
157
+ result = client.execute("DELETE FROM [datatypes]")
158
+ result.do # => 72
159
+ ```
160
+
161
+ Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.
162
+
163
+ ```ruby
164
+ result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
165
+ result.insert # => 420
166
+ ```
167
+
168
+ 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".
169
+
170
+ ```ruby
171
+ sql = ["SELECT TOP (1) [id] FROM [datatypes]",
172
+ "SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
173
+
174
+ set1, set2 = client.execute(sql).each
175
+ set1 # => [{"id"=>11}]
176
+ set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
177
+
178
+ result = client.execute(sql)
179
+
180
+ result.each do |rowset|
181
+ # First time data loading, yields each row from each set.
182
+ # 1st: {"id"=>11}
183
+ # 2nd: {"bigint"=>-9223372036854775807}
184
+ # 3rd: {"bigint"=>9223372036854775806}
185
+ end
186
+
187
+ result.each do |rowset|
188
+ # Second time over (if columns cached), yields each set.
189
+ # 1st: [{"id"=>11}]
190
+ # 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
191
+ end
192
+ ```
193
+
194
+ 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.
195
+
196
+ ```ruby
197
+ client.sqlsent? # = false
198
+ client.canceled? # = false
199
+
200
+ result = client.execute("SELECT * FROM [super_big_table]")
201
+
202
+ client.sqlsent? # = true
203
+ client.canceled? # = false
204
+
205
+ result.each do |row|
206
+ # Assume we break after 20 rows with 80 still pending.
207
+ break if row["id"] > 20
208
+ end
209
+
210
+ client.sqlsent? # = true
211
+ client.canceled? # = false
212
+
213
+ result.cancel
214
+
215
+ client.sqlsent? # = false
216
+ client.canceled? # = true
217
+ ```
218
+
219
+ It is possible to get the return code after executing a stored procedure from either the result or client object.
220
+
221
+ ```ruby
222
+ client.return_code # => nil
223
+
224
+ result = client.execute("EXEC tinytds_TestReturnCodes")
225
+ result.return_code # => 420
226
+ client.return_code # => 420
227
+ ```
228
+
229
+
230
+ ## Query Options
231
+
232
+ 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:
233
+
234
+ * :as => :hash - Object for each row yielded. Can be set to :array.
235
+ * :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
236
+ * :cache_rows => true - Successive calls to #each returns the cached rows.
237
+ * :timezone => :local - Local to the ruby client or :utc for UTC.
238
+ * :empty_sets => true - Include empty results set in queries that return multiple result sets.
239
+
240
+ 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
241
+
242
+ ```ruby
243
+ result.each(:as => :array, :cache_rows => false) do |row|
244
+ # Each row is now an array of values ordered by #fields.
245
+ # Rows are yielded and forgotten about, freeing memory.
246
+ end
247
+ ```
248
+
249
+ 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.
250
+
251
+ ```ruby
252
+ result = client.execute("SELECT * FROM [super_big_table]")
253
+ result.each(:first => true) # => [{'id' => 24}]
254
+ ```
255
+
256
+
257
+ ## Row Caching
258
+
259
+ 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.
260
+
261
+
262
+
263
+ ## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
264
+
265
+ 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.
266
+
267
+ * ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
268
+ * Using TinyTDS: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-TinyTds
269
+
270
+
271
+ ## Using TinyTDS with Azure
272
+
273
+ 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.
274
+
275
+
276
+ ## Using MiniPortile
277
+
278
+ MiniPortile is a minimalistic, simplistic and stupid implementation of a port/recipe system for developers. <https://github.com/luislavena/mini_portile>
279
+
280
+ 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.
281
+
282
+ 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.
283
+
284
+ ```
285
+ $ bundle install
286
+ $ rake compile
287
+ $ rake native gem
288
+ $ gem install pkg/tiny_tds-X.X.X-#{platform}.gem
289
+ ```
290
+
291
+
292
+ ## Development & Testing
293
+
294
+ 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 or azure) to use. For example:
295
+
296
+ ```
297
+ $ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
298
+ or
299
+ $ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
300
+ ```
301
+
302
+ 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.
303
+
304
+ ```
305
+ $ rake TINYTDS_SKIP_PORTS=true
306
+ ```
307
+
308
+
309
+ ## Help & Support
310
+
311
+ * Github Source: http://github.com/rails-sqlserver/tiny_tds
312
+ * Github Issues: http://github.com/rails-sqlserver/tiny_tds/issues
313
+ * Google Group: http://groups.google.com/group/rails-sqlserver-adapter
314
+ * IRC Room: #rails-sqlserver on irc.freenode.net
315
+
316
+
317
+ ## TODO List
318
+
319
+ * Include OpenSSL with Windows binaries for SQL Azure.
320
+ * Install an interrupt handler.
321
+ * Allow #escape to accept all ruby primitives.
322
+
323
+
324
+ ## About Me
325
+
326
+ 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!
327
+
328
+
329
+ ## Special Thanks
330
+
331
+ * Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
332
+ * To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
333
+ * 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/
334
+ * Josh Clayton of Thoughtbot for writing about ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
335
+
336
+
337
+ ## License
338
+
339
+ TinyTDS is Copyright (c) 2010-2011 Ken Collins, <ken@metaskills.net> and 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>
340
+
@@ -1,5 +1,6 @@
1
1
 
2
2
  #include <tiny_tds_ext.h>
3
+ #include <stdint.h>
3
4
 
4
5
  // TINY_TDS_MAX_TIME
5
6
 
@@ -1,3 +1,3 @@
1
1
  module TinyTds
2
- VERSION = '0.5.0.rc1'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -176,6 +176,7 @@ class ResultTest < TinyTds::TestCase
176
176
  end
177
177
 
178
178
  should 'return bigint for #insert when needed' do
179
+ return if sqlserver_azure? # We can not alter clustered index like this test does.
179
180
  rollback_transaction(@client) do
180
181
  seed = 9223372036854775805
181
182
  @client.execute("DELETE FROM [datatypes]").do
metadata CHANGED
@@ -1,15 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_tds
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15424071
5
- prerelease: 6
4
+ hash: 11
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
9
  - 0
10
- - rc
11
- - 1
12
- version: 0.5.0.rc1
10
+ version: 0.5.0
13
11
  platform: x86-mingw32
14
12
  authors:
15
13
  - Ken Collins
@@ -18,7 +16,7 @@ autorequire:
18
16
  bindir: bin
19
17
  cert_chain: []
20
18
 
21
- date: 2011-09-12 00:00:00 -04:00
19
+ date: 2011-10-30 00:00:00 -04:00
22
20
  default_executable:
23
21
  dependencies: []
24
22
 
@@ -37,7 +35,7 @@ files:
37
35
  - Gemfile
38
36
  - MIT-LICENSE
39
37
  - NOTES
40
- - README.rdoc
38
+ - README.md
41
39
  - Rakefile
42
40
  - ext/tiny_tds/client.c
43
41
  - ext/tiny_tds/client.h
@@ -87,14 +85,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
85
  required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  none: false
89
87
  requirements:
90
- - - ">"
88
+ - - ">="
91
89
  - !ruby/object:Gem::Version
92
- hash: 25
90
+ hash: 3
93
91
  segments:
94
- - 1
95
- - 3
96
- - 1
97
- version: 1.3.1
92
+ - 0
93
+ version: "0"
98
94
  requirements: []
99
95
 
100
96
  rubyforge_project:
@@ -1,317 +0,0 @@
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
-
15
- == New & Noteworthy
16
-
17
- * Works with FreeTDS 0.91
18
- * Tested on Windows using MiniPortile & RailsInstaller.
19
- * New :host/:port connection options. Removes need for freetds.conf file.
20
-
21
-
22
-
23
- == Install
24
-
25
- Installing with rubygems should just work. TinyTDS is tested on ruby version 1.8.6, 1.8.7, 1.9.1, 1.9.2, 1.9.3 as well as REE & JRuby.
26
-
27
- $ gem install tiny_tds
28
-
29
- 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"
30
-
31
-
32
-
33
- == FreeTDS Compatibility & Configuration
34
-
35
- TinyTDS is developed against FreeTDS 0.82 & 0.91, 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.
36
-
37
- * <b>Do I need to install FreeTDS?</b> Yes! Somehow, someway, your 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.
38
-
39
- * <b>OK, I am installing FreeTDS, how do I configure it?</b> 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.
40
-
41
- * <b>Do I need to configure "--with-tdsver" equal to anything?</b> Maybe! 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.
42
-
43
- * <b>But I want to use TDS version 7.2 for SQL Server 2005 and up!</b> 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
44
-
45
- * <b>I want to configure FreeTDS using "--enable-msdblib" and/or "--enable-sybase-compat" so it works for my database. Cool?</b> 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!
46
-
47
-
48
-
49
- == Data Types
50
-
51
- 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.
52
-
53
- 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.
54
-
55
- * [date]
56
- * [datetime2]
57
- * [datetimeoffset]
58
- * [time]
59
-
60
-
61
-
62
- == TinyTds::Client Usage
63
-
64
- Connect to a database.
65
-
66
- client = TinyTds::Client.new(:username => 'sa', :password => 'secret', :host => 'mydb.host.net')
67
-
68
- 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.
69
-
70
- * :username - The database server user.
71
- * :password - The user password.
72
- * :dataserver - Many options. Can be the name for your data server as defined in freetds.conf. Raw host name or IP will work here too. You can also used a named instance like 'localhost\SQLEXPRESS' too.
73
- * :host - Used if :dataserver blank. Can be an host name or IP.
74
- * :port - Defaults to 1433. Only used if :host is used.
75
- * :database - The default database to use.
76
- * :appname - Short string seen in SQL Servers process/activity window.
77
- * :tds_version - TDS version. Defaults to 71 (7.1) and is not recommended to change!
78
- * :login_timeout - Seconds to wait for login. Default to 60 seconds.
79
- * :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds.
80
- * :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
81
- * :azure - Pass true to signal that you are connecting to azure.
82
-
83
- 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.
84
-
85
- client.dead? # => false
86
- client.closed? # => false
87
- client.active? # => true
88
- client.execute("SQL TO A DEAD SERVER")
89
- client.dead? # => true
90
- client.closed? # => false
91
- client.active? # => false
92
- client.close
93
- client.closed? # => true
94
- client.active? # => false
95
-
96
- Escape strings.
97
-
98
- client.escape("How's It Going'") # => "How''s It Going''"
99
-
100
- Send a SQL string to the database and return a TinyTds::Result object.
101
-
102
- result = client.execute("SELECT * FROM [datatypes]")
103
-
104
-
105
-
106
- == TinyTds::Result Usage
107
-
108
- 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.
109
-
110
- Calling #each on the result will lazily load each row from the database.
111
-
112
- result.each do |row|
113
- # By default each row is a hash.
114
- # The keys are the fields, as you'd expect.
115
- # The values are pre-built ruby primitives mapped from their corresponding types.
116
- # Here's an leemer: http://is.gd/g61xo
117
- end
118
-
119
- 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.
120
-
121
- result = client.execute("USE [tinytdstest]")
122
- result.fields # => []
123
- result.do
124
-
125
- result = client.execute("SELECT [id] FROM [datatypes]")
126
- result.fields # => ["id"]
127
- result.cancel
128
- result = client.execute("SELECT [id] FROM [datatypes]")
129
- result.each(:symbolize_keys => true)
130
- result.fields # => [:id]
131
-
132
- You can cancel a result object's data from being loading by the server.
133
-
134
- result = client.execute("SELECT * FROM [super_big_table]")
135
- result.cancel
136
-
137
- If the SQL executed by the client returns affected rows, you can easily find out how many.
138
-
139
- result.each
140
- result.affected_rows # => 24
141
-
142
- 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.
143
-
144
- result = client.execute("DELETE FROM [datatypes]")
145
- result.do # => 72
146
-
147
- Likewise for INSERT statements, the #insert method cancels any need for loading the result data and executes a SCOPE_IDENTITY() for the primary key.
148
-
149
- result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
150
- result.insert # => 420
151
-
152
- 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".
153
-
154
- sql = ["SELECT TOP (1) [id] FROM [datatypes]",
155
- "SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
156
-
157
- set1, set2 = client.execute(sql).each
158
- set1 # => [{"id"=>11}]
159
- set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
160
-
161
- result = client.execute(sql)
162
-
163
- result.each do |rowset|
164
- # First time data loading, yields each row from each set.
165
- # 1st: {"id"=>11}
166
- # 2nd: {"bigint"=>-9223372036854775807}
167
- # 3rd: {"bigint"=>9223372036854775806}
168
- end
169
-
170
- result.each do |rowset|
171
- # Second time over (if columns cached), yields each set.
172
- # 1st: [{"id"=>11}]
173
- # 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
174
- end
175
-
176
- 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.
177
-
178
- client.sqlsent? # = false
179
- client.canceled? # = false
180
-
181
- result = client.execute("SELECT * FROM [super_big_table]")
182
-
183
- client.sqlsent? # = true
184
- client.canceled? # = false
185
-
186
- result.each do |row|
187
- # Assume we break after 20 rows with 80 still pending.
188
- break if row["id"] > 20
189
- end
190
-
191
- client.sqlsent? # = true
192
- client.canceled? # = false
193
-
194
- result.cancel
195
-
196
- client.sqlsent? # = false
197
- client.canceled? # = true
198
-
199
- It is possible to get the return code after executing a stored procedure from either the result or client object.
200
-
201
- client.return_code # => nil
202
-
203
- result = client.execute("EXEC tinytds_TestReturnCodes")
204
- result.return_code # => 420
205
- client.return_code # => 420
206
-
207
-
208
- == Query Options
209
-
210
- 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:
211
-
212
- * :as => :hash - Object for each row yielded. Can be set to :array.
213
- * :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
214
- * :cache_rows => true - Successive calls to #each returns the cached rows.
215
- * :timezone => :local - Local to the ruby client or :utc for UTC.
216
- * :empty_sets => true - Include empty results set in queries that return multiple result sets.
217
-
218
- 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
219
-
220
- result.each(:as => :array, :cache_rows => false) do |row|
221
- # Each row is now an array of values ordered by #fields.
222
- # Rows are yielded and forgotten about, freeing memory.
223
- end
224
-
225
- 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.
226
-
227
- result = client.execute("SELECT * FROM [super_big_table]")
228
- result.each(:first => true) # => [{'id' => 24}]
229
-
230
-
231
-
232
- == Row Caching
233
-
234
- 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.
235
-
236
-
237
-
238
- == Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
239
-
240
- 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. 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.
241
-
242
- * ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
243
- * Using TinyTDS: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-TinyTds
244
-
245
-
246
-
247
- == Using TinyTDS with Azure
248
-
249
- 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.
250
-
251
-
252
-
253
- == Using MiniPortile
254
-
255
- MiniPortile is a minimalistic, simplistic and stupid implementation of a port/recipe system for developers. <https://github.com/luislavena/mini_portile>
256
-
257
- 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.
258
-
259
- 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.
260
-
261
- $ bundle install
262
- $ rake compile
263
- $ rake native gem
264
-
265
-
266
-
267
- == Development & Testing
268
-
269
- 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 or azure) to use. For example:
270
-
271
- $ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
272
- or
273
- $ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
274
-
275
- 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.
276
-
277
- $ rake TINYTDS_SKIP_PORTS=true
278
-
279
-
280
-
281
- == Help & Support
282
-
283
- * Github Source: http://github.com/rails-sqlserver/tiny_tds
284
- * Github Issues: http://github.com/rails-sqlserver/tiny_tds/issues
285
- * Google Group: http://groups.google.com/group/rails-sqlserver-adapter
286
- * IRC Room: #rails-sqlserver on irc.freenode.net
287
-
288
-
289
-
290
- == TODO List
291
-
292
- * Include OpenSSL with Windows binaries for SQL Azure.
293
- * Install an interrupt handler.
294
- * Allow #escape to accept all ruby primitives.
295
-
296
-
297
-
298
- == About Me
299
-
300
- 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!
301
-
302
-
303
-
304
- == Special Thanks
305
-
306
- * Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
307
- * To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
308
- * 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/
309
- * Josh Clayton of Thoughtbot for writing about ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
310
-
311
-
312
-
313
- == License
314
-
315
- TinyTDS is Copyright (c) 2010-2011 Ken Collins, <ken@metaskills.net> and 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>
316
-
317
-