Skip to content

Commit

Permalink
feat(PostgreSQL): table fields edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Mar 25, 2021
1 parent e7401cc commit e3f259c
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ I'm actively working on it, hoping to provide cool features and fixes as soon as

Why am I developing an SQL client when there are a lot of them on the market?
The main goal is to develop a totally free, full featured, cross platform and open source alternative, empowered by JavaScript's ecosystem.
An application created with minimalism and semplicity in mind, with features in the right places, not hundreds of tiny buttons or submenu.
A modern application created with minimalism and semplicity in mind, with features in the right places, not hundreds of tiny buttons, tabs or submenu.

## How to contribute

Expand Down
9 changes: 8 additions & 1 deletion src/common/customizations/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ module.exports = {
indexes: false,
foreigns: false,
sortableFields: false,
zerofill: false
unsigned: false,
nullable: false,
zerofill: false,
autoIncrement: false,
comment: false,
collation: false,
arrays: false,
onUpdate: false
};
8 changes: 7 additions & 1 deletion src/common/customizations/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ module.exports = {
indexes: true,
foreigns: true,
sortableFields: true,
zerofill: true
unsigned: true,
nullable: true,
zerofill: true,
autoIncrement: true,
comment: true,
collation: true,
onUpdate: true
};
6 changes: 4 additions & 2 deletions src/common/customizations/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module.exports = {
schedulers: false,
// Settings
databaseEdit: false,
tableSettings: false,
tableSettings: true,
viewSettings: false,
triggerSettings: false,
routineSettings: false,
functionSettings: false,
schedulerSettings: false,
indexes: true,
foreigns: true,
sortableFields: false
sortableFields: false,
nullable: true,
arrays: true
};
2 changes: 1 addition & 1 deletion src/common/data-types/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = [
types: [
{
name: 'TINYINT',
length: true,
length: 4,
collation: false,
unsigned: true,
zerofill: true
Expand Down
41 changes: 18 additions & 23 deletions src/common/data-types/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module.exports = [
types: [
{
name: 'SMALLINT',
length: true,
length: false,
unsigned: true
},
{
name: 'INTEGER',
length: true,
length: false,
unsigned: true
},
{
name: 'BIGINT',
length: true,
length: false,
unsigned: true
},
{
name: 'DECIMAL',
length: true,
length: false,
unsigned: true
},
{
Expand All @@ -29,17 +29,17 @@ module.exports = [
},
{
name: 'SMALLSERIAL',
length: true,
length: false,
unsigned: true
},
{
name: 'SERIAL',
length: true,
length: false,
unsigned: true
},
{
name: 'BIGSERIAL',
length: true,
length: false,
unsigned: true
}
]
Expand All @@ -49,12 +49,12 @@ module.exports = [
types: [
{
name: 'REAL',
length: true,
length: false,
unsigned: true
},
{
name: 'DOUBLE PRECISION',
length: true,
length: false,
unsigned: true
}
]
Expand All @@ -64,7 +64,7 @@ module.exports = [
types: [
{
name: 'money',
length: true,
length: false,
unsigned: true
}
]
Expand All @@ -77,14 +77,9 @@ module.exports = [
length: true,
unsigned: false
},
{
name: 'CHAR',
length: false,
unsigned: false
},
{
name: 'CHARACTER',
length: false,
length: true,
unsigned: false
},
{
Expand All @@ -109,7 +104,7 @@ module.exports = [
types: [
{
name: 'BYTEA',
length: true,
length: false,
unsigned: false
}
]
Expand All @@ -129,17 +124,17 @@ module.exports = [
},
{
name: 'DATE',
length: true,
length: false,
unsigned: false
},
{
name: 'TIME',
length: true,
name: 'TIME WITHOUT TIME ZONE',
length: false,
unsigned: false
},
{
name: 'TIME WITH TIME ZONE',
length: true,
length: false,
unsigned: false
},
{
Expand Down Expand Up @@ -229,12 +224,12 @@ module.exports = [
types: [
{
name: 'BIT',
length: false,
length: true,
unsigned: false
},
{
name: 'BIT VARYING',
length: false,
length: true,
unsigned: false
}
]
Expand Down
75 changes: 49 additions & 26 deletions src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import pg, { Pool, Client, types } from 'pg';
import { Pool, Client, types } from 'pg';
import { parse } from 'pgsql-ast-parser';
import { AntaresCore } from '../AntaresCore';
import dataTypes from 'common/data-types/postgresql';
Expand All @@ -8,11 +8,11 @@ function pgToString (value) {
return value.toString();
}

pg.types.setTypeParser(1082, pgToString); // date
pg.types.setTypeParser(1083, pgToString); // time
pg.types.setTypeParser(1114, pgToString); // timestamp
pg.types.setTypeParser(1184, pgToString); // timestamptz
pg.types.setTypeParser(1266, pgToString); // timetz
types.setTypeParser(1082, pgToString); // date
types.setTypeParser(1083, pgToString); // time
types.setTypeParser(1114, pgToString); // timestamp
types.setTypeParser(1184, pgToString); // timestamptz
types.setTypeParser(1266, pgToString); // timetz

export class PostgreSQLClient extends AntaresCore {
constructor (args) {
Expand Down Expand Up @@ -282,7 +282,7 @@ export class PostgreSQLClient extends AntaresCore {
default: field.column_default,
charset: field.character_set_name,
collation: field.collation_name,
autoIncrement: null,
autoIncrement: false,
onUpdate: null,
comment: ''
};
Expand Down Expand Up @@ -317,7 +317,10 @@ export class PostgreSQLClient extends AntaresCore {
name: row.constraint_name,
column: row.column_name,
indexType: null,
type: row.constraint_type
type: row.constraint_type,
cardinality: null,
comment: '',
indexComment: ''
};
});
}
Expand Down Expand Up @@ -359,6 +362,8 @@ export class PostgreSQLClient extends AntaresCore {
tc.constraint_name,
tc.table_name,
kcu.column_name,
kcu.position_in_unique_constraint,
kcu.ordinal_position,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
Expand Down Expand Up @@ -1020,8 +1025,10 @@ export class PostgreSQLClient extends AntaresCore {
options
} = params;

let sql = `ALTER TABLE \`${table}\` `;
let sql = '';
const alterColumns = [];
const renameColumns = [];
const createSequences = [];

// OPTIONS
if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`);
Expand All @@ -1034,7 +1041,7 @@ export class PostgreSQLClient extends AntaresCore {
const typeInfo = this._getTypeInfo(addition.type);
const length = typeInfo.length ? addition.numLength || addition.charLength || addition.datePrecision : false;

alterColumns.push(`ADD COLUMN \`${addition.name}\`
alterColumns.push(`ADD COLUMN ${addition.name}
${addition.type.toUpperCase()}${length ? `(${length})` : ''}
${addition.unsigned ? 'UNSIGNED' : ''}
${addition.zerofill ? 'ZEROFILL' : ''}
Expand All @@ -1043,8 +1050,7 @@ export class PostgreSQLClient extends AntaresCore {
${addition.default ? `DEFAULT ${addition.default}` : ''}
${addition.comment ? `COMMENT '${addition.comment}'` : ''}
${addition.collation ? `COLLATE ${addition.collation}` : ''}
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}
${addition.after ? `AFTER \`${addition.after}\`` : 'FIRST'}`);
${addition.onUpdate ? `ON UPDATE ${addition.onUpdate}` : ''}`);
});

// ADD INDEX
Expand All @@ -1071,18 +1077,33 @@ export class PostgreSQLClient extends AntaresCore {
changes.forEach(change => {
const typeInfo = this._getTypeInfo(change.type);
const length = typeInfo.length ? change.numLength || change.charLength || change.datePrecision : false;
let localType;

alterColumns.push(`CHANGE COLUMN \`${change.orgName}\` \`${change.name}\`
${change.type.toUpperCase()}${length ? `(${length})` : ''}
${change.unsigned ? 'UNSIGNED' : ''}
${change.zerofill ? 'ZEROFILL' : ''}
${change.nullable ? 'NULL' : 'NOT NULL'}
${change.autoIncrement ? 'AUTO_INCREMENT' : ''}
${change.default ? `DEFAULT ${change.default}` : ''}
${change.comment ? `COMMENT '${change.comment}'` : ''}
${change.collation ? `COLLATE ${change.collation}` : ''}
${change.onUpdate ? `ON UPDATE ${change.onUpdate}` : ''}
${change.after ? `AFTER \`${change.after}\`` : 'FIRST'}`);
switch (change.type) {
case 'SERIAL':
localType = 'integer';
break;
case 'SMALLSERIAL':
localType = 'smallint';
break;
case 'BIGSERIAL':
localType = 'bigint';
break;
default:
localType = change.type.toLowerCase();
}

alterColumns.push(`ALTER COLUMN "${change.orgName}" TYPE ${localType}${length ? `(${length})` : ''} USING "${change.orgName}"::${localType}`);
alterColumns.push(`ALTER COLUMN "${change.orgName}" ${change.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`);
alterColumns.push(`ALTER COLUMN "${change.orgName}" ${change.default ? `SET DEFAULT ${change.default}` : 'DROP DEFAULT'}`);
if (['SERIAL', 'SMALLSERIAL', 'BIGSERIAL'].includes(change.type)) {
const sequenceName = `${table}_${change.name}_seq`.replace(' ', '_');
createSequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequenceName} OWNED BY "${table}"."${change.orgName}"`);
alterColumns.push(`ALTER COLUMN "${change.orgName}" SET DEFAULT nextval('${sequenceName}')`);
}

if (change.orgName !== change.name)
renameColumns.push(`ALTER TABLE "${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`);
});

// CHANGE INDEX
Expand Down Expand Up @@ -1113,7 +1134,7 @@ export class PostgreSQLClient extends AntaresCore {

// DROP FIELDS
deletions.forEach(deletion => {
alterColumns.push(`DROP COLUMN \`${deletion.name}\``);
alterColumns.push(`DROP COLUMN ${deletion.name}`);
});

// DROP INDEX
Expand All @@ -1129,10 +1150,12 @@ export class PostgreSQLClient extends AntaresCore {
alterColumns.push(`DROP FOREIGN KEY \`${deletion.constraintName}\``);
});

sql += alterColumns.join(', ');
if (alterColumns.length) sql += `ALTER TABLE "${table}" ${alterColumns.join(', ')}; `;

// RENAME
if (options.name) sql += `; RENAME TABLE \`${table}\` TO \`${options.name}\``;
if (renameColumns.length) sql += `${renameColumns.join(';')}; `;
if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`;
if (options.name) sql += `ALTER TABLE "${table}" RENAME TO "${options.name}"; `;

return await this.raw(sql);
}
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/components/WorkspacePropsOptionsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
>
</div>
</div>
<div class="form-group">
<div v-if="workspace.customizations.comment" class="form-group">
<label class="form-label col-4">
{{ $t('word.comment') }}
</label>
Expand All @@ -38,7 +38,7 @@
>
</div>
</div>
<div class="form-group">
<div v-if="workspace.customizations.autoIncrement" class="form-group">
<label class="form-label col-4">
{{ $t('word.autoIncrement') }}
</label>
Expand All @@ -50,7 +50,7 @@
>
</div>
</div>
<div class="form-group">
<div v-if="workspace.customizations.collations" class="form-group">
<label class="form-label col-4">
{{ $t('word.collation') }}
</label>
Expand All @@ -66,7 +66,7 @@
</select>
</div>
</div>
<div class="form-group">
<div v-if="workspace.customizations.engines" class="form-group">
<label class="form-label col-4">
{{ $t('word.engine') }}
</label>
Expand Down
Loading

0 comments on commit e3f259c

Please sign in to comment.