From 5fb8e4a13afbc4cc3ef6e7492c020f8abf63b37f Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 14 Apr 2026 01:00:20 +0200 Subject: add MysqlDialect --- plan_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'plan_test.go') diff --git a/plan_test.go b/plan_test.go index 1095016..3568447 100644 --- a/plan_test.go +++ b/plan_test.go @@ -71,6 +71,25 @@ func TestQueryConstructionBasic(t *testing.T) { PrimaryKeyColumnNames: []string{"ID"}, } + t.Run("MysqlDialect", func(t *testing.T) { + plan, err := buildPlan(reflect.TypeFor[record](), MysqlDialect(), opts) + if err != nil { + t.Error(err) + } + assert.Equal(t, plan.Select.Query, "SELECT `ID`, `Description`, `CreatedAt` FROM `basic_records` WHERE ") + assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil) + assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}}) + assert.Equal(t, plan.Insert.Query, "INSERT INTO `basic_records` (`Description`, `CreatedAt`) VALUES (?, ?)") + assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{1}, {2}}) + assert.DeepEqual(t, plan.Insert.ScanIndexes, [][]int{{0}}) + assert.Equal(t, plan.Update.Query, "UPDATE `basic_records` SET `Description` = ?, `CreatedAt` = ? WHERE `ID` = ?") + assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{1}, {2}, {0}}) + assert.DeepEqual(t, plan.Update.ScanIndexes, nil) + assert.Equal(t, plan.Delete.Query, "DELETE FROM `basic_records` WHERE `ID` = ?") + assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}}) + assert.DeepEqual(t, plan.Delete.ScanIndexes, nil) + }) + t.Run("PostgresDialect", func(t *testing.T) { plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts) if err != nil { @@ -119,6 +138,25 @@ func TestQueryConstructionWithoutPrimaryKey(t *testing.T) { TableName: "foo_bar_relations", } + t.Run("MysqlDialect", func(t *testing.T) { + plan, err := buildPlan(reflect.TypeFor[relation](), MysqlDialect(), opts) + if err != nil { + t.Error(err) + } + assert.Equal(t, plan.Select.Query, "SELECT `foo_id`, `bar_id` FROM `foo_bar_relations` WHERE ") + assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil) + assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}}) + assert.Equal(t, plan.Insert.Query, "INSERT INTO `foo_bar_relations` (`foo_id`, `bar_id`) VALUES (?, ?)") + assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}}) + assert.DeepEqual(t, plan.Insert.ScanIndexes, nil) + assert.Equal(t, plan.Update.Query, "") + assert.DeepEqual(t, plan.Update.ArgumentIndexes, nil) + assert.DeepEqual(t, plan.Update.ScanIndexes, nil) + assert.Equal(t, plan.Delete.Query, "") + assert.DeepEqual(t, plan.Delete.ArgumentIndexes, nil) + assert.DeepEqual(t, plan.Delete.ScanIndexes, nil) + }) + t.Run("PostgresDialect", func(t *testing.T) { plan, err := buildPlan(reflect.TypeFor[relation](), PostgresDialect(), opts) if err != nil { @@ -187,6 +225,7 @@ func TestQueryConstructionImpossble(t *testing.T) { } } + t.Run("MysqlDialect", testWith(MysqlDialect())) t.Run("PostgresDialect", testWith(PostgresDialect())) t.Run("SqliteDialect", testWith(SqliteDialect())) } @@ -202,6 +241,25 @@ func TestQueryConstructionWithMultiplePrimaryKeyColumns(t *testing.T) { PrimaryKeyColumnNames: []string{"group_id", "name"}, } + t.Run("MysqlDialect", func(t *testing.T) { + plan, err := buildPlan(reflect.TypeFor[record](), MysqlDialect(), opts) + if err != nil { + t.Error(err) + } + assert.Equal(t, plan.Select.Query, "SELECT `group_id`, `name`, `created_at` FROM `complex_records` WHERE ") + assert.DeepEqual(t, plan.Select.ArgumentIndexes, nil) + assert.DeepEqual(t, plan.Select.ScanIndexes, [][]int{{0}, {1}, {2}}) + assert.Equal(t, plan.Insert.Query, "INSERT INTO `complex_records` (`group_id`, `name`, `created_at`) VALUES (?, ?, ?)") + assert.DeepEqual(t, plan.Insert.ArgumentIndexes, [][]int{{0}, {1}, {2}}) + assert.DeepEqual(t, plan.Insert.ScanIndexes, nil) + assert.Equal(t, plan.Update.Query, "UPDATE `complex_records` SET `created_at` = ? WHERE `group_id` = ? AND `name` = ?") + assert.DeepEqual(t, plan.Update.ArgumentIndexes, [][]int{{2}, {0}, {1}}) + assert.DeepEqual(t, plan.Update.ScanIndexes, nil) + assert.Equal(t, plan.Delete.Query, "DELETE FROM `complex_records` WHERE `group_id` = ? AND `name` = ?") + assert.DeepEqual(t, plan.Delete.ArgumentIndexes, [][]int{{0}, {1}}) + assert.DeepEqual(t, plan.Delete.ScanIndexes, nil) + }) + t.Run("PostgresDialect", func(t *testing.T) { plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts) if err != nil { @@ -252,6 +310,11 @@ func TestQueryConstructionWithMultipleAutoColumns(t *testing.T) { PrimaryKeyColumnNames: []string{"id"}, } + t.Run("MysqlDialect", func(t *testing.T) { + _, err := NewStore[record](MysqlDialect()) + assert.Equal(t, err.Error(), `cannot use type oblast.record for queries: multiple columns are marked as auto-filled (id, created_at), but this SQL dialect only supports at most one per table`) + }) + t.Run("PostgresDialect", func(t *testing.T) { plan, err := buildPlan(reflect.TypeFor[record](), PostgresDialect(), opts) if err != nil { -- cgit v1.2.3