All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.rdbms.mapping.datastore.ArrayRDBMSMapping Maven / Gradle / Ivy

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2015 Andy Jefferson 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:
    ...
**********************************************************************/
package org.datanucleus.store.rdbms.mapping.datastore;

import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;

import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.table.Column;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.util.Localiser;

/**
 * Mapping of an ARRAY RDBMS type.
 * Note that this is designed around how PostgreSQL handles arrays, and is largely limited to what is available for that datastore.
 */
public class ArrayRDBMSMapping extends AbstractDatastoreMapping
{
    /** SQL type for the element. */
    String arrayElemSqlType = null;

    public ArrayRDBMSMapping(JavaTypeMapping mapping, RDBMSStoreManager storeMgr, Column column)
    {
		super(storeMgr, mapping);
		this.column = column;
		initialize();

		// PostgreSQL will have a type like "INT ARRAY" or "TEXT ARRAY", so this finds the element SQL type
		String arrayTypeName = column.getTypeName();
		if (arrayTypeName.indexOf("array") > 0)
		{
		    arrayElemSqlType = arrayTypeName.substring(0, arrayTypeName.indexOf("array")).trim();
		}
		else if (arrayTypeName.indexOf("ARRAY") > 0)
		{
            arrayElemSqlType = arrayTypeName.substring(0, arrayTypeName.indexOf("ARRAY")).trim();
		}
		else
		{
		    throw new NucleusUserException("Do not support handling of type=" + arrayTypeName + " for member=" + mapping.getMemberMetaData().getFullFieldName());
		}
	}

    private void initialize()
    {
		initTypeInfo();
    }

    public int getJDBCType()
    {
        return Types.ARRAY;
    }

    public void setObject(PreparedStatement ps, int param, Object value)
    {
        try
        {
            if (value == null)
            {
                ps.setNull(param, getJDBCType());
            }
            else
            {
                Array array = null;
                if (value.getClass().isArray())
                {
                    // Convert the array into a java.sql.Array
                    int numElems = java.lang.reflect.Array.getLength(value);
                    Object[] elems = new Object[numElems];
                    for (int i=0;i coll;
                    try
                    {
                        Class instanceType = SCOUtils.getContainerInstanceType(mmd.getType(), mmd.getOrderMetaData() != null);
                        coll = (Collection) instanceType.newInstance();
                    }
                    catch (Exception e)
                    {
                        throw new NucleusDataStoreException(e.getMessage(), e);
                    }

                    for (int i=0;i