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 ''; return '';
case 'decimal': case 'decimal':
return ''; return '10';
case 'float': case 'float':
return ''; return '';
@ -278,9 +278,14 @@ export class MssqlUi {
static getDefaultLengthIsDisabled(type) { static getDefaultLengthIsDisabled(type) {
switch (type) { switch (type) {
case 'nvarchar': case 'nvarchar':
case 'numeric':
case 'decimal':
case 'float':
case 'bigint':
case 'int':
case 'tinyint':
return false; return false;
case 'bigint':
case 'binary': case 'binary':
case 'bit': case 'bit':
case 'char': case 'char':
@ -288,17 +293,13 @@ export class MssqlUi {
case 'datetime': case 'datetime':
case 'datetime2': case 'datetime2':
case 'datetimeoffset': case 'datetimeoffset':
case 'decimal':
case 'float':
case 'geography': case 'geography':
case 'geometry': case 'geometry':
case 'heirarchyid': case 'heirarchyid':
case 'image': case 'image':
case 'int':
case 'money': case 'money':
case 'nchar': case 'nchar':
case 'ntext': case 'ntext':
case 'numeric':
case 'real': case 'real':
case 'json': case 'json':
case 'smalldatetime': case 'smalldatetime':
@ -309,7 +310,6 @@ export class MssqlUi {
case 'text': case 'text':
case 'time': case 'time':
case 'timestamp': case 'timestamp':
case 'tinyint':
case 'uniqueidentifier': case 'uniqueidentifier':
case 'varbinary': case 'varbinary':
case 'xml': case 'xml':
@ -462,10 +462,10 @@ export class MssqlUi {
return ''; return '';
case 'decimal': case 'decimal':
return ''; return '2';
case 'float': case 'float':
return ''; return '2';
case 'geography': case 'geography':
return ''; return '';
@ -492,7 +492,7 @@ export class MssqlUi {
return ''; return '';
case 'numeric': case 'numeric':
return ''; return '2';
case 'nvarchar': case 'nvarchar':
return ''; return '';
@ -598,8 +598,12 @@ export class MssqlUi {
// } // }
} }
static showScale(_columnObj) { static showScale(columnObj) {
return false; return (
columnObj.dt === 'float' ||
columnObj.dt === 'decimal' ||
columnObj.dt === 'numeric'
);
} }
static removeUnsigned(columns) { 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 defaultValue = getDefaultValue(n);
const shouldSanitize = true; const shouldSanitize = true;
const scaleAndPrecision =
!getDefaultLengthIsDisabled(n.dt) && n.dtxp
? `(${n.dtxp}${n.dtxs ? `,${n.dtxs}` : ''})`
: '';
if (change === 0) { if (change === 0) {
query = existingQuery ? ',' : ''; query = existingQuery ? ',' : '';
query += this.genQuery(`?? ${n.dt}`, [n.cn], shouldSanitize); 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.rqd ? ' NOT NULL' : ' NULL';
query += n.ai ? ' IDENTITY(1,1)' : ' '; query += n.ai ? ' IDENTITY(1,1)' : ' ';
query += defaultValue query += defaultValue
@ -2599,7 +2603,7 @@ class MssqlClient extends KnexClient {
} }
} else if (change === 1) { } else if (change === 1) {
query += this.genQuery(` ADD ?? ${n.dt}`, [n.cn], shouldSanitize); 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.rqd ? ' NOT NULL' : ' NULL';
query += n.ai ? ' IDENTITY(1,1)' : ' '; query += n.ai ? ' IDENTITY(1,1)' : ' ';
query += defaultValue 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( query += this.genQuery(
`\nALTER TABLE ?? ALTER COLUMN ?? ${n.dt}${ `\nALTER TABLE ?? ALTER COLUMN ?? ${n.dt}${scaleAndPrecision}`,
!getDefaultLengthIsDisabled(n.dt) && n.dtxp ? `(${n.dtxp})` : ''
}`,
[this.getTnPath(t), n.cn], [this.getTnPath(t), n.cn],
shouldSanitize shouldSanitize
); );
@ -2758,35 +2760,32 @@ function getDefaultValue(n) {
function getDefaultLengthIsDisabled(type) { function getDefaultLengthIsDisabled(type) {
switch (type) { switch (type) {
case 'bigint':
case 'bit':
case 'datetimeoffset': case 'datetimeoffset':
case 'decimal':
case 'float':
case 'geography': case 'geography':
case 'geometry': case 'geometry':
case 'heirarchyid': case 'heirarchyid':
case 'image': case 'image':
case 'int':
case 'money': case 'money':
case 'numeric':
case 'real': case 'real':
case 'json': case 'json':
case 'smalldatetime': case 'smalldatetime':
case 'smallint':
case 'smallmoney': case 'smallmoney':
case 'text': case 'text':
case 'time': case 'time':
case 'timestamp': case 'timestamp':
case 'tinyint':
case 'uniqueidentifier': case 'uniqueidentifier':
case 'xml': case 'xml':
return true; return true;
break; break;
default: default:
case 'date': case 'int':
case 'datetime': case 'tinyint':
case 'datetime2': case 'bigint':
case 'bit':
case 'decimal':
case 'float':
case 'numeric':
case 'smallint':
case 'varchar': case 'varchar':
return false; return false;
break; break;

Loading…
Cancel
Save