Пример #1
0
//把对应的map插入到数据库
func Insert(db *sql.DB, data map[string]string) int64 {
	/*
		if db1 == nil {
			db1.Connect(config.DB1)
		}
		if db2 == nil {
			db2.Connect(config.DB2)
		}
	*/
	//sql := "INSERT error"
	keys := ""
	values := ""
	//values := make([]string, len(data))
	//cnt := 0
	for k, v := range data {
		keys += "`" + k + "`,"
		values += "'" + v + "',"
	}
	keys = strings.Trim(keys, ",")
	values = strings.Trim(values, ",")
	sql := "INSERT INTO error (" + keys + ") VALUES (" + values + ")"
	Println(sql)
	res, err := db.Exec(sql)
	model.CheckErr(err, "插入数据库失败")
	if err == nil {
		return -1
	}
	id, err := res.LastInsertId()
	model.CheckErr(err, "获取插入id失败")
	return id
}
Пример #2
0
//只获取一行数据
// @return []string  字段名
// @return []string  值
func QueryOneRow(db *sql.DB, query string) ([]string, []string) {
	//data := make(map[int][]string)
	var data []string
	rows, err := db.Query(query)
	defer rows.Close()
	model.CheckErr(err, "查询失败, query is "+query)
	Columns, _ := rows.Columns()
	//Println(Columns)
	scanArgs := make([]interface{}, len(Columns))
	values := make([]interface{}, len(Columns))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	cnt := 0
	for rows.Next() {
		model.CheckErr(rows.Scan(scanArgs...), "获取行失败")
		if len(values) == 1 && values[0] == nil {
			break
		}
		data = make([]string, len(scanArgs))
		for i, col := range values {
			//Println(col)
			data[i] = string(col.([]byte))
			cnt++
		}
		return Columns, data
		//每行必须只能有一个,结果才能只是一个Arr,不是getOneRow的情况
	}
	return Columns, data
}
Пример #3
0
//单纯获取value,不要k,如show tables这类命令
//select的时候,必须全部取出来
func QueryLines(db *sql.DB, query string) ([]string, map[int][]string) {
	//data := make(map[int][]string)
	data := make(map[int][]string, 64)
	rows, err := db.Query(query)
	defer rows.Close()
	model.CheckErr(err, "查询失败")
	Columns, _ := rows.Columns()
	//Println(Columns)
	scanArgs := make([]interface{}, len(Columns))
	values := make([]interface{}, len(Columns))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	cnt := 0
	for rows.Next() {
		model.CheckErr(rows.Scan(scanArgs...), "获取行失败")

		lineBuf := make([]string, len(scanArgs))
		for i, col := range values {
			if col != nil {
				lineBuf[i] = string(col.([]byte))
			}
		}
		//data = Rom.ExtendMapStringArr(data)
		data[cnt] = lineBuf
		cnt++
	}
	return Columns, data
	//return &res
}
Пример #4
0
//连接数据库
func Connect(config map[string]string) (db *sql.DB, err error) {
	//func Connect(db *sql.DB, config map[string]string) db *sql.DB {
	//var err error
	//Println(config["user"] + ":" + config["passwd"] + "@tcp(" + config["host"] + ":" + config["port"] + ")/" + config["name"])
	//
	db, err = sql.Open("mysql", config["user"]+":"+config["passwd"]+"@tcp("+config["host"]+":"+config["port"]+")/"+config["dbName"])
	//db, err = sql.Open("mysql", config.DB_USER+":"+config.DB_PASSWORD+"@tcp("+config.DB_HOST+":"+config.DB_PORT+")/"+config.DB_NAME)
	//强行连接,判断是不是连接成功
	model.CheckErr(err, "open数据库失败")
	model.CheckErr(db.Ping(), "ping数据库失败")
	return db, nil
}
Пример #5
0
//查询数据库
//func Query(query string) (r Result) {
//select的时候,必须全部取出来
func Query(db *sql.DB, query string) (r *Result) {
	var res Result
	var err error
	rows, err := db.Query(query)
	model.CheckErr(err, "查询失败")
	//Keys, _ := rows.Columns()
	/*
		for k, col := range res.Keys {
			fmt.Printf("%d \t %10s \n", k, col)
		}
	*/
	cnt := 0
	tmap := make(map[int]Field)
	for rows.Next() {
		var tmp Field
		rows.Scan(&tmp.Id, &tmp.Symbol, &tmp.Item, &tmp.Value, &tmp.Times)
		tmap[cnt] = tmp
		cnt++
		Printf("%d\n", cnt)
	}
	cnt = 0
	for rows.Next() {
		Printf("%d\n", cnt)
	}
	res.Fields = tmap
	defer rows.Close()
	return &res
}
Пример #6
0
/*
func unescaped(x string) interface{} { return template.HTML(x) }
func renderTemplate(w http.ResponseWriter, tmpl string, view *Page) {
	t := template.New("")
	t = t.Funcs(template.FuncMap{"unescaped": unescaped})
	t, err := t.ParseFiles("view.html", "edit.html")
	err = t.ExecuteTemplate(w, tmpl+".html", view)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
*/
func Index(w http.ResponseWriter, r *http.Request) {

	//Println(r.Header["Sec-Websocket-Version"][0])
	if r.Proto != "HTTP/1.1" || r.Header["Sec-Websocket-Version"][0] != "13" {
		Fprintf(w, "request proto is not permit")
		return
	}
	defer func() {
		if r := recover(); r != nil {
			model.CheckRecover()
		}
	}()
	var upgrader = websocket.Upgrader{
		ReadBufferSize: 1024, WriteBufferSize: 1024,
		//HandshakeTimeout: HANDSHAKE,
		//握手时间超过3s超时
		//不再检查请求源
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
	r.ParseForm()
	model.Form = r.Form
	if len(r.Form["pid"]) == 0 {
		Println("No pid")
		return
	}
	if model.Isint("pid", r.Form["pid"][0]) == false {
		Println("aid is not int")
		return
	}

	pid, err := strconv.Atoi(r.Form["pid"][0])
	model.CheckErr(err, "转换失败")

	conn, err := upgrader.Upgrade(w, r, nil)
	model.CheckErr(err, "升级失败")
	if err != nil {
		Fprintf(w, "upgrader faied")
		return
	}
	wb.Send("one more")
	wb.StartNew(pid, "", conn)
}
Пример #7
0
//单纯获取value,不要k,如show tables这类命令,结果只有一列多行
//select的时候,必须全部取出来
func GetArr(db *sql.DB, query string) ([]string, int) {
	buff := make([]string, 16, 32)
	//var res []string
	rows, err := db.Query(query)
	defer rows.Close()
	model.CheckErr(err, "查询失败")

	Columns, _ := rows.Columns()
	cnum := len(Columns)
	scanArgs := make([]interface{}, cnum)
	values := make([]interface{}, cnum)
	for i := range values {
		scanArgs[i] = &values[i]
	}
	idx := 0
	cnt := 0
	line := make([]string, cnum)
	for rows.Next() {
		model.CheckErr(rows.Scan(scanArgs...), "获取行失败")
		cnt = 0
		//Println(len(values))
		//Println(values)
		if len(values) == 1 && values[0] == nil {
			break
		}
		for _, col := range values {
			//Println(col)
			line[cnt] = string(col.([]byte))
			cnt++
		}
		//每行必须只能有一个,结果才能只是一个Arr,不是getOneRow的情况
		buff = Rom.ExtendStringArr(buff)
		if cnt == 1 {
			buff = buff[0 : idx+1]
			buff[idx] = line[0]
			idx++
		}
	}
	return buff, idx
}
Пример #8
0
func Home(w http.ResponseWriter, r *http.Request) {
	//t, err := template.ParseFiles("/home/jiamin1/test.html")
	//Println(config.VIEW + "inspect.html")
	t, err := template.ParseFiles(config.VIEW + "inspect.html")
	//href.GetHost(r.URL)
	model.CheckErr(err, "解析模板失败")
	str := wb.Report()
	//str := "hello,world"
	type Data struct {
		Str template.HTML
	}
	t.Execute(w, Data{Str: template.HTML(str)})
}