com.j256.ormlite.table.TableInfo Java Examples

The following examples show how to use com.j256.ormlite.table.TableInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MappedPreparedQueryTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testLimit() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true);
	List<LocalFoo> foos = new ArrayList<LocalFoo>();
	LocalFoo foo = new LocalFoo();
	// create foo #1
	fooDao.create(foo);
	foos.add(foo);
	foo = new LocalFoo();
	// create foo #2
	fooDao.create(foo);
	foos.add(foo);

	TableInfo<LocalFoo, Integer> tableInfo = new TableInfo<LocalFoo, Integer>(databaseType, LocalFoo.class);
	MappedPreparedStmt<LocalFoo, Integer> preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao,
			tableInfo, "select * from " + TABLE_NAME, new FieldType[0], tableInfo.getFieldTypes(),
			new ArgumentHolder[0], 1L, StatementType.SELECT, false);

	checkResults(foos, preparedQuery, 1);
	preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao, tableInfo, "select * from " + TABLE_NAME,
			new FieldType[0], tableInfo.getFieldTypes(), new ArgumentHolder[0], null, StatementType.SELECT, false);
	checkResults(foos, preparedQuery, 2);
}
 
Example #2
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testGeneratedId() throws Exception {
	TableInfo<GeneratedId, Integer> tableInfo =
			new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class);
	Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, false);
	StatementExecutor<GeneratedId, Integer> se =
			new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, dao);
	DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
	databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			isA(GeneratedKeyHolder.class));
	expectLastCall().andAnswer(new IAnswer<Object>() {
		@Override
		public Integer answer() throws Throwable {
			GeneratedKeyHolder keyHolder = (GeneratedKeyHolder) (getCurrentArguments()[3]);
			keyHolder.addKey(2);
			return 1;
		}
	});
	replay(databaseConnection);
	GeneratedId genIdSeq = new GeneratedId();
	se.create(databaseConnection, genIdSeq, null);
	verify(databaseConnection);
}
 
Example #3
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceLong() throws Exception {
	DatabaseType databaseType = new NeedsSequenceDatabaseType();
	connectionSource.setDatabaseType(databaseType);
	Dao<GeneratedIdLong, Long> dao = createDao(GeneratedIdLong.class, false);
	StatementExecutor<GeneratedIdLong, Long> se = new StatementExecutor<GeneratedIdLong, Long>(databaseType,
			new TableInfo<GeneratedIdLong, Long>(databaseType, GeneratedIdLong.class), dao);
	DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
	expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L);
	expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			(GeneratedKeyHolder) isNull())).andReturn(1);

	replay(databaseConnection);
	GeneratedIdLong genIdSeq = new GeneratedIdLong();
	se.create(databaseConnection, genIdSeq, null);
	verify(databaseConnection);
}
 
Example #4
Source File: PostgresDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceAutoName() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
			new TableInfo<GeneratedIdSequenceAutoName, Integer>(databaseType, GeneratedIdSequenceAutoName.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	FieldType idField = tableInfo.getFieldTypes()[0];
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, queriesAfter);
	databaseType.addPrimaryKeySql(new FieldType[] { idField }, additionalArgs, statementsBefore, null,
			queriesAfter);
	String seqName = databaseType
			.generateIdSequenceName(GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
	assertTrue(sb.toString().contains(" DEFAULT NEXTVAL('\"" + seqName + "\"')"));
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0).contains(seqName));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
	assertEquals(0, queriesAfter.size());
}
 
Example #5
Source File: PostgresDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
@Override
public void testGeneratedIdSequence() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<GeneratedIdSequence, Integer> tableInfo =
			new TableInfo<GeneratedIdSequence, Integer>(databaseType, GeneratedIdSequence.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			queriesAfter);
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, queriesAfter);
	assertTrue(sb.toString().contains(" DEFAULT NEXTVAL('\"" + GENERATED_ID_SEQ + "\"')"));
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0).contains(GENERATED_ID_SEQ));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
	assertEquals(0, queriesAfter.size());
}
 
Example #6
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequence() throws Exception {
	DatabaseType databaseType = new NeedsSequenceDatabaseType();
	connectionSource.setDatabaseType(databaseType);
	TableInfo<GeneratedId, Integer> tableInfo =
			new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class);
	Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, false);
	StatementExecutor<GeneratedId, Integer> se =
			new StatementExecutor<GeneratedId, Integer>(databaseType, tableInfo, dao);
	DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
	expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L);
	expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			(GeneratedKeyHolder) isNull())).andReturn(1);

	replay(databaseConnection);
	GeneratedId genIdSeq = new GeneratedId();
	se.create(databaseConnection, genIdSeq, null);
	verify(databaseConnection);
}
 
Example #7
Source File: MysqlDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdBuilt() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<GeneratedId, Integer> tableInfo =
			new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			null);
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains(" AUTO_INCREMENT"));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
}
 
Example #8
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testArgumentHolderSetZero() throws Exception {
	TableInfo<Foo, Integer> tableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class);
	Dao<Foo, Integer> dao = createDao(Foo.class, false);
	MappedCreate<Foo, Integer> mappedCreate = MappedCreate.build(dao, tableInfo);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			isA(GeneratedKeyHolder.class))).andAnswer(new IAnswer<Integer>() {
				@Override
				public Integer answer() throws Throwable {
					GeneratedKeyHolder holder = (GeneratedKeyHolder) getCurrentArguments()[3];
					holder.addKey((Integer) 0);
					return 1;
				}
			});
	replay(conn);
	mappedCreate.insert(databaseType, conn, new Foo(), null);
}
 
Example #9
Source File: HsqldbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Override
@Test
public void testGeneratedIdSequence() throws Exception {
	TableInfo<GeneratedIdSequence, Integer> tableInfo =
			new TableInfo<GeneratedIdSequence, Integer>(databaseType, GeneratedIdSequence.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			null);
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, null);
	assertTrue(sb + " should contain autoincrement stuff",
			sb.toString().contains(" GENERATED BY DEFAULT AS IDENTITY "));
	// sequence, sequence table, insert
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0).contains(GENERATED_ID_SEQ.toUpperCase()));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
}
 
Example #10
Source File: BaseJdbcDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testGeneratedIdSequence() throws Exception {
	if (connectionSource == null) {
		throw new SQLException("Simulate a failure");
	}
	TableInfo<GeneratedIdSequence, Integer> tableInfo =
			new TableInfo<GeneratedIdSequence, Integer>(databaseType, GeneratedIdSequence.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	ArrayList<String> additionalArgs = new ArrayList<String>();
	ArrayList<String> statementsBefore = new ArrayList<String>();
	ArrayList<String> statementsAfter = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore,
			statementsAfter, queriesAfter);
}
 
Example #11
Source File: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testInSubQuery() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	qb.selectColumns(ID_COLUMN_NAME);
	where.in(ID_COLUMN_NAME, qb);
	StringBuilder whereSb = new StringBuilder();
	where.appendSql(null, whereSb, new ArrayList<ArgumentHolder>());
	StringBuilder sb = new StringBuilder();
	databaseType.appendEscapedEntityName(sb, ID_COLUMN_NAME);
	sb.append(" IN (");
	sb.append("SELECT ");
	databaseType.appendEscapedEntityName(sb, ID_COLUMN_NAME);
	sb.append(" FROM ");
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" ) ");
	assertEquals(sb.toString(), whereSb.toString());
}
 
Example #12
Source File: BaseJdbcDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testCreateColumnArg() throws Exception {
	if (connectionSource == null) {
		return;
	}
	List<String> additionalArgs = new ArrayList<String>();
	List<String> moreStmts = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	TableInfo<StringId, String> tableInfo = new TableInfo<StringId, String>(databaseType, StringId.class);
	FieldType fieldType = tableInfo.getIdField();
	StringBuilder sb = new StringBuilder();
	databaseType.appendColumnArg(null, sb, fieldType, additionalArgs, null, moreStmts, queriesAfter);
	assertTrue(sb.toString().contains(fieldType.getColumnName()));
	if (!sb.toString().contains("PRIMARY KEY")) {
		databaseType.addPrimaryKeySql(new FieldType[] { fieldType }, additionalArgs, null, moreStmts, queriesAfter);
		assertEquals(1, additionalArgs.size());
		assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
	}
}
 
Example #13
Source File: UpdateBuilderTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testUpdateDate() throws Exception {
	Dao<UpdateDate, Integer> dao = createDao(UpdateDate.class, true);
	UpdateDate updateDate = new UpdateDate();
	updateDate.date = new Date();
	assertEquals(1, dao.create(updateDate));
	TableInfo<UpdateDate, Integer> tableInfo = new TableInfo<UpdateDate, Integer>(databaseType, UpdateDate.class);
	UpdateBuilder<UpdateDate, Integer> stmtb = new UpdateBuilder<UpdateDate, Integer>(databaseType, tableInfo, dao);
	Date newDate = new Date(System.currentTimeMillis() + 10);
	stmtb.updateColumnValue(UpdateDate.DATE_FIELD, newDate);
	// this used to cause a NPE because of a missing args
	assertEquals(1, dao.update(stmtb.prepare()));
	// make sure the update worked
	UpdateDate updateDate2 = dao.queryForId(updateDate.id);
	assertNotNull(updateDate2);
	assertEquals(newDate, updateDate2.date);
}
 
Example #14
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdSequenceAutoName() throws Exception {
	TableInfo<GeneratedIdSequenceAutoName, Integer> tableInfo =
			new TableInfo<GeneratedIdSequenceAutoName, Integer>(databaseType, GeneratedIdSequenceAutoName.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	FieldType idField = tableInfo.getFieldTypes()[0];
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, idField, additionalArgs, statementsBefore, null, null);
	assertEquals(1, statementsBefore.size());
	String seqName = databaseType
			.generateIdSequenceName(GeneratedIdSequenceAutoName.class.getSimpleName().toLowerCase(), idField);
	assertTrue(statementsBefore.get(0).contains(seqName));
	assertEquals(0, additionalArgs.size());
}
 
Example #15
Source File: OracleDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
@Override
public void testGeneratedIdSequence() throws Exception {
	TableInfo<GeneratedIdSequence, Integer> tableInfo =
			new TableInfo<GeneratedIdSequence, Integer>(databaseType, GeneratedIdSequence.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			null);
	assertEquals(1, statementsBefore.size());
	assertTrue(statementsBefore.get(0) + " should contain sequence",
			statementsBefore.get(0).contains(GENERATED_ID_SEQ.toUpperCase()));
	assertEquals(0, additionalArgs.size());
}
 
Example #16
Source File: MariaDbDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedIdBuilt() throws Exception {
	if (connectionSource == null) {
		return;
	}
	TableInfo<GeneratedId, Integer> tableInfo =
			new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class);
	assertEquals(2, tableInfo.getFieldTypes().length);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			null);
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, null);
	assertTrue(sb.toString().contains(" AUTO_INCREMENT"));
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
}
 
Example #17
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testDeleteThrow() throws Exception {
	TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class);
	DatabaseConnection connection = createMock(DatabaseConnection.class);
	@SuppressWarnings("unchecked")
	PreparedDelete<Foo> delete = createMock(PreparedDelete.class);
	CompiledStatement compiledStmt = createMock(CompiledStatement.class);
	expect(delete.compile(connection, StatementType.DELETE)).andReturn(compiledStmt);
	expect(compiledStmt.runUpdate()).andThrow(new SQLException("expected"));
	compiledStmt.close();
	StatementExecutor<Foo, String> statementExec =
			new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
	replay(connection, compiledStmt, delete);
	try {
		statementExec.delete(connection, delete);
		fail("should have thrown");
	} catch (SQLException e) {
		// expected
	}
	verify(connection, compiledStmt, delete);
}
 
Example #18
Source File: DerbyEmbeddedDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testGeneratedId() throws Exception {
	TableInfo<GeneratedId, Integer> tableInfo =
			new TableInfo<GeneratedId, Integer>(databaseType, GeneratedId.class);
	StringBuilder sb = new StringBuilder();
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	databaseType.appendColumnArg(null, sb, tableInfo.getFieldTypes()[0], additionalArgs, statementsBefore, null,
			null);
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, null, null);
	assertTrue(sb + "should contain the stuff",
			sb.toString().contains(" INTEGER GENERATED BY DEFAULT AS IDENTITY"));
	assertEquals(0, statementsBefore.size());
	assertEquals(1, additionalArgs.size());
	assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
}
 
Example #19
Source File: MappedCreateTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test(expected = SQLException.class)
public void testArgumentHolderDoubleSet() throws Exception {
	TableInfo<Foo, Integer> tableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class);
	Dao<Foo, Integer> dao = createDao(Foo.class, false);
	MappedCreate<Foo, Integer> mappedCreate = MappedCreate.build(dao, tableInfo);
	DatabaseConnection conn = createMock(DatabaseConnection.class);
	expect(conn.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
			isA(GeneratedKeyHolder.class))).andAnswer(new IAnswer<Integer>() {
				@Override
				public Integer answer() throws Throwable {
					GeneratedKeyHolder holder = (GeneratedKeyHolder) getCurrentArguments()[3];
					holder.addKey((Integer) 1);
					holder.addKey((Integer) 2);
					return 1;
				}
			});
	replay(conn);
	mappedCreate.insert(databaseType, conn, new Foo(), null);
}
 
Example #20
Source File: BaseCoreStmtTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Override
@Before
public void before() throws Exception {
	super.before();

	Field field = Foo.class.getDeclaredField("stringField");
	assertEquals(String.class, field.getType());
	stringFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foo.class);
	stringFieldType.configDaoInformation(connectionSource, Foo.class);
	field = Foo.class.getDeclaredField("val");
	assertEquals(int.class, field.getType());
	numberFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foo.class);
	numberFieldType.configDaoInformation(connectionSource, Foo.class);
	field = Foreign.class.getDeclaredField("foo");
	assertEquals(Foo.class, field.getType());
	foreignFieldType = FieldType.createFieldType(databaseType, "BaseFoo", field, Foreign.class);
	foreignFieldType.configDaoInformation(connectionSource, Foreign.class);

	baseFooTableInfo = new TableInfo<Foo, Integer>(databaseType, Foo.class);
	baseSchemaFooTableInfo = new TableInfo<SchemaFoo, Integer>(databaseType, SchemaFoo.class);
}
 
Example #21
Source File: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testExistsSubQuery() throws Exception {
	TableInfo<ForeignFoo, Integer> tableInfo = new TableInfo<ForeignFoo, Integer>(databaseType, ForeignFoo.class);
	Where<ForeignFoo, Integer> where = new Where<ForeignFoo, Integer>(tableInfo, null, databaseType);
	BaseDaoImpl<ForeignFoo, Integer> foreignDao =
			new BaseDaoImpl<ForeignFoo, Integer>(connectionSource, ForeignFoo.class) {
			};
	QueryBuilder<ForeignFoo, Integer> qb = foreignDao.queryBuilder();
	where.exists(qb);
	StringBuilder whereSb = new StringBuilder();
	where.appendSql(null, whereSb, new ArrayList<ArgumentHolder>());
	StringBuilder sb = new StringBuilder();
	sb.append("EXISTS (");
	sb.append("SELECT * FROM ");
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" ) ");
	assertEquals(sb.toString(), whereSb.toString());
}
 
Example #22
Source File: MappedDeleteCollectionTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testNoIdBuildDelete() throws Exception {
	DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
	replay(connectionSource);
	Dao<NoId, Void> dao = createDao(NoId.class, false);
	MappedDeleteCollection.deleteObjects(dao, new TableInfo<NoId, Void>(databaseType, NoId.class),
			databaseConnection, new ArrayList<NoId>(), null);
}
 
Example #23
Source File: StatementExecutorTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCallBatchTasksAutoCommitTrueSynchronized() throws Exception {
	TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class);

	ConnectionSource connectionSource = createMock(ConnectionSource.class);
	DatabaseConnection connection = createMock(DatabaseConnection.class);
	expect(connectionSource.isSingleConnection("foo")).andReturn(true);
	expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection);
	expect(connectionSource.saveSpecialConnection(connection)).andReturn(false);
	connectionSource.clearSpecialConnection(connection);
	connectionSource.releaseConnection(connection);

	expect(connection.isAutoCommitSupported()).andReturn(true);
	expect(connection.isAutoCommit()).andReturn(true);
	connection.setAutoCommit(false);
	connection.setAutoCommit(true);
	StatementExecutor<Foo, String> statementExec =
			new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
	replay(connectionSource, connection);
	final AtomicBoolean called = new AtomicBoolean(false);
	statementExec.callBatchTasks(connectionSource, new Callable<Void>() {
		@Override
		public Void call() {
			called.set(true);
			return null;
		}
	});
	assertTrue(called.get());
	verify(connectionSource, connection);
}
 
Example #24
Source File: MappedQueryForFieldEq.java    From ormlite-core with ISC License 5 votes vote down vote up
protected static <T, ID> String buildStatement(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		FieldType idFieldType) {
	// build the select statement by hand
	StringBuilder sb = new StringBuilder(64);
	appendTableName(databaseType, sb, "SELECT * FROM ", tableInfo);
	appendWhereFieldEq(databaseType, idFieldType, sb, null);
	return sb.toString();
}
 
Example #25
Source File: MappedQueryForFieldEq.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedQueryForFieldEq<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo,
		FieldType idFieldType) throws SQLException {
	if (idFieldType == null) {
		idFieldType = tableInfo.getIdField();
		if (idFieldType == null) {
			throw new SQLException("Cannot query-for-id with " + tableInfo.getDataClass()
					+ " because it doesn't have an id field");
		}
	}
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	String statement = buildStatement(databaseType, tableInfo, idFieldType);
	return new MappedQueryForFieldEq<T, ID>(dao, tableInfo, statement, new FieldType[] { idFieldType },
			tableInfo.getFieldTypes(), "query-for-id");
}
 
Example #26
Source File: MappedDeleteCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the
 * datas could be variable sized.
 */
public static <T, ID> int deleteObjects(Dao<T, ID> dao, TableInfo<T, ID> tableInfo,
		DatabaseConnection databaseConnection, Collection<T> datas, ObjectCache objectCache) throws SQLException {
	MappedDeleteCollection<T, ID> deleteCollection = MappedDeleteCollection.build(dao, tableInfo, datas.size());
	Object[] fieldObjects = new Object[datas.size()];
	FieldType idField = tableInfo.getIdField();
	int objC = 0;
	for (T data : datas) {
		fieldObjects[objC] = idField.extractJavaFieldToSqlArgValue(data);
		objC++;
	}
	return updateRows(databaseConnection, tableInfo.getDataClass(), deleteCollection, fieldObjects, objectCache);
}
 
Example #27
Source File: MappedRefresh.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedRefresh<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException {
	FieldType idField = tableInfo.getIdField();
	if (idField == null) {
		throw new SQLException(
				"Cannot refresh " + tableInfo.getDataClass() + " because it doesn't have an id field");
	}
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	String statement = buildStatement(databaseType, tableInfo, idField);
	return new MappedRefresh<T, ID>(dao, tableInfo, statement, new FieldType[] { tableInfo.getIdField() },
			tableInfo.getFieldTypes());
}
 
Example #28
Source File: UpdateBuilderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUpdateForeignCollection() throws Exception {
	UpdateBuilder<OurForeignCollection, Integer> stmtb =
			new UpdateBuilder<OurForeignCollection, Integer>(databaseType,
					new TableInfo<OurForeignCollection, Integer>(databaseType, OurForeignCollection.class), null);
	stmtb.updateColumnValue(OurForeignCollection.FOOS_FIELD_NAME, null);
}
 
Example #29
Source File: MappedCreate.java    From ormlite-core with ISC License 5 votes vote down vote up
private MappedCreate(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
		String queryNextSequenceStmt, int versionFieldTypeIndex) {
	super(dao, tableInfo, statement, argFieldTypes);
	this.dataClassName = tableInfo.getDataClass().getSimpleName();
	this.queryNextSequenceStmt = queryNextSequenceStmt;
	this.versionFieldTypeIndex = versionFieldTypeIndex;
}
 
Example #30
Source File: UpdateBuilderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUpdateForeignCollectionColumnExpression() throws Exception {
	UpdateBuilder<OurForeignCollection, Integer> stmtb =
			new UpdateBuilder<OurForeignCollection, Integer>(databaseType,
					new TableInfo<OurForeignCollection, Integer>(databaseType, OurForeignCollection.class), null);
	stmtb.updateColumnExpression(OurForeignCollection.FOOS_FIELD_NAME, "1");
}