diff --git a/app/constants/constants.js b/app/constants/constants.js index 2ec7bc479..98287ba68 100644 --- a/app/constants/constants.js +++ b/app/constants/constants.js @@ -295,7 +295,9 @@ export function PREVIEW_QUERY(connection, table, elasticsearchIndex) { case DIALECTS.ATHENA: return `SELECT * FROM ${table} LIMIT 1000`; case DIALECTS.MSSQL: - return `SELECT TOP 1000 * FROM ${connection.database}.dbo.${table}`; + return (connection.database) ? + `SELECT TOP 1000 * FROM "${connection.database}".${table}` : + `SELECT TOP 1000 * FROM ${table}`; case DIALECTS.ELASTICSEARCH: return JSON.stringify({ index: elasticsearchIndex || '_all', diff --git a/backend/persistent/datastores/Datastores.js b/backend/persistent/datastores/Datastores.js index e615de4da..dca55472a 100644 --- a/backend/persistent/datastores/Datastores.js +++ b/backend/persistent/datastores/Datastores.js @@ -4,7 +4,7 @@ function logError(error) { throw error; } -import * as Sql from './Sql'; +import * as Sql from './sql.js'; import * as Elasticsearch from './Elasticsearch'; import * as S3 from './S3'; import * as ApacheDrill from './ApacheDrill'; diff --git a/backend/persistent/datastores/Sql.js b/backend/persistent/datastores/sql.js similarity index 76% rename from backend/persistent/datastores/Sql.js rename to backend/persistent/datastores/sql.js index 35f6638c3..d772061ff 100644 --- a/backend/persistent/datastores/Sql.js +++ b/backend/persistent/datastores/sql.js @@ -26,32 +26,6 @@ const REDSHIFT_OPTIONS = { } }; -const SHOW_TABLES_QUERY = { - [DIALECTS.MYSQL]: 'SHOW TABLES', - [DIALECTS.MARIADB]: 'SHOW TABLES', - [DIALECTS.SQLITE]: 'SELECT name FROM sqlite_master WHERE type="table"', - [DIALECTS.MSSQL]: ( - 'SELECT TABLE_NAME FROM ' + - 'information_schema.tables' - ), - [DIALECTS.POSTGRES]: ` - SELECT table_schema || '."' || table_name || '"' - FROM information_schema.tables - WHERE table_type != 'VIEW' - AND table_schema != 'pg_catalog' - AND table_schema != 'information_schema' - ORDER BY table_schema, table_name - `, - [DIALECTS.REDSHIFT]: ` - SELECT table_schema || '."' || table_name || '"' - FROM information_schema.tables - WHERE table_type != 'VIEW' - AND table_schema != 'pg_catalog' - AND table_schema != 'information_schema' - ORDER BY table_schema, table_name - ` -}; - function createClient(connection) { const { @@ -132,14 +106,43 @@ export function query(queryString, connection) { } export function tables(connection) { + const {dialect} = connection; + + const SHOW_TABLES_QUERY = { + [DIALECTS.MYSQL]: 'SHOW TABLES', + [DIALECTS.MARIADB]: 'SHOW TABLES', + [DIALECTS.SQLITE]: 'SELECT name FROM sqlite_master WHERE type="table"', + [DIALECTS.MSSQL]: ` + SELECT '"' + table_schema + '"."' + table_name + '"' + FROM information_schema.tables + ORDER BY table_schema, table_name + `, + [DIALECTS.POSTGRES]: ` + SELECT table_schema || '."' || table_name || '"' + FROM information_schema.tables + WHERE table_type != 'VIEW' + AND table_schema != 'pg_catalog' + AND table_schema != 'information_schema' + ORDER BY table_schema, table_name + `, + [DIALECTS.REDSHIFT]: ` + SELECT table_schema || '."' || table_name || '"' + FROM information_schema.tables + WHERE table_type != 'VIEW' + AND table_schema != 'pg_catalog' + AND table_schema != 'information_schema' + ORDER BY table_schema, table_name + ` + }; + return createClient(connection).query( - SHOW_TABLES_QUERY[connection.dialect], + SHOW_TABLES_QUERY[dialect], {type: Sequelize.QueryTypes.SELECT} ).then(tableList => { let tableNames; - if (connection.dialect === 'postgres' || connection.dialect === 'redshift') { + if (dialect === 'postgres' || dialect === 'redshift') { tableNames = tableList.map(data => { let tableName = String(data['?column?']); @@ -156,7 +159,7 @@ export function tables(connection) { return tableName; }); - } else if (connection.dialect === 'sqlite') { + } else if (dialect === 'sqlite') { tableNames = tableList; } else { tableNames = tableList.map(object => values(object)[0]); @@ -170,22 +173,23 @@ export function tables(connection) { export function schemas(connection) { const {database, dialect} = connection; - // Suppressing ESLint cause single quote strings beside template strings - // would be inconvenient when changed queries - /* eslint-disable quotes */ let queryString; switch (dialect) { case DIALECTS.MYSQL: case DIALECTS.MARIADB: - queryString = `SELECT table_name, column_name, data_type FROM information_schema.columns ` + - `WHERE table_schema = '${database}' ORDER BY table_name`; + queryString = ` + SELECT table_name, column_name, data_type + FROM information_schema.columns + WHERE table_schema = '${database}' + ORDER BY table_name + `; break; case DIALECTS.SQLITE: return sqlite_schemas(connection); case DIALECTS.POSTGRES: case DIALECTS.REDSHIFT: queryString = ` - SELECT table_schema || '."' || table_name || '"', column_name, data_type + SELECT table_schema || '."' || table_name || '"', column_name, data_type FROM information_schema.columns WHERE table_catalog = '${database}' AND table_schema != 'pg_catalog' @@ -195,17 +199,14 @@ export function schemas(connection) { break; case DIALECTS.MSSQL: queryString = ` - SELECT T.name AS Table_Name, C.name AS Column_Name, P.name AS Data_Type - FROM sys.objects AS T - JOIN sys.columns AS C ON T.object_id = C.object_id - JOIN sys.types AS P ON C.system_type_id = P.system_type_id - WHERE T.type_desc = 'USER_TABLE'; + SELECT '"' + table_schema + '"."' + table_name + '"', column_name, data_type + FROM information_schema.columns + ORDER BY table_schema, table_name, column_name `; break; default: throw new Error(`Dialect ${dialect} is not one of the SQL DIALECTS`); } - /* eslint-enable quotes */ return query(queryString, connection); } diff --git a/package.json b/package.json index 5dc72d7d0..4d778e2c7 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "docker:db2:start": "docker run --rm -ti -p 50000:50000 pdc-db2", "docker:falcon:build": "docker build -t falcon-sql-client:local .", "docker:falcon:start": "docker run -ti --rm -p 9494:9494 -e PLOTLY_CONNECTOR_AUTH_ENABLED=$PLOTLY_CONNECTOR_AUTH_ENABLED -e PLOTLY_CONNECTOR_ALLOWED_USERS=$PLOTLY_CONNECTOR_ALLOWED_USERS falcon-sql-client:local", + "docker:mssql:start": "docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d microsoft/mssql-server-linux:latest", "docker:oracle:build": "docker build test/docker/oracle -t falcon-test-oracle --no-cache", "docker:oracle:start": "docker run --rm -ti -p 1521:1521 falcon-test-oracle", "rebuild:modules:electron": "cross-env FSEVENTS_BUILD_FROM_SOURCE=true node scripts/rebuild-modules.js --electron", diff --git a/test/backend/routes.spec.js b/test/backend/routes.spec.js index 93404fc54..ab1640c93 100644 --- a/test/backend/routes.spec.js +++ b/test/backend/routes.spec.js @@ -366,7 +366,23 @@ describe('Routes:', () => { 'weather_data_seattle_2016', 'world_gdp_with_codes_2014' ]; - if (connection.dialect === 'postgres') { + if (connection.dialect === 'mssql') { + tables = [ + '"dbo"."alcohol_consumption_by_country_2010"', + '"dbo"."apple_stock_2014"', + '"dbo"."ebola_2014"', + '"dbo"."february_aa_flight_paths_2011"', + '"dbo"."february_us_airport_traffic_2011"', + '"dbo"."precipitation_2015_06_30"', + '"dbo"."us_ag_exports_2011"', + '"dbo"."us_cities_2014"', + '"dbo"."usa_states_2014"', + '"dbo"."walmart_store_openings_1962_2006"', + '"dbo"."weather_data_seattle_2016"', + '"dbo"."world_gdp_with_codes_2014"', + '"guest"."apple_stock_2014"' + ]; + } else if (connection.dialect === 'postgres') { tables = [ 'alcohol_consumption_by_country_2010', 'apple_stock_2014', @@ -1082,87 +1098,87 @@ describe('Routes:', () => { ]; } else if (connection.dialect === 'mssql') { rows = [ - [ 'walmart_store_openings_1962_2006', 'storenum', 'int'], - [ 'walmart_store_openings_1962_2006', 'OPENDATE', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'date_super', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'conversion', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'st', 'int'], - [ 'walmart_store_openings_1962_2006', 'county', 'int'], - [ 'walmart_store_openings_1962_2006', 'STREETADDR', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'STRCITY', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'STRSTATE', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'ZIPCODE', 'int'], - [ 'walmart_store_openings_1962_2006', 'type_store', 'varchar'], - [ 'walmart_store_openings_1962_2006', 'LAT', 'decimal'], - [ 'walmart_store_openings_1962_2006', 'LON', 'decimal'], - [ 'walmart_store_openings_1962_2006', 'MONTH', 'int'], - [ 'walmart_store_openings_1962_2006', 'DAY', 'int'], - [ 'walmart_store_openings_1962_2006', 'YEAR', 'int'], - [ 'alcohol_consumption_by_country_2010', 'location', 'varchar'], - [ 'alcohol_consumption_by_country_2010', 'alcohol', 'varchar'], - [ 'february_aa_flight_paths_2011', 'start_lat', 'decimal'], - [ 'february_aa_flight_paths_2011', 'start_lon', 'decimal'], - [ 'february_aa_flight_paths_2011', 'end_lat', 'decimal'], - [ 'february_aa_flight_paths_2011', 'end_lon', 'decimal'], - [ 'february_aa_flight_paths_2011', 'airline', 'varchar'], - [ 'february_aa_flight_paths_2011', 'airport1', 'varchar'], - [ 'february_aa_flight_paths_2011', 'airport2', 'varchar'], - [ 'february_aa_flight_paths_2011', 'cnt', 'int'], - [ 'february_us_airport_traffic_2011', 'iata', 'varchar'], - [ 'february_us_airport_traffic_2011', 'airport', 'varchar'], - [ 'february_us_airport_traffic_2011', 'city', 'varchar'], - [ 'february_us_airport_traffic_2011', 'state', 'varchar'], - [ 'february_us_airport_traffic_2011', 'country', 'varchar'], - [ 'february_us_airport_traffic_2011', 'lat', 'decimal'], - [ 'february_us_airport_traffic_2011', 'long', 'decimal'], - [ 'february_us_airport_traffic_2011', 'cnt', 'int'], - [ 'us_ag_exports_2011', 'code', 'varchar'], - [ 'us_ag_exports_2011', 'state', 'varchar'], - [ 'us_ag_exports_2011', 'category', 'varchar'], - [ 'us_ag_exports_2011', 'total exports', 'decimal'], - [ 'us_ag_exports_2011', 'beef', 'decimal'], - [ 'us_ag_exports_2011', 'pork', 'decimal'], - [ 'us_ag_exports_2011', 'poultry', 'decimal'], - [ 'us_ag_exports_2011', 'dairy', 'decimal'], - [ 'us_ag_exports_2011', 'fruits fresh', 'decimal'], - [ 'us_ag_exports_2011', 'fruits proc', 'decimal'], - [ 'us_ag_exports_2011', 'total fruits', 'decimal'], - [ 'us_ag_exports_2011', 'veggies fresh', 'decimal'], - [ 'us_ag_exports_2011', 'veggies proc', 'decimal'], - [ 'us_ag_exports_2011', 'total veggies', 'decimal'], - [ 'us_ag_exports_2011', 'corn', 'decimal'], - [ 'us_ag_exports_2011', 'wheat', 'decimal'], - [ 'us_ag_exports_2011', 'cotton', 'decimal'], - [ 'apple_stock_2014', 'AAPL_x', 'datetime'], - [ 'apple_stock_2014', 'AAPL_y', 'decimal'], - [ 'ebola_2014', 'Country', 'varchar'], - [ 'ebola_2014', 'Month', 'int'], - [ 'ebola_2014', 'Year', 'int'], - [ 'ebola_2014', 'Lat', 'decimal'], - [ 'ebola_2014', 'Lon', 'decimal'], - [ 'ebola_2014', 'Value', 'varchar'], - [ 'us_cities_2014', 'name', 'varchar'], - [ 'us_cities_2014', 'pop', 'int'], - [ 'us_cities_2014', 'lat', 'decimal'], - [ 'us_cities_2014', 'lon', 'decimal'], - [ 'usa_states_2014', 'rank', 'int'], - [ 'usa_states_2014', 'state', 'varchar'], - [ 'usa_states_2014', 'postal', 'varchar'], - [ 'usa_states_2014', 'pop', 'decimal'], - [ 'world_gdp_with_codes_2014', 'COUNTRY', 'varchar'], - [ 'world_gdp_with_codes_2014', 'GDP (BILLIONS)', 'decimal'], - [ 'world_gdp_with_codes_2014', 'CODE', 'varchar'], - [ 'precipitation_2015_06_30', 'Hrapx', 'varchar'], - [ 'precipitation_2015_06_30', 'Hrapy', 'varchar'], - [ 'precipitation_2015_06_30', 'Lat', 'decimal'], - [ 'precipitation_2015_06_30', 'Lon', 'decimal'], - [ 'precipitation_2015_06_30', 'Globvalue', 'decimal'], - [ 'weather_data_seattle_2016', 'Date', 'varchar'], - [ 'weather_data_seattle_2016', 'Max_TemperatureC', 'varchar'], - [ 'weather_data_seattle_2016', 'Mean_TemperatureC', 'varchar'], - [ 'weather_data_seattle_2016', 'Min_TemperatureC', 'varchar'], - [ 'apple_stock_2014', 'AAPL_x', 'datetime'], - [ 'apple_stock_2014', 'AAPL_y', 'decimal'] + [ '"dbo"."alcohol_consumption_by_country_2010"', 'alcohol', 'varchar' ], + [ '"dbo"."alcohol_consumption_by_country_2010"', 'location', 'varchar' ], + [ '"dbo"."apple_stock_2014"', 'AAPL_x', 'datetime' ], + [ '"dbo"."apple_stock_2014"', 'AAPL_y', 'decimal' ], + [ '"dbo"."ebola_2014"', 'Country', 'varchar' ], + [ '"dbo"."ebola_2014"', 'Lat', 'decimal' ], + [ '"dbo"."ebola_2014"', 'Lon', 'decimal' ], + [ '"dbo"."ebola_2014"', 'Month', 'int' ], + [ '"dbo"."ebola_2014"', 'Value', 'varchar' ], + [ '"dbo"."ebola_2014"', 'Year', 'int' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'airline', 'varchar' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'airport1', 'varchar' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'airport2', 'varchar' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'cnt', 'int' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'end_lat', 'decimal' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'end_lon', 'decimal' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'start_lat', 'decimal' ], + [ '"dbo"."february_aa_flight_paths_2011"', 'start_lon', 'decimal' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'airport', 'varchar' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'city', 'varchar' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'cnt', 'int' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'country', 'varchar' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'iata', 'varchar' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'lat', 'decimal' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'long', 'decimal' ], + [ '"dbo"."february_us_airport_traffic_2011"', 'state', 'varchar' ], + [ '"dbo"."precipitation_2015_06_30"', 'Globvalue', 'decimal' ], + [ '"dbo"."precipitation_2015_06_30"', 'Hrapx', 'varchar' ], + [ '"dbo"."precipitation_2015_06_30"', 'Hrapy', 'varchar' ], + [ '"dbo"."precipitation_2015_06_30"', 'Lat', 'decimal' ], + [ '"dbo"."precipitation_2015_06_30"', 'Lon', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'beef', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'category', 'varchar' ], + [ '"dbo"."us_ag_exports_2011"', 'code', 'varchar' ], + [ '"dbo"."us_ag_exports_2011"', 'corn', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'cotton', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'dairy', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'fruits fresh', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'fruits proc', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'pork', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'poultry', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'state', 'varchar' ], + [ '"dbo"."us_ag_exports_2011"', 'total exports', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'total fruits', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'total veggies', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'veggies fresh', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'veggies proc', 'decimal' ], + [ '"dbo"."us_ag_exports_2011"', 'wheat', 'decimal' ], + [ '"dbo"."us_cities_2014"', 'lat', 'decimal' ], + [ '"dbo"."us_cities_2014"', 'lon', 'decimal' ], + [ '"dbo"."us_cities_2014"', 'name', 'varchar' ], + [ '"dbo"."us_cities_2014"', 'pop', 'int' ], + [ '"dbo"."usa_states_2014"', 'pop', 'decimal' ], + [ '"dbo"."usa_states_2014"', 'postal', 'varchar' ], + [ '"dbo"."usa_states_2014"', 'rank', 'int' ], + [ '"dbo"."usa_states_2014"', 'state', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'conversion', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'county', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'date_super', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'DAY', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'LAT', 'decimal' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'LON', 'decimal' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'MONTH', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'OPENDATE', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'st', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'storenum', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'STRCITY', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'STREETADDR', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'STRSTATE', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'type_store', 'varchar' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'YEAR', 'int' ], + [ '"dbo"."walmart_store_openings_1962_2006"', 'ZIPCODE', 'int' ], + [ '"dbo"."weather_data_seattle_2016"', 'Date', 'varchar' ], + [ '"dbo"."weather_data_seattle_2016"', 'Max_TemperatureC', 'varchar' ], + [ '"dbo"."weather_data_seattle_2016"', 'Mean_TemperatureC', 'varchar' ], + [ '"dbo"."weather_data_seattle_2016"', 'Min_TemperatureC', 'varchar' ], + [ '"dbo"."world_gdp_with_codes_2014"', 'CODE', 'varchar' ], + [ '"dbo"."world_gdp_with_codes_2014"', 'COUNTRY', 'varchar' ], + [ '"dbo"."world_gdp_with_codes_2014"', 'GDP (BILLIONS)', 'decimal' ], + [ '"guest"."apple_stock_2014"', 'AAPL_x', 'datetime' ], + [ '"guest"."apple_stock_2014"', 'AAPL_y', 'decimal' ] ]; } else if (connection.dialect === 'apache impala') { rows = [