8.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
2026-04-16T14:09:58.168174+00:00 | zai-org/GLM-5-FP8 | 1 | 5cf7683c6017efd4 |
Documentation: DTS.Common.Storage
1. Purpose
This module serves as the Data Access Layer (DAL) and schema definition hub for the DTS application (likely a data acquisition system named DataPRO). It provides abstraction over raw ADO.NET operations, managing connections to both LocalDB instances and remote SQL Servers. The module defines strongly-typed mappings for database tables—specifically for Sensors, MME (Measurement and Monitoring Equipment), and Test Setups—via constants and enums. It also includes utilities for type conversion, database lifecycle management (backup/restore/setup), and specific data migration tasks.
2. Public Interface
DTS.Common.Storage.LocalOnlyOperations
A singleton class managing local database connections and command execution.
static LocalOnlyOperations Connection(Property): Singleton instance accessor.static IDbCommand GetCommand(): Returns anIDbCommand(wrapper forGetSQLCommand).static SqlCommand GetSQLCommand(bool newCommand): Returns aSqlCommand. IfnewCommandis false, it reuses a staticcmdinstance. It ensures the connection is open before returning.static void CreateParam(IDbCommand icmd, string name, SqlDbType type, object value): Adds a parameter to the command. HandlesDateTimemin-value constraints and formatting.int ExecuteCommand(IDbCommand icmd): Executes a non-query command against a new connection instance.string GetLocalConnectionString(): Constructs and caches the connection string usingServerandDbNameproperties with Trusted Connection.void ResetLocalConnectionString(): Clears the cached connection string.
DTS.Common.Storage.TestSetups.TestHistory
Represents a historical record of a test run.
TestHistory(IDataReader reader): Constructor that populates the object from a database row, handlingDBNullchecks.static TestHistory[] GetTestHistory(string name): Retrieves test histories by calling the stored proceduresp_TestHistoryGet.static string SerializeToString(TestHistory[] histories): Serializes an array of histories to an XML string usingTestHistoryCollection.
DTS.Common.Storage.TypeConvertor
A utility class for mapping between .NET types, DbType, and SqlDbType.
static Type ToNetType(DbType dbType)static Type ToNetType(SqlDbType sqlDbType)static DbType ToDbType(Type type)static DbType ToDbType(SqlDbType sqlDbType)static SqlDbType ToSqlDbType(Type type)static SqlDbType ToSqlDbType(DbType dbType)
DTS.Common.Storage.DatabaseServices
Provides high-level database setup and maintenance operations.
static void RestoreLocalDatabase(string defaultDbName): Stops the LocalDB instance, restores.mdfand.ldffiles from.BAKfiles, and restarts the instance.static void BackupLocalDatabase(string defaultDbName): Stops the LocalDB instance, copies current.mdf/.ldfto.BAKfiles, and restarts the instance.static int SetupLocal(string defaultDbName): ConfiguresDbOperationsfor a local connection usingDbAPIand returns the DB version.static int SetupLocal2(string defaultDbName): ConfiguresLocalOnlyOperationsfor a local connection.static int SetupForRemoteDb(...): ConfiguresDbOperationsfor a remote SQL Server connection.static bool SimpleDbTest(): Executes a simple querySELECT TOP 1 [DASId] FROM [DAS]to verify connectivity.
DTS.Common.Storage.UserMigrationHelper
Assists in migrating user data between database versions (v52 to v53).
UserMigrationHelper(DataRow row): Parses user data, permissions, and visibility settings from aDataRow.void Commit(Dictionary<string, long> IUIItemNameToID): Inserts the user intoDataPROUsers,UserUIItemSettings, andTagAssignmentstables. Handles both MSSQL and SQLite execution paths.
Schema Definitions (Abstract Classes)
These classes contain const string table names and enum field mappings. They are nested within DbOperations (except SensorDB which is nested but the file structure implies partial classes).
DbOperations.SensorDB: DefinestblSensors,tblSensorModels,tblSensorCalibrations.DbOperations.MMETables: DefinesMMEPossibleChannels,tblMMEDirections,tblMMEFilterClasses, etc. Includes legacy field name constants (MyType,CustomChannelType,Id).DbOperations.TestSetups: DefinestblTestSetups,tblTestSetupHardware,tblTestChannelSettings, etc.
3. Invariants
- Singleton Access:
LocalOnlyOperations.Connectionis thread-safe (useslock) and ensures only one instance exists. - DateTime Handling:
LocalOnlyOperations.CreateParamenforces thatDateTimevalues cannot be less thanSystem.Data.SqlTypes.SqlDateTime.MinValue. If they are, they are clamped to the minimum. - Connection State:
LocalOnlyOperations.GetSQLCommandguarantees the returned command has an open connection. - Schema Enums: The integer values for
SensorDataFieldsstart at 1 (explicitly defined), while other enums rely on default 0-based indexing. - Migration Logic:
UserMigrationHelper.Commitrelies on the external state ofDbOperations._usingMSSQLto determine the correct execution method (ExecuteCommandvsExecuteSQLiteCommand).
4. Dependencies
Internal Dependencies
DTS.Common.Utilities.Logging: Used for logging (APILogger.Log).DbAPI: Used for low-level connection logic (DbAPI.Connections.ConnectionDetails,DbAPI.Connections.ConnectToDb).DbAPI.Errors: Used for error code checking (ErrorCodes.ERROR_SUCCESS).Utils.Database: Used for LocalDB instance management (StartInstance,StopInstance, path retrieval).Utils.FileUtils: Used for file operations during backup/restore.
External Dependencies
System.Data.SqlClient: Heavily used for SQL Server interaction.System.Xml.Serialization: Used inTestHistoryfor serialization.System.IO: Used for file handling inDatabaseServices.
Consumers
- Any business logic layer requiring database access, sensor definitions, or test setup configurations.
- Migration scripts or tools targeting database version 52/53.
5. Gotchas
- Static Command Reuse:
LocalOnlyOperations.GetSQLCommand()(without arguments) reuses a staticcmdfield. If multiple operations call this simultaneously or hold onto the command, they may interfere with each other. Always callGetSQLCommand(true)if a fresh command instance is required to avoid race conditions or parameter pollution. - Legacy Schema Names:
MMETablesdefines constantsMyType,CustomChannelType, andIdwith comments indicating they were exported in older versions (up to 1.3.515) and replaced by newer fields. Code handling imports or legacy data must account for these renamed columns. - DateTime Formatting:
LocalOnlyOperations.CreateParamconvertsDateTimeobjects to a formatted string (yyyy-MM-dd HH:mm:ss) when creating parameters, rather than passing theDateTimeobject directly to theSqlParameter.Value. This strips milliseconds and might cause precision loss or parsing issues depending on the server culture settings, though it ensures the value fits within SQL Server's valid range. - ArrayList in TypeConvertor:
TypeConvertorusesSystem.Collections.ArrayListinstead of generic collections, indicating this is legacy code pre-dating .NET 2.0 generics. It involves boxing/unboxing overhead. - Hardcoded Paths:
DatabaseServicesrelies onEnvironment.CurrentDirectoryand hardcoded folder names (db,SQL Server Scripts). The application must be run from the correct working directory for these methods to succeed. - Connection Management in
ExecuteCommand:LocalOnlyOperations.ExecuteCommandcreates a newSqlConnectionand opens it, ignoring the connection attached to the passedIDbCommand. This means the connection state checked/created inGetSQLCommandis discarded/overwritten in this specific execution path.