8.2 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:24:57.920635+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 460fbd3d82516ff4 |
Database
Documentation: DbAPI.Database Module
1. Purpose
This module provides core database versioning and metadata operations for the DataPRO system. It defines and implements the IDatabase interface, enabling retrieval of the highest database version (GetDatabaseVersion), insertion of new version records (InsertDatabaseVersion), and extraction of the SQL Server major version (GetSQLVersion). It also includes utility methods for resolving versioned stored procedure names (GetStoredProcedureVersion, GetStoredProcedureVersionCached, PrepareForDbAccess), supporting backward/forward compatibility across database schema versions. The module acts as a low-level abstraction layer between application logic and SQL Server, enforcing user authentication and delegating to stored procedures for persistence.
2. Public Interface
The interface IDatabase is the public contract. The concrete implementation Database is internal, so only IDatabase members are externally visible.
IDatabase.GetSQLVersion(IConnectionDetails connection) → int
- Behavior: Retrieves the major version number (e.g.,
15for SQL Server 2019) from the server’sServerVersionstring. - Notes: Does not require user authentication. Returns
0on failure (e.g., parsing error or connection issue).
IDatabase.GetDatabaseVersion(IUserDbRecord user, IConnectionDetails connection, out int version) → ulong
- Behavior: Executes the stored procedure
sp_DBVersionGetto fetch all version records, then returns the maximum version number via theout int versionparameter. - Authentication: Does not enforce user login (despite XML comment stating “User must be logged in” — implementation explicitly allows querying without login).
- Return:
ErrorCodes.ERROR_SUCCESS(0) on success;ErrorCodes.ERROR_UNKNOWNon exception. - Output:
versionis set to0on failure.
IDatabase.InsertDatabaseVersion(IUserDbRecord user, IConnectionDetails connection, int version, int step, DateTime date, string remarks, string userField) → ulong
- Behavior: Inserts a new version record into the database by calling the stored procedure
sp_DbVersionInsert. - Authentication: Explicitly checks
IsUserLoggedIn(user, connection); returnsErrorCodes.ERROR_ACCESS_DENIEDif not logged in. - Parameters:
version,step,date,remarks,userField→ values inserted into the record.
- Return:
ErrorCodes.ERROR_SUCCESSon success;ErrorCodes.ERROR_UNKNOWNon SQL error (non-zero@errorNumberoutput) or exception. - Notes: Does not validate uniqueness or ordering of version numbers — duplicates or regressions are allowed.
Database.GetStoredProcedureVersion(IConnectionDetails connection, string storedProcedure, int clientDbVersion) → string
- Behavior: Resolves the correct stored procedure name (e.g.,
sp_MyProcvssp_MyProc_3) based on client and DB versions. CallsDbAPI.GetStoredProcedureToUse. - Fallback: Returns original
storedProcedurename if resolution fails.
Database.GetStoredProcedureVersionCached(IConnectionDetails connection, string storedProcedure) → string
- Behavior: Same as
GetStoredProcedureVersion, but uses a cached result (viaDbAPI.GetStoredProcedureToUseCached) to avoid repeated DB lookups. - Uses:
connection.ClientDbVersionas the client version.
Database.PrepareForDbAccess(IUserDbRecord user, IConnectionDetails connection, int clientDbVersion, string storedProcedure, out int storedProcedureVersionToUse, out SqlCommand cmd) → ulong
- Behavior: A helper to prepare a
SqlCommandfor a versioned stored procedure. - Steps:
- Checks user login (
IsUserLoggedIn) → returnsERROR_ACCESS_DENIEDif false. - Resolves stored procedure name version via
DbAPI.GetStoredProcedureToUse. - Creates
SqlCommandviaConnectionManager.GetSqlCommand.
- Checks user login (
- Output:
storedProcedureVersionToUse: resolved version suffix (e.g.,3), or0if none.cmd: fully configuredSqlCommandobject.
- Return:
ERROR_SUCCESSon success; otherwise error code (e.g.,ERROR_ACCESS_DENIED,ERROR_UNKNOWN).
3. Invariants
- Authentication Enforcement:
InsertDatabaseVersionandPrepareForDbAccessrequire the user to be logged in (checked viaIsUserLoggedIn). Failure to meet this yieldsERROR_ACCESS_DENIED. - Stored Procedure Naming: Versioned stored procedures follow the pattern
{baseName}_{version}(e.g.,sp_MyProc_2). A version suffix of0implies no suffix (i.e., base name only). - Error Handling: All methods log exceptions to
LogManageratTraceEventType.Errorlevel before returningERROR_UNKNOWN. - Resource Management: All
SqlCommandobjects created viaConnectionManager.GetSqlCommandhave their.Connectiondisposed infinallyblocks. - No Validation on Version Values:
InsertDatabaseVersiondoes not check for duplicate versions, ordering (e.g., regression), or consistency with existing records.
4. Dependencies
Imports/References:
DbAPI.Connections→ ProvidesConnectionManager,IsUserLoggedIn, andGetStoredProcedureToUse/GetStoredProcedureToUseCached.DbAPI.Errors→ DefinesErrorCodes(e.g.,ERROR_SUCCESS,ERROR_ACCESS_DENIED,ERROR_UNKNOWN).DbAPI.Logging→LogManagerfor error logging.DTS.Common.Interface.Database→ InterfacesIUserDbRecord,IConnectionDetails.DTS.Common.Classes→ Likely containsUtility.GetInt(used inGetDatabaseVersion).System.Data,System.Data.SqlClient→ Core ADO.NET types (SqlCommand,SqlParameter,SqlDbType,CommandType,DBNull,IDataReader).
Used By:
- Any module requiring database versioning operations (e.g., upgrade scripts, version reporting, audit logging).
- Likely consumed via
IDatabaseinterface (injected or resolved via DI/container — not evident from source).
5. Gotchas
-
Contradictory Authentication Requirement:
GetDatabaseVersion’s XML comment states “User must be logged into database”, but the implementation does not callIsUserLoggedIn. This is a discrepancy between documentation and behavior. -
No Version Uniqueness/Ordering Checks:
InsertDatabaseVersionblindly inserts records — callers must ensure version numbers are monotonically increasing and unique, or risk data inconsistency. -
GetSQLVersionFragility:
AssumesServerVersionis dot-separated (e.g.,"15.0.2000.5"). Parsing relies onSplit(".")andint.Parseon the first segment — will throw if format changes (e.g.,"15-0-2000"or"15.0.2000.5-SP2"). -
GetDatabaseVersionUsesMax()onList<int>:
If no version records exist,dbVersionsList.Max()throwsInvalidOperationException. However, thecatchblock suppresses this and returnsERROR_UNKNOWNwithversion = 0. This is safe but may mask empty-db scenarios. -
Stored Procedure Name Resolution:
GetStoredProcedureVersionandPrepareForDbAccessrely on externalDbAPI.GetStoredProcedureToUse/GetStoredProcedureToUseCached. Behavior of these (e.g., how version mapping is determined) is not visible here. -
userFieldParameter Misleading Name:
TheuserFieldparameter inInsertDatabaseVersionis documented as “What user to associate with the insert record (does not have to match any actual users)” — implying it’s a free-text field, not a foreign key. Do not assume it validates against a user table. -
No Transaction Support:
All operations are standalone; no mechanism for atomic multi-step versioning (e.g., insert + update). -
GetStoredProcedureVersionCachedUsesconnection.ClientDbVersion:
AssumesIConnectionDetailsexposes aClientDbVersionproperty — not visible in this module but required for correct behavior.
Documentation generated from provided source files. No external behavior or APIs inferred beyond what is explicitly present.