SQL Server for moondb — the TDS protocol, spoken directly in pure MoonBit, with TLS through moontls.
moon add moonbitstack/moonsqlsasync fn run() -> Unit raise {
let db = @moonsqls.SqlServerConn::connect(
"127.0.0.1", 1433, "sa", "your-password", "master",
)
db.execute("CREATE TABLE hero (id INT PRIMARY KEY, name NVARCHAR(64))", []) |> ignore
db.execute("INSERT INTO hero VALUES (?, ?)", [@moondb.Int(1), @moondb.Text("Nova")])
|> ignore
let rows = db.query("SELECT name FROM hero WHERE id = ?", [@moondb.Int(1)])
println(rows[0].text_by("name")) // Nova
db.close()
}prelogin → encryption negotiated in the same packet as the login
↓ TLS handshake carried inside prelogin packets
login7 → credentials, the database, and the 7.4 extension block,
↓ or TDS 8.0 with TLS already under the whole session
batch/RPC → one TDS packet per message, wrapped in TLS once it is on
↓
tokens → COLMETADATA, ROW, NBCROW, DONE, ERROR, ENVCHANGE, RETURNVALUElet copied = conn.bulk_copy("hero", ["id", "name"], [
[@moondb.Int(1), @moondb.Text("Nova")],
[@moondb.Int(2), @moondb.Text("Ivan")],
])| SQL Server | Value |
|---|---|
| BIT | Bool |
| TINYINT, SMALLINT, INT | Int |
| BIGINT | Int64 |
| REAL, FLOAT | Double |
| NVARCHAR, NCHAR | Text |
| VARCHAR, CHAR | Text |
| VARBINARY, BINARY | Blob |
| UNIQUEIDENTIFIER | Uuid |
| DECIMAL, NUMERIC | Numeric (digits converted exactly) |
| DATE, TIME, DATETIME2, DATETIME, SMALLDATETIME | Time |
| NULL | Null |
impl AsyncDriver for SqlServerConnasync fn execute(self : SqlServerConn, sql : String, params : Array[Value]) -> ExecResult raise DbErrorasync fn SqlServerConn::bulk_copy(self : SqlServerConn, table : String, columns : Array[String], rows : Array[Array[Value]]) -> Int64 raise DbErrorlet copied = conn.bulk_copy("hero", ["id", "name"], [
[@moondb.Int(1), @moondb.Text("Nova")],
[@moondb.Int(2), @moondb.Text("Ivan")],
])async fn SqlServerConn::connect(host : String, port : Int, user : String, password : String, database : String, encrypt? : Bool, strict? : Bool, tls? : Client) -> SqlServerConn raise DbErrorasync fn SqlServerConn::execute(self : SqlServerConn, sql : String, params : Array[Value]) -> ExecResult raise DbErrorasync fn SqlServerConn::query(self : SqlServerConn, sql : String, params : Array[Value]) -> Array[Row] raise DbErrorasync fn SqlServerConn::query_stream(self : SqlServerConn, sql : String, params : Array[Value]) -> &AsyncCursor raise DbErrorpub struct SqlServerStmt {
conn : SqlServerConn
sql : String
params : Array[Value]
handle : Int
declaration : String
closed : Bool
}impl AsyncStmt for SqlServerStmtasync fn SqlServerStmt::bind_named(self : SqlServerStmt, args : Map[String, Value]) -> Unit raise DbErrorInstall
Download zipSQL Server for moondb — the TDS protocol, spoken directly in pure MoonBit, with TLS through moontls.