Last active
April 9, 2020 01:29
-
-
Save kencoba/f8f44e2198ce04d8ad4b to your computer and use it in GitHub Desktop.
REPL for JPQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client; | |
import java.io.InputStream; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Scanner; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Parameter; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
/* | |
* REPL for JPQL | |
*/ | |
public class JpqlExecutor { | |
private EntityManager entityManager; | |
public static void main(String[] args) { | |
if (args.length != 1) { | |
System.err.println("No persistence unit name were specified on the command-line parameter!"); | |
System.err.println("Usage : java JpqlExecutor persistence-unit-name"); | |
} else { | |
System.out.println("JPQL Executor ver 1.00"); | |
System.out.println("Program will exit when empty JPQL String is specified."); | |
JpqlExecutor exec = new JpqlExecutor(args[0]); | |
exec.loop(System.in); | |
} | |
} | |
public JpqlExecutor(String unitName) { | |
entityManager = createEntityManager(unitName); | |
} | |
public EntityManager createEntityManager(String unitName) { | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName); | |
return emf.createEntityManager(); | |
} | |
public void loop(InputStream source) { | |
try (Scanner in = new Scanner(source)) { | |
while (true) { | |
System.out.print("JPQL? : "); | |
String jpql = in.nextLine(); | |
// If JPQL string is empty, then exit. | |
if (jpql.trim().equals("")) { | |
break; | |
} | |
try { | |
List<Object> result = eval(in, jpql); | |
print(result); | |
} catch (RuntimeException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
} | |
private void print(List<Object> result) { | |
System.out.println("--- result ---"); | |
for (Object r : result) { | |
if (r instanceof Object[]) { | |
for (Object s : (Object[]) r) { | |
System.out.print(s + " | "); | |
} | |
System.out.println(); | |
} else { | |
System.out.println(r); | |
} | |
} | |
} | |
@SuppressWarnings("unchecked") | |
private List<Object> eval(Scanner in, String jpql) { | |
List<KeyMap> keymaps = new ArrayList<KeyMap>(); | |
Query q = entityManager.createQuery(jpql); | |
for (Parameter<?> p : q.getParameters()) { | |
System.out.printf("value of '%s'? : ", p.getName()); | |
keymaps.add(new KeyMap(p.getName(), p.getParameterType(), in.nextLine())); | |
} | |
return executeQuery(q, keymaps); | |
} | |
public void setEntityManager(EntityManager entityManager) { | |
this.entityManager = entityManager; | |
} | |
public class KeyMap { | |
String key; | |
Class<?> type; | |
String value; | |
public KeyMap(String key, Class<?> type, String value) { | |
this.key = key; | |
this.type = type; | |
this.value = value; | |
} | |
public Object getNativeValue() { | |
switch (type.getName()) { | |
case "java.lang.Integer": | |
return Integer.parseInt(value); | |
case "java.lang.Date": | |
try { | |
return convertStringToDateTime(value); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
return new Date(); | |
} | |
case "java.lang.Long": | |
return Long.parseLong(value); | |
case "java.lang.Boolean": | |
return Boolean.getBoolean(value); | |
default: | |
return value; | |
} | |
} | |
public Date convertStringToDateTime(String stringDate) throws ParseException { | |
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); | |
Date date = dateformat.parse(stringDate); | |
return date; | |
} | |
} | |
@SuppressWarnings("rawtypes") | |
public List executeQuery(Query q, List<KeyMap> mappings) { | |
for (KeyMap m : mappings) { | |
q.setParameter(m.key, m.getNativeValue()); | |
} | |
return q.getResultList(); | |
} | |
@SuppressWarnings("rawtypes") | |
public List executeQuery(String jpql, List<KeyMap> mappings) { | |
return executeQuery(entityManager.createQuery(jpql), mappings); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment