Skip to content

Commit

Permalink
[Feature] gdb Add Exist Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong committed Oct 17, 2024
1 parent c18339b commit 0e88b47
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 2 deletions.
16 changes: 16 additions & 0 deletions contrib/drivers/clickhouse/clickhouse_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,22 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Where(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
15 changes: 15 additions & 0 deletions contrib/drivers/mssql/mssql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,22 @@ func Test_Model_Count(t *testing.T) {
t.AssertNil(err)
t.Assert(count, int64(TableSize))
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,22 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Value_WithCache(t *testing.T) {
table := createTable()
defer dropTable(table)
Expand Down
16 changes: 16 additions & 0 deletions contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,22 @@ func Test_Model_Count(t *testing.T) {

}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
16 changes: 16 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Where(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
16 changes: 16 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,22 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
16 changes: 16 additions & 0 deletions contrib/drivers/sqlitecgo/sqlitecgo_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,22 @@ func Test_Model_Count(t *testing.T) {
})
}

func Test_Model_Exist(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
exist, err := db.Model(table).Raw(fmt.Sprintf("SELECT * FROM %s", table)).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Exist()
t.AssertNil(err)
t.Assert(exist, TableSize > 0)
exist, err = db.Model(table).Where("id", -1).Exist()
t.AssertNil(err)
t.Assert(exist, false)
})
}

func Test_Model_Select(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
1 change: 1 addition & 0 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ const (
queryTypeNormal queryType = iota
queryTypeCount
queryTypeValue
queryTypeExist
)

type joinOperator string
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (m *Model) saveSelectResultToCache(
if len(result) > 0 {
var core = m.db.GetCore()
switch queryType {
case queryTypeValue, queryTypeCount:
case queryTypeCount, queryTypeValue, queryTypeExist:
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
if result[0][internalData.FirstResultColumn].IsEmpty() {
result = nil
Expand Down
43 changes: 42 additions & 1 deletion database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,39 @@ func (m *Model) Count(where ...interface{}) (int, error) {
return 0, nil
}

// Exist does "SELECT 1 FROM ... LIMIT 1" statement for the model.
// The optional parameter `where` is the same as the parameter of Model.Where function,
// see Model.Where.
func (m *Model) Exist(where ...interface{}) (bool, error) {
var (
core = m.db.GetCore()
ctx = core.injectInternalColumn(m.GetCtx())
)
if len(where) > 0 {
return m.Where(where[0], where[1:]...).Exist()
}
var (
sqlWithHolder, holderArgs = m.getFormattedSqlAndArgs(ctx, queryTypeExist, true)
all, err = m.doGetAllBySql(ctx, queryTypeExist, sqlWithHolder, holderArgs...)
)
if err != nil {
return false, err
}
if len(all) > 0 {
if internalData := core.getInternalColumnFromCtx(ctx); internalData != nil {
if v, ok := all[0][internalData.FirstResultColumn]; ok {
return v.Bool(), nil
}
} else {
return false, gerror.NewCode(
gcode.CodeInternalError,
`query count error: the internal context data is missing. there's internal issue should be fixed`,
)
}
}
return false, nil
}

// CountColumn does "SELECT COUNT(x) FROM ..." statement for the model.
func (m *Model) CountColumn(column string) (int, error) {
if len(column) == 0 {
Expand Down Expand Up @@ -651,7 +684,15 @@ func (m *Model) getFormattedSqlAndArgs(
sqlWithHolder = fmt.Sprintf("SELECT COUNT(1) FROM (%s) count_alias", sqlWithHolder)
}
return sqlWithHolder, conditionArgs

case queryTypeExist:
// Raw SQL Model.
if m.rawSql != "" {
sqlWithHolder = fmt.Sprintf("SELECT 1 FROM (%s) LIMIT 1", m.rawSql)
return sqlWithHolder, nil
}
conditionWhere, conditionExtra, conditionArgs := m.formatCondition(ctx, true, false)
sqlWithHolder = fmt.Sprintf("SELECT 1 FROM %s%s", m.tables, conditionWhere+conditionExtra)
return sqlWithHolder, conditionArgs
default:
conditionWhere, conditionExtra, conditionArgs := m.formatCondition(ctx, limit1, false)
// Raw SQL Model, especially for UNION/UNION ALL featured SQL.
Expand Down

0 comments on commit 0e88b47

Please sign in to comment.