Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

39 linhas
871 B

  1. import sqlparse
  2. import webnotes
  3. import webnotes.query_parser
  4. def get_tables(parsed):
  5. start = 0
  6. for t in parsed[0].tokens:
  7. if str(t.ttype)=='Token.Keyword' and t.value.lower()=='from':
  8. start = 1
  9. if start and type(t).__name__=='Identifier':
  10. return [(str(t.get_real_name())),]
  11. if start and type(t).__name__=='IdentifierList':
  12. return [str(i.get_real_name()) for i in t.get_identifiers()]
  13. return tl
  14. def add_condition(query):
  15. parsed = sqlparse.parse(query)
  16. # get the tables
  17. tl = get_tables(parsed)
  18. # rebuild the query till the where clause
  19. q = ''
  20. for t in parsed[0].tokens:
  21. q += str(t)
  22. # where clause comes here
  23. if type(t).__name__=='Where':
  24. # add the conditions for the tables
  25. for t in tl:
  26. if t not in webnotes.query_parser.shared_tables:
  27. q += ' and %s._tenant_id=%s' % (t, webnotes.tenant_id)
  28. return q