-
Notifications
You must be signed in to change notification settings - Fork 12
/
crm_database_operations.go
180 lines (149 loc) · 3.97 KB
/
crm_database_operations.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt,database/sql, net/http, text/template package
import (
// "fmt"
"database/sql"
// "log"
// "net/http"
// "text/template"
// "errors"
_ "github.com/go-sql-driver/mysql"
)
type Customer struct {
CustomerId int
CustomerName string
SSN string
}
func GetConnection() (database *sql.DB) {
databaseDriver := "mysql"
databaseUser := "newuser"
databasePass := "newuser"
databaseName := "crm"
database, error := sql.Open(databaseDriver, databaseUser+":"+databasePass+"@/"+databaseName)
if error != nil {
panic(error.Error())
}
return database
}
func GetCustomerById(customerId int) Customer {
var database *sql.DB
database = GetConnection()
var error error
var rows *sql.Rows
rows, error = database.Query("SELECT * FROM Customer WHERE CustomerId=?", customerId)
if error != nil {
panic(error.Error())
}
//fmt.Println(rows)
var customer Customer
customer = Customer{}
for rows.Next() {
var customerId int
var customerName string
var SSN string
error = rows.Scan(&customerId, &customerName, &SSN)
if error != nil {
panic(error.Error())
}
customer.CustomerId = customerId
customer.CustomerName = customerName
customer.SSN = SSN
}
defer database.Close()
return customer
}
func GetCustomers() []Customer {
var database *sql.DB
database = GetConnection()
var error error
var rows *sql.Rows
rows, error = database.Query("SELECT * FROM Customer ORDER BY Customerid DESC")
if error != nil {
panic(error.Error())
}
var customer Customer
customer = Customer{}
var customers []Customer
customers = []Customer{}
for rows.Next() {
var customerId int
var customerName string
var ssn string
error = rows.Scan(&customerId, &customerName, &ssn)
if error != nil {
panic(error.Error())
}
customer.CustomerId = customerId
customer.CustomerName = customerName
customer.SSN = ssn
customers = append(customers, customer)
}
defer database.Close()
return customers
}
func InsertCustomer(customer Customer) {
var database *sql.DB
database = GetConnection()
var error error
var insert *sql.Stmt
insert, error = database.Prepare("INSERT INTO CUSTOMER(CustomerName,SSN) VALUES(?,?)")
if error != nil {
panic(error.Error())
}
insert.Exec(customer.CustomerName, customer.SSN)
//log.Println("INSERT: Customer Name: " + customer.name + " | SSN: " + customer.ssn)
defer database.Close()
//return Customer{}
}
func UpdateCustomer(customer Customer) {
var database *sql.DB
database = GetConnection()
var error error
var update *sql.Stmt
update, error = database.Prepare("UPDATE CUSTOMER SET CustomerName=?, SSN=? WHERE CustomerId=?")
if error != nil {
panic(error.Error())
}
update.Exec(customer.CustomerName, customer.SSN, customer.CustomerId)
//log.Println("INSERT: Customer Name: " + customer.name + " | SSN: " + customer.ssn)
defer database.Close()
//return Customer{}
}
func DeleteCustomer(customer Customer) {
var database *sql.DB
database = GetConnection()
var error error
var delete *sql.Stmt
delete, error = database.Prepare("DELETE FROM Customer WHERE Customerid=?")
if error != nil {
panic(error.Error())
}
delete.Exec(customer.CustomerId)
//log.Println("INSERT: Customer Name: " + customer.name + " | SSN: " + customer.ssn)
defer database.Close()
//return Customer{}
}
/*func main() {
var customers []Customer
customers = GetCustomers()
fmt.Println(customers)
// var customer Customer
// customer.CustomerName = "Thomas Smith"
// customer.SSN = "2323343"
// InsertCustomer(customer)
//var customer Customer
// customer.CustomerName = "George Thompson"
// customer.SSN = "23233432"
// customer.CustomerId = 2
// UpdateCustomer(customer)
var customer Customer
//customer.CustomerName = "George Thompson"
//customer.SSN = "23233432"
customer.CustomerId = 2
deleteCustomer(customer)
customers = GetCustomers()
fmt.Println(customers)
}
*/