Thursday, 24 September 2015

Compiling Oracle code from Atom text editor

Just yesterday I was watching a webinar titled: "Fill the Glass: Measuring Software Performance" which featured Jorge Rimblas. You can check out the video on Vimeo at the following URL: It's just giving an insight into a project Jorge is working on, giving various tips here and there - so if you have a minute (rather, an hour) to spare, go and check it out.

One of the sections that particularly caught my attention was the fact Jorge was able to compile packages from his editor of choice (which is Sublime text editor) (this happens at around the 18:20 mark). Because I like free (and open source) software, I actually use Atom text editor. Being centred around a plugin ecosystem, I was curious how he was able to do this - and if in fact I'd be able to accomplish the same, in Atom.

So, first of all I did some hunting to see how it was done in Sublime. This led me to this great post by Tim St. Hilaire - - which covers all the plugins he uses with Sublime. The section of interest is titled "Building Code" - where I discovered it used a build system embedded into the application itself; Tim also provides some sample scripts that he uses for the actual compilation, so I grabbed them (it's actually a zip file with a couple of examples) as a reference point.

The next thing was to find how to build code in Atom. I did a search for "atom editor build system". This immediately led me to the package - I installed that and begun hacking away to get this build process working! As per the documentation, depending on the files in your project directory, will determine which build process gets initiated. If your desired build process isn't built in (which it isn't for Oracle), you can add a file to your project .atom-build.json.

Now, because I'm on Ubuntu, my script is a `sh` script, and I'm going to place it in /usr/local/bin - which goes without saying, this file should be executable. Ideally, this will all be packaged into a plugin that extends from the build package, but that's for a future date.

It's worth mentioning here, that of course you will need an Oracle client set up on your system.

So, my .atom-build.json, in the root of my project folder, will look like this:

  "cmd": "/usr/local/bin/",
  "name": "Build Oracle code file",
  "args": [ "${host}", "${port}", "${sid}", "${user}", "${password}", "{FILE_ACTIVE}" ],
  "sh": true,
  "cwd": "{FILE_ACTIVE_PATH}",
  "env": {
    "user": "hr",
    "password" : "hr",
    "host" : "",
    "port" : "1521",
    "sid" : "XE"
  "errorMatch": [
  "keymap": "",
  "targets": {

So, for each project, you will just update the environment settings accordingly. To support for different environments (e.g dev and prod) I would guess the targets could be used here - I haven't got that far yet.

Then, the actual script would look like (again, script based from Tim St. Hilaire's examples):

echo "Running"
echo "host: $1"
echo "port: $2"
echo "sid: $3"
echo "user: $4"
# echo "password: $5"
echo "Compiling file: $6"

sqlplus -S -L $4/$5@$1:$2/$3 << EOF

    set linesize 200
    set tab off
    set serveroutput on

    --Show the details of the connection for confirmation
        user as MY_USER
      , ora_database_name as DB
      --, '$3' as SID
      , systimestamp as NOW
    from dual;



    show error


So, in SQL Developer I confirm I have no packages:

Then, I have the config file for database settings:

So, this is a good template that can be copied and updated to your other Oracle projects, so you can compile directly. You of course would likely want to ignore it from being committed to version control, since the password is included.

Now, I can create a code file and compile it with the keyboard shortcut ctrl+alt+b (cmd + alt+ b for Mac).

And, just to verify, in SQL Developer I refresh the package list and see my code file was in fact compiled to the database.

Friday, 18 September 2015

Making connections to the Oracle Database from Golang

I posted the other day about getting going with Golang[1], and as mentioned am planning to do a series of posts on various aspects of the language - as a means to help with the learning process.

Being an Oracle developer, it seemed logical I would want to be able to make connections to the Oracle database. Built into the language core is an SQL interface (for issuing queries and statements) which is through the module "database/sql". The other side of it is that you need a valid driver. A list of available drivers is on the go wiki[2]. You will see that there are 2 listed for Oracle. How do I judge which project to use? First I see that one has considerably more stars and for another, the online commentary I see also seems to suggest that same package. And that is

Driver setup

Before we get to the driver, you need to make sure you have an Oracle client and the SDK installed on your system. For, this I followed the steps as per the Ubuntu wiki[3] - where you would at minimum want to install the instant client and the SDK. Once the installation is complete, you should end up with an $ORACLE_HOME that points to /usr/lib/oracle/11.2/client64 or something similar depending on your systems architecture and the version of Oracle you installed.

Within $ORACLE_HOME, you should have 3 folders:
  1. bin
  2. include
  3. lib
At this point, if you try to install the aforementioned driver, you will get an error:

$ go get
# pkg-config --cflags oci8
Package oci8 was not found in the pkg-config search path.
Perhaps you should add the directory containing `oci8.pc'
to the PKG_CONFIG_PATH environment variable
No package 'oci8' found
exit status 1

So, before you install the driver, you need to do the pkg-config set up. This application should be already installed on your system, but if not you can do so with sudo apt-get install pkg-config. This program is just a way to provide the necessary details for compiling and linking a program to a library[4].

The project itself does provide an example of a package config file for oci8, however it's example is for Windows, so the config file I came up with was:


Name: OCI
Description: Oracle database driver
Version: 11.2
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}

I just grabbed the path to $ORACLE_HOME and placed it in the variable prefixdir, since the config file doesn't know about environment variables.

Before installing the driver, you need to make this file available in a place that pkg-config knows about. So there are two options here. First, you can place it in the system wide pkg-config directory: /usr/lib/pkgconfig and the system will automatically find it. The other option if you have it some obscure location is to export that location into the environment variable PKG_CONFIG_PATH. Once that is set up, you should be able to successfully install the driver with: go get

You will now find that both the compiled version and source for that package in your GOPATH (in pkg and src folders respectively).

Connecting and querying database

Now that we have the driver available, we can begin with our program. Most of the aspects I describe here are also documented on the go wiki[5].

First, we want to open our connection. This is done with the sql.Open function[6], where you will pass in the name of the driver, and the connection string.

db, err := sql.Open("oci8", "hr/")
if err != nil {
defer db.Close()

Opening the connection is actually deferred until you start issuing statements - so for example, if you enter incorrect password, only when you attempt to query the database will you find out. So in this case, the Ping function[7] might be useful to test the connection is OK.

err = db.Ping()
if err != nil {
    fmt.Printf("Error connecting to the database: %s\n", err)

Then, to the actual querying. Here we have the functions Query[8] and QueryRow[9] depending on if you want return a row set or a single row respectively. You will more than likely want to use bind variables in your queries - the documentation suggests the use of `?` as placeholders. And I've also seen examples of `$1`. However, using either of those methods seemed to return the error:

sql: statement expects 0 inputs; got 1

What I've found works is what you would be used to use an Oracle developer - a number prefixed with a colon (:1). I'm assuming that's just the specific implementation of the driver. note: If you repeat the same bind number in your program, don't expect it to be re-used - it's just the position of the bind in the query, and then the respective parameter index. In the result set, you'll then want to declare variables of suitable data types to store the data in, which is fetched with the Scan function.

rows,err := db.Query("select employee_id, first_name from employees where employee_id < :1", 105)
if err != nil {
    fmt.Println("Error fetching employees")
defer rows.Close()

for rows.Next() {

    var emp_id int
    var first_name string
    rows.Scan(&emp_id, &first_name)
    println(emp_id, first_name)


var last_name string
err = db.QueryRow("select last_name from employees where employee_id = :1", 101).Scan(&last_name)
if err != nil {
    fmt.Println("Error fetching row")

fmt.Printf("Last name is %s\n", last_name)

If you are running a query that doesn't return anything (such as insert or create statements), you would typically use the Exec[10] function. And further, if you are repeating a statement, the Prepare[11] function.

Full program (main.go):

package main

import (
    _ ""

func main(){

    db, err := sql.Open("oci8", "hr/")
    if err != nil {
    defer db.Close()
    if err = db.Ping(); err != nil {
        fmt.Printf("Error connecting to the database: %s\n", err)

    rows,err := db.Query("select employee_id, first_name from employees where employee_id < :1", 105)
    if err != nil {
        fmt.Println("Error fetching employees")
    defer rows.Close()

    for rows.Next() {

        var emp_id int
        var first_name string
        rows.Scan(&emp_id, &first_name)
        println(emp_id, first_name)

    var last_name string
    err = db.QueryRow("select last_name from employees where employee_id = :1", 101).Scan(&last_name)
    if err != nil {
        fmt.Println("Error fetching row")
    fmt.Printf("Last name is %s\n", last_name)



Wednesday, 16 September 2015

Oracle developer choice awards

From June 21, Oracle opened up nominations for what they have dubbed Oracle developer choice awards, in 5 broad categories:

  1. SQL
  2. PL/SQL
  3. ORDS
  4. APEX
  5. Database Design
There has been a panel that has narrowed down the nominations from each category, and now the voting has opened up - until the 15th October. 

You are able to vote for more than one person in each category, the rules don't specify how many votes in each category, but I would encourage selecting a few of the people you think are most deserving in the community and giving them an up-vote. 

Please choose wisely, because the system doesn't allow you to undo your upvote - the only way to undo that vote, is to then down vote. So unless you feel very strongly that that person shouldn't be awarded and wish to down-vote them, I emplore you to choose wisely.

The APEX candidates

Jari Laine

Very active of the OTN APEX forum, from well before I even started working with APEX. The time he has spent assisting the community is really something to be admired. He has an APEX blog - - that he built in APEX, and open sourced! If you prefer to look at samples, go to one of his sample applications: or, both with countless examples.

Juergen Schuster

Juergen is really active promoting APEX, so much so that he created an APEX (apeks) sticker that many people are now rocking on their laptops (the word on the street is that he even sends them to people all over the world, at no charge!). He also recently started a talk back show speaking with prominent figures in the APEX community including the APEX development team - you can find existing podcasts listed here:

Paul MacMillan (fac586)

Also very active on the OTN forums, and in my opinion will be the first to bring anyone up on poor practices new people to APEX might be using in their systems. This can only be a good thing! He is constantly help people get their problems resolved - and definitely deserves some recognition for all his efforts!

Morten Braten

Have you ever heard the Alexandria PL/SQL utility? If you haven't, it's a large collection of pl/sql utilities in one spot! And Morten is the one behind this awesome project. If that's not a contribution to the APEX community, I don't know what is! The project was recently moved over to GitHub -, so if you haven't heard of it, go and check it out.

Kiran Pawar

Yet another member of the community that has become very active in the APEX forums of late (as well as other forums such as ORDS). Between Kiran, Jari and Paul, I think you should be pretty well covered with any questions you may ask on the forums! 

Karen Cannell

What better way giving back to the community the (co)authoring an APEX related book? Karen co-authored Agile Oracle Application Express, which is good supplementary material on how to leverage Application Express with agile methodologies. 

Trent Schafer

Oh hi there, that's me! I started working with APEX about 7 years ago now and I am a firm believer in giving back to the community, so I began answering where I could on the forums, and learning from the other posts I would come across. I honestly believe APEX has a great community! I started a blog as a mean to share back my knowledge to create hopefully a good reference for people. If you think I am worthy, I appreciate any votes you send my way!

Tuesday, 15 September 2015

Prototyping usage of an OAUTH API

There are many services out there that offer an OAUTH2 API - whereby you get an access token, associated to your account, and you use that with requests rather than logging in each request. It's a solid design, because for one, you as a user can opt to grant permission to parts of the system, and for another you can easily revoke access to individual applications.

One problem? It is fairly unlikely there is a client library built for PL/SQL or APEX use. For instance, Google provides libraries for[1]:
  1. Java
  2. JavaScript
  3. .NET
  4. PHP
  5. Python
  6. Objective-C
Another example, Instagram provides[2]:
  1. Python
  2. Ruby
It's not too hard to build it in PL/SQL once you understand the flow of OAUTH. I started working on one for Google services, which is hosted over on my GitHub profile. Before you get too far with that though, you may like to test the requests you are attempting to work with. The one's I have seen tend to provide an API test tool, however you may like to create templates that you can use down the track without relying on their test tools.

So here, I'd like to introduce you to a neat little tool called Postman. I'm sure there are quite similar apps out there, but I'll be focusing on this particular tool. This is an app that is actually available through the Chrome Web Store[3]. And because it's an app offered through the Chrome Web Store, it is cross platform - and you can just launch it from your system launcher - you need not even know its a chrome app!

Since I already have a Google library, I'll focus on the Instagram API. The business case I have is that I've posted a comment on someone's post, and I want to track it down with their API (my goal was to delete it, but Instagram only offers access to additional scopes for applications that have been submitted for review[4] - you can get basic access to Instagram though). The API doesn't offer an endpoint for comments you've posted, but you can list comments on particular media objects, to locate it that way.

The first step with any OAUTH project is to set up a client, where you can give it a name and in turn get a client id and client secret, that are needed for fetching the access token that needs to be sent with each request. Note: A lot of the Instagram requests simply require use of the client id, but for the purposes of this example, I will be fetching the access token and using that in the requests.

As you can see with the redirect URI, you can use a URL suggested by postman so that you can successfully fetch the token in the app. The other bit of information you need before requesting a token, is what scopes you would like access to. You can review available scopes for Instagram by looking at the authentication documentation[5].

So, on your request builder, within the authorization tab you can select which authentication you need. If you change that to OAuth 2.0, you will see a button "Get access token". Click that, and fill out all the required fields. If you review the authentication documentation, you will see that the authorization base URL is and the access token URL is So, we populate that information and specify the scope "basic comments". If all goes well, you should get a success result along with the token.

Once you save the token with a convenient name, it will appear in the authorization tab where you can either add it to the URL or as a request header. The Instagram API works with the token in the URL as a URL parameter, so you will need to add it there. Once you have the request URL defined (e.g. ensure the radio is selected "Add token to URL" and click the recently saved token "Instagram token". 

So now to prototype the actual task you're attempting to perform. By looking at the list of available endpoints[6] you can start to figure out which end points you might need to perform the desired task. Looking at the comments endpoint, you will notice it has a DEL operation, which accepts two paramaters:
  1. media id
  2. comment id
So, from here you will need to find these two identifiers. If you know the shortcode for the media, \youI can perform a request using the /media/shortcode/[code] endpoint. Or if you know the user, you can search to find their user id using the /users/search endpoint, then list their recent media, and get the id of the media that way.

Once you have the media id, you need to figure out the comment id. This can be done through the /media/[media-id]/comments endpoint. Though, it is worth noting that Instagram seems to only return 150 of the most recent comments. 

Once you find the two bits of information, you can then delete the comment (obviously, you can only delete comments you have authored or comments on your own posts). The good thing about Postman is that it allows you to save requests in set collections, so you can easily repeat the process again.

For simplicity, I'll go with the shortcode method of determining the media ID. I attached a comment to media with shortcode: 54-EW8IaHO, giving us the first request URL of: Here, I find that the media id is: 1042856292129022414_528817151.

The next step, is to get the comments. I compile a new request, to From there, scrolling to the end of the response, I can see my comment has the ID 1074977853463962032. In reality, you would need to provide some means of iterating of the returned comments.

Finally, we can compile the DELETE request using the aforementioned identifiers, giving us the request:

As previously mentioned, this specific request isn't possible without Instagram first reviewing your application - but the general idea is there.

Since you can save the request templates, you can easily go back in to repeat any processes you set up as an extra means to debug when your developed solution isn't working as expected - could be useful if there are any API changes. I set up a collection called Instagram, and added all related requests in there.

Once you have the flow prototyped, it should more straightforward to convert it into some functions/procedures in your Oracle application (or other system if no client libraries are unavailable).

Saturday, 12 September 2015

My experience as an Oracle developer running Ubuntu

If you have been following my blog, you may have noticed the desktop environment I use is the Ubuntu distribution of Linux. Growing up, I always tried Linux here and there, but never stuck with it - it was more just to try out. As with anything, if you are going to use something new, you really have to commit to it - pick a period of time you are willing to use it for, and see how you go. If you still don't like it at the end, go back into your old ways.

(I will add that I did once upon a time own a Mac. Actually, I purchased a Powerbook right before Apple made the switch to the Intel CPU architecture. I know Mac OS X seems quite popular in the development community nowadays, however I still prefer to be able to select my hardware, and I believe I get that freedom and also get a bit of Software freedom by running Linux.)

At around 2008 - actually, soon after I started working with APEX - I had a couple of colleagues that were running Ubuntu. Since I started developing with Oracle, I didn't have any reliance on Windows for work, which made the switch possible. I fully committed - both my work workstation and home PC were now running Ubuntu. I believe the first release I used would have been Karmic Koala (9.10). A couple of releases later, I decided that I would just stick to long term support (LTS) releases, which come out every 2 years. 

The installation supports creating different partitions for folders on your Linux system. So I have followed the general methodology of having 2 partitions, one for /home and another for / (root). /home can be akin to "C:/Documents and Settings" or C:/Users in Windows. So what this means is that every time I install a new release, I can opt not to format /home - which is what I do. So all my personal files (and application settings) remain, and it just blows away everything else to install afresh. Note, you can just do a system upgrade, but I just prefer to start afresh on a new installation.

These days Ubuntu uses their own Unity user interface, which seems to face a bit of criticism. Being Linux, you have lots of choice! You are not restricted to one distribution or user interface, but I personally have no problem with the UI.

So, what tools do I have in my repertoire?


If you are going to use Linux, you will want to know how to use the terminal. Whilst you can probably get away using just GUI tools, there are likely going to be times when you need to use the terminal. You may be connecting to your development server over SSH, here is where you will be doing that. And there's a very good chance your server will not be having a GUI available to you.

A terminal will most-definitely come pre-installed on your system. The specific command would be gnome-terminal. There of course are other terminals that you can use with extended/more basic feature sets.

Oracle SQL Developer

Well, being this post is about Oracle development, you will most likely want SQL Developer. Luckily, this is a cross-platform application and supported for Windows, Mac and Linux. Just so long as you have Java available to you. In Ubuntu, there is OpenJDK in the repositories. However, I personally prefer to use the official Oracle Java JDK. This version had to be removed from the repositories due to licensing changes. There is a PPA you can add to your system which will make the installation quite easy - see this article: However, what I tend to do is follow the instructions on askubuntu.com

The next challenge with SQL Developer is that they don't provide an installation file in Debian package format (Ubuntu is based on Debian). They provide 2 options for Linux:

1. RPM (for Redhat based distributions)
2. tar.gz (an archive (or tarball if you will) of all the SQL Developer files)

What I tend to do is grab the RPM file, and use a tool alien to convert it into a debian installation file. The conversion process should take around

sudo apt-get install alien
sudo alien --to-deb -c sqldeveloper-*.noarch.rpm
#sqldeveloper_4. generated

That will give you an installation file Ubuntu knows how to handle, and all is pretty straight forward from there on in. Just double click the generated file and follow the prompts.

Google Chrome

Being APEX, you will probably want a web browser. Ubuntu comes with Firefox, but I personally prefer Chrome. And wanting to be on the cutting-edge I tend to grab the beta release. All the release channels can be seen here -, but the specific link to download is:

Code Editor

Ubuntu by default comes with gedit, which is a nice basic editor that will get you out of trouble. The main editor I have been using lately is the Atom text editor, which is an open sourced text editor released by GitHub. The most common comparison I have seen is that it's quite similar to Sublime text editor. You get download the installation media from: It also comes with a whole raft of packages (extensions). For instance, if your version control system is git, you will likely want to use the git-plus package - - which supports a raft of git operations.

For instance, if you want to see the git diff, you can type in 'diff` in the command palette (ctrl+shift+p).

Code Diffs

A great tool for comparing code files I've found is meld. It supports up to 3 way comparisons and supports either file (or blank, where you can paste code in), and directory comparisons. To give a sample of it's UI, I will add a tweak to the following code:

function sayHello(){
    console.log("hello, world");

As you can see, it highlights both differences on the same line of code, and additional lines of code.

This is easy to install. Just run, sudo apt-get install meld, on the command line.

Lately, when I've been using SQL Developers database diff tool, it has been coming back with differences in some packages. I've found the interface in SQL Developer a little hard to see what differences there between the two, so I have been double checking with meld what difference the packages had (if any) as packages were being returned as differing after a fresh sync.

FTP transfers

For the occasional time I need to do a transfer over FTP, FileZilla is my go to application. It's in the repo's so it's easy to install: sudo apt-get install filezilla.

This program is supported on all platforms, so there's a good chance you are already using it, and have some familiarity.

BI Publisher reports / Unavailable apps

Ok - this is the only problem area. The report builder is built for Microsoft Word, and it's probably no surprise to you that there is no Microsoft Word For Linux. Some folks will probably tell you that they use Wine, but I personally never use this. Instead I have a Windows based virtual machine running through VirtualBox. To install VirtualBox on Linux, it's best to follow the "Debian-based Linux distributions" instructions here:

I also have it set up to to share my home folder so that it is dead simple to move files between the two environments - which is added as a mount point in My Computer.

Nothing much else to stay here - other than try not to rely on this for your software! Find native alternatives where possible. Ubuntu/Linux has LibreOffice/OpenOffice. Outside of the report building phase, most of the documents I make these days are done in Google Drive - I don't actually use LibreOffice too frequently myself.

Expanding abbreviations (for less typing)

I've heard a couple of mentions of this in the community. TextExpander is the name of an OS X application and the basic concept is that you define abbreviations for words and the system will automatically expand them once you've typed them. Once such blog post about the app can be found here, by Jorge Rimblas -

When I saw this post, I did go exploring to see if there was anything similar for Ubuntu, and what I found was AutoKey. Basically, if you just google <name of app> ubuntu, you should get some results on a similar program (usually as a question on

Once you find out the name, the next step is to search the repository:

$ apt-cache search autokey
autokey-common - desktop automation utility - common data
autokey-gtk - desktop automation utility - GTK+ version
autokey-qt - desktop automation utility - KDE version

Then, you can go ahead and install the relevant version. For me, that's the gtk version.

sudo apt-get install autokey-gtk

This application works on phrases. So you first create a phrase, and then set up an abbreviation. Following on from Jorge's demo, I set up a phrase for an APEX URL: "f?p=AppId:PageId:Session:Request:Debug:ItemNames:ItemValues". 

Then set up the abbreviation as: "aurl" 

Now, save the phrase, and wherever you type "aurl" followed by for example a space, the abbreviation will be replaced with the phrase. 

That's of course only a basic example, but if you feel this might be useful to you I encourage you to give it a shot!


What are your toolsets?