Package org.apache.torque.oid

Examples of org.apache.torque.oid.IdGenerator


        String dbName = criteria.getDbName();
        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
        TableMap tableMap = dbMap.getTable(table);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen = tableMap.getIdGenerator();

        ColumnMap pk = getPrimaryKey(criteria);

        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
        // before the insert.
        if (keyGen != null && keyGen.isPriorToInsert())
        {
            // pk will be null if there is no primary key defined for the table
            // we're inserting into.
            if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
            {
                if (keyGen == null)
                {
                    throw new TorqueException(
                            "IdGenerator for table '" + table + "' is null");
                }

                id = getId(pk, keyGen, con, keyInfo);
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            String tableName = SQLBuilder.getFullTableName(table, dbName);
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
        }
        catch (Exception e)
        {
            throwTorqueException(e);
        }
        finally
        {
            VillageUtils.close(tds);
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert())
        {
            id = getId(pk, keyGen, con, keyInfo);
        }

        return id;
View Full Code Here


        }

        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
        TableMap tableMap = dbMap.getTable(tableName);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen = tableMap.getIdGenerator();

        ColumnMap pk = getPrimaryKey(criteria);

        // pk will be null if there is no primary key defined for the table
        // we're inserting into.
        if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
        {
            if (keyGen == null)
            {
                throw new TorqueException(
                    "IdGenerator for table '" + tableName + "' is null");
            }
            // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
            // before the insert.

            if (keyGen.isPriorToInsert())
            {
                try
                {
                    if (pk.getType() instanceof Number)
                    {
                        id = new NumberKey(
                                keyGen.getIdAsBigDecimal(con, keyInfo));
                    }
                    else
                    {
                        id = new StringKey(keyGen.getIdAsString(con, keyInfo));
                    }
                }
                catch (Exception e)
                {
                    throwTorqueException(e);
                }
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            BasePeer.insertOrUpdateRecord(rec, tableName, criteria);
        }
        catch (Exception e)
        {
            throwTorqueException(e);
        }
        finally
        {
            if (tds != null)
            {
                try
                {
                    tds.close();
                }
                catch (Exception e)
                {
                    throwTorqueException(e);
                }
            }
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (pk != null && keyGen != null && keyGen.isPostInsert())
        {
            try
            {
                if (pk.getType() instanceof Number)
                {
                    id = new NumberKey(keyGen.getIdAsBigDecimal(con, keyInfo));
                }
                else
                {
                    id = new StringKey(keyGen.getIdAsString(con, keyInfo));
                }
            }
            catch (Exception e)
            {
                throwTorqueException(e);
View Full Code Here

        String dbName = criteria.getDbName();
        Database database = Torque.getDatabase(dbName);
        DatabaseMap dbMap = database.getDatabaseMap();
        TableMap tableMap = dbMap.getTable(table);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen
                = database.getIdGenerator(tableMap.getPrimaryKeyMethod());

        ColumnMap pk = getPrimaryKey(criteria);

        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
        // before the insert.
        if (keyGen != null && keyGen.isPriorToInsert())
        {
            // pk will be null if there is no primary key defined for the table
            // we're inserting into.
            if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
            {
                id = getId(pk, keyGen, con, keyInfo);
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            String tableName = SQLBuilder.getFullTableName(table, dbName);
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
        }
        catch (DataSetException e)
        {
            throwTorqueException(e);
        }
        catch (SQLException e)
        {
            throwTorqueException(e);
        }
        catch (TorqueException e)
        {
            throwTorqueException(e);
        }
        finally
        {
            VillageUtils.close(tds);
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert())
        {
            id = getId(pk, keyGen, con, keyInfo);
        }

        return id;
View Full Code Here

        String dbName = criteria.getDbName();
        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
        TableMap tableMap = dbMap.getTable(table);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen = tableMap.getIdGenerator();

        ColumnMap pk = getPrimaryKey(criteria);

        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
        // before the insert.
        if (keyGen != null && keyGen.isPriorToInsert())
        {
            // pk will be null if there is no primary key defined for the table
            // we're inserting into.
            if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
            {
                if (keyGen == null)
                {
                    throw new TorqueException(
                            "IdGenerator for table '" + table + "' is null");
                }
               
                id = getId(pk, keyGen, con, keyInfo);
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            String tableName = SQLBuilder.getFullTableName(table, dbName);
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
        }
        catch (Exception e)
        {
            throwTorqueException(e);
        }
        finally
        {
            VillageUtils.close(tds);
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert())
        {
            id = getId(pk, keyGen, con, keyInfo);
        }

        return id;
View Full Code Here

        }

        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
        TableMap tableMap = dbMap.getTable(tableName);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen = tableMap.getIdGenerator();

        ColumnMap pk = getPrimaryKey(criteria);

        // pk will be null if there is no primary key defined for the table
        // we're inserting into.
        if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
        {
            if (keyGen == null)
            {
                throw new TorqueException(
                    "IdGenerator for table '" + tableName + "' is null");
            }
            // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
            // before the insert.

            if (keyGen.isPriorToInsert())
            {
                try
                {
                    if (pk.getType() instanceof Number)
                    {
                        id = new NumberKey(
                                keyGen.getIdAsBigDecimal(con, keyInfo));
                    }
                    else
                    {
                        id = new StringKey(keyGen.getIdAsString(con, keyInfo));
                    }
                }
                catch (Exception e)
                {
                    throwTorqueException(e);
                }
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            BasePeer.insertOrUpdateRecord(rec, tableName, criteria);
        }
        catch (Exception e)
        {
            throwTorqueException(e);
        }
        finally
        {
            if (tds != null)
            {
                try
                {
                    tds.close();
                }
                catch (Exception e)
                {
                    throwTorqueException(e);
                }
            }
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (pk != null && keyGen != null && keyGen.isPostInsert())
        {
            try
            {
                if (pk.getType() instanceof Number)
                {
                    id = new NumberKey(keyGen.getIdAsBigDecimal(con, keyInfo));
                }
                else
                {
                    id = new StringKey(keyGen.getIdAsString(con, keyInfo));
                }
            }
            catch (Exception e)
            {
                throwTorqueException(e);
View Full Code Here

        String dbName = criteria.getDbName();
        Database database = Torque.getDatabase(dbName);
        DatabaseMap dbMap = database.getDatabaseMap();
        TableMap tableMap = dbMap.getTable(table);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen
                = database.getIdGenerator(tableMap.getPrimaryKeyMethod());

        ColumnMap pk = getPrimaryKey(criteria);

        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
        // before the insert.
        if (keyGen != null && keyGen.isPriorToInsert())
        {
            // pk will be null if there is no primary key defined for the table
            // we're inserting into.
            if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
            {
                id = getId(pk, keyGen, con, keyInfo);
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            String tableName = SQLBuilder.getFullTableName(table, dbName);
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
        }
        catch (DataSetException e)
        {
            throwTorqueException(e);
        }
        catch (SQLException e)
        {
            throwTorqueException(e);
        }
        catch (TorqueException e)
        {
            throwTorqueException(e);
        }
        finally
        {
            VillageUtils.close(tds);
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert())
        {
            id = getId(pk, keyGen, con, keyInfo);
        }

        return id;
View Full Code Here

        {
            databaseNameFromInsertValues = getDatabaseName();
        }
        Database database = Torque.getDatabase(databaseNameFromInsertValues);
        Object keyInfo = getIdMethodInfo();
        IdGenerator keyGen = database.getIdGenerator(
                getTableMap().getPrimaryKeyMethod());

        SimpleKey id = null;
        // can currently generate only single column pks, therefore a single
        // columnMap is ok
        ColumnMap primaryKey = null;
        if (keyGen != null)
        {
            // fail on multiple pks
            primaryKey = getTableMap().getPrimaryKey();

            // primaryKey will be null if there is no primary key
            // defined for the table we're inserting into.
            if (keyGen.isPriorToInsert() && primaryKey != null
                    && !insertValues.containsKey(
                            primaryKey))
            {
                id = getId(primaryKey, keyGen, connection, keyInfo);
                insertValues.put(
                        primaryKey,
                        new JdbcTypedValue(id.getValue(), id.getJdbcType()));
            }
        }

        List<String> columnNames = new ArrayList<String>();
        List<JdbcTypedValue> replacementObjects
                = new ArrayList<JdbcTypedValue>();
        for (Map.Entry<Column, JdbcTypedValue> columnValue
                : insertValues.entrySet())
        {
            Column column = columnValue.getKey();
            columnNames.add(column.getColumnName());
            JdbcTypedValue value = columnValue.getValue();
            replacementObjects.add(value);
        }

        String fullTableName = SqlBuilder.getFullTableName(
                getTableMap().getFullyQualifiedTableName(),
                databaseNameFromInsertValues);
        StringBuilder query = new StringBuilder("INSERT INTO ")
            .append(fullTableName)
            .append("(")
            .append(StringUtils.join(columnNames, ","))
            .append(") VALUES (");
        for (int i = 0; i < columnNames.size(); ++i)
        {
            if (i != 0)
            {
                query.append(",");
            }
            query.append("?");
        }
        query.append(")");

        PreparedStatement preparedStatement = null;
        try
        {
            preparedStatement = connection.prepareStatement(query.toString());
            int position = 1;
            for (JdbcTypedValue replacementObject : replacementObjects)
            {
                Object value = replacementObject.getValue();
                if (value != null)
                {
                    if (replacementObject.getJdbcType() != Types.BLOB
                            && replacementObject.getJdbcType() != Types.CLOB)
                    {
                        preparedStatement.setObject(
                                position,
                                value,
                                replacementObject.getJdbcType());
                    }
                    else
                    {
                        preparedStatement.setObject(
                                position,
                                value);
                    }
                }
                else
                {
                    preparedStatement.setNull(
                            position,
                            replacementObject.getJdbcType());
                }
                position++;
            }
            long startTime = System.currentTimeMillis();
            log.debug("Executing insert " + query.toString()
                    + " using parameters " + replacementObjects);

            preparedStatement.executeUpdate();
            long queryEndTime = System.currentTimeMillis();
            log.trace("insert took " + (queryEndTime - startTime)
                    + " milliseconds");

            preparedStatement.close();
            preparedStatement = null;
        }
        catch (SQLException e)
        {
            throw ExceptionMapper.getInstance().toTorqueException(e);
        }
        finally
        {
            if (preparedStatement != null)
            {
                try
                {
                    preparedStatement.close();
                }
                catch (SQLException e)
                {
                    log.warn("error closing prepared statement", e);
                }
            }
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert()
                && primaryKey != null
                && !insertValues.containsKey(
                        primaryKey))
        {
            id = getId(primaryKey, keyGen, connection, keyInfo);
View Full Code Here

        String dbName = criteria.getDbName();
        Database database = Torque.getDatabase(dbName);
        DatabaseMap dbMap = database.getDatabaseMap();
        TableMap tableMap = dbMap.getTable(table);
        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
        IdGenerator keyGen
                = database.getIdGenerator(tableMap.getPrimaryKeyMethod());

        ColumnMap pk = getPrimaryKey(criteria);

        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
        // before the insert.
        if (keyGen != null && keyGen.isPriorToInsert())
        {
            // pk will be null if there is no primary key defined for the table
            // we're inserting into.
            if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
            {
                id = getId(pk, keyGen, con, keyInfo);
                criteria.add(pk.getFullyQualifiedName(), id);
            }
        }

        // Use Village to perform the insert.
        TableDataSet tds = null;
        try
        {
            String tableName = SQLBuilder.getFullTableName(table, dbName);
            tds = new TableDataSet(con, tableName);
            Record rec = tds.addRecord();
            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
        }
        catch (DataSetException e)
        {
            throwTorqueException(e);
        }
        catch (SQLException e)
        {
            throwTorqueException(e);
        }
        catch (TorqueException e)
        {
            throwTorqueException(e);
        }
        finally
        {
            VillageUtils.close(tds);
        }

        // If the primary key column is auto-incremented, get the id
        // now.
        if (keyGen != null && keyGen.isPostInsert())
        {
            id = getId(pk, keyGen, con, keyInfo);
        }

        return id;
View Full Code Here

TOP

Related Classes of org.apache.torque.oid.IdGenerator

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.