check_oql.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Minimal file (with all the needed includes) to check the validity of an OQL by verifying:
  4. * - The syntax (of the OQL query string)
  5. * - The consistency with a given data model (represented by an instance of ModelReflection)
  6. *
  7. * Usage:
  8. *
  9. * require_once(APPROOT.'core/oql/check_oql.php');
  10. *
  11. * $sOQL = "SELECT Zerver WHERE status = 'production'";
  12. * $oModelReflection = new ModelReflectionRuntime();
  13. * $aResults = CheckOQL($sOQL, $oModelReflection);
  14. * if ($aResults['status'] == 'error')
  15. * {
  16. * echo "The query '$sOQL' is not a valid query. Reason: {$aResults['message']}";
  17. * }
  18. * else
  19. * {
  20. * echo "Ok, '$sOQL' is a valid query";
  21. * }
  22. */
  23. if (!class_exists('CoreException', false))
  24. {
  25. class CoreException extends Exception
  26. {
  27. }
  28. }
  29. require_once(__DIR__.'/expression.class.inc.php');
  30. require_once(__DIR__.'/oqlquery.class.inc.php');
  31. require_once(__DIR__.'/oqlexception.class.inc.php');
  32. require_once(__DIR__.'/oql-parser.php');
  33. require_once(__DIR__.'/oql-lexer.php');
  34. require_once(__DIR__.'/oqlinterpreter.class.inc.php');
  35. function CheckOQL($sOQL, ModelReflection $oModelReflection)
  36. {
  37. $aRes = array('status' => 'ok', 'message' => '');
  38. try
  39. {
  40. $oOql = new OqlInterpreter($sOQL);
  41. $oOqlQuery = $oOql->ParseQuery(); // Exceptions thrown in case of issue
  42. $oOqlQuery->Check($oModelReflection,$sOQL); // Exceptions thrown in case of issue
  43. }
  44. catch(Exception $e)
  45. {
  46. $aRes['status'] = 'error';
  47. $aRes['message'] = $e->getMessage();
  48. }
  49. return $aRes;
  50. }