Thursday, September 12, 2013

use of isAssignableFrom

Instead of
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
use
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (! getClass().isAssignableFrom( obj.getClass() ) ) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
Ref: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

Long vs long

If you are using Long for variable that cannot be null, its best to use the type long instead of Long That way you do not need to check for null Only use wrapper classes when you need to support the concept of undefined

Wednesday, March 13, 2013

Making ant targets driven by a property

The ant apache manual clearly defines on the the use of the "if" property when there's a need to have certain ant targets run only for some criteria.

 "A target also has the ability to perform its execution if (or unless) a property has been set. This allows, for example, better control on the building process depending on the state of the system (java version, OS, command-line property defines, etc.). To make a target sense this property, you should add the if (or unless) attribute with the name of the property that the target should react to. Note: In the most simple case Ant will only check whether the property has been set, the value doesn't matter, but using property expansions you can build more complex conditions. See the properties page for more details. For example:


Expanding on this need, here's how you pass the property on the command line so its picked up by the build script
$ant clean build -Dmodule-A-present=anyvalue

Ref: http://ant.apache.org/manual/targets.html

Tuesday, November 1, 2011

Recently Analyzed Table List

Two useful queries to get a list of recently analyzed tables. Can be used to check when the tables were last analyzed. Normally oracle should analyze tables on its own.
SELECT owner,
       SUM (DECODE (NVL (num_rows, 9999999), 9999999, 0, 1)) analyzed,
       SUM (DECODE (NVL (num_rows, 9999999), 9999999, 1, 0)) not_analyzed,
       COUNT (table_name) total
  FROM all_tables
 WHERE owner NOT IN ('SYS', 'SYSTEM')
GROUP BY owner;
SELECT table_name,
       TO_CHAR (last_analyzed, 'MM/DD/YYYY HH24:MI:SS') last_analyzed
  FROM user_tab_columns
 WHERE     last_analyzed IS NOT NULL
       AND column_id = 1
       AND (SYSDATE - last_analyzed) < 30
ORDER BY 2;

Tuesday, October 18, 2011

Time difference

Query to get the time different between 2 date columns
select floor(((end_time - start_time)*24*60*60)/3600)
       || ' HOURS ' ||
       floor((((end_time - start_time)*24*60*60) -
       floor(((end_time - start_time)*24*60*60)/3600)*3600)/60)
       || ' MINUTES ' ||
       round((((end_time - start_time)*24*60*60) -
       floor(((end_time - start_time)*24*60*60)/3600)*3600 -
       (floor((((end_time - start_time)*24*60*60) -
       floor(((end_time - start_time)*24*60*60)/3600)*3600)/60)*60)))
       || ' SECS ' time_difference
from sub_email_jobs;

Tuesday, October 11, 2011

Excel Removing or adding hyperlinks

Useful code to remove and add hyperlinks in Excel using VB
Public Sub Convert_To_Hyperlinks()
    Dim Cell As Range
    For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
        If Cell <> "" Then
            ActiveSheet.Hyperlinks.Add Cell, Cell.Value
        End If
    Next
End Sub
Sub RemoveHyperlinks()

'Remove all hyperlinks from the active sheet
ActiveSheet.Hyperlinks.Delete

End Sub

Friday, October 7, 2011

Datafile Usage on an Oracle Database

Two useful queries to get datafile usage on an Oracle database
SELECT SUBSTR (df.tablespace_name, 1, 20) "Tablespace Name",
       SUBSTR (df.file_name, 1, 40) "File Name",
       ROUND (df.bytes / 1024 / 1024, 2) "Size (M)",
       ROUND (e.used_bytes / 1024 / 1024, 2) "Used (M)",
       ROUND (f.free_bytes / 1024 / 1024, 2) "Free (M)",
       RPAD (' ' || RPAD ('X', ROUND (e.used_bytes * 10 / df.bytes, 0), 'X'),
             11,
             '-')
           "% Used"
  FROM dba_data_files df,
       (SELECT file_id, SUM (DECODE (bytes, NULL, 0, bytes)) used_bytes
          FROM dba_extents
        GROUP BY file_id) e,
       (SELECT MAX (bytes) free_bytes, file_id
          FROM dba_free_space
        GROUP BY file_id) f
 WHERE e.file_id = df.file_id AND df.file_id = f.file_id
ORDER BY df.tablespace_name, df.file_name;
SELECT SUBSTR (df.name, 1, 40) file_name,
       df.bytes / 1024 / 1024 allocated_mb,
       ( (df.bytes / 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024, 0))
           used_mb,
       NVL (SUM (dfs.bytes) / 1024 / 1024, 0) free_space_mb
  FROM v$datafile df, dba_free_space dfs
 WHERE df.file# = dfs.file_id(+)
GROUP BY dfs.file_id,
         df.name,
         df.file#,
         df.bytes
ORDER BY file_name;