enable indices in go sdk

This commit is contained in:
Nikita Sivukhin
2025-08-13 16:24:04 +04:00
parent 857f9147f6
commit 70581aca5b
2 changed files with 49 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math"
"slices"
"testing"
_ "github.com/tursodatabase/turso"
@@ -683,6 +684,53 @@ func TestParameterOrdering(t *testing.T) {
}
}
func TestIndex(t *testing.T) {
newConn, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Error opening new connection: %v", err)
}
sql := "CREATE TABLE users (name TEXT PRIMARY KEY, email TEXT)"
_, err = newConn.Exec(sql)
if err != nil {
t.Fatalf("Error creating table: %v", err)
}
sql = "CREATE INDEX email_idx ON users(email)"
_, err = newConn.Exec(sql)
if err != nil {
t.Fatalf("Error creating index: %v", err)
}
// Test inserting with parameters in a different order than
// the table definition.
sql = "INSERT INTO users VALUES ('alice', 'a@b.c'), ('bob', 'b@d.e')"
_, err = newConn.Exec(sql)
if err != nil {
t.Fatalf("Error inserting data: %v", err)
}
for filter, row := range map[string][]string{
"a@b.c": []string{"alice", "a@b.c"},
"b@d.e": []string{"bob", "b@d.e"},
} {
query := "SELECT * FROM users WHERE email = ?"
rows, err := newConn.Query(query, filter)
if err != nil {
t.Fatalf("Error executing query: %v", err)
}
for rows.Next() {
var name, email string
err := rows.Scan(&name, &email)
t.Log("name,email:", name, email)
if err != nil {
t.Fatal("Error scanning row: ", err)
}
if !slices.Equal([]string{name, email}, row) {
t.Fatal("Unexpected result", row, []string{name, email})
}
}
}
}
func slicesAreEq(a, b []byte) bool {
if len(a) != len(b) {
fmt.Printf("LENGTHS NOT EQUAL: %d != %d\n", len(a), len(b))

View File

@@ -20,7 +20,7 @@ pub unsafe extern "C" fn db_open(path: *const c_char) -> *mut c_void {
}
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let path = path.to_str().unwrap();
let Ok((io, conn)) = Connection::from_uri(path, false, false, false) else {
let Ok((io, conn)) = Connection::from_uri(path, true, false, false) else {
panic!("Failed to open connection with path: {path}");
};
LimboConn::new(conn, io).to_ptr()