diff --git a/src/main/libs/ClientsFactory.js b/src/main/libs/ClientsFactory.js index 9f02d96d..54211908 100644 --- a/src/main/libs/ClientsFactory.js +++ b/src/main/libs/ClientsFactory.js @@ -2,6 +2,12 @@ import { MySQLClient } from './clients/MySQLClient'; import { PostgreSQLClient } from './clients/PostgreSQLClient'; +const queryLogger = sql => { + // Remove comments, newlines and multiple spaces + const escapedSql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' '); + console.log(escapedSql); +}; + export class ClientsFactory { /** * Returns a database connection based on received args. @@ -23,6 +29,8 @@ export class ClientsFactory { * @memberof ClientsFactory */ static getConnection (args) { + args.logger = queryLogger; + switch (args.client) { case 'mysql': case 'maria': diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 9fcef893..2b25db81 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -1181,7 +1181,57 @@ export class MySQLClient extends AntaresCore { * @memberof MySQLClient */ async createTable (params) { - const sql = `CREATE TABLE \`${params.schema}\`.\`${params.name}\` (\`${params.name}_ID\` INT NULL) COMMENT='${params.comment}', COLLATE='${params.collation}', ENGINE=${params.engine}`; + const { + schema, + fields, + foreigns, + indexes, + options + } = params; + const newColumns = []; + const newIndexes = []; + const newForeigns = []; + + let sql = `CREATE TABLE \`${schema}\`.\`${options.name}\``; + + // ADD FIELDS + fields.forEach(field => { + const typeInfo = this._getTypeInfo(field.type); + const length = typeInfo.length ? field.enumValues || field.numLength || field.charLength || field.datePrecision : false; + + newColumns.push(`\`${field.name}\` + ${field.type.toUpperCase()}${length ? `(${length})` : ''} + ${field.unsigned ? 'UNSIGNED' : ''} + ${field.zerofill ? 'ZEROFILL' : ''} + ${field.nullable ? 'NULL' : 'NOT NULL'} + ${field.autoIncrement ? 'AUTO_INCREMENT' : ''} + ${field.default ? `DEFAULT ${field.default}` : ''} + ${field.comment ? `COMMENT '${field.comment}'` : ''} + ${field.collation ? `COLLATE ${field.collation}` : ''} + ${field.onUpdate ? `ON UPDATE ${field.onUpdate}` : ''}`); + }); + + // ADD INDEX + indexes.forEach(index => { + const fields = index.fields.map(field => `\`${field}\``).join(','); + let type = index.type; + + if (type === 'PRIMARY') + newIndexes.push(`PRIMARY KEY (${fields})`); + else { + if (type === 'UNIQUE') + type = 'UNIQUE INDEX'; + + newIndexes.push(`${type} \`${index.name}\` (${fields})`); + } + }); + + // ADD FOREIGN KEYS + foreigns.forEach(foreign => { + newForeigns.push(`CONSTRAINT \`${foreign.constraintName}\` FOREIGN KEY (\`${foreign.field}\`) REFERENCES \`${foreign.refTable}\` (\`${foreign.refField}\`) ON UPDATE ${foreign.onUpdate} ON DELETE ${foreign.onDelete}`); + }); + + sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')}) COMMENT='${options.comment}', COLLATE='${options.collation}', ENGINE=${options.engine}`; return await this.raw(sql); } diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index d7a9263b..a6bef636 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -94,7 +94,7 @@ export class PostgreSQLClient extends AntaresCore { } /** - * Executes an USE query + * Executes an "USE" query * * @param {String} schema * @memberof PostgreSQLClient @@ -1036,8 +1036,54 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async createTable (params) { - const sql = `CREATE TABLE "${params.schema}"."${params.name}" (${params.name}_id INTEGER NULL); ALTER TABLE "${params.schema}"."${params.name}" DROP COLUMN ${params.name}_id`; + const { + schema, + fields, + foreigns, + indexes, + options + } = params; + const newColumns = []; + const newIndexes = []; + const manageIndexes = []; + const newForeigns = []; + let sql = `CREATE TABLE "${schema}"."${options.name}"`; + + // ADD FIELDS + fields.forEach(field => { + const typeInfo = this._getTypeInfo(field.type); + const length = typeInfo.length ? field.enumValues || field.numLength || field.charLength || field.datePrecision : false; + + newColumns.push(`${field.name} + ${field.type.toUpperCase()}${length ? `(${length})` : ''} + ${field.unsigned ? 'UNSIGNED' : ''} + ${field.zerofill ? 'ZEROFILL' : ''} + ${field.nullable ? 'NULL' : 'NOT NULL'} + ${field.default ? `DEFAULT ${field.default}` : ''} + ${field.onUpdate ? `ON UPDATE ${field.onUpdate}` : ''}`); + }); + + // ADD INDEX + indexes.forEach(index => { + const fields = index.fields.map(field => `${field}`).join(','); + const type = index.type; + + if (type === 'PRIMARY') + newIndexes.push(`PRIMARY KEY (${fields})`); + else if (type === 'UNIQUE') + newIndexes.push(`CONSTRAINT ${index.name} UNIQUE (${fields})`); + else + manageIndexes.push(`CREATE INDEX ${index.name} ON "${schema}"."${options.name}" (${fields})`); + }); + + // ADD FOREIGN KEYS + foreigns.forEach(foreign => { + newForeigns.push(`CONSTRAINT ${foreign.constraintName} FOREIGN KEY (${foreign.field}) REFERENCES "${schema}"."${foreign.refTable}" (${foreign.refField}) ON UPDATE ${foreign.onUpdate} ON DELETE ${foreign.onDelete}`); + }); + + sql = `${sql} (${[...newColumns, ...newIndexes, ...newForeigns].join(', ')})`; + if (manageIndexes.length) sql = `${sql}; ${manageIndexes.join(';')}`; return await this.raw(sql); } @@ -1068,12 +1114,6 @@ export class PostgreSQLClient extends AntaresCore { const createSequences = []; const manageIndexes = []; - // OPTIONS - if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`); - if ('engine' in options) alterColumns.push(`ENGINE=${options.engine}`); - if ('autoIncrement' in options) alterColumns.push(`AUTO_INCREMENT=${+options.autoIncrement}`); - if ('collation' in options) alterColumns.push(`COLLATE='${options.collation}'`); - // ADD FIELDS additions.forEach(addition => { const typeInfo = this._getTypeInfo(addition.type); @@ -1084,10 +1124,7 @@ export class PostgreSQLClient extends AntaresCore { ${addition.unsigned ? 'UNSIGNED' : ''} ${addition.zerofill ? 'ZEROFILL' : ''} ${addition.nullable ? 'NULL' : 'NOT NULL'} - ${addition.autoIncrement ? 'AUTO_INCREMENT' : ''} ${addition.default ? `DEFAULT ${addition.default}` : ''} - ${addition.comment ? `COMMENT '${addition.comment}'` : ''} - ${addition.collation ? `COLLATE ${addition.collation}` : ''} ${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`); }); @@ -1106,7 +1143,7 @@ export class PostgreSQLClient extends AntaresCore { // ADD FOREIGN KEYS foreignChanges.additions.forEach(addition => { - alterColumns.push(`ADD CONSTRAINT ${addition.constraintName} FOREIGN KEY (${addition.field}) REFERENCES ${addition.refTable} (${addition.refField}) ON UPDATE ${addition.onUpdate} ON DELETE ${addition.onDelete}`); + alterColumns.push(`ADD CONSTRAINT ${addition.constraintName} FOREIGN KEY (${addition.field}) REFERENCES "${schema}"."${addition.refTable}" (${addition.refField}) ON UPDATE ${addition.onUpdate} ON DELETE ${addition.onDelete}`); }); // CHANGE FIELDS @@ -1163,7 +1200,7 @@ export class PostgreSQLClient extends AntaresCore { // CHANGE FOREIGN KEYS foreignChanges.changes.forEach(change => { alterColumns.push(`DROP CONSTRAINT ${change.oldName}`); - alterColumns.push(`ADD CONSTRAINT ${change.constraintName} FOREIGN KEY (${change.field}) REFERENCES ${change.refTable} (${change.refField}) ON UPDATE ${change.onUpdate} ON DELETE ${change.onDelete}`); + alterColumns.push(`ADD CONSTRAINT ${change.constraintName} FOREIGN KEY (${change.field}) REFERENCES "${schema}"."${change.refTable}" (${change.refField}) ON UPDATE ${change.onUpdate} ON DELETE ${change.onDelete}`); }); // DROP FIELDS diff --git a/src/renderer/components/ModalAskParameters.vue b/src/renderer/components/ModalAskParameters.vue index e893ddde..1f2ff79a 100644 --- a/src/renderer/components/ModalAskParameters.vue +++ b/src/renderer/components/ModalAskParameters.vue @@ -31,7 +31,11 @@ class="form-input" type="text" > - + {{ parameter.type }} {{ parameter.length | wrapNumber }} @@ -127,4 +131,8 @@ export default { .field-type { font-size: 0.6rem; } + + .input-group-addon { + max-width: 100px; + } diff --git a/src/renderer/components/Workspace.vue b/src/renderer/components/Workspace.vue index 06775051..0add3112 100644 --- a/src/renderer/components/Workspace.vue +++ b/src/renderer/components/Workspace.vue @@ -68,6 +68,23 @@ + + + + {{ $t('message.newTable') }} + + + + + - {{ $t('word.table') }} @@ -132,8 +132,8 @@ export default { addNotification: 'notifications/addNotification', changeBreadcrumbs: 'workspaces/changeBreadcrumbs' }), - showCreateTableModal () { - this.$emit('show-create-table-modal'); + openCreateTableTab () { + this.$emit('open-create-table-tab'); }, showCreateViewModal () { this.$emit('show-create-view-modal'); diff --git a/src/renderer/components/WorkspaceExploreBarTableContext.vue b/src/renderer/components/WorkspaceExploreBarTableContext.vue index dff44ef7..10941954 100644 --- a/src/renderer/components/WorkspaceExploreBarTableContext.vue +++ b/src/renderer/components/WorkspaceExploreBarTableContext.vue @@ -116,9 +116,6 @@ export default { removeLoadingElement: 'workspaces/removeLoadingElement', changeBreadcrumbs: 'workspaces/changeBreadcrumbs' }), - showCreateTableModal () { - this.$emit('show-create-table-modal'); - }, showDeleteModal () { this.isDeleteModal = true; }, diff --git a/src/renderer/components/WorkspaceTabNewTable.vue b/src/renderer/components/WorkspaceTabNewTable.vue new file mode 100644 index 00000000..3ca9d9a8 --- /dev/null +++ b/src/renderer/components/WorkspaceTabNewTable.vue @@ -0,0 +1,458 @@ + + + diff --git a/src/renderer/components/WorkspaceTabPropsFunction.vue b/src/renderer/components/WorkspaceTabPropsFunction.vue index 0524f417..edb6046d 100644 --- a/src/renderer/components/WorkspaceTabPropsFunction.vue +++ b/src/renderer/components/WorkspaceTabPropsFunction.vue @@ -37,10 +37,6 @@ {{ $t('word.parameters') }} -
@@ -207,13 +203,6 @@ :height="editorHeight" />
- {{ $t('word.parameters') }} -
@@ -164,13 +160,6 @@ :height="editorHeight" />
- {{ $t('word.foreignKeys') }} -
@@ -160,14 +151,6 @@ @rename-field="renameField" />
- {{ $t('word.clear') }} - -
- -
@@ -98,13 +91,6 @@ :height="editorHeight" /> - workspace.uid === uid); switch (type) { + case 'new-table': + case 'new-trigger': + case 'new-trigger-function': + case 'new-function': + case 'new-routine': + case 'new-scheduler': + tabUid = uidGen('T'); + commit('NEW_TAB', { + uid, + tab: tabUid, + content, + type, + autorun, + schema, + elementName, + elementType + }); + break; case 'temp-data': case 'temp-trigger-props': case 'temp-trigger-function-props':