@Override public long queryForLong(String statement, Object[] args, FieldType[] argFieldTypes) throws SQLException { if (proxy == null) { return 0; } else { return proxy.queryForLong(statement, args, argFieldTypes); } }
@Override public long queryForLong(String statement) throws SQLException { if (proxy == null) { return 0; } else { return proxy.queryForLong(statement); } }
@Override public long queryForLong(String statement) throws SQLException { if (proxy == null) { return 0; } else { return proxy.queryForLong(statement); } }
@Override public long queryForLong(String statement, Object[] args, FieldType[] argFieldTypes) throws SQLException { if (proxy == null) { return 0; } else { return proxy.queryForLong(statement, args, argFieldTypes); } }
private void assignSequenceId(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { // call the query-next-sequence stmt to increment the sequence long seqVal = databaseConnection.queryForLong(queryNextSequenceStmt); logger.debug("queried for sequence {} using stmt: {}", seqVal, queryNextSequenceStmt); if (seqVal == 0) { // sanity check that it is working throw new SQLException("Should not have returned 0 for stmt: " + queryNextSequenceStmt); } assignIdValue(data, seqVal, "sequence", objectCache); }
private void assignSequenceId(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { // call the query-next-sequence stmt to increment the sequence long seqVal = databaseConnection.queryForLong(queryNextSequenceStmt); logger.debug("queried for sequence {} using stmt: {}", seqVal, queryNextSequenceStmt); if (seqVal == 0) { // sanity check that it is working throw new SQLException("Should not have returned 0 for stmt: " + queryNextSequenceStmt); } assignIdValue(data, seqVal, "sequence", objectCache); }
/** * Return a long value which is the number of rows in the table. */ public long queryForCountStar(DatabaseConnection databaseConnection) throws SQLException { if (countStarQuery == null) { StringBuilder sb = new StringBuilder(64); sb.append("SELECT COUNT(*) FROM "); databaseType.appendEscapedEntityName(sb, tableInfo.getTableName()); countStarQuery = sb.toString(); } long count = databaseConnection.queryForLong(countStarQuery); logger.debug("query of '{}' returned {}", countStarQuery, count); return count; }
/** * Return a long value which is the number of rows in the table. */ public long queryForCountStar(DatabaseConnection databaseConnection) throws SQLException { if (countStarQuery == null) { StringBuilder sb = new StringBuilder(64); sb.append("SELECT COUNT(*) FROM "); databaseType.appendEscapedEntityName(sb, tableInfo.getTableName()); countStarQuery = sb.toString(); } long count = databaseConnection.queryForLong(countStarQuery); logger.debug("query of '{}' returned {}", countStarQuery, count); return count; }
@Test public void testQueryForLongStringObjectArrayFieldTypeArray() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "select stuff from foo"; long result = 3123123124141413L; expect(conn.queryForLong(statement, null, null)).andReturn(result); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(result, proxy.queryForLong(statement, null, null)); proxy.close(); verify(conn); }
@Test public void testQueryForLongString() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "select stuff from foo"; long result = 31231231241414L; expect(conn.queryForLong(statement)).andReturn(result); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(result, proxy.queryForLong(statement)); proxy.close(); verify(conn); }
@Test(expected = SQLException.class) public void testSequenceZero() throws Exception { DatabaseConnection databaseConnection = createMock(DatabaseConnection.class); expect(databaseConnection.queryForLong(isA(String.class))).andReturn(0L); replay(databaseConnection); NeedsSequenceDatabaseType needsSequence = new NeedsSequenceDatabaseType(); Dao<GeneratedIdSequence, Integer> dao = createDao(GeneratedIdSequence.class, false); MappedCreate<GeneratedIdSequence, Integer> mappedCreate = MappedCreate.build(dao, new TableInfo<GeneratedIdSequence, Integer>(databaseType, GeneratedIdSequence.class)); mappedCreate.insert(needsSequence, databaseConnection, new GeneratedIdSequence(), null); verify(databaseConnection); }
@Test(expected = SQLException.class) public void testQueryForLongNoResult() throws Exception { DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); try { createDao(Foo.class, true); databaseConnection.queryForLong("select id from foo"); } finally { connectionSource.releaseConnection(databaseConnection); } }
@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); }
@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); }
@Test(expected = SQLException.class) public void testTestConnectionThatWasClosed() throws Exception { JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL); String pingStatement = pooled.getDatabaseType().getPingStatement(); try { DatabaseConnection conn1 = pooled.getReadWriteConnection(null); conn1.queryForLong(pingStatement); pooled.releaseConnection(conn1); // close it behind the pool's back, bad dog conn1.close(); DatabaseConnection conn2 = pooled.getReadWriteConnection(null); assertSame(conn1, conn2); conn2.queryForLong(pingStatement); } finally { pooled.close(); } }
public boolean ifExists(DatabaseConnection connection, ID id) throws SQLException { if (ifExistsQuery == null) { QueryBuilder<T, ID> qb = new QueryBuilder<T, ID>(databaseType, tableInfo, dao); qb.selectRaw("COUNT(*)"); /* * NOTE: bit of a hack here because the select arg is never used but it _can't_ be a constant because we set * field-name and field-type on it. */ qb.where().eq(tableInfo.getIdField().getColumnName(), new SelectArg()); ifExistsQuery = qb.prepareStatementString(); ifExistsFieldTypes = new FieldType[] { tableInfo.getIdField() }; } Object idSqlArg = tableInfo.getIdField().convertJavaFieldToSqlArgValue(id); long count = connection.queryForLong(ifExistsQuery, new Object[] { idSqlArg }, ifExistsFieldTypes); logger.debug("query of '{}' returned {}", ifExistsQuery, count); return (count != 0); }
public boolean ifExists(DatabaseConnection connection, ID id) throws SQLException { if (ifExistsQuery == null) { QueryBuilder<T, ID> qb = new QueryBuilder<T, ID>(databaseType, tableInfo, dao); qb.selectRaw("COUNT(*)"); /* * NOTE: bit of a hack here because the select arg is never used but it _can't_ be a constant because we set * field-name and field-type on it. */ qb.where().eq(tableInfo.getIdField().getColumnName(), new SelectArg()); ifExistsQuery = qb.prepareStatementString(); ifExistsFieldTypes = new FieldType[] { tableInfo.getIdField() }; } Object idSqlArg = tableInfo.getIdField().convertJavaFieldToSqlArgValue(id); long count = connection.queryForLong(ifExistsQuery, new Object[] { idSqlArg }, ifExistsFieldTypes); logger.debug("query of '{}' returned {}", ifExistsQuery, count); return (count != 0); }
@Test public void testDatabasePing() throws Exception { if (connectionSource == null) { return; } if (!isDriverClassExpected()) { return; } String ping = databaseType.getPingStatement(); DatabaseConnection conn = connectionSource.getReadOnlyConnection(null); try { testPingValue(conn.queryForLong(ping)); } finally { connectionSource.releaseConnection(conn); } }
@Test public void testQueryForLong() throws Exception { DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); try { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo = new Foo(); long id = 21321321L; foo.id = id; assertEquals(1, dao.create(foo)); assertEquals(id, databaseConnection.queryForLong("select id from foo")); } finally { connectionSource.releaseConnection(databaseConnection); } }
@Test(expected = SQLException.class) public void testQueryForLongTooManyResults() throws Exception { DatabaseConnection databaseConnection = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME); try { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo = new Foo(); long id = 21321321L; foo.id = id; // insert twice assertEquals(1, dao.create(foo)); assertEquals(1, dao.create(foo)); databaseConnection.queryForLong("select id from foo"); } finally { connectionSource.releaseConnection(databaseConnection); } }