-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHandler.js
More file actions
112 lines (99 loc) · 3.85 KB
/
Copy pathDatabaseHandler.js
File metadata and controls
112 lines (99 loc) · 3.85 KB
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
const mysql = require('mysql2/promise');
class Database {
constructor(config) {
this.connection = null;
this.config = config;
}
async connect() {
if (!this.connection) {
try {
this.connection = await mysql.createConnection(this.config);
} catch (error) {
console.error('Erro ao conectar ao banco de dados:', error.message);
throw error;
}
}
}
async set(table, column, value) {
await this.connect();
try {
const query = `INSERT INTO \`${table}\` (\`${column}\`) VALUES (?) ON DUPLICATE KEY UPDATE \`${column}\` = VALUES(\`${column}\`)`;
await this.connection.execute(query, [value]);
} catch (error) {
console.error('Erro ao executar o método set:', error.message);
throw error;
}
}
async delete(table, column, value) {
await this.connect();
try {
const query = `DELETE FROM \`${table}\` WHERE \`${column}\` = ?`;
await this.connection.execute(query, [value]);
} catch (error) {
console.error('Erro ao executar o método delete:', error.message);
throw error;
}
}
async get(table, whereColumn, columnValue) {
await this.connect();
try {
const query = `SELECT * FROM \`${table}\` WHERE \`${whereColumn}\` = ? LIMIT 1`;
const [rows] = await this.connection.execute(query, [columnValue]);
return rows.length ? rows[0] : null;
} catch (error) {
console.error('Erro ao executar o método get:', error.message);
throw error;
}
}
async update(table, column, value, whereColumn, whereValue) {
await this.connect();
try {
const query = `UPDATE \`${table}\` SET \`${column}\` = ? WHERE \`${whereColumn}\` = ?`;
await this.connection.execute(query, [value, whereValue]);
} catch (error) {
console.error('Erro ao executar o método update:', error.message);
throw error;
}
}
async insert(table, data) {
await this.connect();
try {
const columns = Object.keys(data).map(column => `\`${column}\``).join(', ');
const placeholders = Object.keys(data).map(() => '?').join(', ');
const values = Object.values(data);
const query = `INSERT INTO \`${table}\` (${columns}) VALUES (${placeholders})`;
await this.connection.execute(query, values);
} catch (error) {
console.error('Erro ao executar o método insert:', error.message);
throw error;
}
}
async createTable(table, columns) {
await this.connect();
try {
const checkTableQuery = `SHOW TABLES LIKE '${table}'`;
const [tables] = await this.connection.execute(checkTableQuery);
if (tables.length > 0) return;
const columnDefinitions = Object.entries(columns)
.map(([column, type]) => `\`${column}\` ${type}`)
.join(', ');
const query = `CREATE TABLE \`${table}\` (${columnDefinitions})`;
await this.connection.execute(query);
} catch (error) {
console.error('Erro ao executar o método createTable:', error.message);
throw error;
}
}
async close() {
if (this.connection) {
try {
await this.connection.end();
} catch (error) {
console.error('Erro ao fechar a conexão com o banco de dados:', error.message);
} finally {
this.connection = null;
}
}
}
}
module.exports = Database;