修复安装系统时表前缀不为空,导致查询错误

pull/21/merge
ouqiang 2017-04-26 16:17:18 +08:00
parent 7b450bd376
commit bafafadae1
6 changed files with 73 additions and 16 deletions

View File

@ -13,6 +13,7 @@ import (
type Status int8
type CommonMap map[string]interface{}
var TablePrefix string = ""
var Db *xorm.Engine
const (
@ -67,6 +68,7 @@ func CreateDb(config map[string]string) *xorm.Engine {
}
if config["prefix"] != "" {
// 设置表前缀
TablePrefix = config["prefix"]
mapper := core.NewPrefixMapper(core.SnakeMapper{}, config["prefix"])
engine.SetTableMapper(mapper)
}

View File

@ -44,7 +44,7 @@ type TaskHost struct {
}
func (TaskHost) TableName() string {
return "task"
return TablePrefix + "task"
}
// 新增
@ -85,7 +85,7 @@ func (task *Task) Enable(id int) (int64, error) {
func (task *Task) ActiveList() ([]TaskHost, error) {
list := make([]TaskHost, 0)
fields := "t.*, host.alias,host.name,host.username,host.password,host.port,host.auth_type,host.private_key"
err := Db.Alias("t").Join("LEFT", "host", "t.host_id=host.id").Where("t.status = ?", Enabled).Cols(fields).Find(&list)
err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.status = ?", Enabled).Cols(fields).Find(&list)
return list, err
}
@ -94,7 +94,7 @@ func (task *Task) ActiveList() ([]TaskHost, error) {
func (task *Task) ActiveListByHostId(hostId int16) ([]TaskHost, error) {
list := make([]TaskHost, 0)
fields := "t.*, host.alias,host.name,host.username,host.password,host.port,host.auth_type,host.private_key"
err := Db.Alias("t").Join("LEFT", "host", "t.host_id=host.id").Where("t.status = ? AND t.host_id = ?", Enabled, hostId).Cols(fields).Find(&list)
err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.status = ? AND t.host_id = ?", Enabled, hostId).Cols(fields).Find(&list)
return list, err
}
@ -120,7 +120,7 @@ func (task *Task) NameExist(name string, id int) (bool, error) {
func(task *Task) Detail(id int) (TaskHost, error) {
taskHost := TaskHost{}
fields := "t.*, host.alias,host.name,host.username,host.password,host.port,host.auth_type,host.private_key"
_, err := Db.Alias("t").Join("LEFT", "host", "t.host_id=host.id").Where("t.id=?", id).Cols(fields).Get(&taskHost)
_, err := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id").Where("t.id=?", id).Cols(fields).Get(&taskHost)
return taskHost, err
}
@ -128,8 +128,8 @@ func(task *Task) Detail(id int) (TaskHost, error) {
func (task *Task) List(params CommonMap) ([]TaskHost, error) {
task.parsePageAndPageSize(params)
list := make([]TaskHost, 0)
fields := "t.*, host.alias"
session := Db.Alias("t").Join("LEFT", "host", "t.host_id=host.id")
fields := "t.*, host.alias,host.name"
session := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id")
task.parseWhere(session, params)
err := session.Cols(fields).Desc("t.id").Limit(task.PageSize, task.pageLimitOffset()).Find(&list)
@ -137,7 +137,7 @@ func (task *Task) List(params CommonMap) ([]TaskHost, error) {
}
func (task *Task) Total(params CommonMap) (int64, error) {
session := Db.Alias("t").Join("LEFT", "host", "t.host_id=host.id")
session := Db.Alias("t").Join("LEFT", hostTableName(), "t.host_id=host.id")
task.parseWhere(session, params)
return session.Count(task)
}
@ -167,4 +167,8 @@ func (task *Task) parseWhere(session *xorm.Session, params CommonMap) {
if ok && status.(int) > -1 {
session.And("status = ?", status)
}
}
func hostTableName() []string {
return []string{TablePrefix + "host", "host"}
}

View File

@ -112,6 +112,11 @@ func Store(ctx *macaron.Context, form HostForm) string {
hostModel.Remark = form.Remark
hostModel.PrivateKey = form.PrivateKey
hostModel.AuthType = form.AuthType
if hostModel.AuthType == ssh.HostPublicKey {
hostModel.Password = ""
} else {
hostModel.PrivateKey = ""
}
isCreate := false
if id > 0 {
_, err = hostModel.UpdateBean(id)

View File

@ -52,7 +52,7 @@
</select>
</div>
</div>
<div class="two fields">
<div class="two fields" id="private_key">
<div class="field">
<label>私钥 (~/.ssh/id_rsa)</label>
<div class="ui small input">
@ -60,7 +60,7 @@
</div>
</div>
</div>
<div class="four fields">
<div class="four fields" id="password">
<div class="field">
<label>SSH密码</label>
<div class="ui small input">
@ -85,6 +85,26 @@
<script type="text/javascript">
var $uiForm = $('.ui.form');
$(function() {
changeAuthType($('#authType').val());
});
function changeAuthType(type) {
// 私钥
if (type == 2) {
$('#password').hide();
$('#private_key').show();
return;
}
// 密码
$('#private_key').hide();
$('#password').show();
}
$('#authType').change(function() {
changeAuthType($(this).val());
});
registerSelectFormValidation("selectPrivateKey", $uiForm, $('#authType'), 'auth_type');
registerSelectFormValidation("selectPassword", $uiForm, $('#authType'), 'auth_type');
$($uiForm).form(

View File

@ -8,7 +8,7 @@
<a href="/task/create">
<i class="large add icon"></i>
<div class="content">
添加任务</a>
添加任务
</div>
</a>
</h3>
@ -63,9 +63,9 @@
<p class="sensorStatus">命令:{{{.Command}}}</p>
<p class="sensorStatus">超时时间:{{{if gt .Timeout 0}}}{{{.Timeout}}}秒{{{else}}}不限制{{{end}}}</p>
<p>重试次数: {{{.RetryTimes}}}</p>
<p class="sensorStatus">是否允许多实例:{{{if gt .Multi 0}}}是{{{else}}}否{{{end}}}</p>
<p class="sensorStatus">是否允许多实例运行{{{if gt .Multi 0}}}是{{{else}}}否{{{end}}}</p>
{{{if eq .Protocol 2}}}
<p>主机: {{{.Alias}}}</p>
<p>主机: {{{.Alias}}}-{{{.Name}}}</p>
{{{end}}}
<p>备注: {{{.Remark}}}</p>
</div>

View File

@ -29,8 +29,13 @@
<div class="content">
crontab表达式
<div class="ui blue message">
Linux-crontab表达式语法, 精确到秒 <br>
Linux-crontab时间表达式语法, 支持秒级任务定义 <br>
格式: 秒 分 时 天 月 周 <br>
示例:<br>
1 * * * * * 每分钟第一秒运行 <br>
*/20 * * * * * 每隔20秒运行一次 <br>
0 30 21 * * * 每天晚上21:30:00运行一次 <br>
0 0 23 * * 6 每周六晚上23:00:00 运行一次 <br>
快捷语法: <br>
@yearly 每年运行一次 <br>
@monthly 每月运行一次 <br>
@ -45,7 +50,7 @@
</div>
</label>
<div class="ui small input">
<input type="text" name="spec" value="{{{.Task.Spec}}}" />
<input type="text" name="spec" value="{{{.Task.Spec}}}" placeholder="秒 分 时 天 月 周"/>
</div>
</div>
</div>
@ -64,7 +69,7 @@
</select>
</div>
</div>
<div class="four fields">
<div class="four fields" id="hostField">
<div class="field">
<label>主机</label>
<div class="ui blue message">
@ -112,7 +117,10 @@
</div>
<div class="three fields">
<div class="field">
<label>允许多实例运行</label>
<label>允许多实例同时运行</label>
<div class="ui blue message">
前次任务未执行完成,本次任务是否执行
</div>
<select name="multi">
<option value="1"{{{if .Task}}} {{{if eq .Task.Multi 1}}}selected{{{end}}} {{{end}}}></option>
<option value="2" {{{if .Task}}} {{{if eq .Task.Multi 0}}}selected{{{end}}} {{{end}}}></option>
@ -144,6 +152,24 @@
<script type="text/javascript">
$(function() {
changeProtocol();
});
$('#protocol').change(function() {
changeProtocol();
});
function changeProtocol() {
var protocol = $('#protocol').val();
if (protocol == 2) {
$('#hostField').show();
return;
}
$('#hostField').hide();
}
$('.ui.checkbox')
.checkbox()
;