mysql

package
v1.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2025 License: BSD-3-Clause Imports: 26 Imported by: 0

Documentation

Overview

Copyright 2025 SGNL.ai, Inc.

Copyright 2025 SGNL.ai, Inc.

Copyright 2025 SGNL.ai, Inc.

Copyright 2025 SGNL.ai, Inc.

Copyright 2025 SGNL.ai, Inc.

Copyright 2025 SGNL.ai, Inc.

Index

Constants

View Source
const (
	TestDatasourceForConnectFailure = "test.connect.failure"
)

Variables

This section is empty.

Functions

func ConstructQuery added in v1.40.0

func ConstructQuery(request *Request) (string, []any, error)

func NewAdapter

func NewAdapter(client Client) framework.Adapter[Config]

NewAdapter instantiates a new Adapter.

func ParseResponse

func ParseResponse(rows *sql.Rows, request *Request) ([]map[string]any, *framework.Error)

ParseResponse for parsing the SQL query response.

Types

type Adapter

type Adapter struct {
	MySQLClient Client
}

Adapter implements the framework.Adapter interface to query pages of objects from datasources.

func (*Adapter) GetPage

func (a *Adapter) GetPage(ctx context.Context, request *framework.Request[Config]) framework.Response

GetPage is called by SGNL's ingestion service to query a page of objects from a datasource.

func (*Adapter) RequestPageFromDatasource

func (a *Adapter) RequestPageFromDatasource(
	ctx context.Context, request *framework.Request[Config],
) framework.Response

RequestPageFromDatasource requests a page of objects from a datasource.

func (*Adapter) ValidateGetPageRequest

func (a *Adapter) ValidateGetPageRequest(ctx context.Context, request *framework.Request[Config]) *framework.Error

ValidateGetPageRequest validates the fields of the GetPage Request.

type Client

type Client interface {
	GetPage(ctx context.Context, request *Request) (*Response, *framework.Error)
}

func NewClient

func NewClient(client SQLClient) Client

NewClient returns a Client to query the datasource.

type Config

type Config struct {
	*config.CommonConfig

	// MySQL database to connect to.
	Database string `json:"database,omitempty"`

	Filters map[string]condexpr.Condition `json:"filters,omitempty"`
}

Config is the configuration passed in each GetPage calls to the adapter.

Adapter configuration example: nolint: godot

{
	"requestTimeoutSeconds": 10,
	"localTimeZoneOffset": 43200,
	"database": "sgnl",
	"filters": {
		"users": {
			"or": [
				{
					"and": [
						{
							"field": "age",
							"op": ">",
							"value": 18
						},
						{
							"field": "country",
							"op": "=",
							"value": "USA"
						}
					]
				},
				{
					"field": "verified",
					"op": "=",
					"value": true
				}
			]
		},
		"groups": {
			"field": "country",
			"op": "IN",
			"value": ["active", "inactive"]
		}
	}
}

func (*Config) Validate

func (c *Config) Validate(_ context.Context) error

ValidateConfig validates that a Config received in a GetPage call is valid.

type Datasource

type Datasource struct {
	Client SQLClient
}

func (*Datasource) GetPage

func (d *Datasource) GetPage(ctx context.Context, request *Request) (*Response, *framework.Error)

GetPage for requesting data from a datasource.

func (*Datasource) ProxyRequest added in v1.39.0

func (d *Datasource) ProxyRequest(ctx context.Context, request *Request, ci *connector.ConnectorInfo,
) (*Response, *framework.Error)

ProxyRequest sends serialized SQL query request to the on-premises connector.

func (*Datasource) Request added in v1.39.0

func (d *Datasource) Request(_ context.Context, request *Request) (*Response, *framework.Error)

Request function to directly connect to the SQL datasource and execute a query to fetch data.

type MockSQLClient

type MockSQLClient struct {
	DB   *sql.DB
	Mock sqlmock.Sqlmock
}

func NewMockSQLClient

func NewMockSQLClient() *MockSQLClient

func (*MockSQLClient) Connect

func (c *MockSQLClient) Connect(datasourceName string) error

func (*MockSQLClient) IsProxied added in v1.40.0

func (c *MockSQLClient) IsProxied() bool

func (*MockSQLClient) Proxy added in v1.39.0

func (*MockSQLClient) Query

func (c *MockSQLClient) Query(query string, args ...any) (*sql.Rows, error)

nolint: lll

type Request

type Request struct {
	// BaseURL is the Base URL of the datasource to query.
	BaseURL string `json:"baseURL"`

	// Username is the user name used to authenticate with the MySQL instance.
	Username string `json:"username"`

	// Password is the password used to authenticate with the MySQL instance.
	Password string `json:"password"`

	// PageSize is the maximum number of objects to return from the entity.
	PageSize int64 `json:"pageSize,omitempty"`

	// EntityConfig contains the entity configuration for the current request.
	EntityConfig framework.EntityConfig

	// A filter to apply to the MySQL request when pulling data for the current entity.
	Filter *condexpr.Condition

	// Cursor identifies the first object of the page to return, as returned by
	// the last request for the entity.
	// nil in the request for the first page.
	Cursor *int64 `json:"cursor,omitempty"`

	// MySQL database to connect to.
	Database string `json:"database"`

	// UniqueAttributeExternalID is used to specify the unique ID that should be used when ordering results from
	// the specified table.
	UniqueAttributeExternalID string `json:"uniqueAttributeExternalID"`
}

Request is a request to a MySQL database.

func (*Request) DatasourceName added in v1.39.0

func (r *Request) DatasourceName() string

DatasourceName to connect to.

func (*Request) SimpleSQLValidation added in v1.39.0

func (r *Request) SimpleSQLValidation() *framework.Error

SimpleSQLValidation performs simple validation on specific fields to prevent SQL Ingestion attacks, since we can't use table names or column names in prepared queries which leaves us vulnerable.

type Response

type Response struct {
	// StatusCode is an HTTP status code.
	StatusCode int `json:"statusCode"`

	// RetryAfterHeader is the Retry-After response HTTP header, if set.
	RetryAfterHeader string `json:"retryAfterHeader"`

	// Objects is the list of objects returned from the datasource.
	// May be empty.
	Objects []map[string]any `json:"objects"`

	// NextCursor is the cursor that identifies the first object of the next page.
	// nil if this is the last page in this full sync.
	NextCursor *int64 `json:"nextCursor,omitempty"`
}

Response is a response returned by the datasource.

type SQLClient

type SQLClient interface {
	IsProxied() bool
	Proxy(ctx context.Context, req *grpc_proxy_v1.ProxyRequestMessage) (*grpc_proxy_v1.Response, error)
	Connect(dataSourceName string) error
	Query(query string, args ...any) (*sql.Rows, error)
}

func NewDefaultSQLClient

func NewDefaultSQLClient(client grpc_proxy_v1.ProxyServiceClient) SQLClient

NewDefaultSQLClient creates a new SQLClient instance. It is used to connect to a SQL database and execute queries. The client is not proxied by default. If you want to use a proxied client, you need to provide a grpc_proxy_v1.ProxyServiceClient instance.

type SQLColumnTypes

type SQLColumnTypes map[string]string

type SQLRow

type SQLRow map[string]string

type SQLRows

type SQLRows []SQLRow

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL