package service import ( "DataShare/dao" "DataShare/global" "DataShare/middleware" "DataShare/model" "bytes" "encoding/json" "errors" "fmt" "github.com/sirupsen/logrus" ) func UserLogin(req *model.LoginRequest)(*model.ResponseToken,error){ //1、先检查key是否存在 //2、创建token ts, err := middleware.CreateToken(req.AppId,req.AppSecret) if err != nil { return nil, errors.New("创建token失败") } // save token to redis uidValue := model.UuidValue{ AppId: req.AppId, AppSecret: req.AppSecret, } jsonUidValue,_ := json.Marshal(&uidValue) dao.DeleteTokenByUserNameAndType(req.AppId,req.AppSecret) err = dao.CreateAuth(string(jsonUidValue), ts) if err != nil { return nil, err } result := model.ResponseToken{ AccessToken: ts.AccessToken, RefreshToken: ts.RefreshToken, ExpiresAt: ts.AtExpires, } return &result, nil } func UserLogout()error{ return nil } func getDataBaseInfoById(id int)(*global.DbInfo){ for i:=0;i= total { return &model.CommonList{total,nil},nil } //分页处理 var result []global.DbInfo start := (req.PageNo-1) * req.PageSize end := start + req.PageSize if end < total { result = global.Databases[start:end] }else { result = global.Databases[start:] } tableData := make([]map[string]interface{}, 0) entry := make(map[string]interface{}) for i:=0;i0",tbName) data,_,_,_,err := dao.GetData(db,sql) if err != nil { return nil,err } return &model.CommonList{ Total: len(data), Items: data, },nil }else if dbInfo.SqlType == "kingbase" { sql := fmt.Sprintf("SELECT col_description(a.attrelid,a.attnum) as comments,pg_type.typname as type,a.attname as name, a.attnotnull as notnull" + " FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid " + "where c.relname = '%s' and a.attrelid = c.oid and a.attnum>0",tbName) data,_,_,_,err := dao.GetData(db,sql) if err != nil { return nil,err } return &model.CommonList{ Total: len(data), Items: data, },nil } return nil,nil } //删除数据库 func DatabaseDel(id int) error{ dbInfo := getDataBaseInfoById(id) if dbInfo==nil{ return errors.New("数据库不存在!") } for i:=0;i 0 { sql +="%s %s(%d)" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) args = append(args,req.Columns[n].DataLength) }else { sql +="%s %s" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) } if req.Columns[n].IsIncrement == 1 { sql += " AUTO_INCREMENT" } if req.Columns[n].IsRequired == 1 { sql += " not null" } if n == (len(req.Columns)-1){ if findPK == false { sql += "\n" break } } sql += ",\n" } for n:=0;n 0 { sql +="%s %s(%d)" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) args = append(args,req.Columns[n].DataLength) }else { sql +="%s %s" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) } if req.Columns[n].IsIncrement == 1 { sql += " IDENTITY(1,1)" } if req.Columns[n].IsRequired == 1 { sql += " not null" } if n == (len(req.Columns)-1){ if findPK == false { sql += "\n" break } } sql += ",\n" } for n:=0;n 0 { sql +="%s %s(%d)" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) args = append(args,req.Columns[n].DataLength) }else { sql +="%s %s" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) } if req.Columns[n].IsIncrement == 1 { //sql += " AUTO_INCREMENT" } if req.Columns[n].IsRequired == 1 { sql += " not null" } if n == (len(req.Columns)-1){ if findPK == false { sql += "\n" break } } sql += ",\n" } for n:=0;n 0 { sql +="%s %s(%d)" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) args = append(args,req.Columns[n].DataLength) }else { sql +="%s %s" args = append(args,req.Columns[n].ColName) args = append(args,req.Columns[n].DataType) } if req.Columns[n].IsIncrement == 1 { //sql += " AUTO_INCREMENT" } if req.Columns[n].IsRequired == 1 { sql += " not null" } if n == (len(req.Columns)-1){ if findPK == false { sql += "\n" break } } sql += ",\n" } for n:=0;n 0 { sql += " SQL_CALC_FOUND_ROWS" } args := make([]interface{},0) first := 1 if req.QueryColn == nil { sql += " *" }else{ for i:=0;i 0 { if where == 0 { sql += " where" where = 1 }else{ sql += " and" } if req.Query[zz].Cond == 0 { sql += " %s='%s'" }else if req.Query[zz].Cond == 1 { sql += " %s>'%s'" }else{ sql += " %s<'%s'" } args = append(args,req.Query[zz].ColName) args = append(args,req.Query[zz].ColVal) } } if len(req.Sort.ColName) > 0 { if req.Sort.IsDesc == 1 { sql += " order by %s desc" }else { sql += " order by %s" } args = append(args,req.Sort.ColName) } if req.PageSize > 0 { sql += " limit %d,%d" args = append(args,(req.PageNo-1)*req.PageSize) args = append(args,req.PageSize) } sqlStr := fmt.Sprintf(sql,args...) fmt.Println("------------") fmt.Println(sqlStr) fmt.Println("------------") items,_,_,total,_ := dao.GetData(dbInfo.DbHandle,sqlStr) result := &model.CommonList{ Total: total, Items: items, } return result,nil }else if dbInfo.SqlType == "dm8"{ sql := "select" sqlCount := "select count(*)" args_count := make([]interface{},0) args := make([]interface{},0) first := 1 if req.QueryColn == nil { sql += " *" }else{ for i:=0;i 0 { if where == 0 { sql += " where" sqlCount += " where" where = 1 }else{ sql += " and" sqlCount += " and" } if req.Query[zz].Cond == 0 { sql += " %s='%s'" sqlCount += " %s='%s'" }else if req.Query[zz].Cond == 1 { sql += " %s>'%s'" sqlCount += " %s>'%s'" }else{ sql += " %s<'%s'" sqlCount += " %s<'%s'" } args = append(args,req.Query[zz].ColName) args = append(args,req.Query[zz].ColVal) args_count = append(args_count,req.Query[zz].ColName) args_count = append(args_count,req.Query[zz].ColVal) } } sqlCntStr := fmt.Sprintf(sqlCount,args_count...) total,_:= dao.GetCount(dbInfo.DbHandle,sqlCntStr) if len(req.Sort.ColName) > 0 { if req.Sort.IsDesc == 1 { sql += " order by %s desc" }else { sql += " order by %s" } args = append(args,req.Sort.ColName) } if req.PageSize > 0 { sql += " limit %d,%d" args = append(args,req.PageSize) args = append(args,req.PageNo-1) } sqlStr := fmt.Sprintf(sql,args...) fmt.Println("------------") fmt.Println(sqlStr) fmt.Println("------------") items,_,_,_,_ := dao.GetData(dbInfo.DbHandle,sqlStr) result := &model.CommonList{ Total: total, Items: items, } return result,nil }else if dbInfo.SqlType == "postgres" || dbInfo.SqlType == "kingbase"{ sql := "select" sqlCount := "select count(*)" args_count := make([]interface{},0) args := make([]interface{},0) first := 1 if req.QueryColn == nil { sql += " *" }else{ for i:=0;i 0 { if where == 0 { sql += " where" sqlCount += " where" where = 1 }else{ sql += " and" sqlCount += " and" } if req.Query[zz].Cond == 0 { sql += " %s='%s'" sqlCount += " %s='%s'" }else if req.Query[zz].Cond == 1 { sql += " %s>'%s'" sqlCount += " %s>'%s'" }else{ sql += " %s<'%s'" sqlCount += " %s<'%s'" } args = append(args,req.Query[zz].ColName) args = append(args,req.Query[zz].ColVal) args_count = append(args_count,req.Query[zz].ColName) args_count = append(args_count,req.Query[zz].ColVal) } } sqlCntStr := fmt.Sprintf(sqlCount,args_count...) total,_:= dao.GetCount(dbInfo.DbHandle,sqlCntStr) if len(req.Sort.ColName) > 0 { if req.Sort.IsDesc == 1 { sql += " order by %s desc" }else { sql += " order by %s" } args = append(args,req.Sort.ColName) } if req.PageSize > 0 { sql += " limit %d offset %d" args = append(args,req.PageSize) args = append(args,req.PageNo-1) } sqlStr := fmt.Sprintf(sql,args...) fmt.Println("------------") fmt.Println(sqlStr) fmt.Println("------------") items,_,_,_,_ := dao.GetData(dbInfo.DbHandle,sqlStr) result := &model.CommonList{ Total: total, Items: items, } return result,nil } return nil,nil } //加载默认连接 func LoadDefaultDbConnect(){ if global.RedisClient == nil { global.SystemLogger.Log(logrus.ErrorLevel,"redis connect failed,load default db connect exit.") return } conn_list := dao.LoadConnectInfo() for i:=0;i