Android Open Source - simple-sqlite Schema






From Project

Back to project page simple-sqlite.

License

The source code is released under:

Apache License

If you think the Android project simple-sqlite 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.aldobo.simple.sqlite.entities;
//from w  w w. j  av a2  s. c om
import com.aldobo.simple.sqlite.tags.Column;
import com.aldobo.simple.sqlite.tags.Table;
import com.aldobo.simple.sqlite.utils.Utils;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

public class Schema {

    private String mTableName;
    private List<Field> mFields;
    private Field mPrimaryKey;
    private String[] mColumns;

    private Schema(Schema.Builder builder) {
        mTableName = builder.mTableName;
        mFields = builder.mFields;
        mPrimaryKey = builder.mPrimaryKey;
    }

    public String getTableName() {
        return mTableName;
    }

    public Iterable<Field> getFields() {
        return mFields;
    }

    public Field getField(int index) {
        return mFields.get(index);
    }

    public Field getPrimaryKey() {
        return mPrimaryKey;
    }

    public String[] getColumns() {
        if (mColumns != null)
            return mColumns;
        mColumns = new String[mFields.size()];
        for (int i = 0; i < mFields.size(); i++)
            mColumns[i] = mFields.get(i).getName();
        return mColumns;
    }

    @Override
    public String toString() {
        return String.format("%s(%s)", getTableName(), getColumns().length);
    }

    public static class Builder {
        private String mTableName;
        private List<Field> mFields;
        private Field mPrimaryKey;

        public Builder() {
            mFields = new ArrayList<Field>();
        }

        public Builder setTableName(String tableName) {
            mTableName = tableName;
            return this;
        }

        public Builder addField(Field field) {
            if (field.isPrimaryKey()) {
                if (mPrimaryKey != null)
                    throw new InvalidParameterException("Primary key is already allocated");
                mPrimaryKey = field;
            }
            mFields.add(field);
            return this;
        }

        public Schema build() {
            return new Schema(this);
        }

        public static <T> Schema initialize(Class<T> model) {
            Schema.Builder builder = new Schema.Builder();
            Table annotation = model.getAnnotation(Table.class);
            String tableName;
            if (annotation == null)
                tableName = model.getSimpleName();
            else
                tableName = annotation.tableName();
            builder.setTableName(tableName);

            for (java.lang.reflect.Field f : model.getFields()) {
                Column fieldAnnotation = f.getAnnotation(Column.class);
                if (fieldAnnotation == null)
                    continue;
                Field.Builder fieldBuilder = new Field.Builder();
                //Java field
                fieldBuilder.setJavaField(f);
                //Type
                fieldBuilder.setType(fieldAnnotation.type() == SQLiteType.NONE ?
                        Utils.getSQLiteType(f.getType()):
                        fieldAnnotation.type()
                );
                //Auto increment
                if (fieldAnnotation.autoIncrement())
                    fieldBuilder.setAsAutoIncrement();
                //Null
                if (fieldAnnotation.nullable())
                    fieldBuilder.setNullable();
                //PrimaryKey
                if (fieldAnnotation.primaryKey())
                    fieldBuilder.setAsPrimaryKey();
                //Length
                fieldBuilder.setLength(fieldAnnotation.length());
                //Default value
                fieldBuilder.setSQliteDefaultValue(fieldAnnotation.defaultValue());
                //Name TODO: Let users modify name
                fieldBuilder.setName(f.getName());

                builder.addField(fieldBuilder.build());
            }
            return builder.build();
        }
    }
}




Java Source Code List

com.aldobo.simple.sqlite.SQLiteConfiguration.java
com.aldobo.simple.sqlite.SQLiteManager.java
com.aldobo.simple.sqlite.SQLiteOpenHelperApp.java
com.aldobo.simple.sqlite.SQLiteOpenHelperExtendable.java
com.aldobo.simple.sqlite.entities.Field.java
com.aldobo.simple.sqlite.entities.Model.java
com.aldobo.simple.sqlite.entities.SQLiteDefaultValue.java
com.aldobo.simple.sqlite.entities.SQLiteType.java
com.aldobo.simple.sqlite.entities.Schema.java
com.aldobo.simple.sqlite.sample.MainActivity.java
com.aldobo.simple.sqlite.sample.MainApplication.java
com.aldobo.simple.sqlite.sample.fragments.BaseFragment.java
com.aldobo.simple.sqlite.sample.fragments.MyListFragment.java
com.aldobo.simple.sqlite.sample.models.Person.java
com.aldobo.simple.sqlite.tags.Column.java
com.aldobo.simple.sqlite.tags.Table.java
com.aldobo.simple.sqlite.utils.ModelIterable.java
com.aldobo.simple.sqlite.utils.Utils.java