Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/juja/sqlcmd/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface DatabaseManager {
boolean update(String tableName, int id);

void close() throws SQLException;

boolean hasConnection();
}
5 changes: 5 additions & 0 deletions src/main/java/juja/sqlcmd/InMemoryDatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public void close() throws SQLException {
//NOP
}

@Override
public boolean hasConnection() {
return true;
}

private Table tableByName(String tableName) {
for (Table currentTable : tables) {
if (currentTable.getTableName().equals(tableName)) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/juja/sqlcmd/JdbcDatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ private DataSet[] tableRows(ResultSet resultSet, int rowSize, int tableSize) thr
return tableData;
}

@Override
public boolean hasConnection() {
return connection != null;
}

private int numberOfEntries(String tableName) throws SQLException {
String sqlQuery = String.format("SELECT COUNT(*) as RECORDS FROM %s", tableName);
try (Statement statement = connection.createStatement();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/juja/sqlcmd/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public static void main(String[] args) {
MainController controller = new MainController(view, databaseManager);
controller.run();
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/juja/sqlcmd/controller/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package juja.sqlcmd.controller;

import java.util.HashMap;
import java.util.Map;

import juja.sqlcmd.DatabaseManager;
import juja.sqlcmd.controller.command.Command;
import juja.sqlcmd.controller.command.CommandType;
import juja.sqlcmd.controller.command.Connect;
import juja.sqlcmd.controller.command.Exit;
import juja.sqlcmd.controller.command.UnsupportedCommand;
import juja.sqlcmd.view.View;

public class CommandHandler {
private View view;
private Map<CommandType, Command> registeredCommands = new HashMap<>();
private CommandType[] allCommandTypes;

public CommandHandler(DatabaseManager databaseManager, View view) {
this.view = view;
register(CommandType.CONNECT, new Connect(databaseManager, view));
register(CommandType.EXIT, new Exit(databaseManager, view));
allCommandTypes = CommandType.values();
}

public void handleCommand(String strCommand) {
if (strCommand.toLowerCase().equals("help")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потом лучше, конечно, это в команду help вытащить)

view.write("List of available commands to execute:");
for (CommandType commandType : allCommandTypes) {
view.write(commandType.getDescription());
}
} else {
Command command = getCommandByName(strCommand);
command.execute();
}
}

private Command getCommandByName(String commandName) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А я не могу понять как отрабатывает connect, если как enum это одно слово connect, а как команда он 4 слова

for (CommandType commandType : allCommandTypes) {
if (commandName.toLowerCase().equals(commandType.getName())) {
return registeredCommands.get(commandType);
}
}
return new UnsupportedCommand(view);
}

private void register(CommandType commandType, Command command) {
registeredCommands.put(commandType, command);
}
}
17 changes: 15 additions & 2 deletions src/main/java/juja/sqlcmd/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ public MainController(View view, DatabaseManager databaseManager) {
this.databaseManager = databaseManager;
}

public void run(){

public void run() {
CommandHandler handler = new CommandHandler(databaseManager, view);
view.write("Welcome to SqlCmd application!\n" +
"Please enter help to see a list of commands");
view.write("-----");
while (true) {
String command = view.read();
handler.handleCommand(command);
if (command.toLowerCase().equals("exit")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем этот if, если есть команда exit?)

view.write("Bye!");
break;
}
view.write("-----");
view.write("Please enter next command");
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/juja/sqlcmd/controller/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package juja.sqlcmd.controller.command;

public interface Command {
void execute();
}
28 changes: 28 additions & 0 deletions src/main/java/juja/sqlcmd/controller/command/CommandType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package juja.sqlcmd.controller.command;

public enum CommandType {

CONNECT("connect", "\tconnect"
+ System.lineSeparator()
+ "\t\tconnection with database. Require to enter credentials"),

EXIT("exit", "\texit"
+ System.lineSeparator()
+ "\t\tto exit from this session");

private String name;
private String description;

CommandType(String name, String description) {
this.name = name;
this.description = description;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}
}
41 changes: 41 additions & 0 deletions src/main/java/juja/sqlcmd/controller/command/Connect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package juja.sqlcmd.controller.command;

import juja.sqlcmd.DatabaseManager;
import juja.sqlcmd.view.View;

public class Connect implements Command {
private DatabaseManager databaseManager;
private View view;

public Connect(DatabaseManager databaseManager, View view) {
this.databaseManager = databaseManager;
this.view = view;
}

@Override
public void execute() {
if (databaseManager.hasConnection()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я поэтому сделал 2 метода с коннектом и без, чтобы не городить if else в каждой команде)

view.write("Connection already exists. To close current connection use Disconnect or Exit command");
} else {
view.write("Please enter required data in format:\n" +
"databaseName|login|password|");
String command = view.read();
if (isCommandValid(command)) {
String[] splittedCommand = command.split("\\|");
String dbName = splittedCommand[0];
String login = splittedCommand[1];
String password = splittedCommand[2];
if (databaseManager.connect(dbName, login, password)) {
view.write("Successful connection to database " + dbName);
return;
}
}
view.write("Unsuccessful connection to database. Check if entered data is correct");
}
}

private boolean isCommandValid(String command) {
String[] splittedCommand = command.split("\\|");
return splittedCommand.length == 3;
}
}
30 changes: 30 additions & 0 deletions src/main/java/juja/sqlcmd/controller/command/Exit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package juja.sqlcmd.controller.command;

import java.sql.SQLException;

import juja.sqlcmd.DatabaseManager;
import juja.sqlcmd.view.View;

public final class Exit implements Command {
private DatabaseManager databaseManager;
private View view;

public Exit(DatabaseManager databaseManager, View view) {
this.databaseManager = databaseManager;
this.view = view;
}

@Override
public void execute() {
if (databaseManager.hasConnection()) {
try {
databaseManager.close();
view.write("Connection is closed.");
} catch (SQLException e) {
view.write("Cannot close current connection! Reason: " + e.getMessage());
}
} else {
view.write("There's no connection to close.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package juja.sqlcmd.controller.command;

import juja.sqlcmd.view.View;

public class UnsupportedCommand implements Command {
private View view;

public UnsupportedCommand(View view) {
this.view = view;
}

@Override
public void execute() {
view.write("Entered command is not supported. Enter 'help' to see a list of available commands");
}
}
1 change: 1 addition & 0 deletions src/main/java/juja/sqlcmd/view/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public Console(InputStream inputStream, OutputStream outputStream) {
@Override
public void write(String message) {
try {
message += "\n";
outputStream.write(message.getBytes());
} catch (IOException e) {
throw new RuntimeException("Something goes wrong with write method... Reason:" + e.getMessage());
Expand Down