die Seilerwerks

Chronicling Life, Love, Linux and Oracle database administration.

Posts Tagged ‘pl/sql

GNU basename in PL/SQL

with one comment

Reposted from The Pythian Group blog.

In the process of scripting a database migration, I was in need of something akin to the GNU basename utility that I know and love on Linux. basename is most famous for taking a full file path string and stripping away the leading path component, returning just the name of the file. This can be emulated in PL/SQL with calls to SUBSTR and INSTR, like this:

substr(dirname,instr(dirname,'/',-1)+1)

(Thanks to Ian Cary, who shared this logic on oracle-l)

As you can see, this simply finds the last occurence of /, which is our directory separator on *nix and Solaris operating systems. On Windows, it would be \. It then returns a substring beginning one character after that last separator until the end of the string. Voila, a basic basename routine!

Upon reading the basename man page again, I found that basename also takes an optional parameter, a suffix string. If this suffix string is provided, basename will also truncate that string from the end. For example:

$ basename /home/seiler/bookmarks.html
bookmarks.html
$ basename /home/seiler/bookmarks.html .html
bookmarks

I decided that this would be handy to have, and set out to create a compatible basename function in PL/SQL. Here is what I came up with:


CREATE OR REPLACE FUNCTION basename (v_full_path IN VARCHAR2,
                                        v_suffix IN VARCHAR2 DEFAULT NULL,
                                        v_separator IN CHAR DEFAULT '/')
        RETURN VARCHAR2
        IS
                v_basename VARCHAR2(256);
        BEGIN
                v_basename := SUBSTR(v_full_path, INSTR(v_full_path,v_separator,-1)+1);
                IF v_suffix IS NOT NULL THEN
                        v_basename := SUBSTR(v_basename, 1, INSTR(v_basename, v_suffix, -1)-1);
                END IF;

                RETURN v_basename;
        END;
/

I’ve also added an optional third parameter to specify a directory separator other than the default. It would probably be rarely useful, but not hard to remove if you don’t like it. As you can see, I’ve used similar SUBSTR/INSTR logic to identify the suffix index and prune it out.

Here it is in action:

SQL> COLUMN file_name FORMAT a45;
SQL> COLUMN basename FORMAT a15;
SQL> COLUMN no_suffix FORMAT a12;
SQL> SELECT file_name
  2          , basename(file_name) as basename
  3          , basename(file_name, '.dbf') as no_suffix
  4  FROM dba_data_files;

FILE_NAME                                     BASENAME        NO_SUFFIX
--------------------------------------------- --------------- ------------
/u01/app/oracle/oradata/orcl/users01.dbf      users01.dbf     users01
/u01/app/oracle/oradata/orcl/sysaux01.dbf     sysaux01.dbf    sysaux01
/u01/app/oracle/oradata/orcl/undotbs01.dbf    undotbs01.dbf   undotbs01
/u01/app/oracle/oradata/orcl/system01.dbf     system01.dbf    system01
/u01/app/oracle/oradata/orcl/example01.dbf    example01.dbf   example01

I hope this makes your work just a little bit easier, as it has mine.

Written by Don Seiler

October 1, 2008 at 10:20 pm

Posted in linux, oracle

Tagged with , , , ,

REF CURSOR Reference Reading

without comments

Alliteration abounds! I spent some time today reading about REF CURSORs. Makes me wish I did more PL/SQL development.

Also thought I’d make quick posts once in a while when I read some handy reference docs that the timid newbie might find helpful.  You’ve been served.

Written by Don Seiler

January 16, 2008 at 3:07 pm

Posted in oracle, reference

Tagged with ,

LOL/SQL?

with one comment

It started with the LOLCATS. Then we had the ORLY thing and the “in ur base” thing. It’s all one big conglomeration now, and it has belched forth LOLCODE. I’m told there is a parser/compiler for LOLCODE, but my head hurts just thinking about it.

But it does beg the question of when we’ll see the Oracle extension of LOL/SQL and who will write it. Steven?

Written by Don Seiler

June 2, 2007 at 2:20 pm

Posted in Uncategorized

Tagged with , ,

Feuerstein on Error Tracing

without comments

I’m still catching up on some readme bookmarks, and find myself once again face-to-face with PL/SQL sensei Steven Feuerstein. This man has some of the best headshots I’ve ever seen.

This time Steven is dropping some science regarding the DBMS_UTILITY.FORMAT_ERROR_BACKTRACE function, new in Oracle 10g, for better exception handling in PL/SQL.

Written by Don Seiler

April 25, 2007 at 9:12 pm

Posted in Uncategorized

Tagged with ,

Oracle PL/SQL Best Practices and Common Mistakes

with 4 comments

Eddie Awad dug up some videos from Quest Software of PL/SQL-god and Borat-hater Steve Feuerstein talking about the best and worst of PL/SQL programming.

The videos offer some tasty advice, including tips on writing cleaner, more modular code, and use of BULK COLLECT and FORALL to improve mass data processing performance, and use of table functions. It’s even worth sitting through the Quest infomercial towards the end of each video.

Written by Don Seiler

April 23, 2007 at 1:59 pm

Posted in Uncategorized

Tagged with , , ,

sprintf() in PL/SQL

without comments

René Nyffenegger has hacked up a PL/SQL implementation of sprintf(). I haven’t done too much in PL/SQL, but I didn’t have much a problem with lpad() and rpad(). But who doesn’t love good old sprintf() otherwise?

Written by Don Seiler

April 15, 2007 at 10:25 am

Posted in Uncategorized

Tagged with , ,