7.3 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T03:44:18.623811+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 836c8383a0b90cbc |
DASFactoryDb
Purpose
The DbWrapper class serves as a minimal, internal database access abstraction layer for the DASFactory database, specifically targeting SQL Server. It encapsulates connection string construction, singleton-based instance management, and basic command execution for stored procedures. Its primary role is to provide a centralized, reusable mechanism for retrieving device IDs and handling SQL command execution with error propagation, while supporting future flexibility between local and centralized database configurations via static flags (_usingCentralizedDB, _usingNTLMAuthentication). The class is not intended for general-purpose data access but focuses on a narrow set of operations required by the DASFactory system.
Public Interface
All members are public or internal, but the class is partial and its constructor is protected, suggesting it may be extended elsewhere in the codebase. Only the following are publicly accessible from outside the assembly (based on public modifiers):
-
public static bool _usingCentralizedDB
A static flag indicating whether the system should use a centralized database (currently alwaysfalseper comments). Used for future configuration flexibility. -
public static bool _usingNTLMAuthentication
A static flag indicating whether to use Windows Authentication (NTLM) for database connections (currently alwaystrue). Affects connection string generation. -
public static bool Connected
A read/write static property that always returnsfalse(per itsget => false;implementation). The summary states it is “used to passively indicate connection status, does not perform any connection checking.” Note: This property is non-functional as implemented. -
public static DbWrapper Connection
A static property implementing a thread-safe singleton pattern. Returns the single instance ofDbWrapper(lazily initialized on first access). Useslock(dbLock)to ensure thread-safe initialization. -
public string Server { get; set; }
Gets or sets the database server name. Required for connection string generation; must be non-empty/whitespace whenGetLocalDASFactoryConnectionString()is called. -
public string DBName { get; set; }
Gets or sets the database name. Not used in current implementation—the connection string hardcodes"Database=DASFactory;". -
public string Username { get; set; }
Gets or sets the SQL Server username. Used only when_usingNTLMAuthenticationisfalse. -
public string Password { get; set; }
Gets or sets the SQL Server password. Used only when_usingNTLMAuthenticationisfalse. -
public void ResetLocalDASFactoryConnection()
Resets the cached connection string (_localDASFactoryConnection = null) and resets_usingNTLMAuthenticationtotrue. Intended to force re-computation of the connection string on next access. -
public string GetLocalDASFactoryConnectionString()
Returns a SQL Server connection string for theDASFactorydatabase. Uses cached value if available; otherwise constructs it based onServer,_usingNTLMAuthentication,Username, andPassword. ThrowsException("Empty Server")ifServeris null/whitespace.- If
_usingNTLMAuthentication == true:"Server={Server};Database=DASFactory;Trusted_Connection=TRUE;" - Else:
"Server={Server};Database=DASFactory;User Id={Username};Password={Password};"
- If
-
public static int GetDeviceId(string serialNumber)
Executes the stored proceduresp_IDASCommunicationTableGetRecordIdwith@SerialNumberas input. Returns theRecordId(asint) if found, or-1if no matching record exists. ThrowsException("Not connected")ifConnectedisfalse(which it always is, per the property implementation—see Gotchas). Disposes the underlying connection after use. -
internal static void ProcessReturn(SqlParameter errorNumber, SqlParameter errorMessage)
Checks theValueoferrorNumber. If non-null and non-zero, throws anExceptionconcatenatingerrorNumber.ValueanderrorMessage.Value. Used to propagate SQL stored procedure error codes/messages.
Invariants
- The
Serverproperty must be set to a non-null, non-whitespace string before callingGetLocalDASFactoryConnectionString(); otherwise, an exception is thrown. - The
Connectedproperty is alwaysfalse, regardless of actual database state. Any code checkingif (!Connected)will always throw an exception. - The
DbWrapper.Connectionsingleton is lazily initialized and thread-safe vialock(dbLock). - Connection strings are cached in
_localDASFactoryConnection; subsequent calls return the cached value untilResetLocalDASFactoryConnection()is invoked. GetDeviceId()always opens and disposes its ownSqlConnectioninternally; it does not use the singleton’s state for persistence.
Dependencies
- Direct Dependencies (from imports):
SystemSystem.DataSystem.Data.SqlClientDASFactoryDb.Connection(used inGetDASFactoryCommand()viaConnection.GetLocalDASFactoryConnectionString())
- Inferred Usage:
- The stored procedure
sp_IDASCommunicationTableGetRecordIdmust exist in theDASFactorydatabase. - The
Connectionclass (referenced inGetDASFactoryCommand()) must defineGetLocalDASFactoryConnectionString()—likely in a separate file or module.
- The stored procedure
- Dependents (inferred):
- Any code calling
DbWrapper.Connection,GetDeviceId(), orGetLocalDASFactoryConnectionString()(e.g., device lookup logic). - The
ProcessReturn()method suggests integration with stored procedures that output@ErrorNumberand@ErrorMessageparameters.
- Any code calling
Gotchas
Connectedis non-functional: Itsgetaccessor always returnsfalse, soGetDeviceId()will always throw"Not connected"—this is likely a bug or incomplete implementation.- Hardcoded database name:
DBNameis unused; the connection string always uses"DASFactory". - No connection pooling or reuse:
GetDASFactoryCommand()creates and opens a newSqlConnectionon every call, then disposes it inGetDeviceId()—inefficient for repeated calls. - Exception message format in
ProcessReturn: ConcatenateserrorNumber.Value(likely anint) anderrorMessage.Value(astring) without a separator, potentially causing ambiguous error messages (e.g.,"53Operation failed"). - Thread-safety gap: While
Connectionsingleton is thread-safe,ResetLocalDASFactoryConnection()modifies static state (_usingNTLMAuthentication) without locking—could cause race conditions if called concurrently withGetLocalDASFactoryConnectionString(). - No async support: All database operations are synchronous (
ExecuteReader()), which may block threads in UI or high-throughput services. - Missing null/empty checks in
GetDeviceId: Does not validateserialNumberbefore passing to the stored procedure. _usingCentralizedDBis unused: Despite being declared, no logic in this file branches on its value.