
com.jwebmp.entityassist.querybuilder.statements.InsertStatement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of entity-assist Show documentation
Show all versions of entity-assist Show documentation
A Domain Driven SQL Builder Generator for JPMS/JDK8
package com.jwebmp.entityassist.querybuilder.statements;
import com.google.common.base.Strings;
import com.jwebmp.entityassist.BaseEntity;
import com.jwebmp.entityassist.querybuilder.EntityAssistStrings;
import com.jwebmp.logger.LogFactory;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* An insert statement
*/
public class InsertStatement
implements EntityAssistStrings
{
private static final Logger log = LogFactory.getLog(InsertStatement.class.getName());
private static final String HEXES = "0123456789ABCDEF";
private static InsertStatement insertStatement = new InsertStatement();
/**
* The standard sdf format
*/
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
/**
* Returns teh date formatter
*/
private final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* Returns the date time formmatter
*/
private final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
private InsertStatement()
{
//Nothing needed
}
/**
* Builds the physical insert string for this entity class
*
* @return
*/
@NotNull
public static String buildInsertString(Object o)
{
StringBuilder insertString = new StringBuilder("INSERT INTO ");
Class> c = o.getClass();
Table t = c.getAnnotation(Table.class);
String tableName = "";
if (t != null)
{
tableName = t.name();
}
if (tableName.isEmpty())
{
Entity e = c.getAnnotation(Entity.class);
if (e != null)
{
tableName = e.name();
}
}
if (tableName.isEmpty())
{
tableName = o.getClass()
.getSimpleName();
}
insertString.append(tableName)
.append(" (");
List fields = new ArrayList<>();
Class> i = c;
while (i != null)
{
Collections.addAll(fields, i.getDeclaredFields());
i = i.getSuperclass();
}
List columnsNames = new ArrayList<>();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy