Android Open Source - SORM Creator






From Project

Back to project page SORM.

License

The source code is released under:

MIT License

If you think the Android project SORM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

??package com.annotation.core;
/*from  ww w . ja  va 2  s .  co  m*/


import java.lang.annotation.Annotation;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.List;



import com.annotation.Unique;

import com.annotation.entity.ColumnInfo;

import com.annotation.entity.Sqlable;

import com.annotation.utils.ReflectionUtils;



/**

 * create talbe

 * 

 * @author Jayin Ton

 * 

 */

public class Creator implements Sqlable {

  private String _table;

  List<ColumnInfo> columnInfos;



  public Creator() {

    columnInfos = new ArrayList<ColumnInfo>();

  }



  public Creator from(Class<?> cls) {

    _table = ReflectionUtils.getTableName(cls);

    columnInfos = buildColumns(cls);

    return this;

  }



  private List<ColumnInfo> buildColumns(Class<?> cls) {

    Field[] fields = ReflectionUtils.getColumnFields(cls);

    List<ColumnInfo> list = new ArrayList<ColumnInfo>();

    for (Field field : fields) {

      String typeName = ReflectionUtils.getTypeName(field);

      String columnName = ReflectionUtils.getColumnName(field);

      StringBuffer constraint = null;

      if(columnName.equals("__id")){

        constraint = new StringBuffer().append("Primary Key").append(" ").append("AUTOINCREMENT").append(" ");

      }else{

        constraint = getConstraints(field);

      }

      ColumnInfo info = new ColumnInfo();

      info.setColumnName(columnName);

      info.setTypeName(typeName);

      info.setConstraint(constraint);

      list.add(info);

    }

    return list;



  }



  // expand in here

  private StringBuffer getConstraints(Field field) {

    StringBuffer sb = new StringBuffer();

    Annotation[] annotations = field.getAnnotations();

    for (Annotation a : annotations) {

      if (a.annotationType().equals(Unique.class)) {

        sb.append(a.annotationType().getSimpleName()).append(" ");

      } 

    }

    return sb;

  }



  public String build() {

    if (columnInfos.size() == 0) {

      throw new IllegalStateException(

          "there are no columns define for this table.");

    }

    StringBuffer builder = new StringBuffer();

    builder.append("Create Table").append(" ").append(_table).append(" ");

    builder.append("(");

    for (ColumnInfo info : columnInfos) {

      builder.append(info.build()).append(",");

    }

    builder.deleteCharAt(builder.length() - 1);

    builder.append(")");

    return builder.toString();

  }

}




Java Source Code List

.Creator.java
com.annotation.Column.java
com.annotation.Ignore.java
com.annotation.Index.java
com.annotation.NoNull.java
com.annotation.Table.java
com.annotation.Unique.java
com.annotation.core.Deletor.java
com.annotation.core.Droper.java
com.annotation.core.Indexer.java
com.annotation.core.Inserter.java
com.annotation.core.Model.java
com.annotation.core.Query.java
com.annotation.core.Selector.java
com.annotation.core.Updater.java
com.annotation.entity.ColumnInfo.java
com.annotation.entity.ORMcallback.java
com.annotation.entity.QueryCallback.java
com.annotation.entity.Sqlable.java
com.annotation.entity.Wherable.java
com.annotation.entity.WhereImpl.java
com.annotation.utils.DBHelper.java
com.annotation.utils.DBUtils.java
com.annotation.utils.NameBuilder.java
com.annotation.utils.ReflectionUtils.java
com.annotation.utils.SqlUtils.java
com.annotation.utils._.java