Package org.xorm.datastore

Examples of org.xorm.datastore.Column


        whereClause.append(")");
    }

    /** Parses the non-compound Condition. */
    private boolean parseSimple(Alias alias, SimpleCondition condition) {
        Column column = condition.getColumn();
        Object operand = condition.getValue();
        boolean wasOuter = false;
        StringBuffer buffer = whereClause;
        if (operand instanceof Selector) {
            Selector next = (Selector) operand;
            Alias nextAlias = makeAlias(next);
            if (next.isOuterJoin()) {
                if (next.getCondition() instanceof SimpleCondition) {
                    parseSimple(nextAlias, (SimpleCondition) next.getCondition());
                } else {
                    wasOuter = true;
                }
                fromClause.append(" LEFT OUTER JOIN ")
                    .append(nextAlias.getTable().getName())
                    .append(" ")
                    .append(nextAlias.getName())
                    .append(" ON ");
                buffer = fromClause;
            } else {
                buffer.append("(");
            }
            buffer.append(alias.getName())
                .append(".")
                .append(column.getName())
                .append(" = ");

            // In the usual case this should be nextAlias.primaryKey,
            // but in the case of CONTAINS it should be nextAlias.foreignKey.
            buffer.append(nextAlias.getName())
                .append(".")
                .append(nextAlias.getJoinColumn().getName());

            // Because Many-to-Many operations can cause multiple
            // copies, set the distinct flag.
            if (condition.getOperator() == Operator.CONTAINS) {
                distinct = true;
            }

            if (!next.isOuterJoin()) {
                parse(nextAlias, next);
                buffer.append(")");
            }
        } else {
            whereClause.append("(");
            whereClause.append(alias.getName())
                .append(".")
                .append(column.getName());
            parseOperand(condition.getOperator(), operand);
            whereClause.append(")");
        }
        return wasOuter;
    }
View Full Code Here


        for (int i = 0; i < ordering.length; i++) {
            if (i > 0) {
                orderingClause.append(", ");
            }
            Column column = ordering[i].getColumn();
            Alias alias = null;
            Iterator it = aliases.iterator();
            while (it.hasNext()) {
                alias = (Alias) it.next();
                if (alias.getTable() == column.getTable()) {
                    break;
                }
            }
            orderingClause.append(alias.getName())
                .append('.')
                .append(column.getName())
                .append(' ');

            int order = ordering[i].getOrder();
            switch (order) {
            case Selector.Ordering.ASCENDING:
View Full Code Here

                if (seenAny) {
                    sql.append(", ");
                } else {
                    seenAny = true;
                }
                Column c = (Column) j.next();
                sql.append(alias.getName())
                    .append(".")
                    .append(c.getName());
            }
        }

        sql.append(" FROM ")
            .append(fromClause.toString());
View Full Code Here

                logger.fine("Column: " + columnName +
                            ", type: " + typeName + " (" + dataType +
                            "), nullable: " + isNullable +
                            (isPrimaryKey ? ", primary key" : ""));
               
                Column column = new Column(table, columnName);
                if (isPrimaryKey) {
                    table.setPrimaryKey(column);
                    if (autoIncrementPrimaryKeys) {
                        logger.fine("Setting " + columnName +
                                    " column to auto-increment");
                        column.setAutoIncremented(true);
                    }
                    else if (sequenceNamePattern != null &&
                             !sequenceNamePattern.equals("")) {
                        // Take the sequence name pattern and replace any
                        // instance of "{table}" and/or "{column}" with the
                        // respective names.
                        String seqName = sequenceNamePattern
                            .replaceAll("\\{table\\}", tableName)
                            .replaceAll("\\{column\\}", columnName);
                        logger.fine("Using sequence: " + seqName);
                        column.setSequence(seqName);
                    }
                }
                column.setNonNull(!isNullable);
                column.setType(typeName);
            }
            results.close();
        }
        catch (SQLException e) {
            logger.warning("While trying to load \"" + tableName +
View Full Code Here

    public void create(Row row) throws DriverException {
        if (readOnly) {
            throw new DriverException(I18N.msg("E_read_only_txn", "create"));
        }
        Column primaryKey = row.getTable().getPrimaryKey();
        String statement = getStatements(row.getTable())
            .insertStatement;
        String nextIDStatement = getStatements(row.getTable())
            .nextIDStatement;

        Object useID = null;
        try {
            // If primary key uses a non-autoincremented sequence, get the ID
            if (primaryKey != null && primaryKey.getSequence() != null && !primaryKey.isAutoIncremented()) {
                logger.info(nextIDStatement);
                PreparedStatement ps1 = currentConnection.prepareStatement(nextIDStatement);
                ResultSet rs1 = ps1.executeQuery();
                useID = null;
                if (rs1.next()) {
                    useID = rs1.getObject(1);
                }
                rs1.close();

                row.setValue(row.getTable().getPrimaryKey(), useID);
            }

            logger.info(statement);
            logger.info(row.toString());

            // Now do insert
            PreparedStatement ps = currentConnection.prepareStatement(statement);
            Set columns = row.getTable().getColumns();
            int pos = 1;
            for (Iterator i = columns.iterator(); i.hasNext(); ) {
                Column c = (Column) i.next();
                if ((c == primaryKey && primaryKey.isAutoIncremented())
                    || c.isReadOnly()) {
                    continue;
                }
                Object value = row.getValue(c);
                setObject(ps, pos++, value, c.getType());
            }
            ps.executeUpdate();
            ps.close();

            // If autoincremented, read ID now
View Full Code Here

    public void update(Row row) throws DriverException {
        if (readOnly) {
            throw new DriverException(I18N.msg("E_read_only_txn", "update"));
        }
        Table table = row.getTable();
        Column primaryKey = table.getPrimaryKey();

        // Cannot update rows without a primary key
        if (primaryKey == null) {
            return;
        }

        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE ")
            .append(table.getName())
            .append(" SET ");
        Iterator it = table.getColumns().iterator();
        boolean seenOne = false;
        while (it.hasNext()) {
            Column c = (Column) it.next();
            if ((c == primaryKey && primaryKey.isAutoIncremented())
                || c.isReadOnly()) {
                continue;
            }
            if (row.isDirty(c)) {
                if (seenOne) {
                    sql.append(", ");
                } else {
                    seenOne = true;
                }
                sql.append(c.getName())
                    .append(" = ?");
            }
        }
        // No need to do anything if nothing has changed.
        if (!seenOne) {
            return;
        }
        sql.append(" WHERE ")
            .append(table.getPrimaryKey().getName())
            .append(" = ?");
        String statement = sql.toString();
        logger.info(statement);
        logger.info(row.toString());
        try {
            PreparedStatement ps = currentConnection.prepareStatement(statement);
            int pos = 1;
            Set columns = row.getTable().getColumns();
            for (Iterator i = columns.iterator(); i.hasNext(); ) {
                Column c = (Column) i.next();
                if (c == primaryKey && primaryKey.isAutoIncremented())
                    continue;
                if (row.isDirty(c)) {
                    Object value = row.getValue(c);
                    setObject(ps, pos++, value, c.getType());
                }
            }
            ps.setObject(pos++, row.getPrimaryKeyValue());
            ps.executeUpdate();
            ps.close();
View Full Code Here

                .append("DELETE FROM ")
                .append(row.getTable().getName())
                .append(" WHERE ");
            Iterator it = row.getTable().getColumns().iterator();
            while (it.hasNext()) {
                Column c = (Column) it.next();
                sql.append(c.getName());
                if (row.getValue(c) == null) {
                    sql.append(" IS NULL");
                } else {
                    sql.append(" = ?");
                }
                if (it.hasNext()) sql.append(" AND ");
            }

            String statement = sql.toString();
            logger.info(statement);
            logger.info(row.toString());

            try {
                PreparedStatement ps = currentConnection.prepareStatement(statement);
                Set columns = row.getTable().getColumns();
                int i = 1; // preparedstatement position
                for (it = columns.iterator(); it.hasNext(); ) {
                    Column c = (Column) it.next();
                    Object value = row.getValue(c);
                    if (value != null) {
                        setObject(ps, i++, value, c.getType());
                    }
                }
                ps.executeUpdate();
                ps.close();
            } catch (SQLException e) {
View Full Code Here

                    Set fcs = alias.getFetchColumns();
                    if (!fcs.isEmpty()) {
                        row = new Row(table);
                        Iterator fci = fcs.iterator();
                        while (fci.hasNext()) {
                            Column c = (Column) fci.next();
       
                            // TODO will this be flexible enough?
                            row.setValue(c, rs.getObject(++pos));
                        }
                        if (table.equals(query.getTargetTable())) {
View Full Code Here

            StringBuffer sql = new StringBuffer();
            sql.append("INSERT INTO ")
                .append(table.getName())
                .append(" (");
            Iterator it = table.getColumns().iterator();
            Column primaryKey = table.getPrimaryKey();
            StringBuffer args = new StringBuffer();
            boolean seenAny = false;
            while (it.hasNext()) {
                Column c = (Column) it.next();
                if ((c == primaryKey && primaryKey.isAutoIncremented())
                    || c.isReadOnly()) {
                    continue;
                }
                if (!seenAny) {
                    seenAny = true;
                } else {
                    args.append(",");
                    sql.append(",");
                }
                sql.append(c.getName());
                args.append("?");
            }
            sql.append(") VALUES (")
                .append(args.toString())
                .append(")");
View Full Code Here

  return (table == null? null : table.getName()) + "." + (joinColumn == null ? null: joinColumn.getName()) + " where " + condition;
    }

    public void require(DataFetchGroup fetchGroup) {
  fetchColumns = fetchGroup.getColumns();
  Column column;
  Iterator i = fetchGroup.getSubgroupColumns().iterator();
  while (i.hasNext()) {
      column = (Column) i.next();
      DataFetchGroup subgroup = fetchGroup.getSubgroup(column);
      // Find or create the Selector for the subgroup
      Selector selector = findSelector(condition, subgroup.getTable());
      if (selector == null) {
    selector = new Selector(subgroup.getTable(), null);
    selector.outerJoin = !column.isNonNull();
    Condition join = new SimpleCondition
        (column, Operator.EQUAL, selector);
    if (condition != null) {
        condition = new AndCondition(join, condition);
    } else {
View Full Code Here

TOP

Related Classes of org.xorm.datastore.Column

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.