forked from jnfrati/govwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.go
86 lines (72 loc) · 1.5 KB
/
function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package idor
import (
"database/sql"
"html"
"regexp"
"github.com/govwa/util/database"
)
var DB *sql.DB
var err error
/*func init(){
DB, err = database.Connect()
if err != nil{
log.Println(err.Error())
}
}*/
type Profile struct {
Uid int
Name string
City string
PhoneNumber string
}
func NewProfile() *Profile {
return &Profile{}
}
func (p *Profile) GetData(uid string) error {
/* this funciton use to get data Profile from database with prepare statement */
DB, err = database.Connect()
const (
getProfileSql = `SELECT p.user_id, p.full_name, p.city, p.phone_number
FROM Profile as p,Users as u
where p.user_id = u.id
and u.id=?`
)
stmt, err := DB.Prepare(getProfileSql) //prepare statement
if err != nil {
return err
}
defer stmt.Close()
err = stmt.QueryRow(uid).Scan(&p.Uid, &p.Name, &p.City, &p.PhoneNumber)
if err != nil {
return err
}
return nil
}
func (p *Profile) UpdateProfile(name, city, phoneNumber, uid string) error {
DB, err = database.Connect()
const (
sql = `UPDATE Profile
set full_name=?,
city=?,
phone_number=?
where user_id=?`
)
stmt, err := DB.Prepare(sql)
if err != nil {
return err
}
res, err := stmt.Exec(name, city, phoneNumber, uid)
if err != nil {
return err
}
_, err = res.RowsAffected()
if err != nil {
return err
}
return nil
}
func HTMLEscapeString(text string) string {
filter := regexp.MustCompile("<[^>]*>")
output := filter.ReplaceAllString(text, "")
return html.EscapeString(output)
}