Android Open Source - NexusData Sql Table Builder






From Project

Back to project page NexusData.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project NexusData 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.github.dkharrat.nexusdata.utils;
//  w w  w  .  j  a va 2  s.c  om
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.database.sqlite.SQLiteDatabase;

public class SqlTableBuilder {

    private static final Logger LOG = LoggerFactory.getLogger(SqlTableBuilder.class);

    private String tableName;
    private ArrayList<Column> columns;
    private ArrayList<TableConstraint> tableConstraints;

    public enum ColumnType {
        INTEGER,
        TEXT,
        BOOLEAN,
        DATETIME,
        REAL
    }

    public enum ConflictAction {
        REPLACE,
        ABORT,
        IGNORE
    }

    public enum Order {
        ASC,
        DESC,
        DEFAULT
    }

    public SqlTableBuilder() {

    }

    public SqlTableBuilder tableName(String name) {
        tableName = name;
        columns = new ArrayList<Column>();
        tableConstraints = new ArrayList<TableConstraint>();
        return this;
    }

    public SqlTableBuilder primaryKey(String name, ColumnType type) {
        return this.primaryKey(name, type, Order.DEFAULT, true);
    }

    public SqlTableBuilder primaryKey(String name, ColumnType type, Order order, boolean autoIncrement) {
        PrimaryKey primaryKeyContraint = new PrimaryKey();
        primaryKeyContraint.order = order;
        primaryKeyContraint.autoIncrement = autoIncrement;

        Column column = new Column(name, type);
        column.constraints.add(primaryKeyContraint);

        columns.add(column);

        return this;
    }

    public SqlTableBuilder column(String name, ColumnType type) {
        Column column = new Column(name, type);
        columns.add(column);
        return this;
    }

    public SqlTableBuilder setNullable(boolean nullable) {
        Column column = getLastColumn();
        Nullable nullableContraint = new Nullable(nullable);
        column.constraints.add(nullableContraint);

        return this;
    }

    public SqlTableBuilder setDefaultValue(String defaultValue) {
        Column column = getLastColumn();
        DefaultValue defaultValueContraint = new DefaultValue(defaultValue);
        column.constraints.add(defaultValueContraint);

        return this;
    }

    public SqlTableBuilder setUnique(ConflictAction conflictAction) {
        Column column = getLastColumn();
        Unique uniqueContraint = new Unique(new ConflictClause(conflictAction));
        column.constraints.add(uniqueContraint);

        return this;
    }

    public SqlTableBuilder setUnique(ConflictAction conflictAction, String... columns) {
        TableUniqueConstraint uniqueContraint = new TableUniqueConstraint(new ConflictClause(conflictAction), columns);
        tableConstraints.add(uniqueContraint);

        return this;
    }

    public SqlTableBuilder primaryKey(ConflictAction conflictAction, String... columns) {
        TablePrimaryKeyConstraint primaryKeyContraint = new TablePrimaryKeyConstraint(new ConflictClause(conflictAction), columns);
        tableConstraints.add(primaryKeyContraint);

        return this;
    }

    public void createTable(SQLiteDatabase db) {
        if (tableName == null)
            throw new IllegalStateException("Table name not specified");

        String sqlStatement = "CREATE TABLE " + tableName;

        if (columns.isEmpty())
            throw new IllegalStateException("No columns specified");

        sqlStatement += " (";
        ArrayList<String> columnsSqlStatements = new ArrayList<String>();
        for (Column column : columns) {
            columnsSqlStatements.add(column.toSql());
        }
        for (TableConstraint tableConstraint : tableConstraints) {
            columnsSqlStatements.add(tableConstraint.toSql());
        }
        sqlStatement += StringUtil.join(columnsSqlStatements, ", ") + ")";

        LOG.trace("Executing SQL Statement: " + sqlStatement);
        db.execSQL(sqlStatement);
    }

    // Note Sqlite does not support altering existing columns
    public void alterTable(SQLiteDatabase db) {
        if (tableName == null)
            throw new IllegalStateException("Table name not specified");

        for (Column column : columns) {
            String sqlStatement = "ALTER TABLE " + tableName + " ADD COLUMN " + column.toSql();

            LOG.trace("Executing SQL Statement: " + sqlStatement);
            db.execSQL(sqlStatement);
        }
    }

    private Column getLastColumn() {
        if (columns.isEmpty())
            throw new IllegalStateException("No column previously specified");
        else
            return columns.get(columns.size()-1);
    }

    private class Column {
        public String name;
        public ColumnType type;
        public ArrayList<ColumnConstraint> constraints = new ArrayList<ColumnConstraint>();

        public Column(String name, ColumnType type) {
            this.name = name;
            this.type = type;
        }

        public String toSql() {
            String sqlStatement = "'" + name + "' " + type.name() + " ";

            ArrayList<String> constraintSqlStatements = new ArrayList<String>();
            for (ColumnConstraint contraint : constraints) {
                constraintSqlStatements.add(contraint.toSql());
            }

            sqlStatement += StringUtil.join(constraintSqlStatements, " ");

            return sqlStatement;
        }
    }

    private class ConflictClause {
        public ConflictAction conflictAction;

        public ConflictClause(ConflictAction conflictAction) {
            this.conflictAction = conflictAction;
        }

        public String toSql() {
            return "ON CONFLICT " + conflictAction.name();
        }
    }

    private interface Constraint {
        String toSql();
    }

    private interface ColumnConstraint extends Constraint {
    }

    private interface TableConstraint extends ColumnConstraint {
    }

    private abstract class ColumnBasedTableConstraint implements TableConstraint {
        private final String contraintType;
        private final List<String> columns;
        private ConflictClause conflictClause = null;

        protected ColumnBasedTableConstraint(String constraintType, ConflictClause conflictClause, String... columns) {
            this.contraintType = constraintType;
            this.conflictClause = conflictClause;
            this.columns = Arrays.asList(columns);
        }

        @Override
        public String toSql() {
            String sqlStatement = contraintType;
            if (columns.isEmpty()) {
                throw new IllegalStateException("Table constraint's columns must not be empty");
            }

            sqlStatement += "(" + StringUtil.join(columns, ",") + ")";

            if (conflictClause != null) sqlStatement += " " + conflictClause.toSql();
            return sqlStatement;
        }
    }

    private class PrimaryKey implements ColumnConstraint {
        public Order order = null;
        public ConflictClause conflictClause = null;
        public boolean autoIncrement = false;

        @Override
        public String toSql() {
            String sqlStatement = "PRIMARY KEY";
            if (order != null && order != Order.DEFAULT) sqlStatement += " " + order.name();
            if (conflictClause != null) sqlStatement += " " + conflictClause.toSql();
            if (autoIncrement) sqlStatement += " AUTOINCREMENT";
            return sqlStatement;
        }
    }

    private class TablePrimaryKeyConstraint extends ColumnBasedTableConstraint {
        protected TablePrimaryKeyConstraint(ConflictClause conflictClause, String... columns) {
            super("PRIMARY KEY", conflictClause, columns);
        }
    }

    private class Nullable implements ColumnConstraint {
        public boolean nullable;
        public ConflictClause conflictClause = null;

        public Nullable(boolean nullable) {
            this.nullable = nullable;
        }

        @Override
        public String toSql() {
            String sqlStatement = "";
            if (!nullable) sqlStatement += " NOT NULL";
            if (conflictClause != null) sqlStatement += " " + conflictClause.toSql();
            return sqlStatement;
        }
    }

    private class DefaultValue implements ColumnConstraint {
        public String defaultValue;

        public DefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        @Override
        public String toSql() {
            return "DEFAULT '" + defaultValue + "'";
        }
    }

    private class Unique implements ColumnConstraint {
        public ConflictClause conflictClause;

        public Unique(ConflictClause conflictClause) {
            this.conflictClause = conflictClause;
        }

        @Override
        public String toSql() {
            return "UNIQUE " + conflictClause.toSql();
        }
    }

    private class TableUniqueConstraint extends ColumnBasedTableConstraint {
        protected TableUniqueConstraint(ConflictClause conflictClause, String... columns) {
            super("UNIQUE", conflictClause, columns);
        }
    }
}




Java Source Code List

com.github.dkharrat.nexusdata.core.AtomicStore.java
com.github.dkharrat.nexusdata.core.ChangedObjectsSet.java
com.github.dkharrat.nexusdata.core.FaultingSet.java
com.github.dkharrat.nexusdata.core.FetchRequest.java
com.github.dkharrat.nexusdata.core.IncrementalStore.java
com.github.dkharrat.nexusdata.core.ManagedObject.java
com.github.dkharrat.nexusdata.core.NoSuchPropertyException.java
com.github.dkharrat.nexusdata.core.ObjectContextNotifier.java
com.github.dkharrat.nexusdata.core.ObjectContext.java
com.github.dkharrat.nexusdata.core.ObjectID.java
com.github.dkharrat.nexusdata.core.ObjectsChangedNotification.java
com.github.dkharrat.nexusdata.core.PersistentStoreCoordinator.java
com.github.dkharrat.nexusdata.core.PersistentStoreRequest.java
com.github.dkharrat.nexusdata.core.PersistentStore.java
com.github.dkharrat.nexusdata.core.SaveChangesRequest.java
com.github.dkharrat.nexusdata.core.SortDescriptor.java
com.github.dkharrat.nexusdata.core.StoreCacheNode.java
com.github.dkharrat.nexusdata.metamodel.Attribute.java
com.github.dkharrat.nexusdata.metamodel.Entity.java
com.github.dkharrat.nexusdata.metamodel.ObjectModelJsonParser.java
com.github.dkharrat.nexusdata.metamodel.ObjectModel.java
com.github.dkharrat.nexusdata.metamodel.Property.java
com.github.dkharrat.nexusdata.metamodel.Relationship.java
com.github.dkharrat.nexusdata.modelgen.ModelGenerator.java
com.github.dkharrat.nexusdata.modelgen.Startup.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Attribute.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Entity.java
com.github.dkharrat.nexusdata.modelgen.metamodel.EnumProperty.java
com.github.dkharrat.nexusdata.modelgen.metamodel.ModelWrapper.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Model.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Property.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Relationship.java
com.github.dkharrat.nexusdata.predicate.ComparisonPredicate.java
com.github.dkharrat.nexusdata.predicate.CompoundPredicate.java
com.github.dkharrat.nexusdata.predicate.ConstantExpression.java
com.github.dkharrat.nexusdata.predicate.ExpressionBuilder.java
com.github.dkharrat.nexusdata.predicate.ExpressionVisitor.java
com.github.dkharrat.nexusdata.predicate.Expression.java
com.github.dkharrat.nexusdata.predicate.FieldPathExpression.java
com.github.dkharrat.nexusdata.predicate.NotPredicate.java
com.github.dkharrat.nexusdata.predicate.PredicateBuilder.java
com.github.dkharrat.nexusdata.predicate.Predicate.java
com.github.dkharrat.nexusdata.predicate.ThisExpression.java
com.github.dkharrat.nexusdata.predicate.parser.ComparisonParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ConstantParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ExpressionNode.java
com.github.dkharrat.nexusdata.predicate.parser.GroupParselet.java
com.github.dkharrat.nexusdata.predicate.parser.InfixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.LexerGrammar.java
com.github.dkharrat.nexusdata.predicate.parser.Lexer.java
com.github.dkharrat.nexusdata.predicate.parser.LogicalParselet.java
com.github.dkharrat.nexusdata.predicate.parser.NameParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ParseException.java
com.github.dkharrat.nexusdata.predicate.parser.Parser.java
com.github.dkharrat.nexusdata.predicate.parser.PredicateParser.java
com.github.dkharrat.nexusdata.predicate.parser.PrefixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.Token.java
com.github.dkharrat.nexusdata.store.AndroidSqlPersistentStore.java
com.github.dkharrat.nexusdata.store.DatabaseHelper.java
com.github.dkharrat.nexusdata.store.InMemoryPersistentStore.java
com.github.dkharrat.nexusdata.store.PredicateToSQL.java
com.github.dkharrat.nexusdata.utils.DateUtil.java
com.github.dkharrat.nexusdata.utils.ObjectUtil.java
com.github.dkharrat.nexusdata.utils.SqlTableBuilder.java
com.github.dkharrat.nexusdata.utils.StreamUtil.java
com.github.dkharrat.nexusdata.utils.StringUtil.java
com.github.dkharrat.nexusdata.utils.android.CursorUtil.java
com.github.dkharrat.nexusdata.utils.android.SQLiteDatabaseHelper.java
org.example.todo.MainActivity.java
org.example.todo.NewTaskActivity.java
org.example.todo.Task.java
org.example.todo.TodoApp.java
org.example.todo.User.java
org.example.todo._Task.java
org.example.todo._User.java