Skip to content

Added DELIMITER support in jdbc script runner #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2015
Merged

Added DELIMITER support in jdbc script runner #241

merged 1 commit into from
Feb 22, 2015

Conversation

thekonz
Copy link
Contributor

@thekonz thekonz commented Jul 20, 2014

With this, you can create triggers and stored procedures.

@kzinnen
Copy link

kzinnen commented Jul 29, 2014

Is anyone ever gonna take a look at this?
4 lines of code that have a huge impact.

@emacarron
Copy link
Member

Sorry but I do not understand the change. Can you please explain it further?

@thekonz
Copy link
Contributor Author

thekonz commented Feb 22, 2015

What does the current code do?
The script goes through the SQL line-by-line. It stores every line in the variable "command".
If the line ends with ;, the command gets executed.

How is this a problem?
If you create a trigger, you usually change the delimiter.

delimiter |

CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW
  BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;
    DELETE FROM test3 WHERE a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
  END;
|

delimiter ;

Currently, the code executes the script as follows:

-- part 1
delimiter |

CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW
  BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;

-- part 2
    DELETE FROM test3 WHERE a3 = NEW.a1;

-- part 3
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;

-- part 4
  END;

-- part 5
|

delimiter ;

This would cause a lot of errors and is not what you want the program to do.

What does this patch do?
The patch checks, if the line wants to change the delimiter and then sets the delimiter variable of the ScriptRunner to the new delimiter.

Remember the SQL from before? The ScriptRunner now executes the script as follows:

-- part 1
delimiter |

-- part 2
CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW
  BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;
    DELETE FROM test3 WHERE a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
  END;
|

-- part 3
delimiter ;

Now you can create triggers and stored procedures with this.

@emacarron
Copy link
Member

Clear as water :)

emacarron added a commit that referenced this pull request Feb 22, 2015
Added DELIMITER support in jdbc script runner
@emacarron emacarron merged commit 182e5f7 into mybatis:master Feb 22, 2015
@thekonz
Copy link
Contributor Author

thekonz commented Feb 22, 2015

thank you for merging

@emacarron
Copy link
Member

A very good comment from #355.

@chb0github
Copy link

PR #355 doesn't really address the issue - if you use that change it means (at least for mysql) you have to create native client incompatible files. See this issue in migrations. I created a follow up issue to address this head on

pulllock pushed a commit to pulllock/mybatis-3 that referenced this pull request Oct 19, 2023
Added DELIMITER support in jdbc script runner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants