Advertisement
Guest User

Untitled

a guest
Jan 28th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.58 KB | None | 0 0
  1.  
  2. public interface AvailableForORM{
  3.     public int getID();
  4.     public boolean isExpired();
  5.     public void update();
  6. }
  7.  
  8.  
  9.  
  10. import java.sql.DriverManager;
  11. import java.sql.Connection;
  12. import java.sql.SQLException;
  13. import org.postgresql.Driver;
  14. import java.sql.Statement;
  15. import java.sql.ResultSet;
  16. import java.io.BufferedReader;
  17. import java.io.FileReader;
  18.  
  19. public class Db{
  20.     private Connection c = null;
  21.  
  22.     public Db(){
  23.         try {
  24.             Class.forName("org.postgresql.Driver");
  25.             BufferedReader i = new BufferedReader(new FileReader("C:\\Users\\Pavel\\Desktop\\smilingRaccoon-master\\resources\\loginData.txt"));
  26.             String s = i.readLine();
  27.             String login = s.substring(0, s.indexOf(" "));
  28.             String password = s.substring(s.indexOf(" ") + 1);
  29.  
  30.             c = DriverManager
  31.                 .getConnection("jdbc:postgresql://localhost:5432/studs",
  32.                 login, password);
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.             System.err.println(e.getClass().getName()+": "+e.getMessage());
  36.             System.exit(0);
  37.         }
  38.         System.out.println("Opened database successfully");
  39.     }
  40.  
  41.     public boolean changePassword(String userName, String newPassword){
  42.         try{
  43.             if(getUserPassword(userName) == null){
  44.                 return false;
  45.             }
  46.             Statement stmt = c.createStatement();
  47.  
  48.             stmt.executeUpdate("update user_data set password='" + newPassword + "' where user_name='" + userName + "';");
  49.             stmt.close();
  50.             return true;
  51.         } catch (SQLException e){
  52.             System.err.println(this + " " + e);
  53.         }
  54.         return false;
  55.     }
  56.  
  57.     public boolean addUser(String userName, String password, boolean admin){
  58.         checkUserTableExist();
  59.  
  60.  
  61.         try{
  62.             Statement stmt = c.createStatement();
  63.             ResultSet rs = stmt.executeQuery("select count(*) from (select * from user_data where user_name='" + userName + "') as a;");
  64.  
  65.             rs.next();
  66.             if(rs.getInt("count") != 0){
  67.                 return false;
  68.             }
  69.  
  70.             stmt.executeUpdate("insert into user_data (user_name, password, admin) values ('" + userName + "', '" + password + "', " + admin + ");");
  71.             stmt.close();
  72.         } catch (SQLException e){
  73.             System.err.println(this + " " + e);
  74.         }
  75.         return true;
  76.     }
  77.  
  78.     public String getUserPassword(String userName){
  79.  
  80.         if(!checkUserTableExist()){
  81.             return null;
  82.         }
  83.  
  84.         String result = null;
  85.  
  86.         try{
  87.             Statement stmt = c.createStatement();
  88.             ResultSet rs = stmt.executeQuery( "select password from user_data where user_name='" + userName + "';" );
  89.             while ( rs.next() ) {
  90.                 result = rs.getString("password");
  91.             }
  92.  
  93.             rs.close();
  94.             stmt.close();
  95.         } catch (SQLException e){
  96.             System.err.println(this + " " + e);
  97.         }
  98.         return result;
  99.     }
  100.  
  101.     public boolean getUserA(String userName){
  102.  
  103.         if(!checkUserTableExist()){
  104.             return false;
  105.         }
  106.  
  107.         boolean result = false;
  108.  
  109.         try{
  110.             Statement stmt = c.createStatement();
  111.             ResultSet rs = stmt.executeQuery( "select admin from user_data where user_name='" + userName + "';" );
  112.             while ( rs.next() ) {
  113.                 result = rs.getBoolean(1);
  114.             }
  115.  
  116.             rs.close();
  117.             stmt.close();
  118.         } catch (SQLException e){
  119.             System.err.println(this + " " + e);
  120.         }
  121.         return result;
  122.     }
  123.  
  124.     private boolean checkUserTableExist(){
  125.         try{
  126.             Statement stmt = c.createStatement();
  127.             ResultSet rs = stmt.executeQuery( "select count(*) from (select * from pg_tables where tablename='user_data') as a;" );
  128.             rs.next();
  129.             int amount = rs.getInt("count");
  130.             rs.close();
  131.             if(amount == 0){
  132.                 stmt.close();
  133.                 stmt = c.createStatement();
  134.                 stmt.executeQuery( "create table user_data" +
  135.                                    "(id serial primary key, "+
  136.                                    " user_name text not null," +
  137.                                    " password text not null, " +
  138.                                    " admin boolean not null);");
  139.                 stmt.close();
  140.                 return false;
  141.             } else {
  142.                 return true;
  143.             }
  144.         } catch (SQLException e){
  145.             return false;
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. import java.lang.reflect.Field;
  155. import java.sql.DriverManager;
  156. import java.sql.Connection;
  157. import java.sql.SQLException;
  158. import org.postgresql.Driver;
  159. import java.sql.Statement;
  160. import java.sql.ResultSet;
  161. import java.io.BufferedReader;
  162. import java.io.FileReader;
  163. import java.util.*;
  164. import java.security.MessageDigest;
  165. import java.security.NoSuchAlgorithmException;
  166.  
  167. public class LittleORM{
  168.     private static Connection connection = null;
  169.  
  170.     static {
  171.         try {
  172.             Class.forName("org.postgresql.Driver");
  173.             BufferedReader i = new BufferedReader(new FileReader("C:\\Users\\Pavel\\Desktop\\smilingRaccoon-master\\resources\\loginData.txt"));
  174.             String s = i.readLine();
  175.             String login = s.substring(0, s.indexOf(" "));
  176.             String password = s.substring(s.indexOf(" ") + 1);
  177.  
  178.             connection = DriverManager
  179.                 .getConnection("jdbc:postgresql://localhost:5432/studs",
  180.                 login, password);
  181.         } catch (Exception e) {
  182.             e.printStackTrace();
  183.             System.err.println(e.getClass().getName()+": "+e.getMessage());
  184.             System.exit(0);
  185.         }
  186.         System.out.println("LittleORM has just created a connection to the database!");
  187.     }
  188.  
  189.     public static String getMD5(String str) {
  190.         try {
  191.             MessageDigest md = MessageDigest.getInstance("MD5");
  192.             md.update(str.getBytes());
  193.             System.out.println();
  194.             byte[] mdBytes = md.digest();
  195.             StringBuilder sb = new StringBuilder();
  196.             for(int i = 0; i < mdBytes.length; i++) {
  197.                 sb.append(Integer.toString((mdBytes[i] & 0xff) + 0x100, 16)
  198.                     .substring(1));
  199.         }
  200.             return sb.toString().trim();
  201.         } catch (NoSuchAlgorithmException exc) {
  202.             return null;
  203.         }
  204.     }
  205.  
  206.     private static String getTableName(Class objectClass){
  207.         return "t_" + objectClass.getPackage().getName().toLowerCase() + "_" + objectClass.getSimpleName().toLowerCase();
  208.     }
  209.     private static String getTableName(String className){
  210.         return "t_" + className.replace(".", "_").toLowerCase();
  211.     }
  212.  
  213.     public static boolean checkObjectExist(AvailableForORM object){
  214.         if( !checkTableExist(object.getClass()) ){
  215.             return false;
  216.         }
  217.  
  218.         try{
  219.             Statement stmt = connection.createStatement();
  220.             ResultSet rs = stmt.executeQuery( "select count(*) from ( select * from " + getTableName(object.getClass()) + " where id=" + object.getID() + ") as a;" );
  221.             rs.next();
  222.             if(rs.getInt("count") != 0){
  223.                 return true;
  224.             }
  225.             stmt.close();
  226.         } catch (SQLException e) {
  227.             System.err.println(e);
  228.         }
  229.         return false;
  230.     }
  231.  
  232.     private static boolean checkTableExist(Class objectClass){
  233.         try{
  234.  
  235.             ResultSet rs = connection.getMetaData()
  236.                 .getTables(connection.getCatalog(), null, getTableName(objectClass), null);
  237.             if (!rs.next()) {
  238.                 return false;
  239.             }
  240.  
  241.             //System.out.println(getTableName(objectClass) + " exists");
  242.             rs.close();
  243.         } catch(SQLException e){
  244.             System.out.println("checkTableExist: " + e);
  245.         }
  246.         return true;
  247.     }
  248.  
  249.     private static boolean checkTableExist(String className){
  250.         try{
  251.             System.out.println(getTableName(className));
  252.             ResultSet rs = connection.getMetaData()
  253.                 .getTables(connection.getCatalog(), null, getTableName(className), null);
  254.             if (!rs.next()) {
  255.                 return false;
  256.             }
  257.  
  258.             rs.close();
  259.         } catch(SQLException e){
  260.             System.out.println("checkTableExist: " + e);
  261.         }
  262.         return true;
  263.     }
  264.  
  265.     private static void createTable(Class objectClass){
  266.         if(checkTableExist(objectClass)){
  267.             //System.out.println("Exist!");
  268.             return;
  269.         }
  270.  
  271.  
  272.         String query = "create table " + getTableName(objectClass) + " " +
  273.                        "\n( " +
  274.                        "id integer primary key,\n";
  275.  
  276.         try{
  277.             for (Field field : objectClass.getDeclaredFields()) {
  278.                 field.setAccessible(true);
  279.  
  280.                 String type = field.getGenericType().getTypeName();
  281.  
  282.                 if(type.equals("int") && !field.getName().equals("id")){
  283.                     query = query + field.getName() + " ";
  284.                     query = query + "integer not null,\n";
  285.                 } else if (type.equals("double") || type.equals("float")){
  286.                     query = query + field.getName() + " ";
  287.                     query = query + "real not null,\n";
  288.                 } else if (type.equals("java.lang.String")){
  289.                     query = query + field.getName() + " ";
  290.                     query = query + "text not null,\n";
  291.                 } else if (type.equals("boolean") && !field.getName().equals("isExpired")){
  292.                     query = query + field.getName() + " ";
  293.                     query = query + "boolean not null,\n";
  294.                 } else if (type.indexOf(".") > 0){
  295.                     if(!checkTableExist(type)){
  296.                         createTable(Class.forName(type));
  297.                     }
  298.                     query = query + "id_" + field.getName() + " integer references " +
  299.                             "t_" + type.replace(".", "_").toLowerCase() + "(id),\n";
  300.  
  301.                 }
  302.  
  303.             }
  304.         } catch (Exception e){
  305.             System.err.println("Error was occured while table was being created " + e);
  306.         }
  307.  
  308.         query = query.substring(0, query.lastIndexOf(","));
  309.         query = query + ");";
  310.  
  311.         //System.out.println(query);
  312.  
  313.         try{
  314.             Statement stmt = connection.createStatement();
  315.             ResultSet rs = stmt.executeQuery(query);
  316.             rs.close();
  317.             stmt.close();
  318.         } catch (SQLException e){
  319.             //System.err.println(e);
  320.         }
  321.     }
  322.  
  323.     private static int getNextID(Class objectClass){
  324.         try(Statement stmt = connection.createStatement()){
  325.             ResultSet rs = stmt.executeQuery("select count(*) from (select * from " + getTableName(objectClass) + " ) as a;");
  326.             rs.next();
  327.             return rs.getInt("count") + 1;
  328.  
  329.         } catch (SQLException e){
  330.             System.err.println(e);
  331.         }
  332.  
  333.         return 1;
  334.     }
  335.  
  336.     public static AvailableForORM createObject(Class objectClass, ArrayList<Object> list){
  337.         if(objectClass == null){
  338.             System.err.println("ObjectClass is null");
  339.             return null;
  340.         }
  341.  
  342.         boolean canBeUsed = false;
  343.         for(Class temp : objectClass.getInterfaces()){
  344.             if(temp.getName().equals("DB.AvailableForORM")){
  345.                 canBeUsed = true;
  346.             }
  347.         }
  348.         if(!canBeUsed){
  349.             return null;
  350.         }
  351.  
  352.         if(!checkTableExist(objectClass)){
  353.             createTable(objectClass);
  354.         }
  355.  
  356.         String query = "insert into " + getTableName(objectClass) + "\n" +
  357.                 "values ( " ;
  358.  
  359.         AvailableForORM object = null;
  360.         try{
  361.             object = (AvailableForORM)objectClass.newInstance();
  362.         } catch (InstantiationException|IllegalAccessException e) {
  363.             System.err.println(e);
  364.             return null;
  365.         }
  366.  
  367.         int object_id = getNextID(objectClass);
  368.         try{
  369.             boolean id = false;
  370.             for (Field field : object.getClass().getDeclaredFields()) {
  371.                 field.setAccessible(true);
  372.                 String type = field.getGenericType().getTypeName();
  373.  
  374.                 //System.out.println(list.get(0).getClass().getTypeName());
  375.                 if(!id){
  376.                     query = query + " " + object_id + ", ";
  377.                     field.set(object, object_id);
  378.                     id = true;
  379.                     continue;
  380.                 }
  381.  
  382.                 if(field.getName().equals("isExpired")){
  383.                     continue;
  384.                 }
  385.  
  386.                 //System.out.println(field.getName());
  387.                 if(type.equals("int") && list.get(0).getClass().getTypeName().equals("java.lang.Integer")){
  388.                     query = query +  " " + (Integer)list.get(0) + ", ";
  389.                     field.set(object, list.get(0));
  390.                 } else if ((type.equals("double") || type.equals("float")) && list.get(0).getClass().getTypeName().equals("java.lang.Double")){
  391.                     query = query +  " " + (Double)list.get(0) + ", ";
  392.                     field.set(object, list.get(0));
  393.                 } else if (type.equals("java.lang.String") && list.get(0).getClass().getTypeName().equals("java.lang.String")){
  394.                     query = query +  " '" + (String)list.get(0) + "', ";
  395.                     field.set(object, list.get(0));
  396.                 } else if (type.equals("boolean") && list.get(0).getClass().getTypeName().equals("java.lang.Boolean")){
  397.                     query = query +  " " + (Boolean)list.get(0) + ", ";
  398.                     field.set(object, list.get(0));
  399.                 } else if (type.indexOf(".") > 0){
  400.                     if(!checkTableExist(type)){
  401.                         createTable(Class.forName(type));
  402.                     }
  403.                     AvailableForORM temp = createObject(Class.forName(type), list);
  404.  
  405.                     query = query + " " + temp.getID() + ", ";
  406.                     field.set(object, temp);
  407.                 }
  408.                 if(list.size() > 0){
  409.                     list.remove(0);
  410.                 }
  411.             }
  412.         } catch (Exception e){
  413.             System.err.println("Error was occured while table was being created " + e);
  414.         }
  415.  
  416.         query = query.substring(0, query.lastIndexOf(","));
  417.         query = query + ");";
  418.  
  419.         //System.out.println(query);
  420.  
  421.         try{
  422.             Statement stmt = connection.createStatement();
  423.             ResultSet rs = stmt.executeQuery(query);
  424.             rs.close();
  425.             stmt.close();
  426.         } catch (SQLException e){
  427.             //System.err.println(e);
  428.         }
  429.  
  430.         return object;
  431.     }
  432.  
  433.     public static boolean removeObject(AvailableForORM object){
  434.         if(object.isExpired() || !checkObjectExist(object)){
  435.             return false;
  436.         }
  437.  
  438.         try{
  439.             for (Field field : object.getClass().getDeclaredFields()) {
  440.                 field.setAccessible(true);
  441.  
  442.                 String type = field.getGenericType().getTypeName();
  443.  
  444.                 if (type.indexOf(".") > 0){
  445.                     removeObject((AvailableForORM)field.get(object));
  446.                 }
  447.             }
  448.         } catch (Exception e){
  449.         }
  450.  
  451.         try{
  452.             Statement stmt = connection.createStatement();
  453.             ResultSet rs = stmt.executeQuery( "delete from " + getTableName(object.getClass()) + " where id=" + object.getID() +";" );
  454.             stmt.close();
  455.         } catch (SQLException e) {
  456.             return false;
  457.         }
  458.         return true;
  459.     }
  460.  
  461.     public static boolean updateObject(AvailableForORM object){
  462.         if(!checkObjectExist(object)){
  463.             return false;
  464.         }
  465.  
  466.         String query = "update " + getTableName(object.getClass()) + " set \n";
  467.  
  468.         try{
  469.             for (Field field : object.getClass().getDeclaredFields()) {
  470.                 field.setAccessible(true);
  471.                 String type = field.getGenericType().getTypeName();
  472.  
  473.                 if(field.getName().equals("isExpired")){
  474.                     continue;
  475.                 }
  476.  
  477.                 if(type.equals("int") && !field.getName().equals("id")){
  478.                     query = query +  " " + field.getName() + "=" + (Integer)field.get(object) + ", ";
  479.                 } else if ((type.equals("double") || type.equals("float"))){
  480.                     query = query +  " " + field.getName() + "=" + (Double)field.get(object)+ ", ";
  481.                 } else if (type.equals("java.lang.String")){
  482.                     query = query +  " " + field.getName() + "='" + (String)field.get(object) + "', ";
  483.                 } else if (type.equals("boolean")){
  484.                     query = query +  " " + field.getName() + "=" + (Boolean)field.get(object) + ", ";
  485.                 } else if (type.indexOf(".") > 0){
  486.                     query = query + " " + ((AvailableForORM)field.get(object)).getID() + ", ";
  487.                 }
  488.             }
  489.         } catch (Exception e){
  490.             System.err.println("Error was occured while table was being created " + e);
  491.             return false;
  492.         }
  493.         query = query.substring(0, query.lastIndexOf(","));
  494.         query = query + " where id=" + object.getID() + ";";
  495.  
  496.         //System.out.println(query);
  497.  
  498.         try{
  499.             Statement stmt = connection.createStatement();
  500.             ResultSet rs = stmt.executeQuery(query);
  501.             rs.close();
  502.             stmt.close();
  503.         } catch (SQLException e){
  504.             //System.err.println(e);
  505.         }
  506.  
  507.         return true;
  508.     }
  509.  
  510.     public static AvailableForORM loadObject(Class objectClass, int id){
  511.         if(!checkTableExist(objectClass)){
  512.             return null;
  513.         }
  514.         AvailableForORM object = null;
  515.         try{
  516.             Statement stmt = connection.createStatement();
  517.             ResultSet rs = stmt.executeQuery("select * from " + getTableName(objectClass) + " where id=" + id + ";");
  518.             try{
  519.                 object = (AvailableForORM)objectClass.newInstance();
  520.             } catch (InstantiationException|IllegalAccessException e) {
  521.                 System.err.println(e);
  522.                 return null;
  523.             }
  524.  
  525.             int i = 1;
  526.             while(rs.next()) {
  527.                 for(Field field : object.getClass().getDeclaredFields()){
  528.                     field.setAccessible(true);
  529.                     String type = field.getGenericType().getTypeName();
  530.  
  531.                     if(field.getName().equals("isExpired")){
  532.                         field.set(object, false);
  533.                         continue;
  534.                     }
  535.  
  536.                     if(type.equals("int")){
  537.                         field.set(object, rs.getInt(i));
  538.                         i++;
  539.                     } else if (type.equals("double") || type.equals("float")){
  540.                         field.set(object, rs.getDouble(i));
  541.                         i++;
  542.                     } else if (type.equals("java.lang.String")){
  543.                         field.set(object, rs.getString(i));
  544.                         i++;
  545.                     } else if (type.equals("boolean")){
  546.                         field.set(object, rs.getBoolean(i));
  547.                         i++;
  548.                     } else if (type.indexOf(".") > 0){
  549.                         AvailableForORM temp = loadObject(Class.forName(type), rs.getInt(i));
  550.                         i++;
  551.                         field.set(object, temp);
  552.                     }
  553.                 }
  554.             }
  555.         } catch (Exception e){
  556.             System.err.println("Error was occured while table was being created " + e);
  557.         }
  558.         return object;
  559.     }
  560.  
  561.     public static int getObjectsAmount(Class objectClass){
  562.         if(!checkTableExist(objectClass)){
  563.             return 0;
  564.         }
  565.  
  566.         try{
  567.             Statement stmt = connection.createStatement();
  568.             ResultSet rs = stmt.executeQuery( "select count(*) from ( select * from " + getTableName(objectClass) + ") as a;" );
  569.             rs.next();
  570.             return rs.getInt(1);
  571.         } catch (SQLException e) {
  572.             System.err.println(e);
  573.         }
  574.  
  575.         return 0;
  576.     }
  577. }
  578.  
  579.  
  580.  
  581. import java.lang.reflect.Field;
  582.  
  583. public class User implements AvailableForORM{
  584.     private int id;
  585.     private boolean admin;
  586.     private UserProperties prop;
  587.     private boolean isExpired = false;
  588.  
  589.     public int getID(){
  590.         return id;
  591.     }
  592.  
  593.     public void update(){
  594.         this.isExpired = !LittleORM.checkObjectExist(this);
  595.         // from base
  596.     }
  597.  
  598.     public boolean isAdmin(){
  599.         update();
  600.         return admin;
  601.     }
  602.  
  603.     public void setName(String name){
  604.         if(isExpired){
  605.             return;
  606.         }
  607.         prop.setName(name);
  608.         LittleORM.updateObject(this);
  609.     }
  610.  
  611.     public String getName(){
  612.         update();
  613.         return prop.getName();
  614.     }
  615.  
  616.     public boolean isExpired(){
  617.         update();
  618.         return isExpired;
  619.     }
  620.  
  621.     public void setPassword(String password){
  622.         prop.setPassword(password);
  623.         LittleORM.updateObject(this);
  624.     }
  625.  
  626.     public boolean isPassword(String password){
  627.         update();
  628.         if(isExpired){
  629.             return false;
  630.         }
  631.         return prop.isPassword(password);
  632.     }
  633.  
  634.     public User(){
  635.         ;
  636.     }
  637.  
  638.     public User(boolean admin, String name, String password){
  639.         prop = new UserProperties(name, password);
  640.         this.admin = admin;
  641.  
  642.     }
  643. }
  644.  
  645.  
  646.  
  647. public class UserProperties implements AvailableForORM{
  648.     private int id;
  649.     private String name;
  650.     private String password;
  651.     private boolean isExpired = false;
  652.  
  653.     public UserProperties(){
  654.         ;
  655.     }
  656.  
  657.     public UserProperties(String name, String password){
  658.         this.name = name;
  659.         this.password = password;
  660.     }
  661.  
  662.     public void update(){
  663.         this.isExpired = !LittleORM.checkObjectExist(this);
  664.         // from base
  665.     }
  666.  
  667.     public void setPassword(String password){
  668.         this.password = password;
  669.         LittleORM.updateObject(this);
  670.     }
  671.  
  672.     public boolean isExpired(){
  673.         update();
  674.         return isExpired;
  675.     }
  676.  
  677.     public int getID(){
  678.         return id;
  679.     }
  680.  
  681.     public void setName(String name){
  682.         this.name = name;
  683.         LittleORM.updateObject(this);
  684.     }
  685.  
  686.     public String getName(){
  687.         update();
  688.         return name;
  689.     }
  690.  
  691.     public boolean isPassword(String password){
  692.         update();
  693.         return this.password.equals(password);
  694.     }
  695.  
  696. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement