Advertisement
Coriic

Untitled

Jan 11th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package pl.edu.agh.kis.florist.dao;
  2.  
  3. import org.jooq.DSLContext;
  4. import org.jooq.impl.DSL;
  5. import pl.edu.agh.kis.florist.db.tables.pojos.FolderMetadata;
  6. import pl.edu.agh.kis.florist.db.tables.records.FolderMetadataRecord;
  7.  
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10.  
  11. import static pl.edu.agh.kis.florist.db.Tables.FOLDER_METADATA;
  12.  
  13. public class FolderMetadataDAO {
  14.  
  15. private final String DB_URL = "jdbc:sqlite:test.db";
  16.  
  17. public FolderMetadata loadFolderOfID(int folderId) {
  18. try (DSLContext create = DSL.using(DB_URL)) {
  19. FolderMetadataRecord record = create.selectFrom(FOLDER_METADATA).where(FOLDER_METADATA.FOLDER_ID.equal(folderId)).fetchOne();
  20. FolderMetadata folderMetadata = record.into(FolderMetadata.class);
  21. return folderMetadata;
  22. }
  23. }
  24.  
  25. public List<FolderMetadata> loadAllFolders() {
  26. try (DSLContext create = DSL.using(DB_URL)) {
  27. List<FolderMetadata> folders = create.select(FOLDER_METADATA.fields()).from(FOLDER_METADATA).fetchInto(FolderMetadata.class);
  28. return folders;
  29. }
  30. }
  31.  
  32. public FolderMetadata storeFolderMetadata(FolderMetadata folderMetadata) {
  33. try (DSLContext create = DSL.using(DB_URL)) {
  34. FolderMetadataRecord record = create.newRecord(FOLDER_METADATA, folderMetadata);
  35. record.store();
  36. return record.into(FolderMetadata.class);
  37. }
  38. }
  39.  
  40. public List<FolderMetadata> storeFolderMetadata(List<FolderMetadata> foldersMetadata) {
  41. //in the future can be optimized into single db query
  42. return foldersMetadata.stream().map(this::storeFolderMetadata).collect(Collectors.toList());
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement