DBManager implements Closeable { public Connection getConnection() throws Exception { return ds.getConnection(); } DBManager(BlockingIOProcessor blockingIOProcessor, boolean isEnabled);  DBManager(String propsFilename, BlockingIOProcessor blockingIOProcessor, boolean isEnabled); void deleteUser(UserKey userKey); void saveUsers(ArrayList<User> users); Redeem selectRedeemByToken(String token); boolean updateRedeem(String email, String token); void insertRedeems(List<Redeem> redeemList); FlashedToken selectFlashedToken(String token); boolean activateFlashedToken(String token); boolean insertFlashedTokens(FlashedToken... flashedTokenList); void insertPurchase(Purchase purchase); boolean insertClonedProject(String token, String projectJson); String selectClonedProject(String token); boolean dbIsNotEnabled(); boolean isDBEnabled(); void executeSQL(String sql); String getUserServerIp(String email, String appName); String getServerByToken(String token); void assignServerToToken(String token, String serverIp, String email, int dashId, int deviceId); void removeToken(String... tokens); Connection getConnection(); @Override void close(); public UserDBDao userDBDao; public ForwardingTokenDBDao forwardingTokenDBDao; }
@Test public void test() throws Exception { assertNotNull(dbManager.getConnection()); }
@Test @Ignore("not used right now in read code") public void testCopy100RecordsIntoFile() throws Exception { System.out.println("Starting"); int a = 0; long start = System.currentTimeMillis(); try (Connection connection = dbManager.getConnection(); PreparedStatement ps = connection.prepareStatement(ReportingDBDao.insertMinute)) { String userName = "test@gmail.com"; long minute = (System.currentTimeMillis() / AverageAggregatorProcessor.MINUTE) * AverageAggregatorProcessor.MINUTE; for (int i = 0; i < 100; i++) { ReportingDBDao.prepareReportingInsert(ps, userName, 1, 0, (short) 0, PinType.VIRTUAL, minute, (double) i); ps.addBatch(); minute += AverageAggregatorProcessor.MINUTE; a++; } ps.executeBatch(); connection.commit(); } System.out.println("Finished : " + (System.currentTimeMillis() - start) + " millis. Executed : " + a); try (Connection connection = dbManager.getConnection(); Writer gzipWriter = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(new File("/home/doom369/output.csv.gz"))), "UTF-8")) { CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class)); String selectQuery = "select pintype || pin, ts, value from reporting_average_minute where project_id = 1 and email = 'test@gmail.com'"; long res = copyManager.copyOut("COPY (" + selectQuery + " ) TO STDOUT WITH (FORMAT CSV)", gzipWriter); System.out.println(res); } }
@Test public void testUpsertUser() throws Exception { ArrayList<User> users = new ArrayList<>(); User user = new User("test@gmail.com", "pass", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); user.name = "123"; user.lastModifiedTs = 0; user.lastLoggedAt = 1; user.lastLoggedIP = "127.0.0.1"; users.add(user); user = new User("test@gmail.com", "pass", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); user.lastModifiedTs = 0; user.lastLoggedAt = 1; user.lastLoggedIP = "127.0.0.1"; user.name = "123"; users.add(user); user = new User("test2@gmail.com", "pass", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); user.lastModifiedTs = 0; user.lastLoggedAt = 1; user.lastLoggedIP = "127.0.0.1"; user.name = "123"; users.add(user); dbManager.userDBDao.save(users); try (Connection connection = dbManager.getConnection(); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from users where email = 'test@gmail.com'")) { while (rs.next()) { assertEquals("test@gmail.com", rs.getString("email")); assertEquals(AppNameUtil.BLYNK, rs.getString("appName")); assertEquals("local", rs.getString("region")); assertEquals("123", rs.getString("name")); assertEquals("pass", rs.getString("pass")); assertEquals(0, rs.getTimestamp("last_modified", DateTimeUtils.UTC_CALENDAR).getTime()); assertEquals(1, rs.getTimestamp("last_logged", DateTimeUtils.UTC_CALENDAR).getTime()); assertEquals("127.0.0.1", rs.getString("last_logged_ip")); assertFalse(rs.getBoolean("is_facebook_user")); assertFalse(rs.getBoolean("is_super_admin")); assertEquals(2000, rs.getInt("energy")); assertEquals("{}", rs.getString("json")); } connection.commit(); } }