|
| 1 | +package com.nz1337.easysql.manager; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.concurrent.atomic.AtomicInteger; |
| 9 | + |
| 10 | +public class Column { |
| 11 | + |
| 12 | + private final Connection connection; |
| 13 | + private final Table table; |
| 14 | + private final String tableName; |
| 15 | + private final String primaryKey; |
| 16 | + private final StringBuilder allColumns = new StringBuilder(); |
| 17 | + |
| 18 | + public Column(Table table, Connection connection, ArrayList<String> listedColumns) { |
| 19 | + this.connection = connection; |
| 20 | + this.table = table; |
| 21 | + this.primaryKey = table.getPrimaryKey(); |
| 22 | + this.tableName = this.table.getTable(); |
| 23 | + listedColumns.forEach(column -> { |
| 24 | + allColumns.append("`").append(column).append("`"); |
| 25 | + if (!listedColumns.get(listedColumns.size() - 1).equals(column)) allColumns.append(","); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + public boolean isExists(String column, Object value) { |
| 30 | + try { |
| 31 | + PreparedStatement preparedStatement = this.connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + column + "`=?"); |
| 32 | + preparedStatement.setObject(1, value); |
| 33 | + if (preparedStatement.executeQuery().next()) return true; |
| 34 | + } catch (SQLException e) { |
| 35 | + e.printStackTrace(); |
| 36 | + } |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + public void insertDefault(Object... values) { |
| 41 | + String primary = this.getPrimaryKey(); |
| 42 | + try { |
| 43 | + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + primary + "`=?"); |
| 44 | + preparedStatement.setObject(1, values[0]); |
| 45 | + preparedStatement.executeQuery().next(); |
| 46 | + if (!isExists(primary, values[0])) { |
| 47 | + StringBuilder unknownValue = new StringBuilder(); |
| 48 | + for (int i = 0; i < countChars(this.allColumns.toString(), ',') + 1; i++) unknownValue.append("?,"); |
| 49 | + unknownValue.append(")"); |
| 50 | + PreparedStatement insert = connection.prepareStatement("INSERT INTO `" + this.tableName + "` (" + this.allColumns + ") VALUE (" + unknownValue.toString().replace(",)", ")")); |
| 51 | + for (int i = 0; i < values.length; i++) insert.setObject(i + 1, values[i]); |
| 52 | + insert.executeUpdate(); |
| 53 | + } |
| 54 | + } catch (SQLException e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void editValue(String searchedColumn, Object searchedValue, String editedColumn, Object newValue) { |
| 60 | + try { |
| 61 | + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `" + this.getTableName() + "` SET `" + editedColumn + "`=? WHERE `" + searchedColumn + "`=?"); |
| 62 | + preparedStatement.setObject(1, newValue); |
| 63 | + preparedStatement.setObject(2, searchedValue); |
| 64 | + preparedStatement.executeUpdate(); |
| 65 | + } catch (SQLException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public Object getValue(String column, Object value, String desiredValue) { |
| 71 | + try { |
| 72 | + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + column + "`=?"); |
| 73 | + preparedStatement.setObject(1, value); |
| 74 | + ResultSet results = preparedStatement.executeQuery(); |
| 75 | + results.next(); |
| 76 | + return results.getObject(desiredValue); |
| 77 | + } catch (SQLException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + public void delete(String column, Object value) { |
| 84 | + if (!isExists(column, value)) return; |
| 85 | + try { |
| 86 | + PreparedStatement preparedStatement = this.connection.prepareStatement("DELETE FROM `" + this.tableName + "` WHERE `" + this.tableName + "`.`" + column + "`=?"); |
| 87 | + preparedStatement.setObject(1, value); |
| 88 | + preparedStatement.executeUpdate(); |
| 89 | + } catch (SQLException e) { |
| 90 | + e.printStackTrace(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void makeCustomRequest(String request) { |
| 95 | + try { |
| 96 | + PreparedStatement db = this.connection.prepareStatement(request); |
| 97 | + db.execute(); |
| 98 | + } catch (SQLException e) { |
| 99 | + e.printStackTrace(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public Table getTable() { |
| 104 | + return this.table; |
| 105 | + } |
| 106 | + |
| 107 | + public String getTableName() { |
| 108 | + return tableName; |
| 109 | + } |
| 110 | + |
| 111 | + public String getPrimaryKey() { |
| 112 | + return this.primaryKey; |
| 113 | + } |
| 114 | + |
| 115 | + private int countChars(String s, char c) { |
| 116 | + AtomicInteger i = new AtomicInteger(); |
| 117 | + s.chars().forEach(chars -> { |
| 118 | + if (chars == c) i.getAndIncrement(); |
| 119 | + }); |
| 120 | + return i.get(); |
| 121 | + } |
| 122 | +} |
0 commit comments