Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
343 lines (283 loc) · 10.3 KB
/
Copy pathindex.js
File metadata and controls
343 lines (283 loc) · 10.3 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Global import
import chalk from "chalk";
import inquirer from "inquirer";
import gradient from "gradient-string";
import figlet from "figlet";
import { createSpinner } from "nanospinner";
import mysql from 'mysql2/promise';
let DatabaseNameUse = 'mysql';
// POO Database request / information
class DatabaseManager {
constructor(table) {
this.connection = null;
this.databaseSelected = null;
this.table = table;
}
async connect(database = DatabaseNameUse) {
this.connection = await mysql.createConnection({
host: 'localhost',
user: '****',
password: '****',
database: database,
});
this.databaseSelected = database;
}
async query(sql, params = []) {
const spinner = createSpinner('Loading...').start();
try {
const [rows] = await this.connection.execute(sql, params);
await new Promise(resolve => setTimeout(resolve, 1000));
spinner.success({ text: 'Query completed successfully!' });
return rows;
} catch (error) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.error(chalk.red('SQL Error:'), error.message);
spinner.error({ text: 'Query failed!' });
return [];
}
}
getDatabase() {
return this.databaseSelected || 'none';
}
getTable() {
return this.table || 'none';
}
setTable(table) {
this.table = table;
}
async changeDatabase(database) {
await this.connect(database);
}
}
const dbManager = new DatabaseManager();
async function RequestSQL(command) {
if (!command.includes('melsql -r -') && !command.startsWith('getMethod')) {
return;
}
const parts = command.split('-r -');
let optionRequest = parts[1]?.split(' ')[0]?.trim();
if (command.startsWith('getMethod')) {
optionRequest = command.split(' ')[1]?.trim();
if (!optionRequest) {
console.log(chalk.red('Veuillez fournir une colonne pour getMethod. Exemple: getMethod email'));
return;
}
}
if (!optionRequest) {
console.log(chalk.red('Invalid request format.'));
return;
}
let limitClause = '';
if (command.includes('-l -')) {
const limitParts = command.split('-l -');
const optionLimit = limitParts[1]?.trim();
if (optionLimit && !isNaN(optionLimit)) {
limitClause = ` LIMIT ${optionLimit}`;
} else {
console.log(chalk.red('Invalid limit format.'));
return;
}
}
try {
let data;
let columnName;
if (command.startsWith('melsql -r -database')) {
data = await dbManager.query('SHOW DATABASES');
columnName = 'Database';
} else if (command.startsWith('melsql -r -tables')) {
data = await dbManager.query('SHOW TABLES');
columnName = 'Tables';
} else if (command.startsWith('getMethod')) {
const table = dbManager.getTable();
data = await dbManager.query(`SELECT \`${optionRequest}\` FROM \`${table}\`${limitClause}`);
columnName = optionRequest;
} else {
data = await dbManager.query(`SELECT \`${optionRequest}\` FROM mysql.user${limitClause}`);
columnName = optionRequest;
}
if (data.length === 0) {
console.log(chalk.yellow('No data found.'));
return;
}
let maxLength = columnName.length;
data.forEach((row) => {
const keys = Object.keys(row);
const realKey = keys[0];
const value = String(row[realKey] ?? `Unknown ${columnName}`);
if (value.length > maxLength) {
maxLength = value.length;
}
});
const border = '+-' + '-'.repeat(maxLength) + '-+';
const header = '| ' + columnName.padEnd(maxLength) + ' |';
console.log('');
console.log(gradient.rainbow(border));
console.log(gradient.rainbow(header));
console.log(gradient.rainbow(border));
data.forEach((row) => {
const keys = Object.keys(row);
const realKey = keys[0];
const value = String(row[realKey] ?? `Unknown ${columnName}`);
console.log(gradient.rainbow('| ' + value.padEnd(maxLength) + ' |'));
});
console.log(gradient.rainbow(border));
console.log(chalk.yellow(`${data.length} rows in set`));
console.log('');
} catch (err) {
console.log(chalk.red('Failed to execute SQL request.'));
console.error(err.message);
}
}
async function displayWelcome() {
const message = 'melsql';
return new Promise((resolve, reject) => {
figlet(message, (err, data) => {
if (err) {
console.log('Figlet Error....');
console.dir(err);
reject(err);
return;
}
console.log(gradient.rainbow(data));
const infos = [
'Version 1.1',
'[+] Tool Created by Isoka (???)',
'.:. Write Any Command for your Database .:.',
'⚠️ No (post and update) implémentation ⚠️',
'[01] Melsql Start request: melsql -start',
'[02] Show All Command: melsql -cmd',
'[03] Exit Melsql: melsql -exit',
'© 2025 Isoka. All rights reserved.'
];
infos.forEach(info => {
console.log(gradient.rainbow(" " + info));
console.log('');
});
resolve();
});
});
}
async function getTablesOfBase() {
const data = await dbManager.query(`SHOW TABLES`);
const tables = [];
data.forEach(row => {
const tableName = Object.values(row)[0];
tables.push(tableName);
});
return tables;
}
async function getColumnOfBase(table) {
const columns = await dbManager.query(`SHOW COLUMNS FROM \`${table}\`;`);
return columns.map(col => col.Field);
}
async function StartRequestOption() {
const tables = await getTablesOfBase();
const answersTables = await inquirer.prompt({
name: 'tables',
type: 'list',
message: 'Select your table...',
choices: tables,
});
const selectedTable = answersTables.tables;
dbManager.setTable(selectedTable);
const columns = await getColumnOfBase(selectedTable);
const answersColumn = await inquirer.prompt({
name: 'column',
type: 'list',
message: 'Select your column...',
choices: columns,
});
const selectedColumn = answersColumn.column;
const answersMethod = await inquirer.prompt({
name: 'method',
type: 'list',
message: 'Select your method...',
choices: [
'Get',
'Delete',
],
});
if (answersMethod.method === 'Get') {
const command = `getMethod ${selectedColumn}`;
await RequestSQL(command);
} else if (answersMethod.method === 'Delete') {
const answersId = await inquirer.prompt({
name: 'id',
type: 'input',
message: 'Write the id(number) to delete from table "' + selectedTable + '"...',
default: '1',
});
const deleteValue = answersId.id.trim();
const deleteQuery = `DELETE FROM \`${selectedTable}\` WHERE ID = ?`;
const result = await dbManager.query(deleteQuery, [deleteValue]);
if (result.affectedRows > 0) {
console.log(chalk.green(`${result.affectedRows} row(s) deleted from "${selectedTable}".`));
} else {
console.log(chalk.yellow(`No matching rows found in "${selectedTable}".`));
}
}else {
console.log(chalk.red('Méthode non encore implémentée.'));
}
}
async function BasicCommandPrompt() {
await dbManager.connect();
const commandCheck = [
'[01] Start Request Interaction : melsql -start',
'[02] Show All Available Commands : melsql -cmd',
'[03] Exit Melsql Prompt : melsql -exit',
'[04] Clear Console Output : melsql -clear',
'[05] Show All Databases : melsql -r -database',
'[06] Show Tables in Current Database : melsql -r -tables',
'[07] Switch to Another Database : melsql -use <database_name>',
'[08] Read Column From Table : melsql -r -<column_name> [-l -<limit>]'
];
while (true) {
const answersInput = await inquirer.prompt({
name: 'command',
type: 'input',
message: `PS C:\\Melsql [(${dbManager.getDatabase()})]>`,
default: 'command',
});
const command = answersInput.command.trim();
if (command === 'melsql -exit') {
console.log(gradient.rainbow('Exiting melsql prompt...'));
process.exit();
}
else if (command === 'melsql -clear') {
console.clear();
console.log(gradient.rainbow('Console cleared...'));
} else if (command === 'melsql -start') {
await StartRequestOption();
} else if (command === 'melsql -cmd') {
console.log('');
commandCheck.forEach(cmd => {
console.log(gradient.rainbow(' '.repeat(2) + cmd));
});
console.log('');
}
else if (command.startsWith('melsql -use ')) {
const dbName = command.split(' ')[2];
if (dbName) {
try {
await dbManager.changeDatabase(dbName);
console.log(gradient.rainbow(`Switched to database: ${dbName}`));
} catch (err) {
console.log(chalk.red(`Failed to switch database: ${dbName}`));
}
} else {
console.log(chalk.red('Please specify a database name.'));
}
}
else if (command.includes('melsql -r -') || command.startsWith('getMethod')) {
await RequestSQL(command);
}
else {
console.log(gradient.rainbow('Unknown command... Try again.'));
}
}
}
async function main() {
await displayWelcome();
await BasicCommandPrompt();
}
main();