Tuesday, December 6, 2011

Creating a new user in Oracle 11g

Here's how you create a new user in Oracle 11g to get you started in a project:

1. Login as SYSTEM with the password you set during Oracle's installation.
2. Execute the following script:

CREATE USER myNewUser IDENTIFIED BY myPassword
       DEFAULT TABLESPACE users
       TEMPORARY TABLESPACE temp
       QUOTA UNLIMITED ON users;

CREATE ROLE conn;

GRANT CREATE session, CREATE table, CREATE view,
       CREATE procedure, CREATE synonym,
       ALTER any table, ALTER any materialized view, ALTER any procedure,
       DROP ANY table, DROP any view, DROP any procedure, DROP ANY synonym, CREATE sequence, DROP ANY sequence
       TO conn;

GRANT conn TO myNewUser;

Friday, November 5, 2010

Javascript Singleton Object

Have you come across a problem where you have to create an object that can only be initialized once and you want to reuse that object throughout your whole application? Well maybe you haven't but I actually came across that problem the other day and here's the solution for it:

function singleton() {
var instance = (function() {
var privateVar;

function privateMethod () {
// ...
}

return { // public interface
publicMethod1
: function () {
// private members can be accessed here
},
publicMethod2
: function () {
// ...
}
};
})();

singleton
= function () { // re-define the function for subsequent calls
return instance;
};

return singleton(); // call the new function
}
Solution given by CMS on Stack overflow(http://stackoverflow.com/questions/1895635/javascript-singleton-question)

Tuesday, May 18, 2010

Get number of weeks within fiscal year

The other day I found myself looking for a solution to find the week number we are in within a given fiscal year. Here's the solution I came up with with a snippet from my code:

Date.prototype.getWeek = function (dowOffset)
{
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */
dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4)
{
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52)
{
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else
{
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
}
var todaysDate = new Date();
var currentFiscalYear = todaysDate.getFullYear() - 1;
var numberWeeks;
if(todaysDate.getMonth() > 8 && todaysDate.getMonth() < 11)
{
currentFiscalYear = todaysDate.getFullYear();
}
var beginFiscalDate = new Date();
beginFiscalDate.setFullYear(currentFiscalYear,8,1);
var endFiscalDate = new Date();
endFiscalDate.setFullYear(currentFiscalYear+1,7,31);
var currYearBegFiscalYear = new Date();
currYearBegFiscalYear.setFullYear(todaysDate.getFullYear(), 8,1);
if(todaysDate.getFullYear() > beginFiscalDate.getFullYear()) //Check to see if today comes after september of the current year
{
var decFiscalYear = new Date();
decFiscalYear.setFullYear(currentFiscalYear,11,31);
numberWeeks = todaysDate.getWeek() + (decFiscalYear.getWeek() - beginFiscalDate.getWeek());
}
else
{
numberWeeks = todaysDate.getWeek() - beginFiscalDate.getWeek();
}

Tuesday, March 2, 2010

Mimic mySQL's LIMIT command in Oracle

I have struggled in the past and have heard of people struggling with writing a SQL statement in Oracle that would mimic mySQL's command LIMIT.

LIMIT doesn't exist in Oracle and there are two ways of displaying the range of records you want from a table. The first one is to use PLSQL. I'm not going to get into details here but all you have to do is do a SELECT statement INTO a CURSOR datatype, iterate your cursor and exit when your loop counter hits the number of rows you want.

As you can see this solution is not very effective and the best way of doing this is by creating a simple statement using the rownum column.

For instance, if you wanted to select the 10 first items from the item table you would do the following:

SELECT * FROM item WHERE rownnum < 11;

This should return the first 10 rows.

Tuesday, December 29, 2009

Enum as a class property in Objective-C

For this post I'd like to show how you can define an enum property in Objective-C.
In a nutshell an enum is a datatype you create where in theory only certain values can be assigned to it and none else.

The code is pretty straight forward. Here it goes:

typedef enum
{
North, South, East, West
} Direction;

@interface MyClassName : NSObject
{
Direction direction;
}

@property(nonatomic) Direction direction;

In the above example only the values North, South, East, and West can be assigned to the direction property of a MyClassName instance.

Tuesday, December 8, 2009

Querying an Oracle database using XSLT for dummies

In this post I will explain how you can query an Oracle database using XSLT technology and Xalan as your XML processor.

Requirements:
If you decide that you don't want to use an XML editor you will have to download Xalan and add the necessary libraries to your classpath before processing the XSLT file. If you decide to take that route you can follow the first steps in this tutorial, which explains how to setup your environment without an XML editor.

Tutorial

1. Open Oxygen and create a new document of type XSLT
2. Copy and paste the XSLT code below:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:sql="org.apache.xalan.lib.sql.XConnection"
 extension-element-prefixes="sql">
 <xsl:output method="html" />
 <xsl:template match="/">
  <xsl:variable
   name="movies"
   select="sql:new('oracle.jdbc.driver.OracleDriver'
   ,'jdbc:oracle:thin:@ipaddress:sid','username','password')" />
  <xsl:variable name="streaming" select="sql:disableStreamingMode($movies)" />
  <xsl:variable
   name="queryResults"
   select="sql:query($movies,'SELECT movie, actor FROM movie')" />
  <html>
   <head><title>Oracle Result Set</title></head>
   <body style="font-family: sans-serif;">
    <table border="1" cellpadding="5">
     <tr>
      <xsl:for-each select="$queryResults/sql/metadata/column-header">
       <th><xsl:value-of select="@column-label" /></th>
      </xsl:for-each>
     </tr>
     <xsl:apply-templates select="$queryResults/sql/row-set/row" />
    </table>
   </body>
  </html>
  <xsl:value-of select="sql:close($movies)" />
 </xsl:template>
 <xsl:template match="row">
  <tr><xsl:apply-templates select="col" /></tr>
 </xsl:template>
 <xsl:template match="col">
  <td><xsl:value-of select="text()" /></td>
 </xsl:template>
</xsl:stylesheet>
3. Be sure to make your changes accordingly
4. After pasting your code in the text area click on the XSLT debugger button as shown in the image below:







5.
Click on the Run button



6. VoilĂ ! Now look at the result, and here is what I got from my XSLT:













MOVIEACTOR
Indiana Jones and the Last CrusadeAlison Doody
Indiana Jones and Raiders of the Lost ArkHarrison Ford
Indiana Jones and Raiders of the Lost ArkDenholm Elliott
Indiana Jones and the Last CrusadeSean Connery
Indiana Jones and Raiders of the Lost ArkJonn Rhys-Davies

Friday, February 27, 2009

Turning off the PC Speaker on Ubuntu

Here is how you turn off the PC Speaker on Ubuntu. This command migh even work with other Linux distros.

modprobe -r pcspkr