0

I am preparing a screen in which user will input an anonymous block (declare ... begin ... end) of plsql, and i will save it's block into a file an run it when it is necessary. But I want to validate the user's input against my database whether it has syntax errors or not before execution. when i googled it i found that I can use antlr, but i could not found any working sample. can anyone show me a sample, I am open for other solutions within the java and plsql context.

4
  • 2
    Are you sure you want to allow users to run commands in your DB ? how will you prevent him from spoiling your DB (intentionally or by mistake) ? Commented Feb 15, 2012 at 10:05
  • my application would be used by system administrators. misusage is not a concern for now. Commented Feb 15, 2012 at 11:04
  • @dursun Systems administrators are just as fallible as any other human being. One little typo and it could all go horribly wrong ... Commented Feb 16, 2012 at 12:02
  • @BurhanAli ok you right, But this is what the system administrators need. they don t want to connect to DB from any other tool. they are taking all the responsibility for their own actions. typo is on their own responsibility. How can i convince them to use db tools like toad or sqlplus. do you have any experience on such issues. Commented Feb 17, 2012 at 7:17

1 Answer 1

3

You can parse the plsql command with DBMS_SQL:

SQL> CREATE OR REPLACE PROCEDURE parse(p_command VARCHAR2) AUTHID CURRENT_USER IS
  2     l_cursor INTEGER;
  3  BEGIN
  4     l_cursor := dbms_sql.open_cursor;
  5     dbms_sql.parse(l_cursor, p_command, dbms_sql.native);
  6     dbms_sql.close_cursor(l_cursor);
  7  EXCEPTION
  8     WHEN OTHERS THEN
  9        dbms_sql.close_cursor(l_cursor);
 10        RAISE;
 11  END;
 12  /

Procedure created

SQL> exec parse ('BEGIN NULL;END;');

PL/SQL procedure successfully completed

SQL> exec parse ('BEGIN incorrect_statement;END;');

begin parse ('BEGIN incorrect_statement;END;'); end;

ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'INCORRECT_STATEMENT' must be declared

Be careful with what you parse though: DDL will be executed on parse (!).

Sign up to request clarification or add additional context in comments.

3 Comments

@RogerCornejo how would it be dangerous
@dursun - basically, the code will run anything, that could include SQL Injection ("a hackers gold mine").
Not working for 'begin :x := y; end;' but works for 'declare x number; begin x := y; end;' in Oracle 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.