페이지

2013년 2월 22일 금요일

oracle 한행에 보여주기


SELECT X.JOB,
       COUNT(*) AS CNT,
       SUBSTR(MAX(SYS_CONNECT_BY_PATH(ENAME, '/')), 2) AS ENAMES
  FROM (SELECT JOB,
               ENAME,
               ROW_NUMBER() OVER(PARTITION BY JOB ORDER BY ENAME) SEQ
          FROM SCOTT.EMP
        --      WHERE job = 'CLERK'
        ) X
 START WITH SEQ = 1
CONNECT BY PRIOR SEQ = SEQ - 1
       AND PRIOR JOB = JOB
 GROUP BY JOB
 /*
 1 CLERK     4 ADAMS/JAMES/MILLER/SMITH
 2 SALESMAN  4 ALLEN/MARTIN/TURNER/WARD
 3 PRESIDENT 1 KING
 4 MANAGER   3 BLAKE/CLARK/JONES
 5 ANALYST   2 FORD/SCOTT
 */

java enum 을 활용하자. (learn the enum type)

howto java :: learn the enum type
package com.test;
/**
 * Learn the enum type
 * @author sangyeol.lee(korlsy@gmail.com)
 */
public class EnumExam {
 /**
  * old - bad code
  */
 public static final int FONT_STYLE_NORMAL = 0;
 public static final int FONT_STYLE_BOLD = 1;
 public static final int FONT_STYLE_ITALIC = 2;
 
 /**
  * new - good code
  * 상수를 enum type으로 대체
  * @author Administrator
  *
  */
 public static enum FONT_STYLE{
  NORMAL{
   @Override
   public String fontStyleName() {
    return this.toString();
   }
  }
  , BOLD{
   @Override
   public String fontStyleName() {
    return this.toString();
   }
  }
  , ITALIC{
   @Override
   public String fontStyleName() {
    return this.toString();
   }
   
  };
  public abstract String fontStyleName();
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  /**
   * enum const
   */
  
  //print all font style
  System.out.printf("font style count => %d%n", FONT_STYLE.values().length);//font style count => 3
  for(FONT_STYLE style:FONT_STYLE.values()){
   //style => NORMAL 
   //style => BOLD 
   //style => ITALIC
   System.out.printf("style => %s%n", style.fontStyleName());
  }
  
  
  //good code(jdk5.0 more...)
  setFontStyle(FONT_STYLE.BOLD);
  FONT_STYLE fontStyle = getFontStyle();
  switch(fontStyle){
   case BOLD : /*...*/ break;
   case ITALIC : /*...*/ break;
   case NORMAL : /*...*/ break;
   default : /*...*/
  }
  
  //bad code(jdk1.4.....)
  setFontStyle(FONT_STYLE_BOLD);
 }

 private static void setFontStyle(int fontStyleBold) {
  //....
 }

 private static FONT_STYLE getFontStyle() {
  return FONT_STYLE.ITALIC;
 }

 private static void setFontStyle(FONT_STYLE bold) {
  //.....
 }

}

2013년 2월 21일 목요일

javascript forEach() prototype





Javascript 개발하면서
문득 왜 forEach가 없지 했는데...
불현듯 forEach를 구현해봤다.


(function(){
/**
* 자바스크립트 Array Primitive Type에
* forEach 함수를 prototype한다.
*/
Array.prototype.forEach = function(f){
/**
* this는 Array의 Object를 의미
* 즉 var a = new Array(1, 2, 3);에서 a를 의미.
* forEach를 array element만큼 callback으로 호출
*/
for(var i=0,len=this.length;i
/**
* @param index - array의 index
* @param val - array value
*/
  f.call(this, i, this[i]);//callback처리
}
}
})();


//사용법 :
var values = [1, 2, 3, [4,5]];

//기존 문법
//매번 for문을 작성하기가 무쟈게 귀차니즘....ㅠㅜㅡㅡㅡㅡ
var len = values.length;
for(var index=0;index
alert( "values index["+index+"]=" + values[index] );
}

//forEach 이용
//장점은 callback function을 이용해서 코딩을 효율적으로 이용가능.
values.forEach(function(index, value){
alert( "values index["+index+"]=" + value );
});