mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-09 10:14:21 +01:00
enable indices in go sdk
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user