Skip to content

Commit

Permalink
Refactor EnumValuesDict type, add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
huyphung1602 committed Jan 8, 2025
1 parent 8d707a1 commit d6361cb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
16 changes: 6 additions & 10 deletions packages/dbml-connector/src/connectors/mssqlConnector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import sql, { columns } from 'mssql';
import sql from 'mssql';
import { buildSchemaQuery, parseConnectionString } from '../utils/parseSchema';
import {
DatabaseSchema,
Expand All @@ -14,6 +14,7 @@ import {
RefEndpoint,
Table,
TableConstraintsDictionary,
EnumValuesDict,
} from './types';

// https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types?view=sql-server-ver15
Expand Down Expand Up @@ -111,7 +112,7 @@ const getDbdefault = (data_type: string, column_default: string, default_type: D
};
};

const getEnumValues = (definition: string, constraint_name: string): { columns: string[], enumValues: EnumValue[], constraint_name: string }[] => {
const getEnumValues = (definition: string, constraint_name: string): EnumValuesDict[] => {
// Use the example below to understand the regex:
// ([quantity]>(0))
// ([unit_price]>(0))
Expand Down Expand Up @@ -147,7 +148,8 @@ const getEnumValues = (definition: string, constraint_name: string): { columns:
return sortedA.every((value, index) => value === sortedB[index]);
};

const result: { columns: string[], enumValues: EnumValue[], constraint_name: string }[] = [];
// Please check the comments of the EnumValuesDict type in types.ts to understand the structure
const result: EnumValuesDict[] = [];
const processedKeys = new Set<string>();

Object.keys(colMap).forEach((key) => {
Expand Down Expand Up @@ -206,13 +208,7 @@ const generateTablesFieldsAndEnums = async (client: sql.ConnectionPool, schemas:
fields: FieldsDictionary,
enums: Enum[],
}> => {
const checkConstraintDefinitionDedup: {
[key: string]: {
columns: string[],
enumValues: EnumValue[],
constraint_name: string,
}[]
} = {};
const checkConstraintDefinitionDedup: { [key: string]: EnumValuesDict[] } = {};

const fields: FieldsDictionary = {};
const enums: Enum[] = [];
Expand Down
56 changes: 56 additions & 0 deletions packages/dbml-connector/src/connectors/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { columns } from "mssql";

interface NoteInfo {
value: string;
}
Expand Down Expand Up @@ -106,6 +108,59 @@ interface BigQueryCredentials {
datasets: string[],
}

// Currently, we parse the check constraints as enum values in mssql.
// However, one check constraint can have multiple set of enum values.
// So, we need to store the enum values in a dictionary.
// Examples:
// CREATE TABLE AddressInfo (
// A1 UNIQUEIDENTIFIER NOT NULL,
// A2 UNIQUEIDENTIFIER NOT NULL,
// A3 UNIQUEIDENTIFIER NOT NULL,
// A4 UNIQUEIDENTIFIER NOT NULL,
// CONSTRAINT CK_Address_Valid CHECK (
// A1 IN ('1111', '2222', '333') AND
// A2 IN ('1111', '2222') AND
// A3 IN ('2222', '1111') AND
// A4 IN ('2222', '3333')
// )
// );
// GO
// In the above example, we have a check constraint CK_Address_Valid2 with 4 columns: A1, A2, A3, A4.
// Each column has a different set of enum values. So, we need to store the enum values in a dictionary.
// In the above example, the enum values dictionary will look like this:
// [
// {
// columns : ['A1'],
// enumValues: [
// { name: '1111' },
// { name: '2222' },
// { name: '333' }
// ],
// constraint_name: 'CK_Address_Valid_A1'
// },
// {
// columns : ['A2, A3'], => In this case, these two columns have the same set of enum values.
// enumValues: [
// { name: '1111' },
// { name: '2222' }
// ],
// constraint_name: 'CK_Address_Valid_A2_A3'
// },
// {
// columns : ['A4'],
// enumValues: [
// { name: '2222' },
// { name: '3333' }
// ],
// constraint_name: 'CK_Address_Valid_A4'
// }
// ]
interface EnumValuesDict {
columns: string[],
enumValues: EnumValue[],
constraint_name: string,
}

export {
NoteInfo,
TypeInfo,
Expand All @@ -125,4 +180,5 @@ export {
IndexesDictionary,
DatabaseSchema,
BigQueryCredentials,
EnumValuesDict,
};

0 comments on commit d6361cb

Please sign in to comment.