Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #464
Browse files Browse the repository at this point in the history
mssql: fix issues #299 #409 #462
  • Loading branch information
n-riesco authored Jun 25, 2018
2 parents 52a78ed + d8de690 commit f54de61
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 125 deletions.
4 changes: 3 additions & 1 deletion app/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion backend/persistent/datastores/Datastores.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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?']);

Expand All @@ -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]);
Expand All @@ -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'
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit f54de61

Please # to comment.