-
Notifications
You must be signed in to change notification settings - Fork 0
/
preparedStatement.js
156 lines (121 loc) · 4.37 KB
/
preparedStatement.js
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
// til prepared statements - ikke sikkert det fungerer til mssql
// https://kode-blog.io/nodejs-database-mysql
var mssql = require('mssql');
require('console.table');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database : ''
});
var sql_stmt = "";
function getArgument(argument){
var index = process.argv.indexOf(argument);
return (index === -1) ? null : process.argv[index + 1];
}
connection.connect(function(error){
if(error){
console.log();
console.log('The following error occured while trying to connect to MSSQL ' + error.message);
return;
}
console.log();
console.log('Connection to MSSQL established successfully');
});
function listProducts(){
sql_stmt = "SELECT * FROM TProducts;";
connection.query(sql_stmt,function (err, rows){
console.log();
console.log("Products Listing");
console.log();
console.table(rows);
console.log("Total rows returned: " + rows.length);
});
}
function addProduct(){
var cName = getArgument('--cName');
var cDescription = getArgument('--cDescription');
var nUnitPrice = getArgument('--nUnitPrice');
var nStock = getArgument('--nStock');
// var nAverageRating = getArgument('--nAverageRating'); // skal denne med´????
sql_stmt = "INSERT INTO TProduct(cName,cDescription,nUnitPrice,nStock) VALUES (?,?,?,?)";
var values = [cName, cDescription, nUnitPrice,nStock];
sql_stmt = mssql.format(sql_stmt, values); // I tvivl om denne fungerer??? ændret fra -> sql_stmt = mysql.format(sql_stmt, values);
connection.query(sql_stmt, function (error, result) {
if (error) {
console.log('The following error occured while trying to insert a new record ' + error.message);
}
console.log();
console.log('Created new product with id ' + result.insertId);
})
}
function updateProduct (){
var nProductId = getArgument('--nProductId');
var cName = getArgument('--cName');
var cDescription = getArgument('--cDescription');
var nUnitPrice = getArgument('--nUnitPrice');
var nStock = getArgument('--nStock');
var nAverageRating = getArgument('--nAverageRating');
sql_stmt = "UPDATE TProduct SET cName = ?,cDescription = ?,nUnitPrice = ?,nStock = ?,nAverageRating = ?, WHERE nProductId = ?";
var values = [cName, cDescription, nUnitPrice, nStock, nAverageRating, nProductId];
sql_stmt = mssql.format(sql_stmt, values);
connection.query(sql_stmt, function (error, result) {
if (error) {
console.log('The following error occured while trying to insert a new record ' + error.message);
}
console.log();
console.log('Updated Product with id ' + nProductId);
})
}
function deleteProducts(){
var id = getArgument('--nProductId');
sql_stmt = "DELETE FROM TProduct WHERE nProductId = ?";
var nProductId = [id];
sql_stmt = mssql.format(sql_stmt, nProductId);
connection.query(sql_stmt, function (error, result) {
if (error) {
console.log('The following error occured while trying to insert a new record ' + error.message);
}
console.log();
console.log('Deleted Product with id ' + id);
})
}
app.get('/product', function (req, res) {
const id = req.query.id;
sql_stmt = "SELECT * FROM TProduct WHERE nProductId = " + id;
sqlInstance.connect(configDB, function (err) {
if (err) console.log(err);
// create Request object
var request = new sqlInstance.Request();
// query to the database and get the products
request.query(sql_stmt, function (err, product) {
if (err) console.log(err)
// send records as a response
console.log(product);
res.status(200).json({
product: product
});
});
})
});
var action = getArgument('--action');
switch(action){
case "add":
addRecord();
break;
case "update":
updateRecord();
break;
case "delete":
deleteRecord();
break;
}
listProducts();
connection.end(function(error) {
if (error){
console.log('The following error occured while trying to connect to MSSQL ' + error.message);
}else{
console.log();
console.log('Connection to MSSQL established closed');
}
});