The sqlserver module provides Microsoft SQL Server database integration for Project Forge applications. This module extends the base database module with SQL Server-specific functionality, enterprise features, and optimizations for Microsoft SQL Server environments.
Overview
This module adds Microsoft SQL Server database support to Project Forge applications and requires the database module. It provides:
Enterprise Database: Full support for SQL Server’s enterprise features
Driver Integration: Official Microsoft SQL Server driver with connection pooling
Advanced Features: Support for stored procedures, triggers, and SQL Server-specific syntax
Windows Integration: Native Windows Authentication and Active Directory support
Key Features
SQL Server Compatibility
Support for SQL Server 2017+ (including Azure SQL Database)
Full T-SQL dialect support
Advanced data types (geography, geometry, hierarchyid, etc.)
Enterprise features (partitioning, compression, encryption)
Enterprise Features
Stored Procedures: Full support for complex stored procedures
User-Defined Types: Custom data types and table-valued parameters
JSON Support: Native JSON functions and operations
Spatial Data: Geography and geometry data types
Performance
Optimized connection pooling for SQL Server workloads
Query plan caching and prepared statement support
Connection health monitoring with SQL Server-specific metrics
Bulk operations and Table-Valued Parameters (TVP)
Security
Windows Authentication and Active Directory integration
SSL/TLS encryption with certificate validation
Row-level security and dynamic data masking
Always Encrypted support for sensitive data
Configuration
Environment Variables
The module reads configuration from environment variables (with optional prefix):
db_host - SQL Server hostname (default: localhost)
db_port - SQL Server port (default: 1433)
db_user - Username for database connections
db_password - Password for database connections (optional)
db_database - Database name to connect to
db_schema - Default schema to use (optional, default: dbo)
db_max_connections - Maximum number of active and idle connections
// Create table type in SQL Server first: // create type UserTableType as table (ID int, Name nvarchar(50)) // Use table-valued parameters for bulk operations userTVP:=mssql.TVP{ TypeName:"UserTableType", Value:[][]interface{}{ {1,"Alice"}, {2,"Bob"}, }, } _,err:=db.Exec(`exec BulkInsertUsers @Users = ?`,userTVP)
JSON Operations
1 2 3 4 5 6 7 8
// Insert JSON data _,err:=db.Exec(`insert into users (data) values (?)`,`{"name": "John", "preferences": {"theme": "dark"}}`) // Query with JSON functions rows,err:=db.Query(`select json_value(data, '$.name') from users where json_value(data, '$.active') = 'true'`) // JSON path operations rows,err:=db.Query(`select * from users where json_value(data, '$.preferences.theme') = 'dark'`)
Spatial Data Types
1 2 3 4 5 6 7
// Insert geometry data _,err:=db.Exec(`insert into locations (point) values (geometry::Point(?, ?, 4326))`,longitude,latitude) // Spatial queries rows,err:=db.Query(` select name from locations where point.STDistance(geometry::Point(?, ?, 4326)) < 1000`,userLong,userLat)
MERGE Operations (Upsert)
1 2 3 4 5 6 7 8 9 10
// SQL Server MERGE statement _,err:=db.Exec(` merge users as target using (values (?, ?)) as source (email, name) on target.email = source.email when matched then update set name = source.name, updated_at = getdate() when not matched then insert (email, name, created_at) values (source.email, source.name, getdate());`, email,name)
Bulk Operations
1 2 3 4 5 6 7 8 9 10 11
// Bulk copy for large data inserts // Uses SqlBulkCopy equivalent functionality txn,err:=db.Begin() stmt,err:=txn.Prepare(mssql.CopyIn("users",mssql.BulkOptions{},"name","email")) for_,user:=rangelargeUserList{ _,err=stmt.Exec(user.Name,user.Email) } err=stmt.Close() err=txn.Commit()