Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.tse.fise2.info4.lab11;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.List;
- /**
- *
- * Complete this class for question
- *
- * @author you
- * @author Julien Subercaze
- *
- *
- */
- public class ORMSimple {
- /**
- * Contains the new line characters, for the current OS
- */
- public static final String LINE_SEPARATOR = System.getProperty("line.separator");
- /**
- * Tab separator
- */
- private static final char TAB = '\t';
- /**
- * To put variables into quotes
- */
- private static final char SQL_QUOTE = '`';
- /**
- *
- * @param clazz
- * @return the create statement for MySQL
- */
- public static String generateTable(Class clazz) {
- int i = 0;
- StringBuilder sb = new StringBuilder();
- List<Field> fields = ReflectionUtils.listFields(clazz);
- sb.append("CREATE TABLE ");
- sb.append(clazz.getSimpleName());
- sb.append(" \n(");
- for (Field f : fields) {
- if (i > 0) {
- sb.append(", ");
- }
- sb.append("\n");
- sb.append(TAB + f.getName() + " ");
- sb.append(convertType(f.getType()));
- i++;
- }
- sb.append("\n);");
- return sb.toString();
- }
- /**
- *
- * @param type
- * @return the SQL type corresponding to the input type
- */
- private static String convertType(Class type) {
- if (type.equals(Integer.class)) {
- return "INTEGER";
- } else if (type.equals(String.class)) {
- return "TEXT";
- } else {
- throw new IllegalArgumentException();
- }
- }
- public static String generateInsertStatement(Object obj) throws NoSuchMethodException, SecurityException,
- IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- Class clazz = obj.getClass();
- int i = 0;
- StringBuilder sb = new StringBuilder();
- List<Field> fields = ReflectionUtils.listFields(clazz);
- sb.append("INSERT INTO ");
- sb.append(clazz.getSimpleName());
- sb.append(" (");
- for (Field f : fields) {
- if (i > 0) {
- sb.append(", ");
- }
- sb.append(f.getName());
- i++;
- }
- sb.append(") VALUES (");
- i = 0;
- for (Field f : fields) {
- if (i > 0) {
- sb.append(", ");
- }
- String getterName = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);;
- Method fieldGetter = clazz.getMethod(getterName, null);
- String value = fieldGetter.invoke(obj, null).toString();
- sb.append(SQL_QUOTE + value + SQL_QUOTE);
- i++;
- }
- sb.append(")");
- return sb.toString();
- }
- public static void main(String[] args) {
- // show create statement
- System.out.println(ORMSimple.generateTable(Car.class));
- // show insert statement
- // Car c1 = new Car("peugeot", "205", "2E 234 42", 1997);
- // try {
- // System.out.println(ORMSimple.generateInsertStatement(c1));
- // } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
- // | InvocationTargetException e) {
- // e.printStackTrace();
- // }
- }
- }
Add Comment
Please, Sign In to add comment