@@ -3,19 +3,29 @@ package dbt
33import (
44 "fmt"
55 "path/filepath"
6+ "strconv"
67 "strings"
78
89 "github.com/pterm/pterm"
910)
1011
1112// Constants for data types
1213const (
13- integerType = "integer"
14- varcharType = "varchar"
15- dateType = "date"
16- timestampType = "timestamp"
17- doubleType = "double"
18- booleanType = "boolean"
14+ integerType = "integer"
15+ smallintType = "smallint"
16+ bigintType = "bigint"
17+ floatType = "float"
18+ decimalType = "decimal"
19+ varcharType = "varchar"
20+ charType = "char"
21+ textType = "text"
22+ dateType = "date"
23+ timestampType = "timestamp"
24+ timestamptzType = "timestamptz"
25+ doubleType = "double"
26+ booleanType = "boolean"
27+ jsonType = "json"
28+ intervalType = "interval"
1929)
2030
2131// Constants for SQL data types
@@ -77,6 +87,8 @@ func convertConnectionToDataSource(conn DbtConnection, dbtHomePath, profileName,
7787 return convertToPostgresDataSource (conn )
7888 case "duckdb" :
7989 return convertToLocalFileDataSource (conn , dbtHomePath )
90+ case "sqlserver" :
91+ return convertToMSSQLDataSource (conn )
8092 default :
8193 // For unsupported database types, we can choose to ignore or return error
8294 // Here we choose to return nil and log a warning
@@ -103,6 +115,26 @@ func convertToPostgresDataSource(conn DbtConnection) (*WrenPostgresDataSource, e
103115 return ds , nil
104116}
105117
118+ func convertToMSSQLDataSource (conn DbtConnection ) (* WrenMSSQLDataSource , error ) {
119+ port := strconv .Itoa (conn .Port )
120+ if conn .Port == 0 {
121+ port = "1433"
122+ }
123+
124+ ds := & WrenMSSQLDataSource {
125+ Database : conn .Database ,
126+ Host : conn .Server ,
127+ Port : port ,
128+ User : conn .User ,
129+ Password : conn .Password ,
130+ TdsVersion : "8.0" , // the default tds version for Wren engine image
131+ Driver : "ODBC Driver 18 for SQL Server" , // the driver used by Wren engine image
132+ Kwargs : map [string ]interface {}{"TrustServerCertificate" : "YES" },
133+ }
134+
135+ return ds , nil
136+ }
137+
106138// convertToLocalFileDataSource converts to local file data source
107139func convertToLocalFileDataSource (conn DbtConnection , dbtHome string ) (* WrenLocalFileDataSource , error ) {
108140 // For file types, we need to get URL and format info from Additional fields
@@ -222,6 +254,85 @@ func (ds *WrenPostgresDataSource) MapType(sourceType string) string {
222254 return sourceType
223255}
224256
257+ type WrenMSSQLDataSource struct {
258+ Database string `json:"database"`
259+ Host string `json:"host"`
260+ Port string `json:"port"`
261+ User string `json:"user"`
262+ Password string `json:"password"`
263+ TdsVersion string `json:"tds_version"`
264+ Driver string `json:"driver"`
265+ Kwargs map [string ]interface {} `json:"kwargs"`
266+ }
267+
268+ func (ds * WrenMSSQLDataSource ) GetType () string {
269+ return "mssql"
270+ }
271+
272+ func (ds * WrenMSSQLDataSource ) Validate () error {
273+ if ds .Host == "" {
274+ return fmt .Errorf ("host cannot be empty" )
275+ }
276+ if ds .Database == "" {
277+ return fmt .Errorf ("database cannot be empty" )
278+ }
279+ if ds .User == "" {
280+ return fmt .Errorf ("user cannot be empty" )
281+ }
282+ if ds .Port == "" {
283+ return fmt .Errorf ("port must be specified" )
284+ }
285+ port , err := strconv .Atoi (ds .Port )
286+ if err != nil {
287+ return fmt .Errorf ("port must be a valid number" )
288+ }
289+ if port <= 0 || port > 65535 {
290+ return fmt .Errorf ("port must be between 1 and 65535" )
291+ }
292+ if ds .Password == "" {
293+ return fmt .Errorf ("password cannot be empty" )
294+ }
295+ return nil
296+ }
297+
298+ func (ds * WrenMSSQLDataSource ) MapType (sourceType string ) string {
299+ // This method is not used in WrenMSSQLDataSource, but required by DataSource interface
300+ switch strings .ToLower (sourceType ) {
301+ case "char" , "nchar" :
302+ return charType
303+ case varcharType , "nvarchar" :
304+ return varcharType
305+ case "text" , "ntext" :
306+ return textType
307+ case "bit" , "tinyint" :
308+ return booleanType
309+ case "smallint" :
310+ return smallintType
311+ case "int" :
312+ return integerType
313+ case "bigint" :
314+ return bigintType
315+ case booleanType :
316+ return booleanType
317+ case "float" , "real" :
318+ return floatType
319+ case "decimal" , "numeric" , "money" , "smallmoney" :
320+ return decimalType
321+ case "date" :
322+ return dateType
323+ case "datetime" , "datetime2" , "smalldatetime" :
324+ return timestampType
325+ case "time" :
326+ return intervalType
327+ case "datetimeoffset" :
328+ return timestamptzType
329+ case "json" :
330+ return jsonType
331+ default :
332+ return strings .ToLower (sourceType )
333+ }
334+ }
335+
225336// GetActiveDataSources gets active data sources based on specified profile and target
226337// If profileName is empty, it will use the first found profile
227338// If targetName is empty, it will use the profile's default target
0 commit comments