Skip to content

Commit

Permalink
Various model methods not being passed DBType. GetColumnObjs formatti…
Browse files Browse the repository at this point in the history
…ng was bad due to comments.
  • Loading branch information
lanGrape committed Jul 21, 2022
1 parent 884c6c6 commit 0a0eeef
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
33 changes: 18 additions & 15 deletions backend/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ ipcMain.handle(
async (event, dbName: string, dbType: DBType): Promise<void> => {
event.sender.send('async-started');
try {
await db.connectToDB(dbName);

console.log('Trying to connect to: ' + dbName + ' with DBType of ', dbType);

await db.connectToDB(dbName, dbType);

// send updated db info
const dbsAndTables: DBList = await db.getLists();
const dbsAndTables: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTables);
} finally {
event.sender.send('async-complete');
Expand All @@ -66,14 +69,14 @@ ipcMain.handle(
event.sender.send('async-started');
try {
// if deleting currently connected db, disconnect from db
if (currDB) await db.connectToDB('');
if (currDB) await db.connectToDB('', dbType);

// drop db
const dropDBScript = dropDBFunc(dbName, dbType);
await db.query(dropDBScript);

// send updated db info
const dbsAndTables: DBList = await db.getLists();
const dbsAndTables: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTables);
} finally {
event.sender.send('async-complete');
Expand Down Expand Up @@ -135,7 +138,7 @@ ipcMain.handle(
}

// update frontend with new db list
const dbsAndTableInfo: DBList = await db.getLists();
const dbsAndTableInfo: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTableInfo);
} finally {
// //cleanup temp file
Expand Down Expand Up @@ -191,7 +194,7 @@ ipcMain.handle(
}

// update frontend with new db list
const dbsAndTableInfo: DBList = await db.getLists();
const dbsAndTableInfo: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTableInfo);
} finally {
event.sender.send('async-complete');
Expand All @@ -214,7 +217,7 @@ ipcMain.handle(
try {
let error: string | undefined;
// connect to db to run query
if (selectedDb !== targetDb) await db.connectToDB(targetDb);
if (selectedDb !== targetDb) await db.connectToDB(targetDb, dbType);

// Run Explain
let explainResults;
Expand Down Expand Up @@ -243,11 +246,11 @@ ipcMain.handle(
};
} finally {
// connect back to initialDb
if (selectedDb !== targetDb) await db.connectToDB(selectedDb);
if (selectedDb !== targetDb) await db.connectToDB(selectedDb, dbType);

// send updated db info in case query affected table or database information
// must be run after we connect back to the originally selected so tables information is accurate
const dbsAndTables: DBList = await db.getLists();
const dbsAndTables: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTables);

event.sender.send('async-complete');
Expand Down Expand Up @@ -344,7 +347,7 @@ ipcMain.handle(
};
} finally {
// send updated db info in case query affected table or database information
const dbsAndTables: DBList = await db.getLists();
const dbsAndTables: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTables);

// send feedback back to FE
Expand All @@ -370,10 +373,10 @@ ipcMain.handle(
await db.query(createDBFunc(newDbName, dbType));

// connect to initialized db
await db.connectToDB(newDbName);
await db.connectToDB(newDbName, dbType);

// update DBList in the sidebar to show this new db
const dbsAndTableInfo: DBList = await db.getLists();
const dbsAndTableInfo: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTableInfo);
} catch (e) {
// in the case of an error, delete the created db
Expand Down Expand Up @@ -402,7 +405,7 @@ ipcMain.handle(
try {
let error: string | undefined;
// connect to db to run query
await db.connectToDB(selectedDb);
await db.connectToDB(selectedDb, dbType);

// Run Query
// let returnedRows;
Expand All @@ -414,7 +417,7 @@ ipcMain.handle(
} finally {
// send updated db info in case query affected table or database information
// must be run after we connect back to the originally selected so tables information is accurate
const dbsAndTables: DBList = await db.getLists();
const dbsAndTables: DBList = await db.getLists(dbType);
event.sender.send('db-lists', dbsAndTables);

event.sender.send('async-complete');
Expand Down Expand Up @@ -453,7 +456,7 @@ async (event, backendObj, dbType: DBType) => {
};
} finally {
// send updated db info
const updatedDb: DBList = await db.getLists();
const updatedDb: DBList = await db.getLists(dbType);
event.sender.send('db-lists', updatedDb);

// send feedback back to FE
Expand Down
1 change: 1 addition & 0 deletions backend/helperFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const helperFunctions: HelperFunctions = {
const MYSQL = `CREATE DATABASE "${name}"`;
// const mySQLUse = `USE DATABASE "${name}"`;

console.log('RETURNING DB: ', DBType.Postgres ? PG : MYSQL);
return dbType === DBType.Postgres ? PG : MYSQL;
// if (dbType === DBType.Postgres) {
// return `CREATE DATABASE "${name}"`;
Expand Down
61 changes: 40 additions & 21 deletions backend/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,37 @@ const getColumnObjects = function (
dbType: DBType
): Promise<ColumnObj[]> {
let queryString;

console.log('Trying to get columnObj of: ' + tableName + ' with a dbType of ', dbType);

if (dbType === DBType.Postgres) {
console.log('===========TRYING TO USE PG TO GET COLUMN OBJECTS!!!');

// query string to get constraints and table references as well
queryString =
`SELECT cols.column_name,` + // Gets column name
`cols.data_type,` + // Gets data type
`cols.character_maximum_length,` + // Gets max char length
`cols.is_nullable,` + // Gets is_nullable
`kcu.constraint_name,` + // Gets constraint_name
`cons.constraint_type,` + // Gets constraint_type
`rel_kcu.table_name AS foreign_table,` + // Gets rel_kcu.table_name AS foreign_table
`rel_kcu.column_name AS foreign_column` + // Gets kcu.column_name AS foreign_column
`FROM information_schema.columns cols` + // Gets all this data from information_schema.columns (cols) (lists all columns in the database)
// This
`LEFT JOIN information_schema.key_column_usage kcu` + // Left join will return all data from (cols) and matching records from information_schema.key_column_usage (kcu) where...
`ON cols.column_name = kcu.column_name` + // cols.column_name is equal to kcu.column name AND (has to meet the condition below as well)...
`AND cols.table_name = kcu.table_name` + // cols.table_name = kcu.table_name
`LEFT JOIN information_schema.table_constraints cons` + // Left join all data from (cons) where... (cons lists all constraints from tables in this database)
`ON kcu.constraint_name = cons.constraint_name` + // kcu.constraint_name is equal to cons.constraint_name
`LEFT JOIN information_schema.referential_constraints rco` + // rco lists all foreign keys in database
`ON rco.constraint_name = cons.constraint_name` +
`LEFT JOIN information_schema.key_column_usage rel_kcu` + // key_column_usage lists all columns in the database restricted by primary,unique, foreign or check constraint
`ON rco.unique_constraint_name = rel_kcu.constraint_name` +
`WHERE cols.table_name = $1`; // parameterized query will equal tableName down below
} else if (dbType === DBType.MySQL) {
`SELECT cols.column_name,
cols.data_type,
cols.character_maximum_length,
cols.is_nullable,
kcu.constraint_name,
cons.constraint_type,
rel_kcu.table_name AS foreign_table,
rel_kcu.column_name AS foreign_column
FROM information_schema.columns cols
LEFT JOIN information_schema.key_column_usage kcu
ON cols.column_name = kcu.column_name
AND cols.table_name = kcu.table_name
LEFT JOIN information_schema.table_constraints cons
ON kcu.constraint_name = cons.constraint_name
LEFT JOIN information_schema.referential_constraints rco
ON rco.constraint_name = cons.constraint_name
LEFT JOIN information_schema.key_column_usage rel_kcu
ON rco.unique_constraint_name = rel_kcu.constraint_name
WHERE cols.table_name = $1`;
}
else if (dbType === DBType.MySQL) {
console.log('===========TRYING TO USE MYSQL GETCOLUMNOBJ???');

queryString = `SELECT cols.column_name, cols.data_type, cols.character_maximum_length, cols.is_nullable, kcu.constraint_name, cons.constraint_type, rel_kcu.table_name AS foreign_table, rel_kcu.column_name AS foreign_column
FROM information_schema.columns cols
LEFT JOIN information_schema.key_column_usage kcu
Expand Down Expand Up @@ -159,9 +166,17 @@ const getDBLists = function (dbType: DBType): Promise<TableDetails[]> {
// *********************************************************** POSTGRES/MYSQL ************************************************* //
const PG_DBConnect = async function (db: string) {
const newURI = `postgres://postgres:postgres@localhost:5432/${db}`;

console.log('Trying URI: ', newURI);

const newPool = new Pool({ connectionString: newURI });
await pool.end();

console.log('Ended connection. Trying to connect now.');

pool = newPool;

console.log('New pool set.');
};

const MSQL_DBConnect = function (db: string) {
Expand Down Expand Up @@ -199,10 +214,14 @@ const myObj: MyObj = {

// Change current Db
connectToDB: async function (db: string, dbType: DBType) {
console.log('Starting connect to DB: ' + db + ' with a dbType of ', dbType);

if(dbType === DBType.Postgres) {
console.log('Attempting Postgres connection.');
await PG_DBConnect(db);
}
else if (dbType === DBType.MySQL) {
console.log('Attempting MSQL connection');
await MSQL_DBConnect(db);
}
},
Expand Down

0 comments on commit 0a0eeef

Please sign in to comment.