fix: insert files via add row option

This commit is contained in:
Fabio 2020-08-13 12:42:19 +02:00
parent 2f1dfdc654
commit 3c6e818ba0
5 changed files with 59 additions and 10 deletions

View File

@ -25,7 +25,6 @@ Why am I developing an SQL client when there are a lot of thom on the market?-->
This is a roadmap with major features will come in near future. This is a roadmap with major features will come in near future.
- Options to insert new database records.
- Improvements of query editor area. - Improvements of query editor area.
- Multiple query tabs. - Multiple query tabs.
- Tables management (add/edit/delete). - Tables management (add/edit/delete).

View File

@ -235,10 +235,10 @@ export class AntaresConnector {
for (const key in fields) { for (const key in fields) {
if (fields[key] === null) continue; if (fields[key] === null) continue;
fieldsList.push(key); fieldsList.push(key);
valueList.push(typeof fields[key] === 'number' ? fields[key] : `"${fields[key]}"`); valueList.push(fields[key]);
} }
insertRaw = ` (${fieldsList.join(',')}) VALUES (${valueList.join(',')}) `; insertRaw = `(${fieldsList.join(', ')}) VALUES (${valueList.join(', ')}) `;
} }
const groupByArray = this._query.groupBy.reduce(this._reducer, []); const groupByArray = this._query.groupBy.reduce(this._reducer, []);

View File

@ -51,12 +51,36 @@ export default class {
.run(); .run();
} }
static async insertTableRows (connection, params) { // Prepare every field like updateTableCell method static async insertTableRows (connection, params) {
const insertObj = {};
console.log(params);
for (const key in params.row) {
const type = params.fields[key];
let escapedParam;
if (NUMBER.includes(type))
escapedParam = params.row[key];
else if ([...TEXT, ...LONG_TEXT].includes(type))
escapedParam = `"${sqlEscaper(params.row[key])}"`;
else if (BLOB.includes(type)) {
if (params.row[key]) {
const fileBlob = fs.readFileSync(params.row[key]);
escapedParam = `0x${fileBlob.toString('hex')}`;
}
else
escapedParam = '""';
}
else
escapedParam = `"${sqlEscaper(params.row[key])}"`;
insertObj[key] = escapedParam;
}
for (let i = 0; i < params.repeat; i++) { for (let i = 0; i < params.repeat; i++) {
await connection await connection
.schema(params.schema) .schema(params.schema)
.into(params.table) .into(params.table)
.insert(params.row) .insert(insertObj)
.run(); .run();
} }
} }

View File

@ -32,6 +32,14 @@
:disabled="fieldsToExclude.includes(field.name)" :disabled="fieldsToExclude.includes(field.name)"
:tabindex="key+1" :tabindex="key+1"
> >
<input
v-else-if="inputProps(field).type === 'file'"
class="form-input"
type="file"
:disabled="fieldsToExclude.includes(field.name)"
:tabindex="key+1"
@change="filesChange($event,field.name)"
>
<input <input
v-else v-else
v-model="localRow[field.name]" v-model="localRow[field.name]"
@ -148,7 +156,7 @@ export default {
fieldDefault = field.default; fieldDefault = field.default;
if (DATETIME.includes(field.type)) { if (DATETIME.includes(field.type)) {
if (field.default && field.default.includes('current_timestamp')) { if (field.default && field.default.toLowerCase().includes('current_timestamp')) {
let datePrecision = ''; let datePrecision = '';
for (let i = 0; i < field.datePrecision; i++) for (let i = 0; i < field.datePrecision; i++)
datePrecision += i === 0 ? '.S' : 'S'; datePrecision += i === 0 ? '.S' : 'S';
@ -175,6 +183,13 @@ export default {
Object.keys(rowToInsert).forEach(key => { Object.keys(rowToInsert).forEach(key => {
if (this.fieldsToExclude.includes(key)) if (this.fieldsToExclude.includes(key))
delete rowToInsert[key]; delete rowToInsert[key];
if (typeof rowToInsert[key] === 'undefined')
delete rowToInsert[key];
});
const fieldTypes = {};
this.fields.forEach(field => {
fieldTypes[field.name] = field.type;
}); });
try { try {
@ -183,7 +198,8 @@ export default {
schema: this.workspace.breadcrumbs.schema, schema: this.workspace.breadcrumbs.schema,
table: this.workspace.breadcrumbs.table, table: this.workspace.breadcrumbs.table,
row: rowToInsert, row: rowToInsert,
repeat: this.nInserts repeat: this.nInserts,
fields: fieldTypes
}); });
if (status === 'success') { if (status === 'success') {
@ -249,6 +265,12 @@ export default {
this.fieldsToExclude = this.fieldsToExclude.filter(f => f !== field.name); this.fieldsToExclude = this.fieldsToExclude.filter(f => f !== field.name);
else else
this.fieldsToExclude = [...this.fieldsToExclude, field.name]; this.fieldsToExclude = [...this.fieldsToExclude, field.name];
},
filesChange (event, field) {
const { files } = event.target;
if (!files.length) return;
this.localRow[field] = files[0].path;
} }
} }
}; };

View File

@ -47,7 +47,9 @@
@hide="hideEditorModal" @hide="hideEditorModal"
> >
<template :slot="'header'"> <template :slot="'header'">
{{ $t('word.edit') }} "{{ editingField }}" <div class="d-flex">
<i class="mdi mdi-24px mdi-playlist-edit mr-1" /> {{ $t('word.edit') }} "{{ editingField }}"
</div>
</template> </template>
<div :slot="'body'"> <div :slot="'body'">
<div class="mb-2"> <div class="mb-2">
@ -71,7 +73,9 @@
@hide="hideEditorModal" @hide="hideEditorModal"
> >
<template :slot="'header'"> <template :slot="'header'">
{{ $t('word.edit') }} "{{ editingField }}" <div class="d-flex">
<i class="mdi mdi-24px mdi-playlist-edit mr-1" /> {{ $t('word.edit') }} "{{ editingField }}"
</div>
</template> </template>
<div :slot="'body'"> <div :slot="'body'">
<div class="mb-2"> <div class="mb-2">
@ -299,7 +303,7 @@ export default {
} }
// Inline editable fields // Inline editable fields
this.editingContent = this.$options.filters.typeFormat(this.originalContent, type); this.editingContent = this.$options.filters.typeFormat(this.originalContent, type, this.fieldPrecision(field));
this.$nextTick(() => { // Focus on input this.$nextTick(() => { // Focus on input
event.target.blur(); event.target.blur();