-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-mysql-crud.go
103 lines (90 loc) · 2.41 KB
/
go-mysql-crud.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package database
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"html"
"strings"
)
// Database provides functionalities for database interactions.
//
// Author: Mokter Hossain
// Email: [email protected]
// Website: www.gglink.uk
// GitHub: https://github.com/moktermd08
// LinkedIn: https://www.linkedin.com/in/mr-mokter/
// Twitter: https://twitter.com/moGGLink
//
type Database struct {
Host string
User string
Password string
DBName string
Connection *sql.DB
}
// Connect establishes a database connection.
//
// It returns true on successful connection, false on failure
// and may return an error if connection to the database fails.
//
func (db *Database) Connect() (bool, error) {
connString := fmt.Sprintf("%s:%s@tcp(%s)/%s", db.User, db.Password, db.Host, db.DBName)
conn, err := sql.Open("mysql", connString)
if err != nil {
return false, err
}
db.Connection = conn
return true, nil
}
// Disconnect closes the database connection.
//
// It returns true on success and false on failure.
//
func (db *Database) Disconnect() bool {
if db.Connection != nil {
err := db.Connection.Close()
return err == nil
}
return false
}
// SanitizeInput sanitizes user input.
//
// It takes a string (user input data) and returns a sanitized version of that string.
//
func (db *Database) SanitizeInput(data string) string {
data = strings.TrimSpace(data)
data = strings.Replace(data, "\\", "\\\\", -1)
data = html.EscapeString(data)
if db.Connection != nil {
data = db.Connection.Escape(data)
}
return data
}
// SanitizeOutput sanitizes output.
//
// It takes a string (the data to be sent to the client) and returns a sanitized version of that string.
//
func (db *Database) SanitizeOutput(data string) string {
return html.EscapeString(data)
}
// MySQLDatabase is a concrete implementation of the Database struct for MySQL databases.
//
// You can implement methods specific to MySQLDatabase here.
// It embeds all methods from the Database struct.
//
type MySQLDatabase struct {
Database
}
// Example usage:
// db := &database.MySQLDatabase{}
// db.Host = "localhost"
// db.User = "user"
// db.Password = "password"
// db.DBName = "database"
// connected, err := db.Connect()
// if err != nil {
// fmt.Println("Failed to connect to the database:", err)
// }
// fmt.Println("Connected:", connected)
// fmt.Println("Sanitized Input:", db.SanitizeInput("user' OR '1'='1"))
// db.Disconnect()