antares/src/renderer/components/WorkspaceQueryTable.vue

195 lines
5.0 KiB
Vue
Raw Normal View History

2020-06-10 19:29:10 +02:00
<template>
2020-06-11 23:34:38 +02:00
<BaseVirtualScroll
v-if="results.rows"
ref="resultTable"
2020-06-16 18:01:22 +02:00
:items="localResults"
2020-06-11 23:34:38 +02:00
:item-height="25"
class="vscroll"
:style="{'height': resultsSize+'px'}"
>
<template slot-scope="{ items }">
<div class="table table-hover">
<div class="thead">
<div class="tr">
<div
2020-06-16 18:01:22 +02:00
v-for="field in fields"
2020-06-11 23:34:38 +02:00
:key="field.name"
class="th"
>
2020-06-16 18:01:22 +02:00
<div class="table-column-title">
<i
v-if="field.key"
class="material-icons column-key c-help"
:class="`key-${field.key}`"
:title="keyName(field.key)"
>vpn_key</i>
<span>{{ field.name }}</span>
</div>
2020-06-11 23:34:38 +02:00
</div>
</div>
2020-06-10 19:29:10 +02:00
</div>
2020-06-11 23:34:38 +02:00
<div class="tbody">
<div
2020-06-12 18:10:45 +02:00
v-for="row in items"
:key="row._id"
2020-06-11 23:34:38 +02:00
class="tr"
>
<div
v-for="(col, cKey) in row"
:key="cKey"
class="td"
2020-06-18 19:01:09 +02:00
:class="`type-${fieldType(cKey)}${isNull(col)}`"
:style="{'display': cKey === '_id' ? 'none' : ''}"
2020-06-16 18:01:22 +02:00
tabindex="0"
2020-06-11 23:34:38 +02:00
>
2020-06-18 19:01:09 +02:00
{{ col | typeFormat(fieldType(cKey)) }}
2020-06-11 23:34:38 +02:00
</div>
</div>
2020-06-10 19:29:10 +02:00
</div>
</div>
2020-06-11 23:34:38 +02:00
</template>
</BaseVirtualScroll>
2020-06-10 19:29:10 +02:00
</template>
<script>
2020-06-18 19:01:09 +02:00
import { uidGen, mimeFromHex, formatBytes } from 'common/libs/utilities';
import hexToBinary from 'common/libs/hexToBinary';
import moment from 'moment';
2020-06-11 23:34:38 +02:00
import BaseVirtualScroll from '@/components/BaseVirtualScroll';
2020-06-10 19:29:10 +02:00
export default {
name: 'WorkspaceQueryTable',
2020-06-11 23:34:38 +02:00
components: {
BaseVirtualScroll
},
2020-06-18 19:01:09 +02:00
filters: {
typeFormat (val, type) {
if (!val) return val;
switch (type) {
case 'char':
case 'varchar':
case 'text':
2020-06-19 18:03:52 +02:00
case 'mediumtext':
2020-06-18 19:01:09 +02:00
return val.substring(0, 128);
case 'date':
return moment(val).format('YYYY-MM-DD');
case 'datetime':
2020-06-19 18:03:52 +02:00
case 'timestamp':
2020-06-18 19:01:09 +02:00
return moment(val).format('YYYY-MM-DD HH:mm:ss.SSS');
case 'blob':
case 'mediumblob':
case 'longblob': {
const buff = Buffer.from(val);
if (!buff.length) return '';
const hex = buff.toString('hex').substring(0, 8).toUpperCase();
return `${mimeFromHex(hex).mime} (${formatBytes(buff.length)})`;
}
case 'bit': {
const hex = Buffer.from(val).toString('hex');
return hexToBinary(hex);
}
default:
return val;
}
}
},
2020-06-10 19:29:10 +02:00
props: {
2020-06-16 18:01:22 +02:00
results: Object,
fields: Array
2020-06-10 19:29:10 +02:00
},
2020-06-11 23:34:38 +02:00
data () {
return {
2020-06-16 18:01:22 +02:00
resultsSize: 1000,
localResults: []
2020-06-11 23:34:38 +02:00
};
},
2020-06-16 18:01:22 +02:00
watch: {
results () {
this.localResults = this.results.rows ? this.results.rows.map(item => {
2020-06-12 18:10:45 +02:00
return { ...item, _id: uidGen() };
}) : [];
2020-06-11 23:34:38 +02:00
}
},
2020-06-12 18:10:45 +02:00
updated () {
if (this.$refs.resultTable)
this.resizeResults();
2020-06-11 23:34:38 +02:00
},
mounted () {
window.addEventListener('resize', this.resizeResults);
},
destroyed () {
window.removeEventListener('resize', this.resizeResults);
},
2020-06-10 19:29:10 +02:00
methods: {
2020-06-18 19:01:09 +02:00
fieldType (cKey) {
let type = 'unknown';
const field = this.fields.filter(field => field.name === cKey)[0];
if (field)
type = field.type;
2020-06-10 19:29:10 +02:00
2020-06-18 19:01:09 +02:00
return type;
},
isNull (col) {
return col === null ? ' is-null' : '';
2020-06-11 23:34:38 +02:00
},
2020-06-16 18:01:22 +02:00
keyName (key) {
switch (key) {
case 'pri':
return 'PRIMARY';
case 'uni':
return 'UNIQUE';
case 'mul':
return 'INDEX';
default:
return 'UNKNOWN ' + key;
}
},
2020-06-11 23:34:38 +02:00
resizeResults (e) {
2020-06-12 18:10:45 +02:00
if (this.$refs.resultTable) {
const el = this.$refs.resultTable.$el;
const footer = document.getElementById('footer');
2020-06-11 23:34:38 +02:00
2020-06-12 18:10:45 +02:00
if (el) {
const size = window.innerHeight - el.getBoundingClientRect().top - footer.offsetHeight;
this.resultsSize = size;
}
2020-06-11 23:34:38 +02:00
}
2020-06-10 19:29:10 +02:00
}
}
};
</script>
2020-06-16 18:01:22 +02:00
<style lang="scss">
2020-06-11 23:34:38 +02:00
.vscroll {
height: 1000px;
overflow: auto;
overflow-anchor: none;
}
2020-06-16 18:01:22 +02:00
.table-column-title{
display: flex;
align-items: center;
}
.column-key{
transform: rotate(90deg);
font-size: .7rem;
line-height: 1.5;
margin-right: .2rem;
&.key-pri{
color: goldenrod;
}
&.key-uni{
color: deepskyblue;
}
&.key-mul{
color: palegreen;
}
}
2020-06-10 19:29:10 +02:00
</style>