Search in sources :

Example 61 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class SqlParameterSourceTest method testSource.

@Test
public void testSource() {
    SqlParameterSourceFactory<DbObject> parameterSourceFactory = JdbcTemplateMapperFactory.newInstance().newSqlParameterSourceFactory(DbObject.class);
    DbObject[] dbObjects = new DbObject[10];
    for (int i = 0; i < dbObjects.length; i++) {
        dbObjects[i] = new DbObject();
        dbObjects[i].setId(i);
    }
    validate(parameterSourceFactory.newSqlParameterSources(dbObjects));
    validate(parameterSourceFactory.newSqlParameterSources(Arrays.asList(dbObjects)));
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 62 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class SqlParameterSourceTest method testConstantValue.

@Test
public void testConstantValue() {
    SqlParameterSourceFactory<DbObject> parameterSourceFactory = JdbcTemplateMapperFactory.newInstance().addColumnProperty("id", new ConstantValueProperty<Long>(-3l, Long.class)).newSqlParameterSourceFactory(DbObject.class);
    SqlParameterSource parameterSource = parameterSourceFactory.newSqlParameterSource(new DbObject());
    assertEquals(-3l, parameterSource.getValue("id"));
}
Also used : SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) DbObject(org.simpleflatmapper.test.beans.DbObject) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) Test(org.junit.Test)

Example 63 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class Sql2oIntegrationTest method testSql2O.

@Test
public void testSql2O() throws SQLException, ParseException {
    Connection connection = DbHelper.objectDb();
    try {
        SingleConnectionDataSource scds = new SingleConnectionDataSource(connection, true);
        Sql2o sql2o = new Sql2o(scds);
        Query query = sql2o.open().createQuery(DbHelper.TEST_DB_OBJECT_QUERY);
        query.setAutoDeriveColumnNames(true);
        query.setResultSetHandlerFactoryBuilder(new SfmResultSetHandlerFactoryBuilder());
        List<DbObject> dbObjects = query.executeAndFetch(DbObject.class);
        assertEquals(1, dbObjects.size());
        DbHelper.assertDbObjectMapping(dbObjects.get(0));
    } finally {
        connection.close();
    }
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) Query(org.sql2o.Query) DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) SfmResultSetHandlerFactoryBuilder(org.simpleflatmapper.sql2o.SfmResultSetHandlerFactoryBuilder) Sql2o(org.sql2o.Sql2o) Test(org.junit.Test)

Example 64 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcTemplateCrudTest method testCrud.

@Test
public void testCrud() throws SQLException {
    JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
    DbObject object = DbObject.newInstance();
    assertNull(objectCrud.read(object.getId()));
    // create
    Long key = objectCrud.create(object, new CheckedConsumer<Long>() {

        Long key;

        @Override
        public void accept(Long aLong) throws Exception {
            key = aLong;
        }
    }).key;
    assertNull(key);
    key = object.getId();
    // read
    assertEquals(object, objectCrud.read(key));
    object.setName("Udpdated");
    // update
    objectCrud.update(object);
    assertEquals(object, objectCrud.read(key));
    // delete
    objectCrud.delete(key);
    assertNull(objectCrud.read(key));
    objectCrud.create(object);
    assertEquals(object, objectCrud.read(key));
    objectCrud.delete(key);
    try {
        objectCrud.createOrUpdate(object);
        assertEquals(object, objectCrud.read(key));
    } catch (UnsupportedOperationException e) {
    }
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 65 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcTemplateCrudTest method testWhere.

@Test
public void testWhere() throws SQLException {
    JdbcTemplateCrud<DbObject, Long> objectCrud = JdbcTemplateMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
    DbObject object = DbObject.newInstance();
    ConnectedSelectQuery<DbObject, String> objectByName = objectCrud.where(" name = :name", String.class);
    assertNull(objectByName.readFirst(object.getName()));
    objectCrud.create(object);
    assertEquals(object, objectByName.readFirst(object.getName()));
    assertEquals(Arrays.asList(object), objectByName.read(object.getName(), new ListCollector<DbObject>()).getList());
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3