Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. package fr.tse.fise2.info4.lab11;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.List;
  7.  
  8. /**
  9. *
  10. * Complete this class for question
  11. *
  12. * @author you
  13. * @author Julien Subercaze
  14. *
  15. *
  16. */
  17. public class ORMSimple {
  18.  
  19. /**
  20. * Contains the new line characters, for the current OS
  21. */
  22. public static final String LINE_SEPARATOR = System.getProperty("line.separator");
  23. /**
  24. * Tab separator
  25. */
  26. private static final char TAB = '\t';
  27. /**
  28. * To put variables into quotes
  29. */
  30. private static final char SQL_QUOTE = '`';
  31.  
  32. /**
  33. *
  34. * @param clazz
  35. * @return the create statement for MySQL
  36. */
  37. public static String generateTable(Class clazz) {
  38. int i = 0;
  39. StringBuilder sb = new StringBuilder();
  40. List<Field> fields = ReflectionUtils.listFields(clazz);
  41. sb.append("CREATE TABLE ");
  42. sb.append(clazz.getSimpleName());
  43. sb.append(" \n(");
  44. for (Field f : fields) {
  45. if (i > 0) {
  46. sb.append(", ");
  47. }
  48. sb.append("\n");
  49. sb.append(TAB + f.getName() + " ");
  50. sb.append(convertType(f.getType()));
  51. i++;
  52. }
  53. sb.append("\n);");
  54. return sb.toString();
  55. }
  56.  
  57. /**
  58. *
  59. * @param type
  60. * @return the SQL type corresponding to the input type
  61. */
  62. private static String convertType(Class type) {
  63. if (type.equals(Integer.class)) {
  64. return "INTEGER";
  65. } else if (type.equals(String.class)) {
  66. return "TEXT";
  67. } else {
  68. throw new IllegalArgumentException();
  69. }
  70.  
  71. }
  72.  
  73. public static String generateInsertStatement(Object obj) throws NoSuchMethodException, SecurityException,
  74. IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  75. Class clazz = obj.getClass();
  76. int i = 0;
  77. StringBuilder sb = new StringBuilder();
  78. List<Field> fields = ReflectionUtils.listFields(clazz);
  79. sb.append("INSERT INTO ");
  80. sb.append(clazz.getSimpleName());
  81. sb.append(" (");
  82. for (Field f : fields) {
  83. if (i > 0) {
  84. sb.append(", ");
  85. }
  86. sb.append(f.getName());
  87. i++;
  88. }
  89. sb.append(") VALUES (");
  90.  
  91. i = 0;
  92. for (Field f : fields) {
  93. if (i > 0) {
  94. sb.append(", ");
  95. }
  96. String getterName = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);;
  97.  
  98. Method fieldGetter = clazz.getMethod(getterName, null);
  99. String value = fieldGetter.invoke(obj, null).toString();
  100. sb.append(SQL_QUOTE + value + SQL_QUOTE);
  101. i++;
  102. }
  103. sb.append(")");
  104. return sb.toString();
  105. }
  106.  
  107. public static void main(String[] args) {
  108.  
  109. // show create statement
  110. System.out.println(ORMSimple.generateTable(Car.class));
  111.  
  112. // show insert statement
  113. // Car c1 = new Car("peugeot", "205", "2E 234 42", 1997);
  114. // try {
  115. // System.out.println(ORMSimple.generateInsertStatement(c1));
  116. // } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
  117. // | InvocationTargetException e) {
  118. // e.printStackTrace();
  119. // }
  120. }
  121. }
Add Comment
Please, Sign In to add comment