From df5701eaf3a071ac4151a021b3c45ffc20bcbf82 Mon Sep 17 00:00:00 2001 From: IPdotSetAF Date: Tue, 26 Nov 2024 16:44:21 +0330 Subject: [PATCH] fixed sp aprameters nullable --- .../mssql-scaffolder.component.ts | 45 ++++++++++++------- .../mssql-scaffolder.model.ts | 10 +++++ .../mssql-scaffolder.service.ts | 10 ++++- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.component.ts b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.component.ts index f72fc21..b3ce812 100644 --- a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.component.ts +++ b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.component.ts @@ -7,8 +7,9 @@ import { AppComponent } from '../app/app.component'; import { ConnectRequest, ConnectResponse, ErrorResponse, GetAllDatabasesResponse, GetAllSchemasResponse, GetStoredProceduresResponse, GetTablesResponse } from '../../services/mssql/mssql.model'; import { MssqlScaffolderService } from './mssql-scaffolder.service'; import { Meta } from '@angular/platform-browser'; -import { GetColumnsResponse, GetSPParametersResponse, GetSPReturnColumnsResponse } from './mssql-scaffolder.model'; +import { GetColumnsResponse, GetSPReturnColumnsResponse, SPDefinition, SPParam } from './mssql-scaffolder.model'; import { RouterLink } from '@angular/router'; +import { forkJoin } from 'rxjs'; @Component({ selector: 'app-mssql-scaffolder', @@ -216,24 +217,38 @@ ${res.map((p) => `\tpublic ${MssqlScaffolderComponent.convertDataType(p.DataType private scaffoldSP() { const sc = this.scaffoldForm.getRawValue(); - this.scaffolder.getSPParameters(this.connectionID, sc.database, sc.schema, sc.sp).subscribe(ps => { - ps = ps as GetSPParametersResponse[]; - this.scaffolder.getSPReturnColumns(this.connectionID, sc.database, sc.schema, sc.sp).subscribe(rs => { - rs = rs as GetSPReturnColumnsResponse[]; - - const psc = ps.map((p) => `\tpublic ${MssqlScaffolderComponent.convertDataType(p.Type)}${p.Nullable ? '?' : ''} ${p.Parameter_name} { get; set; }`); - const rsc = rs.map((p) => `\tpublic ${MssqlScaffolderComponent.convertDataType(p.system_type_name)}${p.Nullable ? '?' : ''} ${p.column} { get; set; }`); - this.csCode = -`${psc.length > 0 ? `public class ${sc.sp}Params {`:'' } + forkJoin({ + params: this.scaffolder.getSPDefinition(this.connectionID, sc.database, sc.schema, sc.sp), + result: this.scaffolder.getSPReturnColumns(this.connectionID, sc.database, sc.schema, sc.sp) + }).subscribe(vals => { + const spDef = (vals.params as SPDefinition[])[0].definition; + const spUpper = spDef.toUpperCase(); + + const ps = [...(spDef.substring(spUpper.indexOf("CREATE PROCEDURE"), spUpper.indexOf("AS")) + .matchAll(/@(?\w+) +(?\w+(\(.*\)){0,1}) *=* *(?(NULL){0,1})/gmi))].map(match => { + if (match.groups) + return { + Parameter_name: match.groups['name'], + Type: match.groups['type'], + Nullable: match.groups['nul'] && match.groups['nul'].length > 0 ? true : false + } as SPParam; + return undefined; + }).filter(v => v) as SPParam[]; + const rs = vals.result as GetSPReturnColumnsResponse[]; + + const psc = ps.map((p) => `\tpublic ${MssqlScaffolderComponent.convertDataType(p.Type)}${p.Nullable ? '?' : ''} ${p.Parameter_name} { get; set; }`); + const rsc = rs.map((p) => `\tpublic ${MssqlScaffolderComponent.convertDataType(p.system_type_name)}${p.Nullable ? '?' : ''} ${p.column} { get; set; }`); + + this.csCode = + `${psc.length > 0 ? `public class ${sc.sp}Params {` : ''} ${psc.join("\n")} -${psc.length > 0 ? '}':'' } +${psc.length > 0 ? '}' : ''} -${rsc.length>0? `public class ${sc.sp}Result {`:'' } +${rsc.length > 0 ? `public class ${sc.sp}Result {` : ''} ${rsc.join("\n")} -${rsc.length > 0 ? '}':'' }`; - this.codeFlip = !this.codeFlip; - }); +${rsc.length > 0 ? '}' : ''}`; + this.codeFlip = !this.codeFlip; }); } diff --git a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.model.ts b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.model.ts index e204a10..08a49dd 100644 --- a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.model.ts +++ b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.model.ts @@ -23,4 +23,14 @@ export interface GetColumnsResponse { column: string; system_type_name: string; Nullable: boolean; + } + + export interface SPDefinition{ + definition: string; + } + + export interface SPParam{ + Parameter_name: string; + Type: string; + Nullable: boolean; } \ No newline at end of file diff --git a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.service.ts b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.service.ts index 99de8e7..71f835f 100644 --- a/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.service.ts +++ b/Frontend/src/app/mssql-scaffolder/mssql-scaffolder.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { MssqlService } from '../../services/mssql/mssql.service'; import { map, Observable } from 'rxjs'; -import { GetColumnsResponse, GetSPParametersResponse, GetSPReturnColumnsResponse } from './mssql-scaffolder.model'; +import { GetColumnsResponse, GetSPParametersResponse, GetSPReturnColumnsResponse, SPDefinition } from './mssql-scaffolder.model'; import { ErrorResponse } from '../../services/mssql/mssql.model'; @Injectable({ @@ -60,7 +60,13 @@ export class MssqlScaffolderService { ); } - // 7. Get all return columns and their types from an SP + // 7. Get SP definition lines + getSPDefinition(connection_id: string, dbName: string, schemaName: string, spName: string): Observable{ + const query = `SELECT object_definition(object_id('${schemaName}.${spName}')) AS definition;`; + return this.mssql.executeQuery(connection_id, query, dbName); + } + + // 8. Get all return columns and their types from an SP getSPReturnColumns(connection_id: string, dbName: string, schemaName: string, spName: string): Observable { const query = ` SELECT name AS 'column', system_type_name, is_nullable as 'Nullable'