# SQL Object Naming Convention Design ## Goal Rename stored procedures and functions to follow SQL Server best practice prefix conventions for consistency and clarity: - `usp_` for user stored procedures - `fn_` for functions (scalar and table-valued) ## Current State ### Stored Procedures | Current Name | Has `usp_` Prefix | |--------------|-------------------| | `SubmitSearch` | No | | `StartSearch` | No | | `CompleteSearch` | No | | `ResetPartialSearches` | No | | `usp_ValidateSearchCriteria` | Yes (already correct) | ### Functions | Current Name | Type | Has `fn_` Prefix | |--------------|------|------------------| | `fn_GetSearchMinimumDt` | Scalar | Yes (already correct) | | `fn_GetSearchMaximumDt` | Scalar | Yes (already correct) | | `fn_GetSearchExtractMisData` | Scalar | Yes (already correct) | | `fn_GetSearchWorkOrders` | Inline TVF | Yes (already correct) | | `fn_GetSearchItemNumbers` | Inline TVF | Yes (already correct) | | `fn_GetSearchProfitCenters` | Inline TVF | Yes (already correct) | | `fn_GetSearchWorkCenters` | Inline TVF | Yes (already correct) | | `fn_GetSearchOperatorIDs` | Inline TVF | Yes (already correct) | | `fn_GetSearchComponentLots` | Inline TVF | Yes (already correct) | | `fn_GetSearchPartOperations` | Inline TVF | Yes (already correct) | | `MatchMIS` | Multi-statement TVF | No | ## Target State ### Stored Procedures | Current | New | |---------|-----| | `SubmitSearch` | `usp_SubmitSearch` | | `StartSearch` | `usp_StartSearch` | | `CompleteSearch` | `usp_CompleteSearch` | | `ResetPartialSearches` | `usp_ResetPartialSearches` | ### Functions | Current | New | |---------|-----| | `MatchMIS` | `fn_MatchMIS` | ## Implementation Approach ### 1. SQL Scripts (Modify Existing) Update scripts 040-044 directly with new names: **Stored Procedures:** - `040_CreateSubmitSearchProcedure.sql` → `usp_SubmitSearch` - `041_CreateStartSearchProcedure.sql` → `usp_StartSearch` - `042_CreateCompleteSearchProcedure.sql` → `usp_CompleteSearch` - `043_CreateResetPartialSearchesProcedure.sql` → `usp_ResetPartialSearches` **Functions:** - `044_CreateMatchMisFunction.sql` → `fn_MatchMIS` ### 2. Docker Database (Manual Update) Since DbUp won't re-run already-executed scripts: 1. Drop old procedures and functions from Docker database 2. Execute updated CREATE statements directly ### 3. C# Constants Class (New File) Create `SqlObjects.cs` in `JdeScoping.DataAccess`: ```csharp namespace JdeScoping.DataAccess; /// /// Constants for SQL stored procedure and function names. /// public static class SqlObjects { // Stored Procedures public const string SubmitSearch = "usp_SubmitSearch"; public const string StartSearch = "usp_StartSearch"; public const string CompleteSearch = "usp_CompleteSearch"; public const string ResetPartialSearches = "usp_ResetPartialSearches"; public const string ValidateSearchCriteria = "usp_ValidateSearchCriteria"; // Functions public const string MatchMis = "fn_MatchMIS"; } ``` ### 4. C# Code Updates **`LotFinderRepository.SearchManagement.cs`** - Use constant for stored procedure: ```csharp // Before await using var command = new SqlCommand("SubmitSearch", connection) // After await using var command = new SqlCommand(SqlObjects.SubmitSearch, connection) ``` **`MisQueryBuilder.cs`** - Use constant for function: ```csharp // Before dbo.MatchMIS(c.WorkOrderNumber, ...) // After dbo.{SqlObjects.MatchMis}(c.WorkOrderNumber, ...) ``` ### 5. Documentation Updates Update all files mentioning procedure names: | File | Updates Needed | |------|----------------| | `DOCUMENTATION/Architecture/Testing.md` | Procedure references | | `DOCUMENTATION/Architecture/Database.md` | Procedure references | | `DOCUMENTATION/search_creation_page_new.md` | Procedure references | | OpenSpec specs | Any procedure references | | PLANS files | Historical references | ## Files Changed | Category | Files | |----------|-------| | SQL Scripts | 5 files (040-044) | | C# New | 1 file (`SqlObjects.cs`) | | C# Modified | 2 files (`LotFinderRepository.SearchManagement.cs`, `MisQueryBuilder.cs`) | | Documentation | ~5 files | | Docker | Manual SQL execution | ## Verification 1. Build succeeds 2. All existing tests pass 3. Docker database has renamed objects: - `SELECT name FROM sys.procedures` shows `usp_` prefix on all procedures - `SELECT name FROM sys.objects WHERE type IN ('FN','TF')` shows `fn_` prefix on all functions