Browse Source

fix(api): mssql - enable toggling scale and precision

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4883/head
Pranav C 2 years ago
parent
commit
9e968465bc
  1. 28
      packages/nocodb-sdk/src/lib/sqlUi/MssqlUi.ts
  2. 33
      packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts

28
packages/nocodb-sdk/src/lib/sqlUi/MssqlUi.ts

@ -190,7 +190,7 @@ export class MssqlUi {
return '';
case 'decimal':
return '';
return '10';
case 'float':
return '';
@ -278,9 +278,14 @@ export class MssqlUi {
static getDefaultLengthIsDisabled(type) {
switch (type) {
case 'nvarchar':
case 'numeric':
case 'decimal':
case 'float':
case 'bigint':
case 'int':
case 'tinyint':
return false;
case 'bigint':
case 'binary':
case 'bit':
case 'char':
@ -288,17 +293,13 @@ export class MssqlUi {
case 'datetime':
case 'datetime2':
case 'datetimeoffset':
case 'decimal':
case 'float':
case 'geography':
case 'geometry':
case 'heirarchyid':
case 'image':
case 'int':
case 'money':
case 'nchar':
case 'ntext':
case 'numeric':
case 'real':
case 'json':
case 'smalldatetime':
@ -309,7 +310,6 @@ export class MssqlUi {
case 'text':
case 'time':
case 'timestamp':
case 'tinyint':
case 'uniqueidentifier':
case 'varbinary':
case 'xml':
@ -462,10 +462,10 @@ export class MssqlUi {
return '';
case 'decimal':
return '';
return '2';
case 'float':
return '';
return '2';
case 'geography':
return '';
@ -492,7 +492,7 @@ export class MssqlUi {
return '';
case 'numeric':
return '';
return '2';
case 'nvarchar':
return '';
@ -598,8 +598,12 @@ export class MssqlUi {
// }
}
static showScale(_columnObj) {
return false;
static showScale(columnObj) {
return (
columnObj.dt === 'float' ||
columnObj.dt === 'decimal' ||
columnObj.dt === 'numeric'
);
}
static removeUnsigned(columns) {

33
packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts

@ -2580,11 +2580,15 @@ class MssqlClient extends KnexClient {
const defaultValue = getDefaultValue(n);
const shouldSanitize = true;
const scaleAndPrecision =
!getDefaultLengthIsDisabled(n.dt) && n.dtxp
? `(${n.dtxp}${n.dtxs ? `,${n.dtxs}` : ''})`
: '';
if (change === 0) {
query = existingQuery ? ',' : '';
query += this.genQuery(`?? ${n.dt}`, [n.cn], shouldSanitize);
query += !getDefaultLengthIsDisabled(n.dt) && n.dtxp ? `(${n.dtxp})` : '';
query += scaleAndPrecision;
query += n.rqd ? ' NOT NULL' : ' NULL';
query += n.ai ? ' IDENTITY(1,1)' : ' ';
query += defaultValue
@ -2599,7 +2603,7 @@ class MssqlClient extends KnexClient {
}
} else if (change === 1) {
query += this.genQuery(` ADD ?? ${n.dt}`, [n.cn], shouldSanitize);
query += !getDefaultLengthIsDisabled(n.dt) && n.dtxp ? `(${n.dtxp})` : '';
query += scaleAndPrecision;
query += n.rqd ? ' NOT NULL' : ' NULL';
query += n.ai ? ' IDENTITY(1,1)' : ' ';
query += defaultValue
@ -2629,11 +2633,9 @@ class MssqlClient extends KnexClient {
);
}
if (n.dtxp !== o.dtxp || n.dt !== o.dt || n.rqd !== o.rqd) {
if (n.dtxp !== o.dtxp ||n.dtxs !== o.dtxs || n.dt !== o.dt || n.rqd !== o.rqd) {
query += this.genQuery(
`\nALTER TABLE ?? ALTER COLUMN ?? ${n.dt}${
!getDefaultLengthIsDisabled(n.dt) && n.dtxp ? `(${n.dtxp})` : ''
}`,
`\nALTER TABLE ?? ALTER COLUMN ?? ${n.dt}${scaleAndPrecision}`,
[this.getTnPath(t), n.cn],
shouldSanitize
);
@ -2758,35 +2760,32 @@ function getDefaultValue(n) {
function getDefaultLengthIsDisabled(type) {
switch (type) {
case 'bigint':
case 'bit':
case 'datetimeoffset':
case 'decimal':
case 'float':
case 'geography':
case 'geometry':
case 'heirarchyid':
case 'image':
case 'int':
case 'money':
case 'numeric':
case 'real':
case 'json':
case 'smalldatetime':
case 'smallint':
case 'smallmoney':
case 'text':
case 'time':
case 'timestamp':
case 'tinyint':
case 'uniqueidentifier':
case 'xml':
return true;
break;
default:
case 'date':
case 'datetime':
case 'datetime2':
case 'int':
case 'tinyint':
case 'bigint':
case 'bit':
case 'decimal':
case 'float':
case 'numeric':
case 'smallint':
case 'varchar':
return false;
break;

Loading…
Cancel
Save