Android Open Source - SORM Inserter






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 w  w w . ja  va  2  s . c o  m*/
import java.lang.reflect.Field;
import com.annotation.entity.Sqlable;
import com.annotation.utils.NameBuilder;
import com.annotation.utils.ReflectionUtils;

public class Inserter implements Sqlable {
  // insert into talbe(column1,column2...) values("1","3")
  private StringBuffer _targetColumn, _values;
  private String _table;

  public Inserter() {
    _targetColumn = new StringBuffer();
    _values = new StringBuffer();
  }

  public Inserter insert(Object obj) {
    try {
      _table = ReflectionUtils.getTableName(obj.getClass());
      Field[] fields = ReflectionUtils.getColumnFields(obj.getClass());
      for (Field field : fields) {
        String columnName = ReflectionUtils.getColumnName(field);
        if (columnName.equals("__id"))
          continue;
        _targetColumn.append(columnName).append(",");
        String method_name = NameBuilder.buildGetter(columnName);
        _values.append("\"")
            .append(ReflectionUtils.invokeGetMethod(obj,
                method_name).replaceAll("\\\"", "\\\\\"")).append("\"").append(",");
      }
      _targetColumn.deleteCharAt(_targetColumn.length() - 1);
      _values.deleteCharAt(_values.length() - 1);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return this;
  }

  @Override
  public String build() {
    StringBuffer builder = new StringBuffer();
    builder.append("Insert Into").append(" ");
    builder.append(_table).append(" ");
    builder.append("(").append(_targetColumn).append(")").append(" ");
    builder.append("Values").append(" ");
    builder.append("(").append(_values).append(")").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