How can I support SQL GO statement in Java / jtds application?

I am working on a SqlHawk Java based OSS application which, as one of its functions, should run sql update scripts against the server.

Microsoft made an agreement to split the script into batches using GO , which is a good idea, but just asks for false matches in the string.

At the moment I have a very rudimentary:

// split where GO on its own on a line Pattern batchSplitter = Pattern.compile("^GO", Pattern.MULTILINE); ... String[] splitSql = batchSplitter.split(definition); ... 

which works, but is prone to being encouraged by things like quoted GO instructions or indentation issues.

I think the only way to make this truly reliable is to have an SQL parser in the application, but I don’t know how to do this, or whether it can actually be less reliable (especially if this tool supports several DBMSs).

How can I solve this problem? Code examples would be very helpful for me here.

Relevant sqlHawk code on github.

Jtds is currently used to execute packages found in scripts.

+6
source share
2 answers

GO is the client packet separator command. You can replace it with. It should not be sent to dynamic SQL EXEC.

 USE master GO --<----- client actually send the first batch to SQL and wait for a response SELECT * from sys.databases GO 

Must be translated into

 Application.Exec("USE master"); Application.Exec("SELECT * from sys.databases"); 

or you can write it as follows:

 Application.Exec("'USE master;SELECT * from sys.databases") 

More about GO http://msdn.microsoft.com/en-us/library/ms188037 (v = sql.90) .aspx

+1
source

Okay, so that won't be exactly what you want, but you can find it at the beginning. I released SchemaEngine (which is the basis of most of my products) as open here . There you will find C # code that does what you want very reliably (i.e. without collecting lines, comments, etc. Etc.). It also supports the syntax "GO x" to repeat the packet x times.

If you download this and look at /Atlantis.SchemaEngine/Helpers , you will find a class called BatchParser.cs that contains a method called ParseBatches - which does pretty much what it says about the gesture.

+1
source

Source: https://habr.com/ru/post/916539/


All Articles