|
| 1 | +package datasource |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/k1LoW/tbls/drivers/databricks" |
| 10 | + "github.com/k1LoW/tbls/schema" |
| 11 | +) |
| 12 | + |
| 13 | +// AnalyzeDatabricks analyze `databricks://` |
| 14 | +func AnalyzeDatabricks(urlstr string) (_ *schema.Schema, err error) { |
| 15 | + s := &schema.Schema{} |
| 16 | + |
| 17 | + u, err := url.Parse(urlstr) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + |
| 22 | + catalog := u.Query().Get("catalog") |
| 23 | + if catalog == "" { |
| 24 | + return nil, errors.New("no catalog name in the connection string") |
| 25 | + } |
| 26 | + schemaName := u.Query().Get("schema") |
| 27 | + if schemaName == "" { |
| 28 | + return nil, errors.New("no schema name in the connection string") |
| 29 | + } |
| 30 | + |
| 31 | + // Extract authentication parameters |
| 32 | + token := u.Query().Get("token") |
| 33 | + clientID := u.Query().Get("client_id") |
| 34 | + clientSecret := u.Query().Get("client_secret") |
| 35 | + |
| 36 | + // Validate authentication parameter combinations |
| 37 | + hasToken := token != "" |
| 38 | + hasOAuth := clientID != "" && clientSecret != "" |
| 39 | + |
| 40 | + if !hasToken && !hasOAuth { |
| 41 | + return nil, errors.New("authentication required: provide either 'token' for PAT authentication or both 'client_id' and 'client_secret' for OAuth authentication") |
| 42 | + } |
| 43 | + |
| 44 | + if hasToken && hasOAuth { |
| 45 | + return nil, errors.New("conflicting authentication methods: provide either 'token' for PAT authentication OR 'client_id'/'client_secret' for OAuth authentication, not both") |
| 46 | + } |
| 47 | + |
| 48 | + if clientID != "" && clientSecret == "" { |
| 49 | + return nil, errors.New("incomplete OAuth credentials: 'client_secret' is required when 'client_id' is provided") |
| 50 | + } |
| 51 | + |
| 52 | + if clientSecret != "" && clientID == "" { |
| 53 | + return nil, errors.New("incomplete OAuth credentials: 'client_id' is required when 'client_secret' is provided") |
| 54 | + } |
| 55 | + |
| 56 | + s.Name = fmt.Sprintf("%s.%s", catalog, schemaName) |
| 57 | + |
| 58 | + // Build databricks driver DSN based on authentication method |
| 59 | + var databricksDSN string |
| 60 | + if hasToken { |
| 61 | + // PAT token authentication: token:TOKEN@host:port/path?catalog=CATALOG&schema=SCHEMA |
| 62 | + databricksDSN = fmt.Sprintf("token:%s@%s%s?catalog=%s&schema=%s", token, u.Host, u.Path, catalog, schemaName) |
| 63 | + } else { |
| 64 | + // OAuth client credentials authentication: host:port/path?authType=OauthM2M&clientID=ID&clientSecret=SECRET&catalog=CATALOG&schema=SCHEMA |
| 65 | + databricksDSN = fmt.Sprintf("%s%s?authType=OauthM2M&clientID=%s&clientSecret=%s&catalog=%s&schema=%s", |
| 66 | + u.Host, u.Path, clientID, clientSecret, catalog, schemaName) |
| 67 | + } |
| 68 | + |
| 69 | + db, err := sql.Open("databricks", databricksDSN) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + defer func() { |
| 74 | + _ = db.Close() |
| 75 | + }() |
| 76 | + |
| 77 | + driver := databricks.New(db) |
| 78 | + if err := driver.Analyze(s); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + return s, nil |
| 83 | +} |
0 commit comments