forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryResultDataTable.js
More file actions
137 lines (128 loc) · 4.23 KB
/
Copy pathQueryResultDataTable.js
File metadata and controls
137 lines (128 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React from 'react'
import {Table, Column, Cell} from 'fixed-data-table'
import SpinKitCube from './SpinKitCube.js'
import moment from 'moment'
const _ = window._
const renderValue = (input, fieldMeta) => {
if (input === null || input === undefined) {
return <em>null</em>
} else if (input === true || input === false) {
return input.toString()
} else if (fieldMeta.datatype === 'date') {
return moment(input).format('MM/DD/YYYY HH:mm:ss')
} else if (_.isObject(input)) {
return JSON.stringify(input, null, 2)
} else {
return input
}
}
// NOTE: PureComponent's shallow compare works for this component
// because the isRunning prop will toggle with each query execution
// It would otherwise not rerender on change of prop.queryResult alone
class QueryResultDataTable extends React.PureComponent {
constructor (props) {
super(props)
this.state = {
gridWidth: 0,
gridHeight: 0
}
// This binding is necessary to make `this` work in the callback
this.handleResize = this.handleResize.bind(this)
}
handleResize (e) {
var resultGrid = document.getElementById('result-grid')
if (resultGrid) {
this.setState({
gridHeight: resultGrid.clientHeight,
gridWidth: resultGrid.clientWidth
})
}
}
componentDidMount () {
window.addEventListener('resize', this.handleResize)
this.handleResize()
}
componentWillUnmount () {
window.removeEventListener('resize', this.handleResize)
}
render () {
if (this.props.isRunning) {
return (
<div id='result-grid' className='run-result-notification'>
<SpinKitCube />
</div>
)
} else if (this.props.queryError) {
return (
<div id='result-grid' className='run-result-notification label-danger'>
{this.props.queryError}
</div>
)
} else if (this.props.queryResult && this.props.queryResult.rows) {
var queryResult = this.props.queryResult
var columnNodes = queryResult.fields.map(function (field) {
var fieldMeta = queryResult.meta[field]
var valueLength = fieldMeta.maxValueLength
if (field.length > valueLength) valueLength = field.length
var columnWidth = valueLength * 20
if (columnWidth < 200) columnWidth = 200
else if (columnWidth > 350) columnWidth = 350
var cellWidth = columnWidth - 10
return (
<Column
key={field}
header={<Cell>{field}</Cell>}
cell={({rowIndex}) => {
var value = queryResult.rows[rowIndex][field]
var barStyle
var numberBar
if (fieldMeta.datatype === 'number') {
value = Number(value)
var range = fieldMeta.max - (fieldMeta.min < 0 ? fieldMeta.min : 0)
var left = 0
if (fieldMeta.min < 0 && value < 0) {
left = Math.abs(fieldMeta.min - value) / range * 100 + '%'
} else if (fieldMeta.min < 0 && value >= 0) {
left = Math.abs(fieldMeta.min) / range * 100 + '%'
}
barStyle = {
position: 'absolute',
left: left,
top: 0,
bottom: 0,
width: (Math.abs(value) / range) * 100 + '%',
backgroundColor: '#bae6f7'
}
numberBar = <div style={barStyle} />
}
return (
<Cell>
{numberBar}
<div style={{textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden', width: cellWidth, position: 'absolute'}}>
{renderValue(value, fieldMeta)}
</div>
</Cell>
)
}}
width={columnWidth}
/>
)
})
return (
<div id='result-grid'>
<Table
rowHeight={30}
rowsCount={queryResult.rows.length}
width={this.state.gridWidth}
height={this.state.gridHeight}
headerHeight={30}>
{columnNodes}
</Table>
</div>
)
} else {
return <div id='result-grid' />
}
}
}
export default QueryResultDataTable