|
| 1 | +function getRequestUrl(connection, settings) { |
| 2 | + if (!connection) { |
| 3 | + throw new Error('No Connection') |
| 4 | + } |
| 5 | + const httpProto = connection.connectionUrl.indexOf('//') === -1 ? 'http://' : '' |
| 6 | + |
| 7 | + let url = `${httpProto}${connection.connectionUrl}` |
| 8 | + // add /? |
| 9 | + url = `${url}/?output_format_json_quote_denormals=1&output_format_json_quote_64bit_integers=1&log_queries=1&enable_http_compression=1&add_http_cors_header=1&result_overflow_mode=throw&timeout_overflow_mode=throw&max_execution_time=10&max_result_rows=90000&max_result_bytes=10000000` |
| 10 | + if (connection.password) { |
| 11 | + url += `&user=${encodeURIComponent(connection.username)}&password=${encodeURIComponent( |
| 12 | + connection.password |
| 13 | + )}` |
| 14 | + } else { |
| 15 | + url += `&user=${encodeURIComponent(connection.username)}` |
| 16 | + } |
| 17 | + |
| 18 | + if (settings) { |
| 19 | + url += settings |
| 20 | + } |
| 21 | + |
| 22 | + return url |
| 23 | +} |
| 24 | + |
| 25 | +function request(request, init) { |
| 26 | + return fetch(request, init) |
| 27 | + .then((response) => { |
| 28 | + const contentType = response.headers.get('content-type') |
| 29 | + if ( |
| 30 | + contentType && |
| 31 | + response.status === 200 && |
| 32 | + response.statusText.toLowerCase() === 'ok' && |
| 33 | + (contentType.includes('text/plain') || |
| 34 | + contentType.includes('application/xml') || |
| 35 | + contentType.includes('text/csv') || |
| 36 | + contentType.includes('text/tab-separated-values')) |
| 37 | + ) { |
| 38 | + // if create table && drop table |
| 39 | + return Promise.resolve(response.text()) |
| 40 | + } |
| 41 | + if ( |
| 42 | + contentType && |
| 43 | + contentType.includes('application/json') && |
| 44 | + response.status >= 200 && |
| 45 | + response.status < 300 |
| 46 | + ) { |
| 47 | + return response.json() |
| 48 | + } |
| 49 | + return response.text().then(Promise.reject.bind(Promise)) // refactor ??? |
| 50 | + }) |
| 51 | + .then( |
| 52 | + (response) => { |
| 53 | + if (response === 'OK' || !response) { |
| 54 | + return 'OK' |
| 55 | + } |
| 56 | + return response |
| 57 | + }, |
| 58 | + // refactor: use catch |
| 59 | + (responseBody) => Promise.reject(responseBody) |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +function getRequestInit(query) { |
| 64 | + const init = { |
| 65 | + mode: 'cors', |
| 66 | + method: 'post', |
| 67 | + headers: { |
| 68 | + 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8', |
| 69 | + 'Accept-Encoding': 'gzip', |
| 70 | + }, |
| 71 | + body: query, |
| 72 | + // credentials: 'include', // Error : The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. |
| 73 | + } |
| 74 | + return init |
| 75 | +} |
| 76 | + |
| 77 | +// const url = getRequestUrl(connectionData, settings) |
| 78 | + |
| 79 | +const queryAllColumns = (connection) => { |
| 80 | + const sql = `SELECT * FROM system.columns |
| 81 | +
|
| 82 | + LIMIT 50000 |
| 83 | +
|
| 84 | + FORMAT JSON` |
| 85 | + const init = getRequestInit(sql) |
| 86 | + const url = getRequestUrl(connection, connection.params) |
| 87 | + |
| 88 | + return request(url, init).then(res => { |
| 89 | + return res |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +const queryAllDatabases = (connection) => { |
| 94 | + const sql = `SELECT name FROM system.databases LIMIT 10000 |
| 95 | + FORMAT JSON` |
| 96 | + |
| 97 | + const init = getRequestInit(sql) |
| 98 | + const url = getRequestUrl(connection, connection.params) |
| 99 | + |
| 100 | + return request(url, init).then(res => { |
| 101 | + return res |
| 102 | + }) |
| 103 | +} |
| 104 | + |
| 105 | +const queryAllTables = (connection) => { |
| 106 | + const sql = `SELECT t.database, |
| 107 | + t.name, |
| 108 | + t.engine, |
| 109 | + -- t.*, |
| 110 | + pa.size |
| 111 | + FROM system.tables as t ANY LEFT JOIN ( SELECT database,table as name,formatReadableSize(sum(bytes)) as size FROM system.parts GROUP BY database,name ) as pa USING (database,name) |
| 112 | + LIMIT 10000 |
| 113 | +
|
| 114 | + FORMAT JSON` |
| 115 | + |
| 116 | + const init = getRequestInit(sql) |
| 117 | + const url = getRequestUrl(connection, connection.params) |
| 118 | + |
| 119 | + return request(url, init).then(res => { |
| 120 | + return res |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +const createTree = (columns, tables, database, connection) => { |
| 125 | + const tablesTree = tables.map((item) => { |
| 126 | + const children = columns.filter((col) => col.table === item.name) |
| 127 | + return { |
| 128 | + ...item, |
| 129 | + children |
| 130 | + } |
| 131 | + }) |
| 132 | + |
| 133 | + const databaseTree = database.map(item => { |
| 134 | + const children = tablesTree.filter((table) => table.database === item.name) |
| 135 | + return { |
| 136 | + ...item, |
| 137 | + children |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + const finalTree = { |
| 142 | + name: connection.connectionName, |
| 143 | + children: databaseTree |
| 144 | + } |
| 145 | + return [finalTree] |
| 146 | +} |
| 147 | + |
| 148 | +self.addEventListener('message', function (e) { |
| 149 | + // self.postMessage(e.data) |
| 150 | + const connection = JSON.parse(e.data) |
| 151 | + Promise.all([queryAllColumns(connection), queryAllTables(connection), queryAllDatabases(connection)]) |
| 152 | + .then(res => { |
| 153 | + const dataArr = res.map(item => item.data) |
| 154 | + const treeData = createTree( |
| 155 | + dataArr[0], |
| 156 | + dataArr[1], |
| 157 | + dataArr[2], |
| 158 | + connection |
| 159 | + ) |
| 160 | + self.postMessage(JSON.stringify({ |
| 161 | + tree: treeData, |
| 162 | + columns: dataArr[0] |
| 163 | + })) |
| 164 | + }) |
| 165 | +}, false); |
0 commit comments