SQL Templates - Code Snippets
419
Views
6
Comments
New
Database

Would be good to have Advanced SQL template queries in OS.

Let's say that by inserting the Customer entity in "OutputEntities / Structure" (or not) we could have templates regarding:

  • DELETE

DELETE FROM Customer

WHERE Col = 1;

  • INSERT

INSERT INTO Customer(Col1, Col2)

VALUES (1, 2);

  • SELECT

SELECT *

FROM Customer

WHERE Col = 1;

  • UPDATE

UPDATE Customer

SET Col = val, Col = val

WHERE Col = 1;


By clicking in a button, we would have the templates displayed, It can be a great accelerator when developing.


PS: Idea taken from Notepad++ plugin "Snippets

Honestly I think any SQL simple enough to be possible for a snippet to exist is going to be code that should just be done in an Aggregate. Other than bulk delete or update (which is *extremely* rare... I need it for the occassional data model change and that's it), and the use of the IN clause, using Advanced SQL is usually a sign of a poor data model, which does not lend itself well to generic code snippets.

J.Ja

So, what you are saying is that you would have no need for any type of snippets. And those who need it is because of a weak database model?

In my experience - supported by 13+ years of using OutSystems - the use of SQL beyond an IN clause is *almost always* a red flag that your data model is poor. I have had numerous discussions with other OutSystems MVPs, as well as my team of 10+ architects at my current position, plus plenty of others over the years, and there is general (though not universal, not that there ever will be with things like this! :) ) consensus that this is correct.

There *are* times to use SQL:

* IN clause (even IN clause is often bad, it can be a performance killer... it is often better to do a JOIN to the things that produced that IN clause and filter those JOIN conditions... basically the IN should be saved for something like a search filter where the user can select multiple things at once).

* A data migration where you need to do something like "UPDATE {Customer} SET {Customer}.[Status] = @DefaultStatusId WHERE {Customer}.[Status] = 0 or {Customer}.[Status] IS NULL" because a deployment is making a major change to the data model, and looping through millions of records and changing one by one will be super slow.

* Data management for a non-production environment where you need to wipe out a lot of data at once (which, in general, *should* be rare if you are testing things properly!).

* Edge case scenarios most of which are caused by bad data models.

When I see OutSystems developers using a lot of SQL, the root cause is one of these:

* They don't know how to use Aggregates to do certain queries

* They refuse to let go of SQL and use Aggregates

* Their data model is terrible and they can't meet the business needs, so they are constantly doing cursors and temp tables and CASE and sub-queries

* Their data model is terrible and they need to do things like use SQL hints to get the performance where it needs to be

So... in my experience, people should not be using SQL too often, and when they do, the right scenarios are not common enough for snippets to be super-helpful.

J.Ja

Hi Nivaldo,


Agree with Justin that there's no strong use case for adding this as a feature to Service Studio.


And strongly agree that a lot of problems stem from bad datamodel design, but unfortunately as a developer, that's not always in your control to (still) change.


For those rare occasions that you need to write some SQL and feel insecure/lazy, you could maybe look at using something like SQLSandbox to prepare your sql for you, but I wouldn't bother, it feels to me like starting it up is more work than it's worth.


Dorine

Hello Justin/Dorine,

Well, I trust you guys, and thanks for the detailed input on why it would not be a plus for the platform. 

N.

I just want to notice that at this moment, OutSystems do have something similar, but only to SELECT statement:

Following on from the previous comments, I just wanted make an update and say that I've just noticed that OutSystems already has something similar, but only for the SELECT statement.