|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div |
| 4 | + class="drop-area" |
| 5 | + @dragleave.prevent |
| 6 | + @dragover.prevent |
| 7 | + @drop.prevent="onDrop"> |
| 8 | + <p>Drag and drop file</p> |
| 9 | + </div> |
| 10 | + |
| 11 | + <db-table-info :column-names="baseColumnNames"></db-table-info> |
| 12 | + |
| 13 | + <div v-show="this.baseColumnNames.length > 0"> |
| 14 | + <textarea v-model="sql" class="textarea is-info" placeholder="select * from hoge"></textarea> |
| 15 | + <button class="button is-fullwidth is-info" @click="runQuery">Run Query</button> |
| 16 | + </div> |
| 17 | + |
| 18 | + <div class="columns is-mobile is-centered"> |
| 19 | + <div class="column is-half"> |
| 20 | + <result-table :column-names="columnNames" :result="result"></result-table> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | +</template> |
| 25 | + |
| 26 | +<script lang="ts"> |
| 27 | +import Vue from 'vue'; |
| 28 | +import { Component, Prop } from "vue-property-decorator"; |
| 29 | +import { Database } from "../sql" |
| 30 | +import ResultTable from "~/components/ResultTable" |
| 31 | +import DbTableInfo from "~/components/DbTableInfo" |
| 32 | +
|
| 33 | +@Component({ |
| 34 | + components: { |
| 35 | + ResultTable, |
| 36 | + DbTableInfo |
| 37 | + } |
| 38 | +}) |
| 39 | +export default class TopPage extends Vue { |
| 40 | + result: string[] = []; |
| 41 | + columnNames: string[] = []; |
| 42 | +
|
| 43 | + baseColumnNames: string[] = []; |
| 44 | + db: any = new Database(); |
| 45 | + sql: string = ''; |
| 46 | +
|
| 47 | + createTable (values) { |
| 48 | + this.columnNames = values.map((_, i) => { |
| 49 | + return `c${i + 1}`; |
| 50 | + }); |
| 51 | + this.baseColumnNames = this.columnNames |
| 52 | + const columnsString = this.columnNames.map((name) => { |
| 53 | + return `${name} char`; |
| 54 | + }).join(','); |
| 55 | + this.db.run(`drop table if exists hoge; create table hoge (${columnsString});`); |
| 56 | + } |
| 57 | +
|
| 58 | + onDrop (event) { |
| 59 | + this.columnNames = []; |
| 60 | + this.result = []; |
| 61 | +
|
| 62 | + const files = event.dataTransfer.files; |
| 63 | + const reader = new FileReader(); |
| 64 | + reader.onload = (e) => { |
| 65 | + const rows = e.target.result.split('\n').map((line) => { |
| 66 | + return line.split(','); |
| 67 | + }); |
| 68 | + this.createTable(rows[0]); |
| 69 | + const columnLength = rows[0].length; |
| 70 | + rows.forEach((row) => { |
| 71 | + if (row.length !== columnLength) return; |
| 72 | +
|
| 73 | + this.result.push(row); |
| 74 | + const sql = `insert into hoge values(${ |
| 75 | + row.map(v => `"${v}"`).join(',') |
| 76 | + })`; |
| 77 | + this.db.run(sql); |
| 78 | + }) |
| 79 | + this.loading = false |
| 80 | + }; |
| 81 | + reader.readAsText(files[0]); |
| 82 | + } |
| 83 | +
|
| 84 | + runQuery () { |
| 85 | + try { |
| 86 | + const result = this.db.exec(this.sql)[0]; |
| 87 | + this.columnNames = result.columns; |
| 88 | + this.result = result.values; |
| 89 | + } catch (e) { |
| 90 | + console.log(e); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +</script> |
| 95 | + |
| 96 | +<style lang="scss" scoped> |
| 97 | +.drop-area { |
| 98 | + display: block; |
| 99 | + border: 2px dashed #bbb; |
| 100 | + border-radius: 5px; |
| 101 | + color: #bbb; |
| 102 | + padding: 25px; |
| 103 | + text-align: center; |
| 104 | + margin: 10px auto 5px; |
| 105 | + font-size: 18px; |
| 106 | + font-weight: bold; |
| 107 | + -khtml-user-drag: element; |
| 108 | + margin: 5% auto; |
| 109 | + width: 80%; |
| 110 | +} |
| 111 | +</style> |
0 commit comments