add sql support

master
兔子 6 years ago
parent cacf168a5b
commit 99f10c8fa9

@ -2,23 +2,35 @@ package starainrt
import (
"bufio"
"bytes"
"crypto/md5"
"crypto/rc4"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"database/sql"
"errors"
"encoding/base64"
"encoding/hex"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var HttpNul, HttpNul2 map[string]string
var HttpTimeOut int64 = 15
var DBRes *sql.DB
var DBRows *sql.Rows
func Exists(filepath string) bool {
_, err := os.Stat(filepath)
if err != nil && os.IsNotExist(err) {
@ -669,3 +681,186 @@ func AttachFile(source, target, output string) (bool, string) {
fp.Close()
return true, strconv.FormatInt(filesize, 10)
}
func CurlGet(url string) (error, string) {
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
return err, res
}
func CurlPost(url, postdata string) (error, string) {
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
return err, res
}
func HttpNulReset() {
var tmp map[string]string
HttpNul, HttpNul2 = tmp, tmp
}
func Curl(url string, postdata string, header map[string]string, cookie map[string]string, method string) (error, int, string, http.Header, []*http.Cookie) {
var req *http.Request
if method == "" {
if len(postdata) != 0 {
method = "POST"
} else {
method = "GET"
}
}
BytePostData := []byte(postdata)
if postdata == "" || len(postdata) == 0 {
req, _ = http.NewRequest(method, url, nil)
} else {
req, _ = http.NewRequest(method, url, bytes.NewBuffer(BytePostData))
}
if (len(header) == 0 || header == nil) && method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
for k, v := range header {
req.Header.Set(k, v)
}
if len(cookie) != 0 {
for k, v := range cookie {
cookie1 := &http.Cookie{Name: k, Value: v, HttpOnly: true}
req.AddCookie(cookie1)
}
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(time.Duration(HttpTimeOut) * time.Second)
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(HttpTimeOut))
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
}}
resp, err := client.Do(req)
var rte []*http.Cookie
if err != nil {
return err, 0, "", req.Header, rte
}
defer resp.Body.Close()
statuscode := resp.StatusCode
hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
return nil, statuscode, string(body), hea, resp.Cookies()
}
func FetchAll(rows *sql.Rows)(error,map[int]map[string]string){
var ii int=0
records := make(map[int]map[string]string)
columns, err:= rows.Columns()
if err!=nil {
return err,records
}
scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
for rows.Next() {
if err := rows.Scan(scanArgs...);err!=nil{
return err,records
}
record := make(map[string]string)
for i, col := range values {
switch vtype:=col.(type){
case int64:
record[columns[i]] = strconv.FormatInt(vtype,10)
default:
record[columns[i]] = string(vtype.([]byte))
}
}
records[ii]=record
ii++
}
return nil,records
}
func OpenDB(Method,ConnStr string)error{
var err error
DBRes,err=sql.Open(Method,ConnStr)
return err
}
func CloseDB(){
DBRes.Close()
DBRows.Close()
}
func Query(args ...interface{})(error,map[int]map[string]string){
var err error
records := make(map[int]map[string]string)
if err=DBRes.Ping();err!=nil{
return err,records
}
if len(args)==0 {
return errors.New("no args"),records
}
if(len(args)==1){
sql:=args[0]
if DBRows,err=DBRes.Query(sql.(string));err!=nil{
return err,records
}
return FetchAll(DBRows)
}
sql:=args[0]
stmt,err:=DBRes.Prepare(sql.(string))
if err!=nil{
return err,records
}
var para []interface{}
for k,v:=range args{
if k!=0{
switch vtype:=v.(type){
default:
para=append(para,vtype)
}
}
}
if DBRows,err=stmt.Query(para...);err!=nil{
return err,records
}
return FetchAll(DBRows)
}
func DBExec(args ...interface{})(error){
var err error
if err=DBRes.Ping();err!=nil{
return err
}
if len(args)==0 {
return errors.New("no args")
}
if(len(args)==1){
sql:=args[0]
if _,err=DBRes.Exec(sql.(string));err!=nil{
return err
}
return nil
}
sql:=args[0]
stmt,err:=DBRes.Prepare(sql.(string))
if err!=nil{
return err
}
var para []interface{}
for k,v:=range args{
if k!=0{
switch vtype:=v.(type){
default:
para=append(para,vtype)
}
}
}
if _,err=stmt.Exec(para...);err!=nil{
return err
}
return nil
}
Loading…
Cancel
Save