Sunday, September 23, 2012

Oracle: Insert row with unique Id

If we have a table called “employee” containing two columns “id” and “name” and we want to write a query that could be reused to add a new row to the table, we could use the following:

Insert into employee (id, name) values ((select max(id) +1 from employee), ‘name_of_employee’)

This is a very simple solution, however the problem here is that if we have two servers trying to run this query at the same time, things could become a bit messy (in case if the same id value is returned to both these servers).

A clean solution for this is to use a trigger and a sequence. The theory behind this is that we have a sequence that goes through each row of a table and returns the first available value of id. We could then create a trigger that will run when an insert statement is executed with a null id value. This trigger will use the sequence to get the first available id and assign it to the new row being added.

Because this would all be happening at the database level, we will not have to worry about multiple servers (application servers) trying to add a new row to the database (which is usually in a separate database server).

An implementation of the given theory can be as follows:

  • A sequence is created that returns the first available id.
  • A trigger is created that runs whenever a new row is being added with a null id, replacing it with what the sequence returns.
  • A code that uses the following insert statement: insert into employee (id, name) values (null, ‘name_of_employee’).

To create a simple sequence we could use the following code:

Create sequence NAME_OF_SEQUENCE
Minvalue 1
Maxvalue 999999999
Increment by 1
Start with 1
Nocache
Noorder
Nocycle

To create the trigger discussed above, we could use the following sql:

Create or replace Trigger NAME_OF_TRIGGER
Before insert On NAME_OF_TABLE
For each row
Declare

Begin
If :new.ID is null then
Select NAME_OF_SEQUENCE.nextval
Into :new.ID
From dual
End if;
END;

And that's it, a nice clean solution giving a unique id to new rows.

PS: This solution will not reassign the id values of deleted rows, so every time the sequence runs, it starts from where it last ended and not from 1.

Wednesday, March 28, 2012

Atom feeds using .NET's built in functionality

We can create RSS 2.0 and Atom 1.0 feeds in .NET using the Syndication feed class as shown below. In this example we will generate Atom 1.0 feeds (you could also do some minor tweaks to this code and generate RSS 2.0 feeds).

For clarification, we will create a feed called “News” containing a single item called “Article1”.



public XmlElement TestFeed()
{
var feed = new SyndicationFeed();
       feed.Title = new TextSyndicationContent("News");
       feed.Id = "123";   
feed.LastUpdatedTime = DateTime.Now;

       SyndicationItem item = new SyndicationItem();
       item.Id = "1";
       item.Title = new TextSyndicationContent("Article1");
       item.LastUpdatedTime = DateTime.Now;

       item.Authors.Add(new SyndicationPerson(null, "Author-Name", null));
       item.Content = new TextSyndicationContent("The entire article comes here");
       item.Links.Add(new SyndicationLink(new Uri("http://www.news.com/etc")));
        
       List<SyndicationItem> items = new List<SyndicationItem>();
       items.Add(item);

       feed.Items = items;

       XmlDocument document = new XmlDocument();
       XmlWriter writer = document.CreateNavigator().AppendChild();
       Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
       atomFormatter.WriteTo(writer);
       writer.Close();

       return document.DocumentElement;        
}

According to Atom 1.0 feeds standard, the only compulsory fields in an item (“Article1” in this case) are “id”, “title”, and “updated”, the rest are optional. They have been added just to clear some ambiguities regarding the addition of url and author name in a syndication class. 

For more information about Atom 1.0 feeds standard you could visit this page.

Thursday, February 16, 2012

Omniture Site Catalyst Overview

Omniture Site catalyst is a web analytics solution that gives us a summarised overview of customer behaviours on our site.

It is implemented through javascript code executed by the browser while rendering the page. This code gathers all relevant information and sends it to Omniture’s site catalyst servers as a request for a 1×1 pixel transparent image.

Implementing Site Catalyst

Site catalyst implementation involves adding the following two components to a website:
  • Adding/hosting a javascript file to the solution (this file can be taken from Omniture once an account for website tracking is created there)
  • Injecting some javascript code in every page that needs to be tracked.

A sample javascript code which is injected in each tracked page is shown below (taken from www.webmetric.org).

Note: In this code, the values we give to each of the variables such as pageName, server, channel etc. is the customer information we will be collecting. For example we can give a different pageName value to each page and get individual page traffic for each of our pages.

<!-- SiteCatalyst code version: H.17.
Copyright 1997-2008 Omniture, Inc. More info available at
http://www.omniture.com -->

<script language="JavaScript" type="text/javascript" src="http://INSERT-
DOMAIN-AND-PATH-TO-CODE-HERE/s_code.js"></script>

<script language="JavaScript" type="text/javascript">
<!--
/* You may give each page an identifying name, server, and channel on
the next lines. */
s.pageName=""
s.server=""
s.channel=""
s.pageType=""
s.prop1=""
s.prop2=""
s.prop3=""
s.prop4=""
s.prop5=""
/* Conversion Variables */
s.campaign=""
s.state=""
s.zip=""
s.events=""
s.products=""
s.purchaseID=""
s.eVar1=""
s.eVar2=""
s.eVar3=""
s.eVar4=""
s.eVar5=""

/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code=s.t();if(s_code)document.write(s_code)//--></script>
<script language="JavaScript" type="text/javascript"><!--
if(navigator.appVersion.indexOf('MSIE')>=0)document.write(unescape('%3C')+'\!
-'+'-')
//--></script><noscript><a href="http://www.omniture.com" title="Web
Analytics"><img
src="http://devgundersen.112.2O7.net/b/ss/devgundersen/1/H.17--NS/0"
height="1" width="1" border="0" alt="" /></a></noscript><!--/DO NOT REMOVE/-->

<!-- End SiteCatalyst code version: H.17. -->