Add a type mapper callback for java.net.URL
-------------------------------------------

         Key: IBATIS-299
         URL: http://issues.apache.org/jira/browse/IBATIS-299
     Project: iBatis for Java
        Type: Wish

    Versions: 2.1.7    
    Reporter: Ken Weiner


It would be nice if iBATIS came with a TypeHandlerCallback implementation for 
mapping URL object properties to their String representation stored in a 
database field.  Here is some code that could be used to implement this:

package com.ibatis.sqlmap.client.extensions;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.sql.Types;

/**
 * An iBATIS type handler callback for java.net.URLs that are mapped to
 * Strings in the database.  If a URL cannot be constructed based on the
 * String, then the URL will be set to <code>null</code>.
 * <p>
 * @author Ken Weiner
 */
public class UrlTypeHandlerCallback implements TypeHandlerCallback {

    public Object getResult(ResultGetter getter) throws SQLException {
        String value = getter.getString();
        if (getter.wasNull()) {
            return null;
        }
        return this.valueOf(value);
    }

    public void setParameter(ParameterSetter setter, Object parameter) throws 
SQLException {
        if (parameter == null) {
            setter.setNull(Types.VARCHAR);
        } else {
            URL url = (URL) parameter;
            setter.setString(url.toExternalForm());
        }
    }

    public Object valueOf(String s) {
        URL url;
        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
            url = null;
        }
        return url;
    }

}


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to