2014-12-16 05:04:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2014-12-16 14:05:49 -06:00
|
|
|
"time"
|
|
|
|
|
2017-10-19 10:28:54 -05:00
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-11-16 02:54:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/securejsondata"
|
2017-08-23 06:31:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2014-12-16 05:04:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-16 14:05:49 -06:00
|
|
|
bus.AddHandler("sql", GetDataSources)
|
2014-12-16 09:45:07 -06:00
|
|
|
bus.AddHandler("sql", AddDataSource)
|
2017-02-09 20:11:36 -06:00
|
|
|
bus.AddHandler("sql", DeleteDataSourceById)
|
|
|
|
bus.AddHandler("sql", DeleteDataSourceByName)
|
2014-12-17 10:32:22 -06:00
|
|
|
bus.AddHandler("sql", UpdateDataSource)
|
2014-12-19 00:27:25 -06:00
|
|
|
bus.AddHandler("sql", GetDataSourceById)
|
2015-02-15 13:10:34 -06:00
|
|
|
bus.AddHandler("sql", GetDataSourceByName)
|
2014-12-18 13:26:06 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:27:25 -06:00
|
|
|
func GetDataSourceById(query *m.GetDataSourceByIdQuery) error {
|
2017-09-06 04:23:52 -05:00
|
|
|
metrics.M_DB_DataSource_QueryById.Inc()
|
2017-08-23 06:31:26 -05:00
|
|
|
|
2016-06-06 10:11:46 -05:00
|
|
|
datasource := m.DataSource{OrgId: query.OrgId, Id: query.Id}
|
|
|
|
has, err := x.Get(&datasource)
|
2014-12-18 13:26:06 -06:00
|
|
|
|
2015-02-15 13:10:34 -06:00
|
|
|
if !has {
|
|
|
|
return m.ErrDataSourceNotFound
|
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
|
|
|
query.Result = &datasource
|
2015-02-15 13:10:34 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDataSourceByName(query *m.GetDataSourceByNameQuery) error {
|
2016-06-06 10:11:46 -05:00
|
|
|
datasource := m.DataSource{OrgId: query.OrgId, Name: query.Name}
|
|
|
|
has, err := x.Get(&datasource)
|
2015-02-15 13:10:34 -06:00
|
|
|
|
2014-12-18 13:26:06 -06:00
|
|
|
if !has {
|
|
|
|
return m.ErrDataSourceNotFound
|
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
|
|
|
query.Result = &datasource
|
2014-12-18 13:26:06 -06:00
|
|
|
return err
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:05:49 -06:00
|
|
|
func GetDataSources(query *m.GetDataSourcesQuery) error {
|
2016-09-12 03:45:17 -05:00
|
|
|
sess := x.Limit(1000, 0).Where("org_id=?", query.OrgId).Asc("name")
|
2014-12-16 14:05:49 -06:00
|
|
|
|
2014-12-17 10:32:22 -06:00
|
|
|
query.Result = make([]*m.DataSource, 0)
|
|
|
|
return sess.Find(&query.Result)
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:11:36 -06:00
|
|
|
func DeleteDataSourceById(cmd *m.DeleteDataSourceByIdCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
var rawSql = "DELETE FROM data_source WHERE id=? and org_id=?"
|
|
|
|
_, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId)
|
2014-12-17 10:32:22 -06:00
|
|
|
return err
|
|
|
|
})
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
2014-12-16 09:45:07 -06:00
|
|
|
|
2017-02-09 20:11:36 -06:00
|
|
|
func DeleteDataSourceByName(cmd *m.DeleteDataSourceByNameCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2017-02-09 20:11:36 -06:00
|
|
|
var rawSql = "DELETE FROM data_source WHERE name=? and org_id=?"
|
|
|
|
_, err := sess.Exec(rawSql, cmd.Name, cmd.OrgId)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-16 09:45:07 -06:00
|
|
|
func AddDataSource(cmd *m.AddDataSourceCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-10-01 09:41:27 -05:00
|
|
|
existing := m.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
|
2016-10-01 10:36:56 -05:00
|
|
|
has, _ := sess.Get(&existing)
|
2016-10-01 09:41:27 -05:00
|
|
|
|
|
|
|
if has {
|
|
|
|
return m.ErrDataSourceNameExists
|
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
ds := &m.DataSource{
|
2015-03-02 02:58:35 -06:00
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Database: cmd.Database,
|
|
|
|
IsDefault: cmd.IsDefault,
|
|
|
|
BasicAuth: cmd.BasicAuth,
|
|
|
|
BasicAuthUser: cmd.BasicAuthUser,
|
|
|
|
BasicAuthPassword: cmd.BasicAuthPassword,
|
2016-03-16 09:16:28 -05:00
|
|
|
WithCredentials: cmd.WithCredentials,
|
2015-03-02 02:58:35 -06:00
|
|
|
JsonData: cmd.JsonData,
|
2016-11-16 02:54:26 -06:00
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
2015-03-02 02:58:35 -06:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2017-10-19 10:28:54 -05:00
|
|
|
Version: 1,
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
if _, err := sess.Insert(ds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := updateIsDefaultFlag(ds, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 10:32:22 -06:00
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
cmd.Result = ds
|
|
|
|
return nil
|
2014-12-17 10:32:22 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
func updateIsDefaultFlag(ds *m.DataSource, sess *DBSession) error {
|
2015-01-09 09:36:23 -06:00
|
|
|
// Handle is default flag
|
|
|
|
if ds.IsDefault {
|
2015-03-30 20:11:39 -05:00
|
|
|
rawSql := "UPDATE data_source SET is_default=? WHERE org_id=? AND id <> ?"
|
2015-04-01 01:26:02 -05:00
|
|
|
if _, err := sess.Exec(rawSql, false, ds.OrgId, ds.Id); err != nil {
|
2015-01-09 09:36:23 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-17 10:32:22 -06:00
|
|
|
func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-01-09 09:36:23 -06:00
|
|
|
ds := &m.DataSource{
|
2015-03-02 02:58:35 -06:00
|
|
|
Id: cmd.Id,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Database: cmd.Database,
|
|
|
|
IsDefault: cmd.IsDefault,
|
|
|
|
BasicAuth: cmd.BasicAuth,
|
|
|
|
BasicAuthUser: cmd.BasicAuthUser,
|
|
|
|
BasicAuthPassword: cmd.BasicAuthPassword,
|
2015-12-08 21:37:08 -06:00
|
|
|
WithCredentials: cmd.WithCredentials,
|
2015-03-02 02:58:35 -06:00
|
|
|
JsonData: cmd.JsonData,
|
2016-11-16 02:54:26 -06:00
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
2015-03-02 02:58:35 -06:00
|
|
|
Updated: time.Now(),
|
2017-05-11 03:50:24 -05:00
|
|
|
Version: cmd.Version + 1,
|
2015-01-09 09:36:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
sess.UseBool("is_default")
|
2015-03-02 02:58:35 -06:00
|
|
|
sess.UseBool("basic_auth")
|
2015-12-08 21:37:08 -06:00
|
|
|
sess.UseBool("with_credentials")
|
2015-01-09 09:36:23 -06:00
|
|
|
|
2017-10-19 10:28:54 -05:00
|
|
|
var updateSession *xorm.Session
|
|
|
|
if cmd.Version != 0 {
|
|
|
|
updateSession = sess.Where("id=? and org_id=? and (version = ? or version < ?)", ds.Id, ds.OrgId, cmd.Version, cmd.Version)
|
|
|
|
} else {
|
|
|
|
updateSession = sess.Where("id=? and org_id=?", ds.Id, ds.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := updateSession.Update(ds)
|
2015-01-09 09:36:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2017-10-19 10:28:54 -05:00
|
|
|
if affected == 0 {
|
|
|
|
return m.ErrDataSouceUpdatingOldVersion
|
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
err = updateIsDefaultFlag(ds, sess)
|
2017-10-19 10:28:54 -05:00
|
|
|
|
|
|
|
cmd.Result = ds
|
2014-12-16 14:05:49 -06:00
|
|
|
return err
|
|
|
|
})
|
2014-12-16 09:45:07 -06:00
|
|
|
}
|