Java Code Examples for com.j256.ormlite.support.GeneratedKeyHolder#addKey()
The following examples show how to use
com.j256.ormlite.support.GeneratedKeyHolder#addKey() .
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: JdbcDatabaseConnectionTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testQueryKeyHolderNoKeys() throws Exception { DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); try { createDao(Foo.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); expect(keyHolder.getColumnName()).andReturn("id"); keyHolder.addKey(or(eq(0), eq(0L))); // Depends on driver replay(keyHolder); StringBuilder sb = new StringBuilder(); sb.append("insert into foo ("); databaseType.appendEscapedEntityName(sb, "id"); sb.append(") values (2)"); databaseConnection.insert(sb.toString(), new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
Example 2
Source File: JdbcDatabaseConnectionTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testIdColumnInteger() throws Exception { // NOTE: this doesn't seem to generate an INTEGER type, oh well DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOINT_TABLE_NAME); try { createDao(FooInt.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); expect(keyHolder.getColumnName()).andReturn("id"); keyHolder.addKey(or(eq(2), eq(2L))); // Depends on driver replay(keyHolder); StringBuilder sb = new StringBuilder(); sb.append("insert into fooint ("); databaseType.appendEscapedEntityName(sb, "id"); sb.append(") values (2)"); databaseConnection.insert(sb.toString(), new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
Example 3
Source File: JdbcDatabaseConnectionTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testIdColumnInvalid() throws Exception { // NOTE: this doesn't seem to generate an INTEGER type, oh well DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOINT_TABLE_NAME); try { createDao(FooInt.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); expect(keyHolder.getColumnName()).andReturn("id"); keyHolder.addKey(or(eq(1), eq(1L))); // Depends on driver replay(keyHolder); StringBuilder sb = new StringBuilder(); sb.append("insert into fooint ("); databaseType.appendEscapedEntityName(sb, "stuff"); sb.append(") values ('zipper')"); databaseConnection.insert(sb.toString(), new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
Example 4
Source File: JdbcDatabaseConnectionTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testIdColumnChangedFromStringToNumber() throws Exception { // NOTE: trying to get the database to return a string as a result but could not figure it out DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOOSTRING_TABLE_NAME); try { createDao(FooString.class, true); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); expect(keyHolder.getColumnName()).andReturn("id"); keyHolder.addKey(or(eq(0), eq(0L))); // Depends on driver replay(keyHolder); StringBuilder sb = new StringBuilder(); sb.append("insert into foostring ("); databaseType.appendEscapedEntityName(sb, "id"); sb.append(", "); databaseType.appendEscapedEntityName(sb, "stuff"); sb.append(") values ('12', 'zipper')"); databaseConnection.insert(sb.toString(), new Object[0], new FieldType[0], keyHolder); verify(keyHolder); } finally { connectionSource.releaseConnection(databaseConnection); } }
Example 5
Source File: AndroidDatabaseConnection.java From ormlite-android with ISC License | 6 votes |
@Override public int insert(String statement, Object[] args, FieldType[] argFieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { SQLiteStatement stmt = null; try { stmt = db.compileStatement(statement); bindArgs(stmt, args, argFieldTypes); long rowId = stmt.executeInsert(); if (keyHolder != null) { keyHolder.addKey(rowId); } /* * I've decided to not do the CHANGES() statement here like we do down below in UPDATE because we know that * it worked (since it didn't throw) so we know that 1 is right. */ int result = 1; logger.trace("{}: insert statement is compiled and executed, changed {}: {}", this, result, statement); return result; } catch (android.database.SQLException e) { throw SqlExceptionUtil.create("inserting to database failed: " + statement, e); } finally { closeQuietly(stmt); } }
Example 6
Source File: H2DatabaseConnection.java From ormlite-core with ISC License | 6 votes |
@Override public int insert(String statement, Object[] args, FieldType[] argFieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { PreparedStatement stmt; if (keyHolder == null) { stmt = connection.prepareStatement(statement); } else { stmt = connection.prepareStatement(statement, new String[] { keyHolder.getColumnName() }); } statementSetArgs(stmt, args, argFieldTypes); int rowN = stmt.executeUpdate(); if (keyHolder != null) { ResultSet resultSet = stmt.getGeneratedKeys(); ResultSetMetaData metaData = resultSet.getMetaData(); int colN = metaData.getColumnCount(); while (resultSet.next()) { for (int colC = 1; colC <= colN; colC++) { // get the id column data so we can pass it back to the caller thru the keyHolder Number id = getIdColumnData(resultSet, metaData, colC); keyHolder.addKey(id); } } } return rowN; }
Example 7
Source File: JdbcDatabaseConnection.java From ormlite-jdbc with ISC License | 5 votes |
@Override public int insert(String statement, Object[] args, FieldType[] argFieldTypes, GeneratedKeyHolder keyHolder) throws SQLException { PreparedStatement stmt; if (keyHolder == null) { stmt = connection.prepareStatement(statement); } else { stmt = connection.prepareStatement(statement, new String[] { keyHolder.getColumnName() }); } try { statementSetArgs(stmt, args, argFieldTypes); int rowN = stmt.executeUpdate(); logger.trace("insert statement is prepared and executed: {}", statement); if (keyHolder != null) { ResultSet resultSet = stmt.getGeneratedKeys(); ResultSetMetaData metaData = resultSet.getMetaData(); int colN = metaData.getColumnCount(); boolean wasSet = false; while (resultSet.next()) { for (int colC = 1; colC <= colN; colC++) { // get the id column data so we can pass it back to the caller thru the keyHolder Number id = getIdColumnData(resultSet, metaData, colC); keyHolder.addKey(id); wasSet = true; } } if (!wasSet) { throw new SQLException( "no generated-keys were returned from statement, maybe a schema mismatch between entity and database table?: " + statement); } } return rowN; } finally { stmt.close(); } }
Example 8
Source File: JdbcDatabaseConnectionTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testInsertReleaseConnection() throws Exception { Connection connection = createMock(Connection.class); PreparedStatement prepStmt = createMock(PreparedStatement.class); GeneratedKeyHolder keyHolder = createMock(GeneratedKeyHolder.class); ResultSet resultSet = createMock(ResultSet.class); ResultSetMetaData metaData = createMock(ResultSetMetaData.class); JdbcDatabaseConnection jdc = new JdbcDatabaseConnection(connection); String statement = "statement"; expect(keyHolder.getColumnName()).andReturn("id"); expect(connection.prepareStatement(eq(statement), aryEq(new String[]{"id"}))).andReturn(prepStmt); expect(prepStmt.executeUpdate()).andReturn(1); expect(prepStmt.getGeneratedKeys()).andReturn(resultSet); expect(resultSet.getMetaData()).andReturn(metaData); expect(resultSet.next()).andReturn(true); expect(metaData.getColumnCount()).andReturn(1); expect(metaData.getColumnType(1)).andReturn(Types.INTEGER); int keyHolderVal = 123131; expect(resultSet.getInt(1)).andReturn(keyHolderVal); keyHolder.addKey(keyHolderVal); expect(resultSet.next()).andReturn(false); // should close the statement prepStmt.close(); connection.close(); replay(connection, prepStmt, keyHolder, resultSet, metaData); jdc.insert(statement, new Object[0], new FieldType[0], keyHolder); jdc.close(); verify(connection, prepStmt, keyHolder, resultSet, metaData); }