| 제목 | sql 문 작성은 어떻게들 하시나요? | ||
|---|---|---|---|
| 카테고리 | CI 2, 3 | ||
| 글쓴이 | 하늘치 | 작성시각 | 2017/05/26 10:00:04 | 
|  | |||
| 처음 ci 를 접했을 때, controllers, libraries, models 등에서 select 문 작성할 때, 그 때 그 때 상황에 맞춰서 만들었던 기억이 납니다... 
 
 시간이 좀 흐르고 난 후에는 다른 분들의 소스를 참고했습니다. 기본 모델(Common_model.php)에 get_common_result(), get_common_row() 등의 공통 함수를 만들어서 사용했구요. 
 
	function get_common_result($sst=FALSE, $sod=FALSE, $sfl=FALSE, $stx=FALSE, $sql_select = FALSE, $sql_from = FALSE, $sql_where = FALSE, $sql_group_by = FALSE, $sql_order_by = FALSE, $limit=FALSE, $offset=0, $page=1) {
		$this->db->start_cache();
		if ( ! empty($sql_where))
		{
			$this->db->where($sql_where);
		}
		$this->db->stop_cache();
		$result['total_count'] = $this->db->count_all_results($sql_from);
		// Group by
		if ($sql_group_by)
		{
			$this->db->group_by($sql_group_by);
		}
		// Order by
		if($sst && $sod) 
		{
			$this->db->order_by($sst, $sod);
		}
		if ($sql_order_by)
		{
			$this->db->order_by($sql_order_by);
		}
		// Limit
		if ( ! empty($limit) )
		{
			$this->db->limit($limit, $offset);
		}
		// Set directly submitted SELECT and WHERE clauses.
		if ( ! empty($sql_select))
		{
			$this->db->select($sql_select);
		}
		$result['qry'] = $this->db->get($sql_from)->result();
		$this->db->flush_cache();
		return $result;
	}
 물론, 처음에는 더 간단했지요. 그데, 이렇게 사용하다보니, 자꾸 하나씩 하나씩 덧붙이게 되더라구요. 위에 있는 것만해도 조인이 있는 경우, 사용할 수 없지요;;; 
 
 그래서 생각했던 게.. 
	function get_arr_result($sql_from=FALSE, $sql_select='*', $sql_arr=FALSE) {
		foreach($sql_arr as $key => $val)
		{
			if('where' === $key){
				$this->db->start_cache();
				$this->db->where($val);
				$this->db->stop_cache();
				//$result['total_count'] = $this->db->count_all_results($sql_from);
			}
			elseif('group_by' === $key){
				$this->db->group_by($val);
			}
			elseif('order_by' === $key){
				$this->db->order_by($val);
			}
			elseif('limit' === $key){
				$this->db->limit($val);
			}
			elseif('join' === $key){
				$arr_join = $val;
				$this->db->join( $arr_join['join_table'], $arr_join['join_on'], $arr_join['join_opt']); 
			}
		}
		// Set directly submitted SELECT and WHERE clauses.
		if ( ! empty($sql_select))
		{
			$this->db->select($sql_select);
		}
		$result['qry'] = $this->db->get($sql_from)->result();
		$this->db->flush_cache();
		return $result;
	}
 사용은.. 
	$result = array();
	$sql_from = 'board as b';
	$sql_select = 'idx, subject';
	$sql_arr = array(
		'where' => array('idx'=>$idx),
		'join' => array('join_table'=>'files as f', 'join_on'=>'f.bbs_fk = b.idx', 'join_opt'=>'left outer'),
		'limit' => 3,
		'order_by' => 'b.idx DESC'
	);
	$result = $this->Common_model->get_arr_result($sql_from, $sql_select, $sql_arr);
	$count = $this->Common_model->get_arr_count($sql_from, $sql_select, $sql_arr);
	$result['total_count'] = $count;
 
 이런 식으로 사용하면 어떨까 싶었습니다. 아, 물론 위에서는 조인을 사용할 경우, $result['total_count'] 값이 문제가 되더군요;; 문제가 되면 빼버리면 되니까요 뭐.. ㅎㅎ ^^; 암튼, 이렇게 만들어놓으면, 확장성이랄까요.. 좀 좋은 듯 해서요. 
 혹시 더 멋진 방법이 있다면 공유해주세요~ ^^ 
 
 
 덧. 찬찬히 다시 보니, 굳이 이렇게 쓸 필요가 없기도 하겠군요;; 그냥 상황에 맞춰 액티브 레코드를 사용하는 게 제일 맘편한건지도 모르겠스니다. 헛헛.. 
 
 | |||
| 다음글 | foreach 문 입니다. 제가 작성한 코드보다 더 간... (3) | ||
| 이전글 | 페이지네이션에서 뒤에 처리 (1) | ||
| 
                                변종원(웅파)
                                /
                                2017/05/26 13:52:28 /
                                추천
                                0
                             | 
| 
                                한대승(불의회상)
                                /
                                2017/05/26 14:53:21 /
                                추천
                                0
                             정년 보신다면 튜닝 끝을 볼 수 있을것 같군요. 
 | 
| 
                                jcoop
                                /
                                2017/05/27 22:42:33 /
                                추천
                                0
                             
                                sql일반 select, update,delete정도 해봤습니다. 회원가입부분으로요. 
                             | 
| 
                                하늘치
                                /
                                2017/05/29 13:01:36 /
                                추천
                                0
                             변종원// 튜닝의 끝은 순정이다.. 과연 명언이로군요. 한대승// 정말, 정년까지 쭈욱 일하고 싶네요.ㅎㅎ jcoop// 제가 글에서처럼 고민했던 주된 이유가.. 아마도 검색이나, 페이징 때문이었지 싶어요. ㅎㅎ 
 | 
튜닝의 끝은 순정이다라는 명언이 있습니다. ^^
모델은 공용화하지 않고 사용합니다. 같은 기능이면 그 함수를 이용하는 정도로
또는 약간의 분기 정도 처리하는 정도로 사용합니다. (그래야 유지보수가 편합니다)