Roughsoftie

Raw SQL vs SQLAlchemy

Feb 5th, 2021 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. SQL:
  2. WITH split(word, str) AS (
  3. SELECT '', tagword ||','
  4. UNION ALL SELECT
  5. substr(str, 0, instr(str, ',')),
  6. substr(str, instr(str, ',')+1)
  7. FROM split WHERE str !=''
  8. ),
  9. filter_tags as (
  10. select word from split where word != ''
  11. )
  12. select *
  13. from file
  14. join match
  15. on file.id = match.file_id
  16. join tag
  17. on match.tag_id = tag.id
  18. join filter_tags
  19. on tag.name = filter_tags.word
  20. group by match.file_id
  21. having count(1) = (select count(1) from filter_tags)
  22. order by file.title asc
  23.  
  24. -------------------------------------------------------------------------
  25.  
  26. Python:
  27. taglist = tagword.split(",")
  28. q = session.query(File).join(
  29. File.contains).filter(
  30. Tag.name.in_(taglist)).group_by(
  31. File.title).having(
  32. func.count()==len(taglist)).all()
Add Comment
Please, Sign In to add comment