Package org.jpox.store.rdbms.mapping

Source Code of org.jpox.store.rdbms.mapping.BlobRDBMSMapping

/**********************************************************************
Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
2004 Andy Jefferson - localised messages
2006 Andy Jefferson - migrated to support serialised and non-serialised BLOBs
    ...
**********************************************************************/
package org.jpox.store.rdbms.mapping;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.store.mapped.DatastoreField;
import org.jpox.store.mapped.MappedStoreManager;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.rdbms.datatype.BlobImpl;
import org.jpox.store.rdbms.typeinfo.TypeInfo;

/**
* Mapping of a BLOB RDBMS type.
* A BLOB column can be treated in two ways in terms of storage and retrieval.
* <ul>
* <li>Serialise the field into the BLOB using ObjectOutputStream, and deserialise
* it back using ObjectInputStream</li>
* <li>Store the field using a byte[] stream, and retrieve it in the same way.</li>
* </ul>
*
* @version $Revision: 1.16 $
*/
public class BlobRDBMSMapping extends AbstractLargeBinaryRDBMSMapping
{
    /**
     * Constructor.
     * @param storeMgr Store Manager
     * @param mapping Java type mapping
     */
    protected BlobRDBMSMapping(MappedStoreManager storeMgr, JavaTypeMapping mapping)
    {
        super(storeMgr, mapping);
    }

    /**
     * Constructor.
     * @param mapping Java type mapping
     * @param storeMgr Store Manager
     * @param field Field to be mapped
     */
    public BlobRDBMSMapping(JavaTypeMapping mapping, MappedStoreManager storeMgr, DatastoreField field)
    {
    super(mapping, storeMgr, field);
  }

    /**
     * Accessor for the RDBMS BLOB type being represented.
     * @return TypeInfo for the BLOB type.
     */
    public TypeInfo getTypeInfo()
    {
        return getDatabaseAdapter().getTypeInfo(Types.BLOB);
    }

    public void setString(Object ps, int param, String value)
    {
        try
        {
            if (getDatabaseAdapter().supportsSettingBlobUsingSetString())
            {
                if (value == null)
                {
                    if (column.isDefaultable() && column.getDefaultValue() != null)
                    {
                        ((PreparedStatement) ps).setString(param,column.getDefaultValue().toString().trim());
                    }
                    else
                    {
                        ((PreparedStatement) ps).setNull(param,getTypeInfo().dataType);
                    }
                }
                else
                {
                    ((PreparedStatement) ps).setString(param, value);
                }
            }
            else
            {
                if (value == null)
                {
                    if (column.isDefaultable() && column.getDefaultValue() != null)
                    {
                        ((PreparedStatement) ps).setBlob(param,new BlobImpl(column.getDefaultValue().toString().trim()));
                    }
                    else
                    {
                        ((PreparedStatement) ps).setNull(param,getTypeInfo().dataType);
                    }
                }
                else
                {
                    ((PreparedStatement) ps).setBlob(param, new BlobImpl(value));
                }
            }
        }
        catch (SQLException e)
        {
            throw new JPOXDataStoreException(LOCALISER.msg("055001", "String", "" + value, column, e.getMessage()), e);
        }
        catch (IOException e)
        {
            throw new JPOXDataStoreException(LOCALISER.msg("055001", "String", "" + value, column, e.getMessage()), e);
        }
    }

    public String getString(Object rs, int param)
    {
        String value;

        try
        {
            if (getDatabaseAdapter().supportsSettingBlobUsingSetString())
            {
                value = ((ResultSet) rs).getString(param);
            }
            else
            {
                byte[] bytes = ((ResultSet) rs).getBytes(param);
                if (bytes == null)
                {
                    value = null;
                }     
                else
                {
                    BlobImpl blob = new BlobImpl(bytes);
                    value = (String)blob.getObject();
                }
            }
        }
        catch (SQLException e)
        {
            throw new JPOXDataStoreException(LOCALISER.msg("055002", "String", "" + param, column, e.getMessage()), e);
        }

        return value;
    }
}
TOP

Related Classes of org.jpox.store.rdbms.mapping.BlobRDBMSMapping

TOP
Copyright © 2018 www.massapi.com. 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.