Search in sources :

Example 6 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class MethodAccessorsGenerator method newGetter.

public Getter newGetter(final Method method) {
    final Class type = method.getReturnType();
    final MethodAccessor methodAccessor = newMethodAccessor(method);
    return new Getter() {

        public Object getProperty(Object obj) {
            try {
                return methodAccessor.invoke(obj, null);
            } catch (InvocationTargetException e) {
                throw new Sql2oException("error while calling getter method with name " + method.getName() + " on class " + obj.getClass().toString(), e);
            }
        }

        public Class getType() {
            return type;
        }
    };
}
Also used : Sql2oException(org.sql2o.Sql2oException) MethodAccessor(sun.reflect.MethodAccessor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class Pojo method getProperty.

@SuppressWarnings("unchecked")
public Object getProperty(String propertyPath, Quirks quirks) {
    // String.split uses RegularExpression
    // this is overkill for every column for every row
    int index = propertyPath.indexOf('.');
    Getter getter;
    if (index > 0) {
        final String substring = propertyPath.substring(0, index);
        getter = metadata.getPropertyGetter(substring);
        String newPath = propertyPath.substring(index + 1);
        Object subValue = this.metadata.getValueOfProperty(substring, this.object);
        if (subValue == null) {
            try {
                subValue = getter.getType().newInstance();
            } catch (InstantiationException e) {
                throw new Sql2oException("Could not instantiate a new instance of class " + getter.getType().toString(), e);
            } catch (IllegalAccessException e) {
                throw new Sql2oException("Could not instantiate a new instance of class " + getter.getType().toString(), e);
            }
            return getter.getProperty(this.object);
        }
        PojoMetadata subMetadata = new PojoMetadata(getter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure);
        Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue);
        return subPojo.getProperty(newPath, quirks);
    } else {
        getter = metadata.getPropertyGetter(propertyPath);
        Converter converter;
        try {
            converter = throwIfNull(getter.getType(), quirks.converterOf(getter.getType()));
        } catch (ConverterException e) {
            throw new Sql2oException("Cannot convert column " + propertyPath + " to type " + getter.getType(), e);
        }
        try {
            return converter.convert(getter.getProperty(this.object));
        } catch (ConverterException e) {
            throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + getter.getType(), e);
        }
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) ConverterException(org.sql2o.converters.ConverterException) Converter(org.sql2o.converters.Converter)

Example 8 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class IssuesTest method testIssue134ThrowOnMappingErrorProperty.

/**
 * Testing for github issue #134.
 * Add option to ignore mapping errors
 */
@Test
public void testIssue134ThrowOnMappingErrorProperty() {
    String sql = "select 1 id, 'foo' val1, 'bar' val2 from (values(0))";
    class Pojo {

        public int id;

        public String val1;
    }
    try (Connection connection = sql2o.open()) {
        try {
            Pojo pojo = connection.createQuery(sql).executeAndFetchFirst(Pojo.class);
            fail("Expeced an exception to be thrown");
        } catch (Sql2oException e) {
            assertEquals("Could not map VAL2 to any property.", e.getMessage());
        }
        Pojo pojo = connection.createQuery(sql).throwOnMappingFailure(false).executeAndFetchFirst(Pojo.class);
        assertEquals(1, pojo.id);
        assertEquals("foo", pojo.val1);
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) Issue1Pojo(org.sql2o.issues.pojos.Issue1Pojo) Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 9 with Sql2oException

use of org.sql2o.Sql2oException in project sql2o by aaberg.

the class IssuesTest method testForNpeInRowGet.

/**
 *  Tests for issue #4 https://github.com/aaberg/sql2o/issues/4
 *
 *  NPE when typing wrong column name in row.get(...)
 *  Also, column name should not be case sensitive, if sql2o not is in casesensitive property is false.
 */
@Test
public void testForNpeInRowGet() {
    sql2o.createQuery("create table issue4table(id integer identity primary key, val varchar(20))").executeUpdate();
    sql2o.createQuery("insert into issue4table (val) values (:val)").addParameter("val", "something").addToBatch().addParameter("val", "something else").addToBatch().addParameter("val", "hello").addToBatch().executeBatch();
    Table table = sql2o.createQuery("select * from issue4table").executeAndFetchTable();
    Row row0 = table.rows().get(0);
    String row0Val = row0.getString("vAl");
    assertEquals("something", row0Val);
    Row row1 = table.rows().get(1);
    boolean failed = false;
    try {
        // Should fail with an sql2o exception
        String row1Value = row1.getString("ahsHashah");
    } catch (Sql2oException ex) {
        failed = true;
        assertTrue(ex.getMessage().startsWith("Column with name 'ahsHashah' does not exist"));
    }
    assertTrue("assert that exception occurred", failed);
}
Also used : Sql2oException(org.sql2o.Sql2oException) Table(org.sql2o.data.Table) Row(org.sql2o.data.Row) Test(org.junit.Test)

Aggregations

Sql2oException (org.sql2o.Sql2oException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Test (org.junit.Test)3 Connection (org.sql2o.Connection)2 Converter (org.sql2o.converters.Converter)2 ConverterException (org.sql2o.converters.ConverterException)2 MethodAccessor (sun.reflect.MethodAccessor)2 Row (org.sql2o.data.Row)1 Table (org.sql2o.data.Table)1 Issue1Pojo (org.sql2o.issues.pojos.Issue1Pojo)1 ConstructorAccessor (sun.reflect.ConstructorAccessor)1