8 files + 169 − 0 Side-by-side Compare changes Side-by-side Inline Show whitespace changes Files 8 java-utils @ 31e85409 Original line number Original line Diff line number Diff line Subproject commit 31e85409a0d548c7d6c13033a09be1c2a7ab5dfd pom.xml +7 −0 Original line number Original line Diff line number Diff line Loading @@ -20,4 +20,11 @@ </plugin> </plugin> </plugins> </plugins> </build> </build> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.1</version> </dependency> </dependencies> </project> </project> src/main/java/fr/univlille/iut/info/webutils/model/data/DAOBasic.java 0 → 100644 +78 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; abstract class DAOBasic<T> { private static final Map<Class, Method> sqlGetters; static { try { sqlGetters = Map.of( String.class, ResultSet.class.getDeclaredMethod("getString", String.class) ); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } private Class<T> pojoClass; private Field[] pojoAttributes; private Constructor<T> pojoConstructor; private List<JDBCReflection> jdbcReflectionsMethods; private Object[] pojoConstructorParams; private String tableName; public DAOBasic(Class<T> pojoClass) throws NoSuchMethodException { this.pojoClass = pojoClass; this.pojoAttributes = pojoClass.getDeclaredFields(); this.pojoConstructor = pojoClass.getConstructor(pojoClass.getConstructors()[0].getParameterTypes()); this.jdbcReflectionsMethods = new ArrayList<JDBCReflection>(pojoClass.getDeclaredFields().length); this.pojoConstructorParams = new Object[pojoClass.getDeclaredFields().length]; for (Field attribute : this.pojoAttributes) { this.jdbcReflectionsMethods.add(new JDBCReflection(DAOBasic.sqlGetters.get(attribute.getType()), attribute.getName())); } this.tableName = pojoClass.getSimpleName().toLowerCase(); } public List<T> findAll() throws SQLException, IOException, ClassNotFoundException { String sqlRequest = "SELECT * FROM " + this.tableName; DBLoop<List<T>> loop = new DBLoop<List<T>>(new ArrayList<T>(), sqlRequest, (((list, resultSet) -> { for (int i = 0; i < this.pojoAttributes.length; i++) { try { this.pojoConstructorParams[i] = this.jdbcReflectionsMethods.get(i).execute(resultSet); } catch (InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } } try { list.add(this.pojoClass.cast(this.pojoConstructor.newInstance(this.pojoConstructorParams))); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }))); return loop.executeLoop(); } } No newline at end of file src/main/java/fr/univlille/iut/info/webutils/model/data/JDBCReflection.java 0 → 100644 +27 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.ResultSet; class JDBCReflection { private Method sqlMethod; private String colName; public JDBCReflection(Method sqlMethod, String colName) { this.sqlMethod = sqlMethod; this.colName = colName; } public Method getSqlMethod() { return sqlMethod; } public Object execute(ResultSet rs) throws InvocationTargetException, IllegalAccessException { return this.sqlMethod.invoke(rs, this.colName); } } src/test/java/fr/univlille/iut/info/webutils/model/data/DAOBasicTest.java 0 → 100644 +21 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.util.List; public class DAOBasicTest { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); DBConnector.init(new DBConnectionDescriptor( "org.postgresql.Driver", "jdbc:postgresql://localhost:5432/but2", "nicolasdagneauxetu", "moi")); EtudiantDAO dao = new EtudiantDAO(); List<Etudiant> etus = dao.findAll(); System.out.println(etus); } } src/test/java/fr/univlille/iut/info/webutils/model/data/Etudiant.java 0 → 100644 +9 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.sql.SQLException; import java.util.List; public record Etudiant(String nom, String prenom) { } src/test/java/fr/univlille/iut/info/webutils/model/data/EtudiantDAO.java 0 → 100644 +18 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.sql.SQLException; import java.util.List; public class EtudiantDAO extends DAOBasic<Etudiant> { public EtudiantDAO() throws NoSuchMethodException { super(Etudiant.class); } public List<Etudiant> findAll() throws SQLException, IOException, ClassNotFoundException { return super.findAll(); } } web.utils.iml 0 → 100644 +8 −0 Original line number Original line Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <module version="4"> <component name="AdditionalModuleElements"> <content url="file://$MODULE_DIR$" dumb="true"> <excludeFolder url="file://$MODULE_DIR$/old" /> </content> </component> </module> No newline at end of file
java-utils @ 31e85409 Original line number Original line Diff line number Diff line Subproject commit 31e85409a0d548c7d6c13033a09be1c2a7ab5dfd
pom.xml +7 −0 Original line number Original line Diff line number Diff line Loading @@ -20,4 +20,11 @@ </plugin> </plugin> </plugins> </plugins> </build> </build> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.1</version> </dependency> </dependencies> </project> </project>
src/main/java/fr/univlille/iut/info/webutils/model/data/DAOBasic.java 0 → 100644 +78 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; abstract class DAOBasic<T> { private static final Map<Class, Method> sqlGetters; static { try { sqlGetters = Map.of( String.class, ResultSet.class.getDeclaredMethod("getString", String.class) ); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } private Class<T> pojoClass; private Field[] pojoAttributes; private Constructor<T> pojoConstructor; private List<JDBCReflection> jdbcReflectionsMethods; private Object[] pojoConstructorParams; private String tableName; public DAOBasic(Class<T> pojoClass) throws NoSuchMethodException { this.pojoClass = pojoClass; this.pojoAttributes = pojoClass.getDeclaredFields(); this.pojoConstructor = pojoClass.getConstructor(pojoClass.getConstructors()[0].getParameterTypes()); this.jdbcReflectionsMethods = new ArrayList<JDBCReflection>(pojoClass.getDeclaredFields().length); this.pojoConstructorParams = new Object[pojoClass.getDeclaredFields().length]; for (Field attribute : this.pojoAttributes) { this.jdbcReflectionsMethods.add(new JDBCReflection(DAOBasic.sqlGetters.get(attribute.getType()), attribute.getName())); } this.tableName = pojoClass.getSimpleName().toLowerCase(); } public List<T> findAll() throws SQLException, IOException, ClassNotFoundException { String sqlRequest = "SELECT * FROM " + this.tableName; DBLoop<List<T>> loop = new DBLoop<List<T>>(new ArrayList<T>(), sqlRequest, (((list, resultSet) -> { for (int i = 0; i < this.pojoAttributes.length; i++) { try { this.pojoConstructorParams[i] = this.jdbcReflectionsMethods.get(i).execute(resultSet); } catch (InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } } try { list.add(this.pojoClass.cast(this.pojoConstructor.newInstance(this.pojoConstructorParams))); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }))); return loop.executeLoop(); } } No newline at end of file
src/main/java/fr/univlille/iut/info/webutils/model/data/JDBCReflection.java 0 → 100644 +27 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.ResultSet; class JDBCReflection { private Method sqlMethod; private String colName; public JDBCReflection(Method sqlMethod, String colName) { this.sqlMethod = sqlMethod; this.colName = colName; } public Method getSqlMethod() { return sqlMethod; } public Object execute(ResultSet rs) throws InvocationTargetException, IllegalAccessException { return this.sqlMethod.invoke(rs, this.colName); } }
src/test/java/fr/univlille/iut/info/webutils/model/data/DAOBasicTest.java 0 → 100644 +21 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.util.List; public class DAOBasicTest { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); DBConnector.init(new DBConnectionDescriptor( "org.postgresql.Driver", "jdbc:postgresql://localhost:5432/but2", "nicolasdagneauxetu", "moi")); EtudiantDAO dao = new EtudiantDAO(); List<Etudiant> etus = dao.findAll(); System.out.println(etus); } }
src/test/java/fr/univlille/iut/info/webutils/model/data/Etudiant.java 0 → 100644 +9 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.sql.SQLException; import java.util.List; public record Etudiant(String nom, String prenom) { }
src/test/java/fr/univlille/iut/info/webutils/model/data/EtudiantDAO.java 0 → 100644 +18 −0 Original line number Original line Diff line number Diff line package fr.univlille.iut.info.webutils.model.data; import java.io.IOException; import java.sql.SQLException; import java.util.List; public class EtudiantDAO extends DAOBasic<Etudiant> { public EtudiantDAO() throws NoSuchMethodException { super(Etudiant.class); } public List<Etudiant> findAll() throws SQLException, IOException, ClassNotFoundException { return super.findAll(); } }
web.utils.iml 0 → 100644 +8 −0 Original line number Original line Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <module version="4"> <component name="AdditionalModuleElements"> <content url="file://$MODULE_DIR$" dumb="true"> <excludeFolder url="file://$MODULE_DIR$/old" /> </content> </component> </module> No newline at end of file