Skip to content

Commit

Permalink
Conflicts Resolution and Squash Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanpreet Singh committed Apr 7, 2022
1 parent 9c04eda commit faa6164
Show file tree
Hide file tree
Showing 19 changed files with 412 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void writeUserDetails(String filePath, String registrationDetails) {

public static Optional<UserRegistration> readUserDetails(String filePath, String hashedUserId) {
//TODO get user authentication details from the user details file
try(FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr)){
try (FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr)) {
String entireLine;
while ((entireLine=br.readLine())!=null) {
String [] userDetailsArr = entireLine.split(MiscConstants.PIPE);
while ((entireLine = br.readLine()) != null) {
String[] userDetailsArr = entireLine.split(MiscConstants.PIPE);
if (!userDetailsArr[0].equals(hashedUserId))
continue;
// create userRegistration model from
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/dal/distributed/authentication/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class Login {
public void flow(Scanner sc) throws IOException {
logger.info("For login, please provide your userId and press enter");
String userId = sc.nextLine();
if(userId == null || userId.isEmpty()) {
if (userId == null || userId.isEmpty()) {
logger.info("Please type something before enter!");
return;
}
logger.info("Please provide your password and press enter");
String password = sc.nextLine();
if(password == null || password.isEmpty()) {
if (password == null || password.isEmpty()) {
logger.error("Password can't be empty!");
return;
}
Expand All @@ -36,7 +36,7 @@ public void flow(Scanner sc) throws IOException {
}
UserRegistration user = userOpt.get();
String hashedPassword = getHashedValue(password);
if(!hashedPassword.equals(user.getPassword())) {
if (!hashedPassword.equals(user.getPassword())) {
logger.error("Either userId/password is not correct");
return;
}
Expand All @@ -45,18 +45,18 @@ public void flow(Scanner sc) throws IOException {
logger.info("Please answer following security question and press enter");
logger.info(securityQuestion.getQuestion());
String securingAnsByUser = sc.nextLine();
if(securingAnsByUser == null || securingAnsByUser.isEmpty()) {
if (securingAnsByUser == null || securingAnsByUser.isEmpty()) {
logger.error("Your security answer can't be empty!");
return;
}
if(!securingAnsByUser.equals(securityQuestion.getAnswer())) {
if (!securingAnsByUser.equals(securityQuestion.getAnswer())) {
logger.info("Invalid answer please try again!");
return;
}
logger.info("You are successfully logged in!!");
OperationsMenu operationsMenu = new OperationsMenu();
operationsMenu.displayOperationsMenu(userId, sc);
return ;
return;
}

private String getHashedValue(String originalString) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/dal/distributed/constant/DataConstants.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.dal.distributed.constant;

public class DataConstants {
public static final String DATABASES_FOLDER_LOCATION="usr/dpg9/databases/";
public static final String DATABASES_FOLDER_LOCATION = "usr/dpg9/databases/";

public static final String LOGS_FILE_LOCATION="usr/dpg9/logs/";
public static final String LOGS_FILE_LOCATION = "usr/dpg9/logs/";

public static final String QUERY_LOGS_FILE_LOCATION = "usr/dpg9/logs/";

public static final String QUERY_LOG_FILE_NAME = "query_logs";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dal.distributed.constant;

public class RelationalOperators {
public static final String EQUAL = "=";
public static final String NOTEQUAL = "!=";
public static final String NOTEQUAL1 = "<>";
public static final String NOTEQUAL2 = "~=";
public static final String GREATER = ">";
public static final String LESS = "<";
public static final String GREATEREQUAL = ">=";
public static final String LESSEQUAL = "<=";
}
13 changes: 7 additions & 6 deletions src/main/java/com/dal/distributed/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import com.dal.distributed.authentication.Registration;
import com.dal.distributed.logger.Logger;
import com.dal.distributed.miscellaneous.MiscOperations;

import java.io.IOException;
import java.util.Scanner;

public class Main {
public static String databaseName="default";
public static String databaseName="";

public static void main(String [] args) throws IOException {
public static void main(String [] args) throws Exception {
Logger logger = Logger.instance();
MiscOperations.createInitFolders();
logger.info("Welcome to DPG9 Distributed Database");
Expand All @@ -21,15 +19,18 @@ public static void main(String [] args) throws IOException {
logger.info("3. Exit\n");
logger.info("Please select an option from the above list");
Scanner sc = new Scanner(System.in);
//new OperationsMenu().implementQuery(sc);
final String userInput = sc.nextLine();
switch (userInput) {
case "1":
Registration registration = new Registration();
registration.registerUser();
break;
case "2":
Login login = new Login();
login.flow(sc);
// Login login = new Login();
// login.flow(sc);
OperationsMenu operationsMenu = new OperationsMenu();
operationsMenu.displayOperationsMenu(null, sc);
break;
case "3":
break;
Expand Down
133 changes: 46 additions & 87 deletions src/main/java/com/dal/distributed/main/OperationsMenu.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.dal.distributed.main;

import com.dal.distributed.constant.AuthConstants;
import com.dal.distributed.constant.DataConstants;
import com.dal.distributed.constant.QueryTypes;
import com.dal.distributed.logger.Logger;

import com.dal.distributed.queryImpl.*;

import java.io.IOException;
import com.dal.distributed.queryImpl.model.QueryLog;
import com.dal.distributed.utils.FileOperations;
import java.sql.Timestamp;
import java.util.Map;
import java.util.Scanner;

public class OperationsMenu {

Logger logger = Logger.instance();

public void displayOperationsMenu(String userId, Scanner scanner) throws IOException {
public void displayOperationsMenu(String userId, Scanner scanner) throws Exception {
while (true) {
logger.info("Please choose from the following options:");
logger.info("\n1. Write Queries");
Expand All @@ -26,7 +27,7 @@ public void displayOperationsMenu(String userId, Scanner scanner) throws IOExcep
String userInput = scanner.nextLine();
switch (userInput) {
case "1":
implementQuery(scanner);
implementQuery(scanner, userId);
break;
case "2":
break;
Expand All @@ -39,14 +40,14 @@ public void displayOperationsMenu(String userId, Scanner scanner) throws IOExcep
default:
logger.error("Please choose valid option!");
}
if("5".equals(userInput)) {
if ("5".equals(userInput)) {
logger.info("You are logged out");
break;
}
}
}

public void implementQuery(Scanner sc) throws IOException {
public void implementQuery(Scanner sc, String userId) throws Exception {
QueryValidator queryExecutorObj = new QueryValidator();
CreateDatabase createDatabase = new CreateDatabase();
UseDatabase useDatabase = new UseDatabase();
Expand All @@ -56,88 +57,46 @@ public void implementQuery(Scanner sc) throws IOException {
UpdateTable updateTable = new UpdateTable();
DeleteDataFromTable deleteDataFromTable = new DeleteDataFromTable();

Map queryValidatorResults;
while (true) {
String query="";
logger.info("Please choose from the following options:");
logger.info("\n1. Create databases");
logger.info("2. Use databases");
logger.info("3. Create table");
logger.info("4. Insert into table");
logger.info("5. Select from table with single where condition");
logger.info("6. Update one column with single where condition");
logger.info("7. Delete a row with single where condition");
logger.info("8. Exit");
String userInput = sc.nextLine();
if(!userInput.equals("8")){
logger.info("Write query for selected option:");
query=sc.nextLine();
}
logger.info("Write query for selected option:");
String query = sc.nextLine();

switch (userInput) {
case "1":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.CREATE_DATABASE)){
createDatabase.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "2":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.USE)){
useDatabase.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "3":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.CREATE_TABLE)){
createTable.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "4":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.INSERT)){
insertIntoTable.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "5":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.SELECT)){
selectQuery.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "6":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.UPDATE)){
updateTable.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
case "7":
queryValidatorResults = queryExecutorObj.validateQuery(query);
if(((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType")== QueryTypes.DELETE)){
deleteDataFromTable.execute(query);
}
else
logger.error("Oops.. looks like I encountered error in parsing query");
break;
default:
logger.error("Please choose valid option!");
}
if("8".equals(userInput)) {
logger.info("You are logged out.");
break;
QueryLog logQuery = new QueryLog();
logQuery.setFlag("valid");
logQuery.setQuery(query);
logQuery.setSubmissionTimestamp(String.valueOf(new Timestamp(System.currentTimeMillis())));
logQuery.setSubmittedBy(userId);

Map queryValidatorResults = queryExecutorObj.validateQuery(query);

if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.CREATE_DATABASE)) {
if (createDatabase.execute(query)) {
logger.info("Action: " + query + "\nMessage: 1 row(s) affected.\n");
}
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.USE)) {
if (useDatabase.execute(query)) {
logger.info("Action: " + query + "\nMessage: 0 row(s) affected.\n");
}
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.CREATE_TABLE)) {
if (createTable.execute(query)) {
logger.info("Action: " + query + "\nMessage: 0 row(s) affected.\n");
}
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.INSERT)) {
insertIntoTable.execute(query);
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.SELECT)) {
selectQuery.execute(query);
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.UPDATE)) {
if (updateTable.execute(query)) {
logger.info("Action: " + query + "\nMessage: 1 row(s) affected.\n");
}
} else if (((boolean) queryValidatorResults.get("isValidate")) && (queryValidatorResults.get("queryType") == QueryTypes.DELETE)) {
if (deleteDataFromTable.execute(query)) {
logger.info("Action: " + query + "\nMessage: 1 row(s) affected.\n");
}
} else {
logQuery.setFlag("invalid");
logger.error("Oops.. looks like I encountered error in parsing query");
}
FileOperations.writeToExistingFile(logQuery.toString(),
DataConstants.QUERY_LOG_FILE_NAME, DataConstants.QUERY_LOGS_FILE_LOCATION);
}
}
}
28 changes: 27 additions & 1 deletion src/main/java/com/dal/distributed/queryImpl/CreateDatabase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
package com.dal.distributed.queryImpl;

import java.io.File;
import java.io.IOException;

import com.dal.distributed.constant.DataConstants;
import com.dal.distributed.logger.Logger;
import com.dal.distributed.utils.FileOperations;

public class CreateDatabase {
public void execute(String query) {

Logger logger = Logger.instance();

public boolean execute(String query) throws IOException {
String[] sql = query.split("\\s+");
if (sql[0].equalsIgnoreCase("create") && sql[1].equalsIgnoreCase("database")) {
//Remove the semicolon from database name
String databaseName = sql[2].substring(0, sql[2].length() - 1).toLowerCase();
File[] databases = FileOperations.readFiles(DataConstants.DATABASES_FOLDER_LOCATION);
for (File file : databases) {
if (file.getName().equalsIgnoreCase(databaseName)) {
logger.error("Error Code: 1007. Can't create database '" + databaseName + "'; Database exists.");
return false;
}
}
FileOperations.createNewFolder(DataConstants.DATABASES_FOLDER_LOCATION, databaseName);
//FileOperations.writeToExistingFile(databaseName+"|","databases.psv", DataConstants.LOGS_FILE_LOCATION);
return true;
} else
return false;

}
}
33 changes: 32 additions & 1 deletion src/main/java/com/dal/distributed/queryImpl/CreateTable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
package com.dal.distributed.queryImpl;

import com.dal.distributed.constant.DataConstants;
import com.dal.distributed.main.Main;
import com.dal.distributed.utils.FileOperations;

public class CreateTable {
public void execute(String query) {
public boolean execute(String query) {

String[] sql = query.split("\\s+");
if(sql.length>3&& sql[0].toLowerCase().equals("create")&&sql[1].toLowerCase().equals("table"))
{
String mainStatement=query.substring(query.indexOf(sql[2]));
String tableName=mainStatement.substring(0,mainStatement.indexOf("("));
String schema=mainStatement.substring(mainStatement.indexOf("(")+1,mainStatement.indexOf(";")-1);
String columnNames="";
String[] columns= schema.split(",");
int i=0;
for(String col:columns)
{
columnNames+=col.substring(0,col.indexOf(" "));
if(i!=columns.length-1)
columnNames+="|";
i++;
}
schema=schema.replaceAll(",", "|");
FileOperations.writeToExistingFile(columnNames, tableName+".psv", DataConstants.DATABASES_FOLDER_LOCATION+Main.databaseName+"/");
FileOperations.writeToExistingFile(schema, tableName+"_Schema"+".psv", DataConstants.DATABASES_FOLDER_LOCATION+Main.databaseName+"/");
//FileOperations.writeToExistingFile(tableName+"|", Main.databaseName+".psv", DataConstants.LOGS_FILE_LOCATION);
return true;
}
else
return false;

}

}

Loading

0 comments on commit faa6164

Please sign in to comment.