Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:4paradigm OpenMLDB Go SDK Connection

From Leeroopedia
Knowledge Sources
Domains SDK, Database
Last Updated 2026-10-12 12:00 GMT

Overview

Go SQL connection implementation that executes queries against OpenMLDB via HTTP and parses JSON responses into standard database/sql/driver.Rows.

Description

The Go SDK Connection (conn struct) implements the driver.Conn, driver.Pinger, driver.ExecerContext, driver.QueryerContext, driver.SessionResetter, and driver.Validator interfaces. It sends SQL queries as HTTP POST requests to the OpenMLDB API server endpoint /dbs/{db}, with the SQL and parameters serialized as JSON. Responses are parsed from JSON into respDataRows which implements driver.Rows for iteration. The connection supports three query modes: offsync, offasync, and online.

Usage

This connection is created automatically by the Go SDK driver when sql.Open or db.Conn(ctx) is called. Users do not instantiate it directly — it is used through the standard database/sql API.

Code Reference

Source Location

Signature

// Connection struct
type conn struct {
    host   string    // host or host:port
    db     string    // database name
    mode   queryMode // offsync, offasync, or online
    closed bool
}

// Query modes
type queryMode string
const (
    ModeOffsync  queryMode = "offsync"
    ModeOffasync queryMode = "offasync"
    ModeOnline   queryMode = "online"
)

// Core query method (internal)
func (c *conn) query(ctx context.Context, sql string, parameters ...interfaces.Value) (rows interfaces.Rows, err error)

// Public API methods
func (c *conn) QueryContext(ctx context.Context, query string, args []interfaces.NamedValue) (interfaces.Rows, error)
func (c *conn) ExecContext(ctx context.Context, query string, args []interfaces.NamedValue) (interfaces.Result, error)
func (c *conn) Ping(ctx context.Context) error
func (c *conn) Close() error
func (c *conn) ResetSession(ctx context.Context) error
func (c *conn) IsValid() bool

// Result iteration
type respDataRows struct {
    respData
    i int
}
func (r respDataRows) Columns() []string
func (r *respDataRows) Next(dest []interfaces.Value) error
func (r *respDataRows) Close() error

// JSON parsing
func parseReqToJson(mode, sql string, input ...interfaces.Value) ([]byte, error)
func parseRespFromJson(respBody io.Reader) (*queryResp, error)

Import

// Used internally by the driver; not imported directly
// Accessed through database/sql API
import (
    "database/sql"
    _ "github.com/4paradigm/openmldb/go"
)

I/O Contract

Inputs

Name Type Required Description
ctx context.Context Yes Request context for timeout/cancellation
query string Yes SQL statement to execute
args []NamedValue No Parameter values for parameterized queries
mode queryMode Yes Set at connection time: offsync, offasync, online

Outputs

Name Type Description
QueryContext returns interfaces.Rows respDataRows for SELECT queries
ExecContext returns interfaces.Result ResultNoRows for DDL/DML
Ping returns error nil if SELECT 1 succeeds
HTTP request POST POST http://{host}/dbs/{db} with JSON body containing mode, SQL, and optional input data

Usage Examples

Query with Parameters

// Query a deployed SQL with input parameters
db, _ := sql.Open("openmldb", "openmldb://127.0.0.1:9080/demo_db?mode=online")
defer db.Close()

// Parameterized query — parameters are sent as JSON input data
rows, err := db.QueryContext(ctx,
    "SELECT sum(trip_distance) OVER w1 as total FROM taxi_tour " +
    "WINDOW w1 AS (PARTITION BY id ORDER BY time ROWS 60 PRECEDING) " +
    "AND id = ?",
    "taxi_001",
)
if err != nil {
    log.Fatal(err)
}
defer rows.Close()

cols, _ := rows.Columns()
fmt.Println("Columns:", cols)

for rows.Next() {
    vals := make([]sql.NullString, len(cols))
    ptrs := make([]interface{}, len(cols))
    for i := range vals {
        ptrs[i] = &vals[i]
    }
    rows.Scan(ptrs...)
    fmt.Println(vals)
}

Execute DDL/DML

// Execute a DDL statement (no result rows)
_, err := db.ExecContext(ctx,
    "CREATE TABLE test (id string, val int, PRIMARY KEY id)",
)
if err != nil {
    log.Fatal(err)
}

// Execute a DML statement
_, err = db.ExecContext(ctx, "INSERT INTO test VALUES ('a', 1)")

HTTP Request Format

// Internal: conn.query sends HTTP POST to http://{host}/dbs/{db}
// Request body (JSON):
// {
//   "mode": "online",
//   "sql": "SELECT ...",
//   "input": {
//     "schema": ["string", "int64"],
//     "data": ["taxi_001", 100]
//   }
// }
//
// Response body (JSON):
// {
//   "code": 0,
//   "msg": "ok",
//   "data": {
//     "schema": ["total"],
//     "data": [[123.45]]
//   }
// }

Related Pages

Implements Principle

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment