tiny_tds 3.2.0-x86_64-linux-gnu

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +20 -0
  3. data/.gitattributes +1 -0
  4. data/.github/workflows/ci.yml +590 -0
  5. data/.gitignore +22 -0
  6. data/.rubocop.yml +31 -0
  7. data/CHANGELOG.md +305 -0
  8. data/CODE_OF_CONDUCT.md +31 -0
  9. data/Gemfile +2 -0
  10. data/ISSUE_TEMPLATE.md +38 -0
  11. data/MIT-LICENSE +23 -0
  12. data/README.md +493 -0
  13. data/Rakefile +67 -0
  14. data/VERSION +1 -0
  15. data/bin/defncopy-ttds +3 -0
  16. data/bin/tsql-ttds +3 -0
  17. data/docker-compose.yml +34 -0
  18. data/exe/.keep +0 -0
  19. data/ext/tiny_tds/client.c +492 -0
  20. data/ext/tiny_tds/client.h +53 -0
  21. data/ext/tiny_tds/extconf.rb +190 -0
  22. data/ext/tiny_tds/extconsts.rb +8 -0
  23. data/ext/tiny_tds/result.c +626 -0
  24. data/ext/tiny_tds/result.h +32 -0
  25. data/ext/tiny_tds/tiny_tds_ext.c +15 -0
  26. data/ext/tiny_tds/tiny_tds_ext.h +17 -0
  27. data/lib/tiny_tds/2.7/tiny_tds.so +0 -0
  28. data/lib/tiny_tds/3.0/tiny_tds.so +0 -0
  29. data/lib/tiny_tds/3.1/tiny_tds.so +0 -0
  30. data/lib/tiny_tds/3.2/tiny_tds.so +0 -0
  31. data/lib/tiny_tds/3.3/tiny_tds.so +0 -0
  32. data/lib/tiny_tds/3.4/tiny_tds.so +0 -0
  33. data/lib/tiny_tds/bin.rb +90 -0
  34. data/lib/tiny_tds/client.rb +132 -0
  35. data/lib/tiny_tds/error.rb +12 -0
  36. data/lib/tiny_tds/gem.rb +23 -0
  37. data/lib/tiny_tds/result.rb +5 -0
  38. data/lib/tiny_tds/version.rb +3 -0
  39. data/lib/tiny_tds.rb +42 -0
  40. data/patches/freetds/1.00.27/0001-mingw_missing_inet_pton.diff +34 -0
  41. data/patches/freetds/1.00.27/0002-Don-t-use-MSYS2-file-libws2_32.diff +28 -0
  42. data/patches/libiconv/1.14/1-avoid-gets-error.patch +17 -0
  43. data/ports/x86_64-linux-gnu/bin/defncopy +0 -0
  44. data/ports/x86_64-linux-gnu/bin/tsql +0 -0
  45. data/ports/x86_64-linux-gnu/lib/libsybdb.so.5 +0 -0
  46. data/setup_cimgruby_dev.sh +25 -0
  47. data/start_dev.sh +21 -0
  48. data/tasks/native_gem.rake +16 -0
  49. data/tasks/package.rake +6 -0
  50. data/tasks/ports.rake +24 -0
  51. data/tasks/test.rake +7 -0
  52. data/test/bin/install-freetds.sh +18 -0
  53. data/test/bin/install-mssql.ps1 +42 -0
  54. data/test/bin/install-mssqltools.sh +9 -0
  55. data/test/bin/install-openssl.sh +18 -0
  56. data/test/bin/restore-from-native-gem.ps1 +10 -0
  57. data/test/bin/setup_tinytds_db.sh +7 -0
  58. data/test/bin/setup_volume_permissions.sh +10 -0
  59. data/test/client_test.rb +266 -0
  60. data/test/gem_test.rb +100 -0
  61. data/test/result_test.rb +708 -0
  62. data/test/schema/1px.gif +0 -0
  63. data/test/schema/sqlserver_2017.sql +140 -0
  64. data/test/schema/sqlserver_azure.sql +140 -0
  65. data/test/schema_test.rb +417 -0
  66. data/test/sql/db-create.sql +18 -0
  67. data/test/sql/db-login.sql +38 -0
  68. data/test/test_helper.rb +244 -0
  69. data/test/thread_test.rb +89 -0
  70. data/tiny_tds.gemspec +31 -0
  71. metadata +259 -0
data/README.md ADDED
@@ -0,0 +1,493 @@
1
+ # TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
2
+
3
+ * [![Gem Version](https://img.shields.io/gem/v/tiny_tds.svg)](https://rubygems.org/gems/tiny_tds) - Gem Version
4
+ * [![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
5
+
6
+ ## About TinyTDS
7
+
8
+ The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server from Ruby using the FreeTDS's DB-Library API.
9
+
10
+ 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.
11
+
12
+ The API is simple and consists of these classes:
13
+
14
+ * TinyTds::Client - Your connection to the database.
15
+ * TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
16
+ * TinyTds::Error - A wrapper for all FreeTDS exceptions.
17
+
18
+
19
+ ## Install
20
+
21
+ tiny_tds is tested with Ruby v2.7 and upwards.
22
+
23
+ ### Windows and Linux (64-bit)
24
+
25
+ We precompile tiny_tds with FreeTDS and supporting libraries, which are dynamically linked at runtime. Therefore, you can run:
26
+
27
+ ```shell
28
+ gem install tiny_tds
29
+ ```
30
+
31
+ It should find the platform-specific gem.
32
+
33
+ You can also avoid getting the platform-specific gem if you want to compile FreeTDS and supporting libraries yourself:
34
+
35
+ ```shell
36
+ gem install tiny_tds --platform ruby
37
+ ```
38
+
39
+ ### Mac
40
+
41
+ Install FreeTDS via Homebrew:
42
+
43
+ ```shell
44
+ brew install openssl@3 libiconv
45
+ brew install freetds
46
+ ```
47
+
48
+ Then you can install tiny_tds:
49
+
50
+ ```shell
51
+ gem install tiny_tds
52
+ ```
53
+
54
+ ### Everybody else
55
+
56
+ `tiny_tds` will find FreeTDS and other libraries based on your compiler paths. Below you can see an example on how to install FreeTDS on a Debian system.
57
+
58
+ ```shell
59
+ $ apt-get install wget
60
+ $ apt-get install build-essential
61
+ $ apt-get install libc6-dev
62
+
63
+ $ wget http://www.freetds.org/files/stable/freetds-1.4.23.tar.gz
64
+ $ tar -xzf freetds-1.4.23.tar.gz
65
+ $ cd freetds-1.4.23
66
+ $ ./configure --prefix=/usr/local --with-tdsver=7.4 --disable-odbc
67
+ $ make
68
+ $ make install
69
+ ```
70
+
71
+ You can also tell `tiny_tds` where to find your FreeTDS installation.
72
+
73
+ ```shell
74
+ gem install tiny_tds -- --with-freetds-dir=/opt/freetds
75
+ ```
76
+
77
+ ## Getting Started
78
+
79
+ Optionally, Microsoft has done a great job writing [an article](https://learn.microsoft.com/en-us/sql/connect/ruby/ruby-driver-for-sql-server?view=sql-server-ver16) on how to get started with SQL Server and Ruby using TinyTDS, however, the articles are using outdated versions.
80
+
81
+ ## Data Types
82
+
83
+ 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.
84
+
85
+ Below is a list of the data types we support when using the 7.4 TDS protocol version. Using a lower protocol version will result in these types being returned as strings.
86
+
87
+ * [date]
88
+ * [datetime2]
89
+ * [datetimeoffset]
90
+ * [time]
91
+
92
+
93
+ ## TinyTds::Client Usage
94
+
95
+ Connect to a database.
96
+
97
+ ```ruby
98
+ client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'
99
+ ```
100
+
101
+ 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.
102
+
103
+ * :username - The database server user.
104
+ * :password - The user password.
105
+ * :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).
106
+ * :host - Used if :dataserver blank. Can be an host name or IP.
107
+ * :port - Defaults to 1433. Only used if :host is used.
108
+ * :database - The default database to use.
109
+ * :appname - Short string seen in SQL Servers process/activity window.
110
+ * :tds_version - TDS version. Defaults to "7.3".
111
+ * :login_timeout - Seconds to wait for login. Default to 60 seconds.
112
+ * :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds. Timeouts caused by network failure will raise a timeout error 1 second after the configured timeout limit is hit (see [#481](https://github.com/rails-sqlserver/tiny_tds/pull/481) for details).
113
+ * :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
114
+ * :azure - Pass true to signal that you are connecting to azure.
115
+ * :contained - Pass true to signal that you are connecting with a contained database user.
116
+ * :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.
117
+ * :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:
118
+
119
+ ```ruby
120
+ opts = ... # host, username, password, etc
121
+ opts[:message_handler] = Proc.new { |m| puts m.message }
122
+ client = TinyTds::Client.new opts
123
+ # => Changed database context to 'master'.
124
+ # => Changed language setting to us_english.
125
+ client.execute("print 'hello world!'").do
126
+ # => hello world!
127
+ ```
128
+
129
+ 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.
130
+
131
+ ```ruby
132
+ client.dead? # => false
133
+ client.closed? # => false
134
+ client.active? # => true
135
+ client.execute("SQL TO A DEAD SERVER")
136
+ client.dead? # => true
137
+ client.closed? # => false
138
+ client.active? # => false
139
+ client.close
140
+ client.closed? # => true
141
+ client.active? # => false
142
+ ```
143
+
144
+ Escape strings.
145
+
146
+ ```ruby
147
+ client.escape("How's It Going'") # => "How''s It Going''"
148
+ ```
149
+
150
+ Send a SQL string to the database and return a TinyTds::Result object.
151
+
152
+ ```ruby
153
+ result = client.execute("SELECT * FROM [datatypes]")
154
+ ```
155
+
156
+
157
+ ## TinyTds::Result Usage
158
+
159
+ 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.
160
+
161
+ Calling #each on the result will lazily load each row from the database.
162
+
163
+ ```ruby
164
+ result.each do |row|
165
+ # By default each row is a hash.
166
+ # The keys are the fields, as you'd expect.
167
+ # The values are pre-built Ruby primitives mapped from their corresponding types.
168
+ end
169
+ ```
170
+
171
+ 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.
172
+
173
+ ```ruby
174
+ result = client.execute("USE [tinytdstest]")
175
+ result.fields # => []
176
+ result.do
177
+
178
+ result = client.execute("SELECT [id] FROM [datatypes]")
179
+ result.fields # => ["id"]
180
+ result.cancel
181
+ result = client.execute("SELECT [id] FROM [datatypes]")
182
+ result.each(:symbolize_keys => true)
183
+ result.fields # => [:id]
184
+ ```
185
+
186
+ You can cancel a result object's data from being loading by the server.
187
+
188
+ ```ruby
189
+ result = client.execute("SELECT * FROM [super_big_table]")
190
+ result.cancel
191
+ ```
192
+
193
+ You can use results cancelation in conjunction with results lazy loading, no problem.
194
+
195
+ ```ruby
196
+ result = client.execute("SELECT * FROM [super_big_table]")
197
+ result.each_with_index do |row, i|
198
+ break if row > 10
199
+ end
200
+ result.cancel
201
+ ```
202
+
203
+ If the SQL executed by the client returns affected rows, you can easily find out how many.
204
+
205
+ ```ruby
206
+ result.each
207
+ result.affected_rows # => 24
208
+ ```
209
+
210
+ 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`.
211
+
212
+ ```ruby
213
+ result = client.execute("DELETE FROM [datatypes]")
214
+ result.do # => 72
215
+ ```
216
+
217
+ Likewise for `INSERT` statements, the #insert method cancels any need for loading the result data and executes a `SCOPE_IDENTITY()` for the primary key.
218
+
219
+ ```ruby
220
+ result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
221
+ result.insert # => 420
222
+ ```
223
+
224
+ 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".
225
+
226
+ ```ruby
227
+ sql = ["SELECT TOP (1) [id] FROM [datatypes]",
228
+ "SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')
229
+
230
+ set1, set2 = client.execute(sql).each
231
+ set1 # => [{"id"=>11}]
232
+ set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
233
+
234
+ result = client.execute(sql)
235
+
236
+ result.each do |rowset|
237
+ # First time data loading, yields each row from each set.
238
+ # 1st: {"id"=>11}
239
+ # 2nd: {"bigint"=>-9223372036854775807}
240
+ # 3rd: {"bigint"=>9223372036854775806}
241
+ end
242
+
243
+ result.each do |rowset|
244
+ # Second time over (if columns cached), yields each set.
245
+ # 1st: [{"id"=>11}]
246
+ # 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
247
+ end
248
+ ```
249
+
250
+ 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.
251
+
252
+ ```ruby
253
+ client.sqlsent? # = false
254
+ client.canceled? # = false
255
+
256
+ result = client.execute("SELECT * FROM [super_big_table]")
257
+
258
+ client.sqlsent? # = true
259
+ client.canceled? # = false
260
+
261
+ result.each do |row|
262
+ # Assume we break after 20 rows with 80 still pending.
263
+ break if row["id"] > 20
264
+ end
265
+
266
+ client.sqlsent? # = true
267
+ client.canceled? # = false
268
+
269
+ result.cancel
270
+
271
+ client.sqlsent? # = false
272
+ client.canceled? # = true
273
+ ```
274
+
275
+ It is possible to get the return code after executing a stored procedure from either the result or client object.
276
+
277
+ ```ruby
278
+ client.return_code # => nil
279
+
280
+ result = client.execute("EXEC tinytds_TestReturnCodes")
281
+ result.do
282
+ result.return_code # => 420
283
+ client.return_code # => 420
284
+ ```
285
+
286
+
287
+ ## Query Options
288
+
289
+ 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:
290
+
291
+ * :as => :hash - Object for each row yielded. Can be set to :array.
292
+ * :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
293
+ * :cache_rows => true - Successive calls to #each returns the cached rows.
294
+ * :timezone => :local - Local to the Ruby client or :utc for UTC.
295
+ * :empty_sets => true - Include empty results set in queries that return multiple result sets.
296
+
297
+ 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
298
+
299
+ ```ruby
300
+ result.each(:as => :array, :cache_rows => false) do |row|
301
+ # Each row is now an array of values ordered by #fields.
302
+ # Rows are yielded and forgotten about, freeing memory.
303
+ end
304
+ ```
305
+
306
+ 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.
307
+
308
+ ```ruby
309
+ result = client.execute("SELECT * FROM [super_big_table]")
310
+ result.each(:first => true) # => [{'id' => 24}]
311
+ ```
312
+
313
+
314
+ ## Row Caching
315
+
316
+ 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.
317
+
318
+
319
+ ## Encoding Error Handling
320
+
321
+ 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.
322
+
323
+
324
+ ## Timeout Error Handling
325
+
326
+ TinyTDS will raise a `TinyTDS::Error` when a timeout is reached based on the options supplied to the client. Depending on the reason for the timeout, the connection could be dead or alive. When db processing is the cause for the timeout, the connection should still be usable after the error is raised. When network failure is the cause of the timeout, the connection will be dead. If you attempt to execute another command batch on a dead connection you will see a `DBPROCESS is dead or not enabled` error. Therefore, it is recommended to check for a `dead?` connection before trying to execute another command batch.
327
+
328
+ ## Binstubs
329
+
330
+ The TinyTDS gem uses binstub wrappers which mirror compiled [FreeTDS Utilities](https://www.freetds.org/userguide/usefreetds.html) 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.
331
+
332
+ * tsql - Used to test connections and debug compile time settings.
333
+ * defncopy - Used to dump schema structures.
334
+
335
+
336
+ ## Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.
337
+
338
+ 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.
339
+
340
+ * ActiveRecord SQL Server Adapter: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
341
+
342
+
343
+ ## Using TinyTDS with Azure
344
+
345
+ 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.
346
+
347
+ **IMPORTANT**: Do not use `username@server.database.windows.net` for the username connection option! You must use the shorter `username@server` instead!
348
+
349
+ 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!
350
+
351
+ ## Connection Settings
352
+
353
+ 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.
354
+
355
+ #### SQL Server
356
+
357
+ ```sql
358
+ SET ANSI_DEFAULTS ON
359
+
360
+ SET QUOTED_IDENTIFIER ON
361
+ SET CURSOR_CLOSE_ON_COMMIT OFF
362
+ SET IMPLICIT_TRANSACTIONS OFF
363
+ SET TEXTSIZE 2147483647
364
+ SET CONCAT_NULL_YIELDS_NULL ON
365
+ ```
366
+
367
+ #### Azure
368
+
369
+ ```sql
370
+ SET ANSI_NULLS ON
371
+ SET ANSI_NULL_DFLT_ON ON
372
+ SET ANSI_PADDING ON
373
+ SET ANSI_WARNINGS ON
374
+
375
+ SET QUOTED_IDENTIFIER ON
376
+ SET CURSOR_CLOSE_ON_COMMIT OFF
377
+ SET IMPLICIT_TRANSACTIONS OFF
378
+ SET TEXTSIZE 2147483647
379
+ SET CONCAT_NULL_YIELDS_NULL ON
380
+ ```
381
+
382
+
383
+ ## Thread Safety
384
+
385
+ 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:
386
+
387
+ * ConnectionPool Gem - https://github.com/mperham/connection_pool
388
+
389
+ 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.
390
+
391
+
392
+ ## Emoji Support 😍
393
+
394
+ This is possible. Since FreeTDS v1.0, utf-16 is enabled by default and supported by tiny_tds. You can toggle it by using `use_utf16` when establishing the connection.
395
+
396
+ ## Development & Testing
397
+
398
+ First, clone the repo using the command line or your Git GUI of choice.
399
+
400
+ ```shell
401
+ $ git clone git@github.com:rails-sqlserver/tiny_tds.git
402
+ ```
403
+
404
+ 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, you can use [docker-compose](https://docs.docker.com/compose/install/) to run the necessary containers for testing.
405
+
406
+ ```shell
407
+ $ docker-compose up -d
408
+ ```
409
+
410
+ This will download the official SQL Server for Linux Docker image from [Microsoft](https://hub.docker.com/r/microsoft/mssql-server-linux/). This will also download a [toxiproxy](https://github.com/shopify/toxiproxy) Docker image which we can use to simulate network failures for tests. Basically, it does the following:
411
+
412
+ ```shell
413
+ $ docker network create main-network
414
+ $ docker pull mcr.microsoft.com/mssql/server:2017-latest
415
+ $ docker run -p 1433:1433 -d --name sqlserver --network main-network mcr.microsoft.com/mssql/server:2017-latest
416
+ $ docker pull shopify/toxiproxy
417
+ $ docker run -p 8474:8474 -p 1234:1234 -d --name toxiproxy --network main-network shopify/toxiproxy
418
+ ```
419
+
420
+ Make sure to run these SQL scripts as SA to get the test database and user installed. If needed, install [sqlcmd as described by Microsoft for your platform](https://learn.microsoft.com/en-us/sql/tools/sqlcmd/sqlcmd-utility?view=sql-server-ver16&tabs=go%2Clinux&pivots=cs1-bash).
421
+
422
+ ```shell
423
+ /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P super01S3cUr3 -i ./test/sql/db-create.sql
424
+ /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P super01S3cUr3 -i ./test/sql/db-login.sql
425
+ ```
426
+
427
+ From here you can build and run tests against an installed version of FreeTDS.
428
+
429
+ ```shell
430
+ $ bundle install
431
+ $ bundle exec rake
432
+ ```
433
+
434
+ Examples us using enviornment variables to customize the test task.
435
+
436
+ ```
437
+ $ rake TINYTDS_UNIT_DATASERVER=mydbserver
438
+ $ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2017
439
+ $ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
440
+ ```
441
+
442
+ ### Code formatting
443
+
444
+ We are using `standardrb` to format our code. Just run `bundle exec standardrb --fix` to format all Ruby files.
445
+
446
+ ### Compiling Gems for Windows and Linux
447
+
448
+ > [!WARNING]
449
+ > Compiling the Gems on native Windows currently does not work.
450
+
451
+ For the convenience, TinyTDS ships pre-compiled gems for supported versions of Ruby on Windows and Linux. In order to generate these gems, [rake-compiler-dock](https://github.com/rake-compiler/rake-compiler-dock) is used.
452
+
453
+ Run the following rake task to compile the gems. This will check the availability of [Docker](https://www.docker.com/) 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:
454
+
455
+ ```shell
456
+ bundle exec rake gem:native
457
+ ```
458
+
459
+ The compiled gems will exist in `./pkg` directory.
460
+
461
+ If you only need a specific gem for one platform and architecture, run this command:
462
+
463
+ ```shell
464
+ bundle exec rake gem:native:x64-mingw-ucrt
465
+ ```
466
+
467
+ All the supported architectures and platforms are listed in the `Rakefile` in the `CrossLibraries` constant.
468
+
469
+ ## Help & Support
470
+
471
+ * Github Source: http://github.com/rails-sqlserver/tiny_tds
472
+ * Github Issues: http://github.com/rails-sqlserver/tiny_tds/issues
473
+ * Gitter Chat: https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter
474
+ * IRC Room: #rails-sqlserver on irc.freenode.net
475
+
476
+
477
+ ## About Me
478
+
479
+ 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!
480
+
481
+
482
+ ## Special Thanks
483
+
484
+ * Lars Kanis for all his help getting the Windows builds working again with rake-compiler-dock.
485
+ * Erik Bryn for joining the project and helping me thru a few tight spots. - http://github.com/ebryn
486
+ * To the authors and contributors of the Mysql2 gem for inspiration. - http://github.com/brianmario/mysql2
487
+ * 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/
488
+ * Josh Clayton of Thoughtbot for writing about Ruby C extensions. - http://robots.thoughtbot.com/post/1037240922/get-your-c-on
489
+
490
+
491
+ ## License
492
+
493
+ 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 and Linux binaries contain pre-compiled versions of FreeTDS <http://www.freetds.org/> and `libconv` which is licensed under the GNU LGPL license at <http://www.gnu.org/licenses/lgpl-2.0.html>. They also contain OpenSSL, which is licensed under the OpenSSL license at <https://openssl-library.org/source/license/index.html>.
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require "rbconfig"
2
+ require "rake"
3
+ require "rake/clean"
4
+ require "rake/extensiontask"
5
+
6
+ SPEC = Gem::Specification.load(File.expand_path("../tiny_tds.gemspec", __FILE__))
7
+
8
+ CrossLibrary = Struct.new :platform, :openssl_config
9
+ CrossLibraries = [
10
+ ["x64-mingw-ucrt", "mingw64"],
11
+ ["x64-mingw32", "mingw64"],
12
+ ["x86_64-linux-gnu", "linux-x86_64"],
13
+ ["x86_64-linux-musl", "linux-x86_64"],
14
+ ["aarch64-linux-gnu", "linux-aarch64"],
15
+ ["aarch64-linux-musl", "linux-aarch64"]
16
+ ].map do |platform, openssl_config|
17
+ CrossLibrary.new platform, openssl_config
18
+ end
19
+
20
+ # Add our project specific files to clean for a rebuild
21
+ CLEAN.include FileList["{ext,lib}/**/*.{so,#{RbConfig::CONFIG["DLEXT"]},o}"],
22
+ FileList["exe/*"]
23
+
24
+ # Clobber all our temp files and ports files including .install files
25
+ # and archives
26
+ CLOBBER.include FileList["tmp/**/*"],
27
+ FileList["ports/**/*"].exclude(%r{^ports/archives})
28
+
29
+ Dir["tasks/*.rake"].sort.each { |f| load f }
30
+
31
+ Rake::ExtensionTask.new("tiny_tds", SPEC) do |ext|
32
+ ext.lib_dir = "lib/tiny_tds"
33
+ ext.cross_compile = true
34
+ ext.cross_platform = CrossLibraries.map(&:platform)
35
+
36
+ # Add dependent DLLs to the cross gems
37
+ ext.cross_compiling do |spec|
38
+ # The fat binary gem doesn't depend on the freetds package, since it bundles the library.
39
+ spec.metadata.delete("msys2_mingw_dependencies")
40
+
41
+ if /mingw/.match?(spec.platform.to_s)
42
+ spec.files += [
43
+ "ports/#{spec.platform}/bin/libsybdb-5.dll",
44
+ "ports/#{spec.platform}/bin/defncopy.exe",
45
+ "ports/#{spec.platform}/bin/tsql.exe"
46
+ ]
47
+ elsif /linux/.match?(spec.platform.to_s)
48
+ spec.files += [
49
+ "ports/#{spec.platform}/lib/libsybdb.so.5",
50
+ "ports/#{spec.platform}/bin/defncopy",
51
+ "ports/#{spec.platform}/bin/tsql"
52
+ ]
53
+ end
54
+ end
55
+
56
+ ext.cross_config_options += CrossLibraries.map do |xlib|
57
+ {
58
+ xlib.platform => [
59
+ "--with-cross-build=#{xlib.platform}",
60
+ "--with-openssl-platform=#{xlib.openssl_config}"
61
+ ]
62
+ }
63
+ end
64
+ end
65
+
66
+ task build: [:clean, :compile]
67
+ task default: [:build, :test]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
data/bin/defncopy-ttds ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/tiny_tds/bin'
3
+ Process.exit TinyTds::Bin.exe('defncopy', *ARGV)
data/bin/tsql-ttds ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/tiny_tds/bin'
3
+ Process.exit TinyTds::Bin.exe('tsql', *ARGV)
@@ -0,0 +1,34 @@
1
+ version: '3'
2
+
3
+ networks:
4
+ main-network:
5
+
6
+ services:
7
+ mssql:
8
+ image: mcr.microsoft.com/mssql/server:${MSSQL_VERSION:-2017}-latest
9
+ container_name: sqlserver
10
+ environment:
11
+ ACCEPT_EULA: Y
12
+ MSSQL_SA_PASSWORD: super01S3cUr3
13
+ ports:
14
+ - "1433:1433"
15
+ network_mode: "host"
16
+
17
+ toxiproxy:
18
+ image: shopify/toxiproxy
19
+ container_name: toxiproxy
20
+ command: '/toxiproxy -host=127.0.0.1'
21
+ network_mode: "host"
22
+
23
+ cimgruby:
24
+ image: "cimg/ruby:${RUBY_VERSION:-2.7}"
25
+ container_name: cimg_ruby
26
+ environment:
27
+ TESTOPTS: '-v'
28
+ TINYTDS_UNIT_HOST: '127.0.0.1'
29
+ SA_PASSWORD: super01S3cUr3
30
+ TOXIPROXY_HOST: '127.0.0.1'
31
+ command: tail -F anything
32
+ volumes:
33
+ - .:/home/circleci/project
34
+ network_mode: "host"
data/exe/.keep ADDED
File without changes