Package org.xorm.datastore

Examples of org.xorm.datastore.DriverException


    public void begin(boolean readOnly) throws DriverException {
        this.readOnly = readOnly;
        // Acquire a connection from the pool
        try {
            if (currentConnection != null) {
                throw new DriverException(I18N.msg("E_txn_in_use"), lastBegin);
            }
            currentConnection = dataSource.getConnection();
            currentConnection.setAutoCommit(readOnly);
            currentConnection.setReadOnly(readOnly);

            // Populate lastBegin
            try {
                throw new Exception();
            } catch (Exception e) {
                lastBegin = e;
            }
        } catch (SQLException e) {
            throw new DriverException(e);
        }
    }
View Full Code Here


                currentConnection.commit();
            }
            currentConnection.close();
            currentConnection = null;
        } catch (SQLException e) {
            throw new DriverException(e);
        }
    }
View Full Code Here

                currentConnection.rollback();
            }
            currentConnection.close();
            currentConnection = null;
        } catch (SQLException e) {
            throw new DriverException(e);
        }
    }
View Full Code Here

        return statements;
    }

    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
            if (primaryKey != 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();
                logger.info("using ID " + useID);
                // TODO inform other objects about ID?
                row.setValue(row.getTable().getPrimaryKey(), useID);
            }

        } catch (SQLException e) {
            e.printStackTrace();
            throw new DriverException(e);
        }
    }
View Full Code Here

     * Updates a row against the database.  Uses the dirty bit settings
     * on Row to determine which fields to update.
     */
    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();
        } catch (SQLException e) {
            throw new DriverException(e);
        }
    }
View Full Code Here

        }
    }

    public void delete(Row row) throws DriverException {
        if (readOnly) {
            throw new DriverException(I18N.msg("E_read_only_txn", "delete"));
        }
        // If there is no primary key, delete using all columns explicitly
        if (row.getTable().getPrimaryKey() == null) {
            StringBuffer sql = new StringBuffer()
                .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) {
                throw new DriverException(e);
            }
        } else {
            // Usually we have a primary key to delete with
            String statement = getStatements(row.getTable())
                .deleteStatement;
            logger.info(statement);
            logger.info("{ " + row.getPrimaryKeyValue() + " }");
            try {
                PreparedStatement ps = currentConnection.prepareStatement(statement);
                ps.setObject(1, row.getPrimaryKeyValue());
                ps.executeUpdate();
                ps.close();
            } catch (SQLException e) {
                throw new DriverException(e);
            }
        }
    }
View Full Code Here

                size = rs.getInt(1);
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            throw new DriverException(e);
        }
        return size;
    }
View Full Code Here

                } // for each alias
            } // for each result
            rs.close();
            ps.close();
        } catch (SQLException e) {
            throw new DriverException(e);
        }
        return list;
    }
View Full Code Here

TOP

Related Classes of org.xorm.datastore.DriverException

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.