Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM add function: LeftJoinOnFields,RightJoinOnFields,InnerJoinOnFields #2921

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions contrib/drivers/mysql/mysql_feature_model_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,75 @@ func Test_Model_InnerJoinOnField(t *testing.T) {
})
}

func Test_Model_LeftJoinOnFields(t *testing.T) {
var (
table1 = gtime.TimestampNanoStr() + "_table1"
table2 = gtime.TimestampNanoStr() + "_table2"
)
createInitTable(table1)
defer dropTable(table1)
createInitTable(table2)
defer dropTable(table2)

gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table1).
FieldsPrefix(table1, "*").
LeftJoinOnFields(table2, "id", "=", "id").
WhereIn("id", g.Slice{1, 2}).
Order("id asc").All()
t.AssertNil(err)
t.Assert(len(r), 2)
t.Assert(r[0]["id"], "1")
t.Assert(r[1]["id"], "2")
})
}

func Test_Model_RightJoinOnFields(t *testing.T) {
var (
table1 = gtime.TimestampNanoStr() + "_table1"
table2 = gtime.TimestampNanoStr() + "_table2"
)
createInitTable(table1)
defer dropTable(table1)
createInitTable(table2)
defer dropTable(table2)

gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table1).
FieldsPrefix(table1, "*").
RightJoinOnFields(table2, "id", "=", "id").
WhereIn("id", g.Slice{1, 2}).
Order("id asc").All()
t.AssertNil(err)
t.Assert(len(r), 2)
t.Assert(r[0]["id"], "1")
t.Assert(r[1]["id"], "2")
})
}

func Test_Model_InnerJoinOnFields(t *testing.T) {
var (
table1 = gtime.TimestampNanoStr() + "_table1"
table2 = gtime.TimestampNanoStr() + "_table2"
)
createInitTable(table1)
defer dropTable(table1)
createInitTable(table2)
defer dropTable(table2)

gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table1).
FieldsPrefix(table1, "*").
InnerJoinOnFields(table2, "id", "=", "id").
WhereIn("id", g.Slice{1, 2}).
Order("id asc").All()
t.AssertNil(err)
t.Assert(len(r), 2)
t.Assert(r[0]["id"], "1")
t.Assert(r[1]["id"], "2")
})
}

func Test_Model_FieldsPrefix(t *testing.T) {
var (
table1 = gtime.TimestampNanoStr() + "_table1"
Expand Down
51 changes: 51 additions & 0 deletions database/gdb/gdb_model_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,57 @@ func (m *Model) InnerJoinOnField(table, field string) *Model {
))
}

// LeftJoinOnFields performs as LeftJoin. It specifies different fields and comparison operator.
//
// Eg:
// Model("user").LeftJoinOnFields("order", "id", "=", "user_id")
// Model("user").LeftJoinOnFields("order", "id", ">", "user_id")
// Model("user").LeftJoinOnFields("order", "id", "<", "user_id")
func (m *Model) LeftJoinOnFields(table, firstField, operator, secondField string) *Model {
return m.doJoin("LEFT", table, fmt.Sprintf(
`%s.%s %s %s.%s`,
m.tablesInit,
m.db.GetCore().QuoteWord(firstField),
operator,
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(secondField),
))
}

// RightJoinOnFields performs as RightJoin. It specifies different fields and comparison operator.
//
// Eg:
// Model("user").RightJoinOnFields("order", "id", "=", "user_id")
// Model("user").RightJoinOnFields("order", "id", ">", "user_id")
// Model("user").RightJoinOnFields("order", "id", "<", "user_id")
func (m *Model) RightJoinOnFields(table, firstField, operator, secondField string) *Model {
return m.doJoin("RIGHT", table, fmt.Sprintf(
`%s.%s %s %s.%s`,
m.tablesInit,
m.db.GetCore().QuoteWord(firstField),
operator,
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(secondField),
))
}

// InnerJoinOnFields performs as InnerJoin. It specifies different fields and comparison operator.
//
// Eg:
// Model("user").InnerJoinOnFields("order", "id", "=", "user_id")
// Model("user").InnerJoinOnFields("order", "id", ">", "user_id")
// Model("user").InnerJoinOnFields("order", "id", "<", "user_id")
func (m *Model) InnerJoinOnFields(table, firstField, operator, secondField string) *Model {
return m.doJoin("INNER", table, fmt.Sprintf(
`%s.%s %s %s.%s`,
m.tablesInit,
m.db.GetCore().QuoteWord(firstField),
operator,
m.db.GetCore().QuoteWord(table),
m.db.GetCore().QuoteWord(secondField),
))
}

// doJoin does "LEFT/RIGHT/INNER JOIN ... ON ..." statement on the model.
// The parameter `table` can be joined table and its joined condition,
// and also with its alias name.
Expand Down
Loading