Author |
Message |
MarcinH
Joined: 18 May 2019 Posts: 7 Country: Poland |
|
Code snippets: how to escape pipe character |
|
How can I escape pipe character in code snippet?
There is a solution for $ character but not for |
|
|
Fri May 08, 2020 5:16 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
I recall only the first one being interpreted as a location beacon, so I guess if you double that one, the rest will be good to go.
|
|
Fri May 08, 2020 6:31 pm |
|
 |
MarcinH
Joined: 18 May 2019 Posts: 7 Country: Poland |
|
|
|
No.
If I double | it'll double ||
|
|
Sat May 09, 2020 9:41 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Could you please share the code of the snippet and indicate where you need the pipe symbol appear in the code
|
|
Sun May 10, 2020 2:29 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Here is a generic method that can be used with all databases, use custom SQL query to append strings to the snippet output. For example, with SQL Server you can use the following code snippet to generate SELECT '|'
 |
 |
SELECT '$$SELECT CHAR(124)$$' |
|
|
Sun May 10, 2020 2:34 am |
|
 |
MarcinH
Joined: 18 May 2019 Posts: 7 Country: Poland |
|
|
|
Yes, I know I can use custom SQL Query.
And I have to use it just to escape pipe character which looks awful.
There is escape character for $ (^) but it does not work for |.
I use SQL Server
This is a generic snippet code where I want to pipe characters to stay in comment:
 |
 |
-- This is a script ... | Created $DATE$ | By $OSUSER$ | Do a lot of things to earn some $^
SELECT *
FROM $OBJECT$
|
And the output is:
 |
 |
-- This is a script ... Created 2020-05-10 By myUser Do a lot of things to earn some $
SELECT *
FROM dbo.Table1
|
|
|
Sun May 10, 2020 8:49 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
I'm sure you know that | is used as a special reference to where to place cursor after the snippety is executed and its output is injected into the editor. The only exception to that known to me is using double pipes ||, which is there to support database servers like Oracle, PostgreSQL, DB2 and others that use || as a string concatenation operator. I'm not aware of an escape character for |. If there was then normally it would be ^, which I tried too and that didn't help. If you wish I can submit an enhancement request to implement such a feature.
What's wrong with using $$SELECT CHAR(124)$$ macro within the snippet?
|
|
Sun May 10, 2020 11:08 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Sometimes (quite often) it is used as a separator, especially in strings and comments. I guess the code can get very ugly and hard to maintain when it has lots of those characters. But that's just my two cents.
Are there any reasons the ^ escape character does not work for this one?
|
|
Mon May 11, 2020 2:44 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Unfortunately, from my previous experiments it looks like there is no escape character, at least I failed to find one. It's been this way since snippets were introduced in version 3. I'm going to ask and revert back to you.
|
|
Mon May 11, 2020 8:34 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Is it possible that the escape character ^ stopped working properly? I've started working with Oracle (again) and I wanted to create a few snippets that would come in handy. One of them would use Execute and Display Output Results with the following piece of code:
 |
 |
$$
select
(select username from v$session where sid=a.sid) blocker,
a.sid,
' is blocking ',
(select username from v$session where sid=b.sid) blockee,
b.sid
from
v$lock a,
v$lock b
where
a.block = 1
and
b.request > 0
and
a.id1 = b.id1
and
a.id2 = b.id2;
$$
|
Now, having $ in the code itself should have forecasted the forthcoming rain but as being enveloped between $$ pairs for a custom query, it had its syntax highlight disabled, thus I had no idea it wouldn't work. Removing the $$ pairs immediately cast some light on the two problematic blocks:
I recall code parts between the single $ pairs are interpreted as a $PROMPT()$ macro-created variable and there were no $PROMPT()$ macros to create such variables (therefore no values assigned to them), still, they are replaced in the pasted code somehow inconsistently. Nevermind, the code is faulty, so I should not expect it to work properly anyway. So I add the escape characters everywhere I deem necessary, and check the syntax highlight for the following code:
Everything seems to be in order (at least according to syntax highlight). However, switching back to Insert Output Into Code and triggering the snippet, the code inserted is this:
 |
 |
select
(select username from v^session where sid=a.sid) blocker,
a.sid,
' is blocking ',
(select username from v^
b.sid
from
v^lock a,
v^
where
a.block = 1
and
b.request > 0
and
a.id1 = b.id1
and
a.id2 = b.id2;
|
which is rather far from the one expected (and I haven't even tried to execute that code between $$ pairs yet). Any hints/ideas what went wrong here?
|
|
Thu Aug 06, 2020 10:31 am |
|
 |
|