Some more small DB optimizations
This commit is contained in:
		
							parent
							
								
									e8094c78ec
								
							
						
					
					
						commit
						5c0cb6463a
					
				
					 2 changed files with 5 additions and 5 deletions
				
			
		| 
						 | 
					@ -8,7 +8,7 @@ const connection = new Pool({
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.getGuild = async (query) => {
 | 
					exports.getGuild = async (query) => {
 | 
				
			||||||
  return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1 limit 1", [query])).rows[0];
 | 
					  return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.setPrefix = async (prefix, guild) => {
 | 
					exports.setPrefix = async (prefix, guild) => {
 | 
				
			||||||
| 
						 | 
					@ -68,10 +68,10 @@ exports.getCounts = async () => {
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.addCount = async (command) => {
 | 
					exports.addCount = async (command) => {
 | 
				
			||||||
  let count = await connection.query("SELECT * FROM counts WHERE command = $1 limit 1", [command]);
 | 
					  let count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]);
 | 
				
			||||||
  if (!count.rows[0]) {
 | 
					  if (!count.rows[0]) {
 | 
				
			||||||
    await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
 | 
					    await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
 | 
				
			||||||
    count = await connection.query("SELECT * FROM counts WHERE command = $1 limit 1", [command]);
 | 
					    count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  await connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count ? count.rows[0].count + 1 : 1, command]);
 | 
					  await connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count ? count.rows[0].count + 1 : 1, command]);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,7 +9,7 @@ exports.setup = async () => {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    counts = connection.prepare("SELECT * FROM counts").all();
 | 
					    counts = connection.prepare("SELECT * FROM counts").all();
 | 
				
			||||||
  } catch {
 | 
					  } catch {
 | 
				
			||||||
    connection.prepare("CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL )").run();
 | 
					    connection.prepare("CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL )").run();
 | 
				
			||||||
    counts = [];
 | 
					    counts = [];
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,7 +50,7 @@ exports.fixGuild = async (guild) => {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
 | 
					    guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
 | 
				
			||||||
  } catch {
 | 
					  } catch {
 | 
				
			||||||
    connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run();
 | 
					    connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (!guildDB) {
 | 
					  if (!guildDB) {
 | 
				
			||||||
    logger.log(`Registering guild database entry for guild ${guild.id}...`);
 | 
					    logger.log(`Registering guild database entry for guild ${guild.id}...`);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue