Monday, June 23, 2014

What's Up With OPEN DATA and Government?

Intro:  Code for America has a chapter here in Hawaii.  Although it is based on Oahu, I still manage to keep up with their activity from here on Maui.


Watching Over State Legislator and Employee Spending


Many of you database developers may wonder in an existential sense if there is anything out there beyond the systems we support each day.  Yes, the government sector alone has a myriad of information that is of the public domain that is a vital link to citizen awareness of their own governance.

The self-description of the CfA Brigade in Hawaii introduces them as:

This group [sic. Hawaii CfA Brigade] is for all those interested in open data, data access, apps, big data, data visualization, APIs and anything related to civic engagement and collaborative technologies.

In the news:  The personal allowance budgets of lawmakers (and their staff) who represent Hawaii are now available for public review.  More details on the committee decisions that led to this is detailed in a Civil Beat article on lawmaker allowance spending reviews.

A look at how you can query and download some of these figures for your own review is explained below in the next section.

How to Export Data from the Legislator Allowance Tool


Legislative Allowance Tool
Legislative Allowance Tool @khon2

The site that provides this information is hosted at:

http://khon2.com/legislative-allowance-tool/

1. The first step is to select the initial query parameters to collect the larger set of information you are querying.


2. Look for the "Download" link next to the output report grid and select it to initiate the generation of a comma-separated-value file for downloading to your local computer.

3. The .csv text file downloaded from the tool can be ingested into any relational database or aggregation system for further research and analysis.

An example comma separated value (.csv) file format opened using spreadsheet software:

Sample Download Contents: Lawmaker Allowance Data Hawaii
What the Data Looks Like from the Web App Tool
Give it a try.  Even if you aren't super savvy at SQL or database technology, even a spreadsheet formula or two transforms this raw data into a source of accountability as the Hawaii State Ethics committee probably also believes in as well.  Check it out.


Monday, June 16, 2014

Parsing a Comma Delimited Input String with PL/SQL

A colleague recently introduced me to an Oracle 11g PL/SQL feature of syntax called "comma_to_table".  It is a part of the DBMS_UTILITY built in package.  She used it as a method to break apart a string input that had components delimited by a comma ",".

How Complicated is Delimited String Parsing?


The more commonly known function: LISTAGG converts a single column output of many rows into a string with a custom delimiter.  There originally was no real alternative to do the reverse except through a procedural path in our code:



        for i in 1 .. l_count
            loop
              select regexp_substr(string_to_parse,'[^,]+',1,i)
                into l_value from dual;
                dbms_output.put_line(l_value);
            end loop; ...

[Sourced from an example on: Oradev.com]

This example loops through the input string "string_to_parse" and breaks the string into pieces based on a self-declared delimiter.  A fancier approach is to use the CONNECT BY syntax (a hierarchical SQL syntax concept) and make an artificial cartesian product to force an iteration over multiple delimited elements:

[Ask Tom has an example] of this:

      select trim( substr (txt, instr (txt, ',', 1, level ) + 1,
             instr (txt, ',', 1, level+1) - instr (txt, ',', 1, level) -1 ) )
        from dual 
     connect by level <= length(txt) - length(replace(txt,',','')) -1)

this one is clever and elegant but it takes a while to interpret.  The use of "connect by...level" is not readily obvious and looks like something is missing from the clause when I look at it.

Hidden Constraints:  The Trouble with Misapplied PL/SQL Syntax


The summary of the comma_to_table function is that it was created with a specific purpose in mind and does not have a fully generic application for all cases.  With a single command, this function allows coders to convert a comma-separated series of string values such as:

     '1,2,3,four,fifteen,orange,shack'

to a row-wise output:

        1
        2
        3
        four
        fifteen
        orange
        shack

Ask-Tom also warns that the original intent of this function derives from the expression: NAME TOKENIZE. [1]  This function was made to assist with internal database ETL processes where the task was to parse valid object names (such as tables) from comma delimited string inputs.  This limits the types of values that can be contained within the string:

  1. Each delimited element must be no longer than about 30 characters.
  2. The string values must represent valid database object names.
  3. Objects are defined as:
    "[schema].[object_name].[procedure|function]@[database link]"
  4. Certain reserved words that do not make valid object names will cause an error when applied.
In general, the comma-to-table function (Oracle APEX also has a similar command within its built-in package library) has enough limitations to make it a clear recommendation to avoid.  General parsing capability is not available because of its exceptions and the output can defy a coder's original intent.


Source Citations:


  1. DBMS_UTILITY.comma_to_table Limitations from "Ask Tom" at:  https://asktom.oracle.com/pls/asktom ...

Wednesday, June 4, 2014

A Reminder: UTL_SMTP or UTL_MAIL for Sending Mail?

I thought this was worth mentioning because it is often confused by old-school Oracle developers who have survived more than one or two major releases of this RDBMS.

From the docs at Oracle [1]:
The UTL_SMTP package is designed for sending electronic mails (emails) over Simple Mail Transfer Protocol (SMTP) as specified by RFC821.
The folks at PSOUG[2] have also noted that historically, this built-in package has been around since Oracle 8i:

UTL_SMTP Package Detail: sourced from PSOUG.org
UTL_SMTP Definition[3]

The UTL_MAIL package was similarly created to replace UTL_SMTP. Both work slightly differently from one another.  Oracle announced the eventual replacement of the newer mail utility around Release of the Oracle 10g RDBMS product release.  It's definition:

UTL_MAIL Package Detail: sourced from PSOUG.org

UTL_MAIL Definition [4]
I have noticed and used UTL_MAIL since at least the 10g RDBMS Release.  Anyways, it would be wise to stick with the new replacement to add longevity to your PL/SQL procedural code which uses email features supplied by the database system.


  1. Oracle PL/SQL Documentation: UTL_SMTP
    http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_smtp.htm#i1002798
  2. (PSOUG) Puget Sound Oracle User Group
  3. UTL_SMTP History
    http://psoug.org/reference/utl_smtp.html
  4. UTL_MAIL History
    http://psoug.org/reference/utl_mail.html


Tuesday, January 22, 2013

Importance of Addressing the Little Issues in Software Development

Saw a brief interview clip from +Martin D'Sousa, Board Member of ODTUG, who talks about some of the aspects of building a better development team. His take is that if you look at a company's internal software development problems as a pyramid with the smallest part (the top) being the most difficult problems and the bottom, wider part consisting of smaller, simpler problems to solve there is a strategy that will help the organization overall. By addressing the smaller, simpler problems D'Sousa suggests, one will notice that the more difficult ones will be less and less frequent.

What are some of the common things one can do to address this "simpler problem" approach? Agreeing on coding standards such as formatting and code reviews to share learning and cross train team members on various projects. These things are relatively easy to do and can help a team by reducing ambiguity of the simple problems and freeing up developers to focus on creativity in solving the more important problems at hand. The important part D'Sousa explains is that the team is "on the same page" and is in agreement with basic issues related to their development responsibilities.

I agree with much of this approach from my own experience because I see that many of the more complicated and difficult issues facing an organization's software development teams are only symptomatic of much simpler but cumulative problems. If you systematize and standardize the solutions for these simpler problems, many of the typical complications can and do disappear.

Getting Started with Oracle SQL and Relational Databases

Got a useful link to some resources for people looking to get started in relational databases and the SQL language. Oracle Magazine has an online version which is free and over the past year, they have had a monthly series in SQL 101 or basic lessons in coding in the SQL programming language; the page that links to all their tutorials can be reached here:

http://www.oracle.com/technetwork/issue-archive/index-1429427.html

Need a database to practice on? You can download the free edition of the Oracle database engine at:

http://www.oracle.com/technetwork/products/express-edition/overview/index.html

Oracle XE can be installed on your own home computer or laptop and has most of the features used by development teams and corporations that use the paid versions. At least it's a starting point so that you can get a feel for the product and what it can do. Give it a try!

Monday, January 14, 2013

Basic Concepts of Version Control

I've spent much of my recent database development career using different SCM (Source Code Management) tools such as CVS, Subversion (SVN), Perforce and GIT. I used them even if there wasn't any widespread support or integration of SCM methods among my peers because it would be sheer insanity if I didn't. Once you start using any of them, you'll probably agree... while life without an SCM is still possible, the timesavings, boost in productivity and reduction of risk due to mistakes all point to a real win for the professional who chooses to use SCM methods and tools. For example (explanation borrowed from gitref.org):

Why Would I Use SCM?


If I didn't use an SCM tool, I would most likely start out by copying my current source code file with some distinguishing naming criteria so that I would have at least one good copy (one for archiving) and one for my current changes:

cp somesource somesource.bak

Then maybe as a project progresses, I may have multiple copies with some other naming methodology to allow uniqueness between each "version" of my file:

somesource-01132013
somesource-v1
somesource-project1
etc...

I could post these files to some shared resource so other developers could review or see my versions. But what if one of these files were mistakenly used by another developer as the base version then applied to the database? How would a team communicate between each other that an object was being worked on by another? An even bigger question is: how does a team of developers package up their work and manage changes between different tiers (development, test, production, etc) and still keep things organized? 

The primary answer to these issues is to invest some time into thinking through a source code management (SCM) solution, which may include conventions for version control, build and release management. It is imperative to keep track of the software your write, how it changes over time, who changes it and for what purpose it is changed for. Change... (as the mantra goes) is inevitable. It is important to start picking up tools for SCM and the skills to apply it properly.

Examples of Things SCM Solves

Some typical problems that developers can solve and answer with SCM includes:
  • How do you keep a history of changes?
  • If I don't like a change, how do I roll back to the last known, working state?
  • How do I keep from clobbering someone else changes in progress?
  • How do I keep track of my own changes privately before publishing the final product for use?

Comment Madness

The first of the questions: How do I keep a history of changes? Many developers may attest that "temporary" or unsure changes are usually "commented out" of code. The code continues to exist, but marked so that the database compiler ignores the excution of the instructions just commented out. This leads to code bloat (inclusion of unnecessary or unused lines of code) and additional confusion as other developers struggle to sort through which is the functional code and which is just commented out. SCM tools keep track of snapshots of versions of code in various states so there is no loss of code. Unused portions can be confidently deleted out with the knowledge that the deleted code can be recovered at any time simply by searching through past versions. This can be said of the need to "roll back" or recover older working versions when new changes fail to pass the test process.

Organizing Changes

Sometimes changes are made in parallel between multiple developers. Simple features like "check out" or "check in" in SCM tools allows changes to be organized between these programmers. SCM tools handles this concept differently so it may help to do a little research first. For example, some tools such as Perforce allows a developer to "lock" a file upon checkout so that others cannot make changes to the file in the master repository until the one who has checked it out has submitted their edits.

Sandboxing Projects in Progress

Some SCM tools require synchronization of source code files between a master repository (remote) and what is kept locally on one's machine. Git, for example allows the cloning of a remote repository to a private sandbox (local machine) where edits and adds can be made. Git allows an unlimited amount of submit actions to the local repository. Then, when the developer is ready to push their final changes to the remote, shared repository, all their tiny edits are represented as one single change... the final product since the last time they copied from the remote repository... preventing the remote repository from being littered with the atomic edits made during the development process.

Getting Started with SCM

There is a free tutorial video and interactive demo available at Code School (http://codeschool.com) called "Try Git" which even gives you an opportunity to set up a free account with github, a popular cloud-based Git hosting service. Other places where SCM tools are actively used and available includes: Source Forge, Google Code, and others. Give it a try and see what it can do for your productivity as a single developer or as a member of a team of programmers together!

Friday, January 11, 2013

Now That's Ugly!

Welcome to my never-ending search for ugly SQL. That's the well-meaning, but ill-purposed use of SQL code to accomplish basic to complex database operations. I will also be sharing some examples of simple solutions to typical problems encountered by database users. SQL optimization is quickly becoming a lost art in light of more powerful machines and automated software. The ability to code with optimization in mind is something I have come to appreciate and hope to show to others.