mirror of https://github.com/grafana/grafana
Chore: implement sqlx into tag service (#55908)
* add sqlx store to tag service * add sqlx into tag service * fix test * change to camal cases * change in xorm camal casepull/55928/head
parent
8869d6fe7a
commit
2472777ce2
@ -0,0 +1,34 @@ |
||||
package tagimpl |
||||
|
||||
import ( |
||||
"context" |
||||
"database/sql" |
||||
"errors" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/session" |
||||
"github.com/grafana/grafana/pkg/services/tag" |
||||
) |
||||
|
||||
type sqlxStore struct { |
||||
sess *session.SessionDB |
||||
} |
||||
|
||||
func (s *sqlxStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) { |
||||
for _, tagElement := range tags { |
||||
var existingTag tag.Tag |
||||
err := s.sess.Get(ctx, &existingTag, `SELECT * FROM tag WHERE "key"=? AND "value"=?`, tagElement.Key, tagElement.Value) |
||||
if err != nil { |
||||
if errors.Is(err, sql.ErrNoRows) { |
||||
tagElement.Id, err = s.sess.ExecWithReturningId(ctx, `INSERT INTO tag ("key", "value") VALUES (?, ?)`, tagElement.Key, tagElement.Value) |
||||
if err != nil { |
||||
return tags, err |
||||
} |
||||
} else { |
||||
return tags, err |
||||
} |
||||
} else { |
||||
tagElement.Id = existingTag.Id |
||||
} |
||||
} |
||||
return tags, nil |
||||
} |
||||
@ -0,0 +1,13 @@ |
||||
package tagimpl |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
) |
||||
|
||||
func TestIntegrationSQLxSavingTags(t *testing.T) { |
||||
testIntegrationSavingTags(t, func(ss *sqlstore.SQLStore) store { |
||||
return &sqlxStore{sess: ss.GetSqlxSession()} |
||||
}) |
||||
} |
||||
@ -0,0 +1,35 @@ |
||||
package tagimpl |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db" |
||||
"github.com/grafana/grafana/pkg/services/tag" |
||||
) |
||||
|
||||
type sqlStore struct { |
||||
db db.DB |
||||
} |
||||
|
||||
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) { |
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error { |
||||
for _, tagElement := range tags { |
||||
var existingTag tag.Tag |
||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagElement.Key, tagElement.Value).Get(&existingTag) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if exists { |
||||
tagElement.Id = existingTag.Id |
||||
} else { |
||||
_, err := sess.Table("tag").Insert(tagElement) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
} |
||||
return nil |
||||
}) |
||||
return tags, err |
||||
} |
||||
@ -0,0 +1,13 @@ |
||||
package tagimpl |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
) |
||||
|
||||
func TestIntegrationXormSavingTags(t *testing.T) { |
||||
testIntegrationSavingTags(t, func(ss *sqlstore.SQLStore) store { |
||||
return &sqlStore{db: ss} |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue