public_host 4 years ago
parent
commit
7331b28a49
  1. 48
      hasaki-gen-crud/hasaki-gen-crud.js
  2. 194
      test.go

48
hasaki-gen-crud/hasaki-gen-crud.js

@ -35,31 +35,40 @@ try {
var stack = ['{']
var endLine = 0
var endChar = 0
for (var i = start + 1; i < context_arr.length; i++) {
for (let k = 0; k < context_arr[i].length; k++) {
const element = context_arr[i][k];
if (element == '{') {
stack.push('{')
}
if (element == '}') {
stack.pop()
}
if (stack.length == 0) {
endLine = i
endChar = k
break
var endChar = 0;
(function () {
for (var i = start + 1; i < context_arr.length; i++) {
for (let k = 0; k < context_arr[i].length; k++) {
const element = context_arr[i][k];
console.log(element)
if (element == '{') {
stack.push('{')
console.log(stack)
}
if (element == '}') {
stack.pop()
console.log(stack)
}
if (stack.length == 0) {
endLine = i
endChar = k + 1
return
}
}
}
})()
}
if (endLine < start) {
vscode.window.showErrorMessage("结构异常")
return
}
let endPosition = vscode.Position(endLine, endChar)
let endPosition = new vscode.Position(endLine, endChar)
axios({
method: 'get',
url: 'http://git.ouxuan.net/3136352472/vscode-helper-js/raw/branch/master/hasaki-gen-crud/template.go',
@ -72,9 +81,9 @@ try {
text = tmp[1]
text = text.replace(/Temp3A60/g, struct)
console.log(text)
console.log(endPosition)
vscode.window.activeTextEditor.edit(function (textEditorEdit: vscode.TextEditorEdit) {
vscode.window.activeTextEditor.edit(function (textEditorEdit) {
textEditorEdit.insert(
endPosition,
text,
@ -82,6 +91,7 @@ try {
})
// vscode.window.showErrorMessage(res)
}).catch(error => {
vscode.window.showErrorMessage(`err:${error.message}`)
})

194
test.go

@ -1,5 +1,199 @@
package main
import (
"fmt"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin"
)
type MyTestr struct {
Id int
}
func (that *MyTestr) where() (q interface{}, args []interface{}) {
//todo 根据需求修改
return "`id`=?", []interface{}{
that.Id,
}
}
func (that *MyTestr) Save(isAllCols ...bool) (err error) {
tmp := MyTestr{}
whereQuery, whereArgs := that.where()
hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&tmp)
if tmp.Id == 0 {
_, err = hskdb.GetXormAuto().Insert(that)
} else {
if len(isAllCols) > 0 && isAllCols[0] {
_, err = hskdb.GetXormAuto().AllCols().Where(whereQuery, whereArgs...).Update(that)
} else {
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Update(that)
}
}
return err
}
func (that *MyTestr) Load() (err error) {
whereQuery, whereArgs := that.where()
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&that)
return
}
func (that *MyTestr) Data() (data []MyTestr) {
hskdb.GetXormAuto().OrderBy("id desc").Find(&data)
return
}
func (that *MyTestr) PageData(page, pageSize int) (data []MyTestr, count int64) {
count, _ = hskdb.GetXormAuto().OrderBy("id desc").Limit(pageSize, page*pageSize).FindAndCount(&data)
return
}
func (that *MyTestr) Delete() (err error) {
whereQuery, whereArgs := that.where()
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Delete(&that)
return
}
//该方法请不要放在db文件中
func InitializationMyTestr(router *hskgin.GinHelper) {
var authority = func(ctx *hskgin.GinContextHelper) {
//todo 权限控制
}
router.Any("/?????/MyTestr/get", func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr{}
ctx.Bind(&req)
err := req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
router.Any("/?????/MyTestr/save", func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr{}
ctx.Bind(&req)
err := req.Save()
ctx.CheckErrDisplayByError(err)
err = req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
router.Any("/?????/MyTestr/data", func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr{}
ctx.Bind(&req)
data := req.Data()
ctx.DisplayByData(data)
})
router.Any("/?????/MyTestr/list", func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr{}
ctx.Bind(&req)
page := ctx.ParamRequired2Page(true)
data, count := req.PageData(page, 10)
ctx.DisplayByPage(data, count)
})
}
type MyTestr2 struct {
Id int
}
type MyTestr3 struct {
Id int
Mi int
}
func (that *MyTestr3) where() (q interface{}, args []interface{}) {
//todo 根据需求修改
return "`id`=?", []interface{}{
that.Id,
}
}
func (that *MyTestr3) Save(isAllCols ...bool) (err error) {
tmp := MyTestr3{}
whereQuery, whereArgs := that.where()
hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&tmp)
if tmp.Id == 0 {
_, err = hskdb.GetXormAuto().Insert(that)
} else {
if len(isAllCols) > 0 && isAllCols[0] {
_, err = hskdb.GetXormAuto().AllCols().Where(whereQuery, whereArgs...).Update(that)
} else {
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Update(that)
}
}
return err
}
func (that *MyTestr3) Load() (err error) {
whereQuery, whereArgs := that.where()
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&that)
return
}
func (that *MyTestr3) Data() (data []MyTestr3) {
hskdb.GetXormAuto().OrderBy("id desc").Find(&data)
return
}
func (that *MyTestr3) PageData(page, pageSize int) (data []MyTestr3, count int64) {
count, _ = hskdb.GetXormAuto().OrderBy("id desc").Limit(pageSize, page*pageSize).FindAndCount(&data)
return
}
func (that *MyTestr3) Delete() (err error) {
whereQuery, whereArgs := that.where()
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Delete(&that)
return
}
//该方法请不要放在db文件中
func InitializationMyTestr3(prefix string, router *hskgin.GinHelper) {
var authority = func(ctx *hskgin.GinContextHelper) {
//todo 权限控制
}
router.Any(fmt.Sprintf("/%s/MyTestr3/get", prefix), func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr3{}
ctx.Bind(&req)
err := req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
router.Any(fmt.Sprintf("/%s/MyTestr3/save", prefix), func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr3{}
ctx.Bind(&req)
err := req.Save()
ctx.CheckErrDisplayByError(err)
err = req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
router.Any(fmt.Sprintf("/%s/MyTestr3/data", prefix), func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr3{}
ctx.Bind(&req)
data := req.Data()
ctx.DisplayByData(data)
})
router.Any(fmt.Sprintf("/%s/MyTestr3/list", prefix), func(ctx *hskgin.GinContextHelper) {
authority(ctx)
req := MyTestr3{}
ctx.Bind(&req)
page := ctx.ParamRequired2Page(true)
data, count := req.PageData(page, 10)
ctx.DisplayByPage(data, count)
})
}
Loading…
Cancel
Save