Skip to content

Commit

Permalink
Add supporting SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsuyui committed Nov 21, 2017
1 parent 0f5e7ec commit 2e194cf
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
15 changes: 14 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
FROM ruby:2.3.1-slim
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev libmysqlclient-dev nodejs nodejs-legacy npm git
RUN apt-get update -qq && \
apt-get install -y \
build-essential libpq-dev libmysqlclient-dev nodejs nodejs-legacy npm git \
freetds-dev freetds-bin unixodbc unixodbc-dev tdsodbc libc6-dev wget

RUN mkdir /tmp/freetds && \
cd /tmp/freetds && \
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.21.tar.gz && \
tar -xzf freetds-1.00.21.tar.gz && \
cd freetds-1.00.21 && \
./configure --prefix=/usr/local --with-tdsver=7.3 && \
make && \
make install

RUN mkdir /app

ADD Gemfile /tmp/Gemfile
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ gem 'rails', '~> 4.2.6'
gem 'pg', '~> 0.15'
gem 'mysql2'
gem 'activerecord4-redshift-adapter', '~> 0.2.0'
gem 'activerecord-sqlserver-adapter', '~> 4.2.0'
gem 'tiny_tds'

gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ GEM
activemodel (= 4.2.7.1)
activesupport (= 4.2.7.1)
arel (~> 6.0)
activerecord-sqlserver-adapter (4.2.18)
activerecord (~> 4.2.1)
activerecord4-redshift-adapter (0.2.1)
activerecord (~> 4.2.0)
pg
Expand Down Expand Up @@ -301,6 +303,7 @@ GEM
thread_safe (0.3.5)
tilt (2.0.5)
tins (1.6.0)
tiny_tds (2.1.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.3)
Expand All @@ -315,6 +318,7 @@ PLATFORMS
DEPENDENCIES
action_args
active_decorator
activerecord-sqlserver-adapter (~> 4.2.0)
activerecord4-redshift-adapter (~> 0.2.0)
addressable
byebug
Expand Down Expand Up @@ -354,7 +358,8 @@ DEPENDENCIES
ruby-prof
sass-rails (~> 5.0)
simplecov
tiny_tds
uglifier (>= 1.3.0)

BUNDLED WITH
1.12.5
1.16.0
8 changes: 8 additions & 0 deletions app/models/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def source_table_names
) tables
ORDER BY schemaname, tablename;
SQL
when ActiveRecord::ConnectionAdapters::SQLServerAdapter
schemas_and_tables = source_base_class.connection.select_rows(<<-SQL, 'SCHEMA')
SELECT table_schema
, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';
SQL
schemas_and_tables.map {|table_schema, table_name| [table_schema, table_name]}
else
source_base_class.connection.tables.map {|table_name| [dbname, table_name] }
end
Expand Down
12 changes: 9 additions & 3 deletions app/models/data_source_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ def fetch_rows(limit=20)
data_source.access_logging do
adapter = connection.pool.connections.first
column_names = columns.map {|column| adapter.quote_column_name(column.name) }.join(", ")
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
SELECT #{column_names} FROM #{adapter.quote_table_name(full_table_name)} LIMIT #{limit};
SQL
if data_source[:adapter] == 'sqlserver'
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
SELECT TOP #{limit} #{column_names} FROM #{adapter.quote_table_name(full_table_name)};
SQL
else
rows = connection.select_rows(<<-SQL, "#{table_name.classify} Load")
SELECT #{column_names} FROM #{adapter.quote_table_name(full_table_name)} LIMIT #{limit};
SQL
end
rows.map {|row|
columns.zip(row).map {|column, value| column.type_cast_from_database(value) }
}
Expand Down
21 changes: 16 additions & 5 deletions app/models/table_memo_raw_dataset_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ class TableMemoRawDatasetColumn < ActiveRecord::Base
belongs_to :table_memo_raw_dataset, class_name: "TableMemoRawDataset"

def format_value(value)
case sql_type
when 'timestamp without time zone'
d, t, z = value.to_s.split
"#{d} #{t}"
datasource_encoding = table_memo_raw_dataset.table_memo.database_memo.data_source.encoding
datasource_adapter = table_memo_raw_dataset.table_memo.database_memo.data_source.adapter
if datasource_adapter == 'sqlserver'
case sql_type
when 'timestamp'
'0x' + value.unpack('H*')[0]
else
value.to_s.encode(Encoding.default_internal, datasource_encoding).gsub(/\u0000/, '')
end
else
value.to_s
case sql_type
when 'timestamp without time zone'
d, t, z = value.to_s.split
"#{d} #{t}"
else
value.to_s
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/data_sources/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.col-sm-10= f.text_field :description, class: "form-control", placeholder: "localhost database"
.form-group
= f.label :adapter, class: "col-sm-2 control-label"
.col-sm-10= f.select :adapter, %w(redshift postgresql mysql2), {}, class: "form-control"
.col-sm-10= f.select :adapter, %w(redshift postgresql mysql2 sqlserver), {}, class: "form-control"
.form-group
= f.label :host, class: "col-sm-2 control-label"
.col-sm-10= f.text_field :host, class: "form-control", placeholder: "localhost"
Expand Down
1 change: 1 addition & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ test:
database: dmemo_test

production:
<<: *default
url: <%= ENV["DATABASE_URL"] %>

0 comments on commit 2e194cf

Please sign in to comment.