Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.stuckdexter.sophiadb;
- import com.sun.deploy.util.StringUtils;
- import de.stuckdexter.sophiadb.exceptions.*;
- import java.io.*;
- import java.net.InetAddress;
- import java.net.Socket;
- /*
- / / ____ _ _
- /_/___ / ___| ___ _ __ | |__ (_) __ _
- \ \ _ \ \___ \ / _ \| '_ \| '_ \| |/ _` |
- |\_\__/ ___) | (_) | |_) | | | | | (_| |
- |/|/ |____/ \___/| .__/|_| |_|_|\__,_|
- / / |_|
- */
- public class SophiaDbConnection{
- private String username;
- private String password;
- private String address;
- public SophiaDbConnection(String username, String password, String address){
- this.username = username;
- this.password = password;
- this.address = address;
- }
- private String request(String message){
- Socket sock = null;
- try {
- sock = new Socket(InetAddress.getByName(address), 3000);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- PrintWriter printWriter = null;
- if (sock != null) {
- printWriter = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
- printWriter.print(message);
- printWriter.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- String nachricht = "";
- BufferedReader bufferedReader =
- null;
- try {
- bufferedReader = new BufferedReader(
- new InputStreamReader(
- sock.getInputStream()));
- char[] buffer = new char[1024];
- int anzahlZeichen = bufferedReader.read(buffer, 0, 1024);
- nachricht = new String(buffer, 0, anzahlZeichen);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return nachricht;
- }
- public String[] get(String table, String row) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
- String result = request("{" + username + "," + password + "}" + table + ";get;" + row);
- switch (result){
- case "error1":
- throw new NotExistException();
- case "error2":
- throw new AlreadyExistException();
- case "error3":
- throw new SynatxNotValidException();
- case "error4":
- throw new CmdNotExistException();
- case "error5":
- throw new TableNotExistException();
- case "error6":
- throw new InvalidLoginException();
- case "error7":
- throw new NoPermissionException();
- default:
- result = result.replace("'", "");
- result = result.replace("[", "");
- result = result.replace("]", "");
- return result.split(",");
- }
- }
- public String[] getAll(String table, String row) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
- String result = request("{" + username + "," + password + "}" + table + ";getall");
- switch (result){
- case "error1":
- throw new NotExistException();
- case "error2":
- throw new AlreadyExistException();
- case "error3":
- throw new SynatxNotValidException();
- case "error4":
- throw new CmdNotExistException();
- case "error5":
- throw new TableNotExistException();
- case "error6":
- throw new InvalidLoginException();
- case "error7":
- throw new NoPermissionException();
- default:
- result = result.replace("'", "");
- result = result.replace("[", "");
- result = result.replace("]", "");
- return result.split(",");
- }
- }
- public boolean set(String table, String row, String[] data) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
- String dat = "[" + String.join(",", data) + "]";
- String result = request("{" + username + "," + password + "}" + table + ";set;" + row + ";" + dat);
- switch (result){
- case "error1":
- throw new NotExistException();
- case "error2":
- throw new AlreadyExistException();
- case "error3":
- throw new SynatxNotValidException();
- case "error4":
- throw new CmdNotExistException();
- case "error5":
- throw new TableNotExistException();
- case "error6":
- throw new InvalidLoginException();
- case "error7":
- throw new NoPermissionException();
- case "OK":
- return true;
- }
- return false;
- }
- public boolean add(String table, String row, String[] data) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
- String dat = "[" + String.join(",", data) + "]";
- String result = request("{" + username + "," + password + "}" + table + ";add;" + row + ";" + dat);
- switch (result){
- case "error1":
- throw new NotExistException();
- case "error2":
- throw new AlreadyExistException();
- case "error3":
- throw new SynatxNotValidException();
- case "error4":
- throw new CmdNotExistException();
- case "error5":
- throw new TableNotExistException();
- case "error6":
- throw new InvalidLoginException();
- case "error7":
- throw new NoPermissionException();
- case "OK":
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement