com.github.drinkjava2.jsqlbox
Class SqlBoxContextUtils

java.lang.Object
  extended by com.github.drinkjava2.jsqlbox.SqlBoxContextUtils

public abstract class SqlBoxContextUtils
extends Object

Since:
1.7.0
Author:
Yong Zhu

Constructor Summary
SqlBoxContextUtils()
           
 
Method Summary
protected static void appendLeftJoinSQL(PreparedSQL ps)
          Based on PreparedSQL's models and alias, automatically build and append a SQL like below:
static void createLastAutoAliasName(PreparedSQL ps)
          Create auto Alias name based on capital letters of class name in models of PreparedSQL, if alias already exists, put a number at end, for example:
User ->u
User, UserRole, UserOther, Order, Order_a -> u, u1, u2, o, o1 if no EntityClass, alais will be created by table name, for example:
user ->u
user_role -> UR
static EntityNet entityAutoNet(SqlBoxContext ctx, Class<?>... entityClasses)
           
static int entityCountAll(SqlBoxContext ctx, Class<?> entityClass, Object... optionItems)
          Count quantity of all entity, this method does not support sharding
static int entityDeleteByIdTry(SqlBoxContext ctx, Class<?> entityClass, Object id, Object... optionItems)
          Try delete entity by Id, return row affected, return row affected
static int entityDeleteTry(SqlBoxContext ctx, Object entityBean, Object... optionItems)
          Delete entityBean in database according primary key value, return row affected
static boolean entityExist(SqlBoxContext ctx, Object entityBean, Object... optionItems)
          Check if entityBean exist in database by its id
static boolean entityExistById(SqlBoxContext ctx, Class<?> entityClass, Object id, Object... optionItems)
          Try delete entity by Id, return row affected
static
<T> List<T>
entityFindAll(SqlBoxContext ctx, Class<T> entityClass, Object... optionItems)
           
static
<T> List<T>
entityFindBySample(SqlBoxContext ctx, Object sampleBean, Object... sqlItems)
           
static
<E> List<E>
entityFindRelatedList(SqlBoxContext ctx, Object entity, Object... sqlItems)
           
static
<E> Map<Object,E>
entityFindRelatedMap(SqlBoxContext ctx, Object entity, Object... sqlItems)
           
static
<E> E
entityFindRelatedOne(SqlBoxContext ctx, Object entity, Object... sqlItems)
           
static
<E> Set<E>
entityFindRelatedSet(SqlBoxContext ctx, Object entity, Object... sqlItems)
           
static int entityInsertTry(SqlBoxContext ctx, Object entityBean, Object... optionItems)
          Insert entityBean into database, and change ID fields to values generated by IdGenerator (identity or sequence or UUID...), return row affected
static
<T> T
entityLoadByIdTry(SqlBoxContext ctx, Class<T> entityClass, Object id, Object... optionItems)
          Create a new Entity, load from DB according given ID, return null if entity does not exist in DB
static int entityLoadTry(SqlBoxContext ctx, Object entityBean, Object... optionItems)
          Load entity according entity's id fields, return row affected
static
<T> T
entityOrClassToBean(Object entityOrClass)
          Transfer Object to TableModel, object can be SqlBox instance, entityClass or entity Bean
static int entityUpdateTry(SqlBoxContext ctx, Object entityBean, Object... optionItems)
          Update entityBean according primary key, return row affected
static TableModel[] findAllModels(Object... sqlItems)
          Extract models from sqlItems
static TableModel findEntityOrClassTableModel(Object entityOrClass)
           
static TableModel findFirstModel(Object... optionItems)
           
static Object[] findModelAlias(Object... sqlItems)
          Find model and alias items from sqlItems
static Object[] findNotModelAlias(Object... sqlItems)
          Find not model/alias items from sqlItems
static TableModel findTableModel(Object entityOrClass, Object... optionItems)
          if optionItems has a TabelModel, use it, otherwise find tablemodel from entityOrClass
static TableModel findTailModel(SqlBoxContext ctx, TableModel entityModel, Object... optionItems)
          Return first tail TableModel, if not found return null
static SqlBoxContext getShardedDB(SqlBoxContext currentCtx, Object entityOrClass, Object... shardKey)
          Use current SqlBoxContext's shardingTools to calculate the master SqlBoxContext
static String getShardedTB(SqlBoxContext ctx, Object entityOrClass, Object... shardKey)
          Use current SqlBoxContext's shardingTools to calculate the real shardTable name
static TableModel[] loadMetaTableModels(SqlBoxContext ctx, Dialect dialect)
          Read database Meta info into SqlBox[]
static
<T> T
mapToEntityBean(TableModel model, Map<String,Object> oneRow)
          Convert one row data into EntityBean
static Object readValueFromBeanFieldOrTail(Object entityBean, ColumnModel columnModel)
          Read value from entityBean field or tail
static void writeValueToBeanFieldOrTail(Object entityBean, ColumnModel columnModel, Object value)
          write value to entityBean field or tail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlBoxContextUtils

public SqlBoxContextUtils()
Method Detail

loadMetaTableModels

public static TableModel[] loadMetaTableModels(SqlBoxContext ctx,
                                               Dialect dialect)
Read database Meta info into SqlBox[]


getShardedTB

public static String getShardedTB(SqlBoxContext ctx,
                                  Object entityOrClass,
                                  Object... shardKey)
Use current SqlBoxContext's shardingTools to calculate the real shardTable name


getShardedDB

public static SqlBoxContext getShardedDB(SqlBoxContext currentCtx,
                                         Object entityOrClass,
                                         Object... shardKey)
Use current SqlBoxContext's shardingTools to calculate the master SqlBoxContext


findTableModel

public static TableModel findTableModel(Object entityOrClass,
                                        Object... optionItems)
if optionItems has a TabelModel, use it, otherwise find tablemodel from entityOrClass


findEntityOrClassTableModel

public static TableModel findEntityOrClassTableModel(Object entityOrClass)

findFirstModel

public static TableModel findFirstModel(Object... optionItems)

findTailModel

public static TableModel findTailModel(SqlBoxContext ctx,
                                       TableModel entityModel,
                                       Object... optionItems)
Return first tail TableModel, if not found return null


findAllModels

public static TableModel[] findAllModels(Object... sqlItems)
Extract models from sqlItems


findModelAlias

public static Object[] findModelAlias(Object... sqlItems)
Find model and alias items from sqlItems


findNotModelAlias

public static Object[] findNotModelAlias(Object... sqlItems)
Find not model/alias items from sqlItems


createLastAutoAliasName

public static void createLastAutoAliasName(PreparedSQL ps)
Create auto Alias name based on capital letters of class name in models of PreparedSQL, if alias already exists, put a number at end, for example:
User ->u
User, UserRole, UserOther, Order, Order_a -> u, u1, u2, o, o1 if no EntityClass, alais will be created by table name, for example:
user ->u
user_role -> UR


mapToEntityBean

public static <T> T mapToEntityBean(TableModel model,
                                    Map<String,Object> oneRow)
Convert one row data into EntityBean


entityOrClassToBean

public static <T> T entityOrClassToBean(Object entityOrClass)
Transfer Object to TableModel, object can be SqlBox instance, entityClass or entity Bean
 1. TableModel instance, will use it
 2. SqlBox instance, will use its tableModel
 3. Class, will call TableModelUtils.entity2Model to create tableModel
 4. Object, will call TableModelUtils.entity2Model(entityOrClass.getClass()) to create a SqlBox instance
 


appendLeftJoinSQL

protected static void appendLeftJoinSQL(PreparedSQL ps)
Based on PreparedSQL's models and alias, automatically build and append a SQL like below:
 select a.**, b.**, c.**... from xxx a  
 left join xxx b on a.bid=b.id  
 left join xxx c on b.cid=c.id ...
 


readValueFromBeanFieldOrTail

public static Object readValueFromBeanFieldOrTail(Object entityBean,
                                                  ColumnModel columnModel)
Read value from entityBean field or tail


writeValueToBeanFieldOrTail

public static void writeValueToBeanFieldOrTail(Object entityBean,
                                               ColumnModel columnModel,
                                               Object value)
write value to entityBean field or tail


entityInsertTry

public static int entityInsertTry(SqlBoxContext ctx,
                                  Object entityBean,
                                  Object... optionItems)
Insert entityBean into database, and change ID fields to values generated by IdGenerator (identity or sequence or UUID...), return row affected


entityUpdateTry

public static int entityUpdateTry(SqlBoxContext ctx,
                                  Object entityBean,
                                  Object... optionItems)
Update entityBean according primary key, return row affected


entityDeleteTry

public static int entityDeleteTry(SqlBoxContext ctx,
                                  Object entityBean,
                                  Object... optionItems)
Delete entityBean in database according primary key value, return row affected


entityDeleteByIdTry

public static int entityDeleteByIdTry(SqlBoxContext ctx,
                                      Class<?> entityClass,
                                      Object id,
                                      Object... optionItems)
Try delete entity by Id, return row affected, return row affected


entityLoadTry

public static int entityLoadTry(SqlBoxContext ctx,
                                Object entityBean,
                                Object... optionItems)
Load entity according entity's id fields, return row affected


entityLoadByIdTry

public static <T> T entityLoadByIdTry(SqlBoxContext ctx,
                                      Class<T> entityClass,
                                      Object id,
                                      Object... optionItems)
Create a new Entity, load from DB according given ID, return null if entity does not exist in DB


entityExist

public static boolean entityExist(SqlBoxContext ctx,
                                  Object entityBean,
                                  Object... optionItems)
Check if entityBean exist in database by its id


entityExistById

public static boolean entityExistById(SqlBoxContext ctx,
                                      Class<?> entityClass,
                                      Object id,
                                      Object... optionItems)
Try delete entity by Id, return row affected


entityCountAll

public static int entityCountAll(SqlBoxContext ctx,
                                 Class<?> entityClass,
                                 Object... optionItems)
Count quantity of all entity, this method does not support sharding


entityFindAll

public static <T> List<T> entityFindAll(SqlBoxContext ctx,
                                        Class<T> entityClass,
                                        Object... optionItems)

entityFindBySample

public static <T> List<T> entityFindBySample(SqlBoxContext ctx,
                                             Object sampleBean,
                                             Object... sqlItems)

entityAutoNet

public static EntityNet entityAutoNet(SqlBoxContext ctx,
                                      Class<?>... entityClasses)

entityFindRelatedOne

public static <E> E entityFindRelatedOne(SqlBoxContext ctx,
                                         Object entity,
                                         Object... sqlItems)

entityFindRelatedList

public static <E> List<E> entityFindRelatedList(SqlBoxContext ctx,
                                                Object entity,
                                                Object... sqlItems)

entityFindRelatedSet

public static <E> Set<E> entityFindRelatedSet(SqlBoxContext ctx,
                                              Object entity,
                                              Object... sqlItems)

entityFindRelatedMap

public static <E> Map<Object,E> entityFindRelatedMap(SqlBoxContext ctx,
                                                     Object entity,
                                                     Object... sqlItems)


Copyright © 2018. All rights reserved.