Skip to content

Commit 2e194cf

Browse files
committed
Add supporting SQL Server
1 parent 0f5e7ec commit 2e194cf

File tree

8 files changed

+57
-11
lines changed

8 files changed

+57
-11
lines changed

Dockerfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
FROM ruby:2.3.1-slim
2-
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev libmysqlclient-dev nodejs nodejs-legacy npm git
2+
RUN apt-get update -qq && \
3+
apt-get install -y \
4+
build-essential libpq-dev libmysqlclient-dev nodejs nodejs-legacy npm git \
5+
freetds-dev freetds-bin unixodbc unixodbc-dev tdsodbc libc6-dev wget
6+
7+
RUN mkdir /tmp/freetds && \
8+
cd /tmp/freetds && \
9+
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.21.tar.gz && \
10+
tar -xzf freetds-1.00.21.tar.gz && \
11+
cd freetds-1.00.21 && \
12+
./configure --prefix=/usr/local --with-tdsver=7.3 && \
13+
make && \
14+
make install
15+
316
RUN mkdir /app
417

518
ADD Gemfile /tmp/Gemfile

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ gem 'rails', '~> 4.2.6'
77
gem 'pg', '~> 0.15'
88
gem 'mysql2'
99
gem 'activerecord4-redshift-adapter', '~> 0.2.0'
10+
gem 'activerecord-sqlserver-adapter', '~> 4.2.0'
11+
gem 'tiny_tds'
1012

1113
gem 'sass-rails', '~> 5.0'
1214
gem 'uglifier', '>= 1.3.0'

Gemfile.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ GEM
3232
activemodel (= 4.2.7.1)
3333
activesupport (= 4.2.7.1)
3434
arel (~> 6.0)
35+
activerecord-sqlserver-adapter (4.2.18)
36+
activerecord (~> 4.2.1)
3537
activerecord4-redshift-adapter (0.2.1)
3638
activerecord (~> 4.2.0)
3739
pg
@@ -301,6 +303,7 @@ GEM
301303
thread_safe (0.3.5)
302304
tilt (2.0.5)
303305
tins (1.6.0)
306+
tiny_tds (2.1.0)
304307
tzinfo (1.2.2)
305308
thread_safe (~> 0.1)
306309
uglifier (3.0.3)
@@ -315,6 +318,7 @@ PLATFORMS
315318
DEPENDENCIES
316319
action_args
317320
active_decorator
321+
activerecord-sqlserver-adapter (~> 4.2.0)
318322
activerecord4-redshift-adapter (~> 0.2.0)
319323
addressable
320324
byebug
@@ -354,7 +358,8 @@ DEPENDENCIES
354358
ruby-prof
355359
sass-rails (~> 5.0)
356360
simplecov
361+
tiny_tds
357362
uglifier (>= 1.3.0)
358363

359364
BUNDLED WITH
360-
1.12.5
365+
1.16.0

app/models/data_source.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def source_table_names
7575
) tables
7676
ORDER BY schemaname, tablename;
7777
SQL
78+
when ActiveRecord::ConnectionAdapters::SQLServerAdapter
79+
schemas_and_tables = source_base_class.connection.select_rows(<<-SQL, 'SCHEMA')
80+
SELECT table_schema
81+
, table_name
82+
FROM information_schema.tables
83+
WHERE table_type = 'BASE TABLE';
84+
SQL
85+
schemas_and_tables.map {|table_schema, table_name| [table_schema, table_name]}
7886
else
7987
source_base_class.connection.tables.map {|table_name| [dbname, table_name] }
8088
end

app/models/data_source_table.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ def fetch_rows(limit=20)
1717
data_source.access_logging do
1818
adapter = connection.pool.connections.first
1919
column_names = columns.map {|column| adapter.quote_column_name(column.name) }.join(", ")
20-
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
21-
SELECT #{column_names} FROM #{adapter.quote_table_name(full_table_name)} LIMIT #{limit};
22-
SQL
20+
if data_source[:adapter] == 'sqlserver'
21+
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
22+
SELECT TOP #{limit} #{column_names} FROM #{adapter.quote_table_name(full_table_name)};
23+
SQL
24+
else
25+
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
26+
SELECT #{column_names} FROM #{adapter.quote_table_name(full_table_name)} LIMIT #{limit};
27+
SQL
28+
end
2329
rows.map {|row|
2430
columns.zip(row).map {|column, value| column.type_cast_from_database(value) }
2531
}

app/models/table_memo_raw_dataset_column.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ class TableMemoRawDatasetColumn < ActiveRecord::Base
22
belongs_to :table_memo_raw_dataset, class_name: "TableMemoRawDataset"
33

44
def format_value(value)
5-
case sql_type
6-
when 'timestamp without time zone'
7-
d, t, z = value.to_s.split
8-
"#{d} #{t}"
5+
datasource_encoding = table_memo_raw_dataset.table_memo.database_memo.data_source.encoding
6+
datasource_adapter = table_memo_raw_dataset.table_memo.database_memo.data_source.adapter
7+
if datasource_adapter == 'sqlserver'
8+
case sql_type
9+
when 'timestamp'
10+
'0x' + value.unpack('H*')[0]
11+
else
12+
value.to_s.encode(Encoding.default_internal, datasource_encoding).gsub(/\u0000/, '')
13+
end
914
else
10-
value.to_s
15+
case sql_type
16+
when 'timestamp without time zone'
17+
d, t, z = value.to_s.split
18+
"#{d} #{t}"
19+
else
20+
value.to_s
21+
end
1122
end
1223
end
1324
end

app/views/data_sources/_form.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
.col-sm-10= f.text_field :description, class: "form-control", placeholder: "localhost database"
99
.form-group
1010
= f.label :adapter, class: "col-sm-2 control-label"
11-
.col-sm-10= f.select :adapter, %w(redshift postgresql mysql2), {}, class: "form-control"
11+
.col-sm-10= f.select :adapter, %w(redshift postgresql mysql2 sqlserver), {}, class: "form-control"
1212
.form-group
1313
= f.label :host, class: "col-sm-2 control-label"
1414
.col-sm-10= f.text_field :host, class: "form-control", placeholder: "localhost"

config/database.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ test:
1313
database: dmemo_test
1414

1515
production:
16+
<<: *default
1617
url: <%= ENV["DATABASE_URL"] %>

0 commit comments

Comments
 (0)