Advertisement
eyuprog

M_db.php

Apr 18th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.88 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Minang Igniter
  4.  *
  5.  * Engine code untuk pengembangan aplikasi berbasis codeigniter
  6.  * @link    http://ilmuprogrammer.com
  7.  *
  8.  * M_db
  9.  *
  10.  * Description:
  11.  * Librari ini digunakan untuk memudahkan penggunaan active record codeigniter
  12.  *
  13.  * Copy file ini pada application/libraries/M_db.php
  14.  *
  15.  * @copyright   Copyright (c) 2015 Heru Rahmat Akhnuari
  16.  * @version     1.0
  17.  *
  18.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  19.  * of this software and associated documentation files (the "Software"), to deal
  20.  * in the Software without restriction, including without limitation the rights
  21.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22.  * copies of the Software, and to permit persons to whom the Software is
  23.  * furnished to do so, subject to the following conditions:
  24.  *
  25.  * The above copyright notice and this permission notice shall be included in
  26.  * all copies or substantial portions of the Software.
  27.  *
  28.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34.  * THE SOFTWARE.
  35.  **/
  36. defined('BASEPATH') OR exit('No direct script access allowed');
  37. class M_db{
  38.     protected $CI;
  39.  
  40.     function __construct()
  41.     {      
  42.         $this->CI=& get_instance();
  43.         $this->CI->load->database();
  44.     }
  45.  
  46.     function noTable(){
  47.         die('Gunakan nama table terlebih dahulu pada parameter');
  48.     }
  49.  
  50.     /**
  51.      * [fetchData Mengambil data]
  52.      * @param  string $table [libraries form_validation rule-reference]
  53.      * @param  string $where [Parameter seperti $this->db->where()]
  54.      * @param  string $order [Parameter seperti $this->db->order_by()]
  55.      * @param  string $group [Parameter seperti $this->db->group_by()]
  56.      * @param  string $limit [Parameter seperti $this->db->limit()]
  57.      * @return stdClass        [Bisa diloop]
  58.      */
  59.     function get_data($table,$where=array(),$order='',$group='',$limit=null,$start=null){
  60.  
  61.  
  62.         if(!empty($table))  {
  63.             if(!empty($where)){
  64.                 $this->CI->db->where($where);
  65.             }
  66.  
  67.             if(!empty($order)){
  68.                 $this->CI->db->order_by($order);
  69.             }
  70.  
  71.             if(!empty($group)){
  72.                 $this->CI->db->group_by($group);
  73.             }
  74.  
  75.             if(!empty($limit)){
  76.                 $this->CI->db->limit($limit,$start);
  77.             }
  78.  
  79.             $result=$this->CI->db->get($table);
  80.             if($result->num_rows() > 0){
  81.                 return $result->result();
  82.             }else{
  83.                 return null;
  84.             }
  85.         }else{
  86.             $this->noTable();
  87.         }
  88.     }
  89.    
  90.     function get_data_in($table,$where=array(),$order='',$group='',$limit=null,$start=null){
  91.  
  92.  
  93.         if(!empty($table))  {
  94.             if(!empty($where)){
  95.                 $this->CI->db->where_in($where);
  96.             }
  97.  
  98.             if(!empty($order)){
  99.                 $this->CI->db->order_by($order);
  100.             }
  101.  
  102.             if(!empty($group)){
  103.                 $this->CI->db->group_by($group);
  104.             }
  105.  
  106.             if(!empty($limit)){
  107.                 $this->CI->db->limit($limit,$start);
  108.             }
  109.  
  110.             $result=$this->CI->db->get($table);
  111.             if($result->num_rows() > 0){
  112.                 return $result->result();
  113.             }else{
  114.                 return null;
  115.             }
  116.         }else{
  117.             $this->noTable();
  118.         }
  119.     }
  120.    
  121.     function get_data_not_in($table,$where=array(),$order='',$group='',$limit=null,$start=null){
  122.  
  123.  
  124.         if(!empty($table))  {
  125.             if(!empty($where)){
  126.                 $this->CI->db->where_not_in($where);
  127.             }
  128.  
  129.             if(!empty($order)){
  130.                 $this->CI->db->order_by($order);
  131.             }
  132.  
  133.             if(!empty($group)){
  134.                 $this->CI->db->group_by($group);
  135.             }
  136.  
  137.             if(!empty($limit)){
  138.                 $this->CI->db->limit($limit,$start);
  139.             }
  140.  
  141.             $result=$this->CI->db->get($table);
  142.             if($result->num_rows() > 0){
  143.                 return $result->result();
  144.             }else{
  145.                 return null;
  146.             }
  147.         }else{
  148.             $this->noTable();
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * [addRow Menambahkan data]
  154.      * @param string $table [Nama Table]
  155.      * @param array $data  [Array data]
  156.      */
  157.     function add_row($table,$data=array()){
  158.  
  159.         if(empty($table)){
  160.             $this->noTable();
  161.         }else{
  162.             if(!empty($data))
  163.             {
  164.                 $this->CI->db->insert($table,$data);
  165.                 if($this->CI->db->affected_rows()>0){
  166.                     return true;
  167.                 }else{
  168.                     return false;
  169.                 }
  170.             }else{
  171.                 return false;
  172.             }
  173.         }
  174.     }
  175.    
  176.     function add_row_multiple($table,$data=array()){
  177.  
  178.         if(empty($table)){
  179.             $this->noTable();
  180.         }else{
  181.             if(!empty($data))
  182.             {
  183.                 $this->CI->db->insert_batch($table,$data);
  184.                 if($this->CI->db->affected_rows()>0){
  185.                     return true;
  186.                 }else{
  187.                     return false;
  188.                 }
  189.             }else{
  190.                 return false;
  191.             }
  192.         }
  193.     }
  194.        
  195.  
  196. /**
  197.  * [lastInsertID mengambil ID terakhir setelah menambahkan data]
  198.  * @return integer
  199.  */
  200.     function last_insert_id(){
  201.         $id=$this->CI->db->insert_id();
  202.         return $id;
  203.     }
  204.  
  205. /**
  206.  * [editRow description]
  207.  * @param  string $table [Nama Table]
  208.  * @param  array $data  [array data]
  209.  * @param  array $where [array search]
  210.  * @return boolean        [description]
  211.  */
  212.     function edit_row($table,$data=array(),$where=array()){
  213.         if(empty($table)){
  214.             $this->noTable();
  215.         }else{
  216.             if(!empty($where)){
  217.                 $this->CI->db->where($where);
  218.             }
  219.            
  220.             if(!empty($data))
  221.             {
  222.                 $this->CI->db->update($table,$data);
  223.                 if($this->CI->db->affected_rows()>-1){
  224.                     return true;
  225.                 }else{
  226.                     return false;
  227.                 }
  228.             }else{
  229.                 return false;
  230.             }
  231.         }
  232.     }
  233.  
  234. /**
  235.  * [deleteRow description]
  236.  * @param  string $table [Nama Table]
  237.  * @param  string $where [array where]
  238.  * @return boolean        [description]
  239.  */
  240.     function delete_row($table,$where=array()){
  241.         if(empty($table)){
  242.             $this->noTable();
  243.         }else{
  244.             if(!empty($where)){
  245.                 $this->CI->db->where($where);
  246.             }
  247.  
  248.             $this->CI->db->delete($table);
  249.             if($this->CI->db->affected_rows() > 0){
  250.                 return true;
  251.             }else{
  252.                 return false;
  253.             }
  254.  
  255.         }
  256.     }
  257.  
  258.  
  259. /**
  260.  * [isBOF description]
  261.  * @param  string  $table [Nama Table]
  262.  * @param  string  $where [array where]
  263.  * @return boolean        [description]
  264.  */
  265.     function is_bof($table,$where=array()){
  266.         if(empty($table)){
  267.             $this->noTable();
  268.         }else{
  269.             if(!empty($where)){
  270.                 $this->CI->db->where($where);
  271.             }
  272.            
  273.             $sql = $this->CI->db->get($table);
  274.             if($sql->num_rows() > 0){
  275.                 return false;
  276.             } else {
  277.                 return true;
  278.             }
  279.         }
  280.     }
  281.  
  282. /**
  283.  * [countData description]
  284.  * @param  string $table [Nama Table]
  285.  * @param  string $where [array where]
  286.  * @return integer        [description]
  287.  */
  288.     function count_data($table,$where=array()){
  289.         if(empty($table)){
  290.             $this->noTable();
  291.         }else{
  292.             if(!empty($where)){
  293.                 $this->CI->db->where($where);
  294.             }
  295.             $sql = $this->CI->db->get($table);
  296.             $count=$sql->num_rows();
  297.             return $count;
  298.         }
  299.     }
  300.  
  301. /**
  302.  * [recordCount description]
  303.  * @param  string $table [Nama Table]
  304.  * @param  string $where [array where]
  305.  * @return integer        [description]
  306.  */
  307.     function row_count($table,$where=array()){
  308.         if(empty($table)){
  309.             $this->noTable();
  310.         }else{
  311.             if(!empty($where)){
  312.                 $this->CI->db->where($where);
  313.             }
  314.             $sql = $this->CI->db->get($table);
  315.             $count=$sql->num_rows();
  316.             return $count;
  317.         }
  318.     }
  319.  
  320. /**
  321.  * [fieldRow description]
  322.  * @param  string $table [Nama Table]
  323.  * @param  string $where [array where]
  324.  * @param  string $field [Nama field]
  325.  * @return string        [description]
  326.  */
  327.     function get_row($table,$where=array(),$field,$order=array()){
  328.         if(empty($table)){
  329.             $this->noTable();
  330.         }else{
  331.             if(!empty($order)){
  332.                 $this->CI->db->order_by($order);
  333.             }
  334.             if(!empty($where)){
  335.                 $this->CI->db->where($where);
  336.             }
  337.             $sql = $this->CI->db->get($table);
  338.             if($sql->num_rows() > 0){
  339.                 return $sql->row()->$field;
  340.             } else {
  341.                 return "";
  342.             }
  343.         }
  344.     }
  345.  
  346.     function get_avg_row($table,$where=array(),$field){
  347.         if(empty($table)){
  348.             $this->noTable();
  349.         }else{
  350.             if(!empty($where)){
  351.                 $this->CI->db->where($where);
  352.             }
  353.             $this->CI->db->select_avg($field);
  354.             $sql = $this->CI->db->get($table);
  355.             if($sql->num_rows() > 0){
  356.                 return $sql->row()->$field;
  357.             } else {
  358.                 return "";
  359.             }
  360.         }
  361.     }
  362.  
  363. /**
  364.  * [sumRow Jumlah Sub Total]
  365.  * @param  string $table [Nama Table]
  366.  * @param  string $where [array where]
  367.  * @param  string $field [Nama Field]
  368.  * @return integer        [description]
  369.  */
  370.     function get_sum_row($table,$where=array(),$field){
  371.         if(empty($table)){
  372.             $this->noTable();
  373.         }else{
  374.             if(!empty($where)){
  375.                 $this->CI->db->where($where);
  376.             }
  377.             $this->CI->db->select_sum($field);
  378.             $sql = $this->CI->db->get($table);
  379.             if($sql->num_rows() > 0){
  380.                 return $sql->row()->$field;
  381.             } else {
  382.                 return 0;
  383.             }
  384.         }
  385.     }
  386.  
  387. /**
  388.  * [maxRow nilai tertinggi]
  389.  * @param  string $table [Nama Table]
  390.  * @param  string $where [array where]
  391.  * @param  string $field [Nama Field]
  392.  * @return string        [description]
  393.  */
  394.     function get_max_row($table,$where=array(),$field){
  395.         if(empty($table)){
  396.             $this->noTable();
  397.         }else{
  398.             if(!empty($where)){
  399.                 $this->CI->db->where($where);
  400.             }
  401.             $this->CI->db->select_max($field);
  402.             $sql = $this->CI->db->get($table);
  403.             if($sql->num_rows() > 0){
  404.                 return $sql->row()->$field;
  405.             } else {
  406.                 return "";
  407.             }
  408.         }
  409.     }
  410.  
  411. /**
  412.  * [minRow nilai terendah]
  413.  * @param  string $table [Nama Table]
  414.  * @param  string $where [array where]
  415.  * @param  string $field [Nama Field]
  416.  * @return string        [description]
  417.  */
  418.     function get_min_row($table,$where=array(),$field){
  419.         if(empty($table)){
  420.             $this->noTable();
  421.         }else{
  422.             if(!empty($where)){
  423.                 $this->CI->db->where($where);
  424.             }
  425.             $this->CI->db->select_min($field);
  426.             $sql = $this->CI->db->get($table);
  427.             if($sql->num_rows() > 0){
  428.                 return $sql->row()->$field;
  429.             } else {
  430.                 return "";
  431.             }
  432.         }
  433.     }
  434.  
  435. /**
  436.  * [fetchQuery description]
  437.  * @param  string $sqlQuery [description]
  438.  * @return stdClass           [description]
  439.  */
  440.     function get_query_data($sqlQuery){
  441.         $sql=$this->CI->db->query($sqlQuery);
  442.         if($sql->num_rows()>0){
  443.             $data=$sql->result();
  444.             return $data;
  445.         }else{
  446.             return null;
  447.         }
  448.     }
  449.  
  450. /**
  451.  * [fieldQuery mengambil satu data]
  452.  * @param  string $sqlQuery [Query Native]
  453.  * @param  string $field    [Nama Field]
  454.  * @return string           [description]
  455.  */
  456.     function get_query_row($sqlQuery,$field){
  457.         $sql=$this->CI->db->query($sqlQuery);
  458.         if($sql->num_rows()>0){
  459.             $row=$sql->row();
  460.             return $row->$field;
  461.         }else{
  462.             return "";
  463.         }
  464.     }
  465.  
  466. /**
  467.  * [countQuery Jumlah Data]
  468.  * @param  string $sqlQuery [description]
  469.  * @return integer           [description]
  470.  */
  471.     function count_query($sqlQuery){
  472.         $sql=$this->CI->db->query($sqlQuery);
  473.         $count=$this->CI->db->count_all_results();
  474.         return $count;
  475.     }
  476.  
  477. /**
  478.  * [toolsBackupDB description]
  479.  * @param  string $fileType      [txt,zip,gzip]
  480.  * @param  string $name          [nama file backup]
  481.  * @param  string $tables        [description]
  482.  * @param  boolean $addDrop       [Membuat parameter drop/hapus terlebih dahulu]
  483.  * @param  boolean $addInsert     [Memebuat parameter insert terlebih dahulu]
  484.  * @param  boolean $forceDownload [Paksa download file]
  485.  * @param  string $Location      [lokasi upload jika tidak didownload]
  486.  * @return boolean                [description]
  487.  */
  488.     function backup_db($fileType,$name,$tables=array(),$addDrop=FALSE,$addInsert=FALSE,$forceDownload=TRUE,$Location=''){
  489.         $this->CI->load->dbutil();
  490.         $config = array(
  491.             'tables'        =>$tables,
  492.             'format'        => $fileType,
  493.             'filename'      => $name.'.sql',
  494.             'add_drop'      => $addDrop,
  495.             'add_insert'    => $addInsert,
  496.             'newline'       => "\n"
  497.         );
  498.  
  499.         $backup =$this->CI->dbutil->backup($config);
  500.         $nameBackup=$name.'.'.$fileType;
  501.         if($forceDownload==TRUE){
  502.             $this->CI->load->helper('download');
  503.             force_download($nameBackup,$backup);
  504.             return true;
  505.         }else{
  506.             if($forceDownload==FALSE && empty($Location))
  507.             {
  508.                 return false;
  509.             }else{
  510.                 $this->CI->load->helper('file');
  511.                 write_file($Location.$nameBackup, $backup);
  512.                 return true;
  513.             }
  514.         }
  515.     }
  516.  
  517. /**
  518.  * [toolsOptimizeDB description]
  519.  * @return boolean [description]
  520.  */
  521.     function optimize_db(){
  522.         $this->CI->load->dbutil();
  523.         $result = $this->CI->dbutil->optimize_database();
  524.         return TRUE;
  525.     }
  526.  
  527. /**
  528.  * [toolsRepairTable description]
  529.  * @param  string $table [Nama Table]
  530.  * @return boolean        [description]
  531.  */
  532.     function repair_table($table)
  533.     {
  534.         $this->CI->load->dbutil();
  535.         if ($this->CI->dbutil->repair_table($table))
  536.         {
  537.             return true;
  538.         }else{
  539.             return false;
  540.         }
  541.     }
  542.  
  543. /**
  544.  * [toolsTableExits description]
  545.  * @param  string $table [Nama Table]
  546.  * @return boolean        [description]
  547.  */
  548.     function is_table_exists($table){
  549.  
  550.         if(!empty($table)){
  551.             if($this->CI->db->table_exists($table)){
  552.                 return true;
  553.             }else{
  554.                 return false;
  555.             }
  556.         }else{
  557.             $this->noTable();
  558.         }
  559.  
  560.     }
  561.  
  562. /**
  563.  * [toolsListTable Mengambil daftar table]
  564.  * @return stdClass [description]
  565.  */
  566.     function get_list_table(){
  567.         $output=array();
  568.         $tables = $this->CI->db->list_tables();
  569.         foreach ($tables as $table)
  570.         {
  571.                 $output[]=$table;
  572.         }
  573.         return $output;
  574.     }
  575.  
  576. /**
  577.  * [toolsFieldTable Mengambil info file pada table]
  578.  * @param  string $table [Nama Table]
  579.  * @return stdClass        [description]
  580.  */
  581.     function get_field_table($table){
  582.  
  583.         if(!empty($table)){
  584.             $output=array();
  585.             $fields = $this->CI->db->list_fields($table);
  586.  
  587.             foreach ($fields as $field)
  588.             {
  589.                 $output[]=$field;
  590.             }
  591.             return $output;
  592.         }else{
  593.             $this->noTable();
  594.         }
  595.     }
  596.  
  597. /**
  598.  * [toolsMetadataTable Mengambil info field pada table datatype,length,dll]
  599.  * @param  string $table [Nama Table]
  600.  * @return stdClass        [description]
  601.  */
  602.     function get_meta_table($table){
  603.  
  604.         if(!empty($table)){
  605.             $metadata=$this->CI->db->field_data($table);
  606.             return $metadata;
  607.         }else{
  608.             $this->noTable();
  609.         }
  610.     }
  611.  
  612.  
  613.     /**
  614.      * [forgeCreateDB Membuat database]
  615.      * @param  string $dbname [Nama Database]
  616.      * @return boolean         [description]
  617.      */
  618.     function create_db($dbname){
  619.  
  620.         if(!empty($dbname)){
  621.             $this->CI->load->dbforge();
  622.             if($this->CI->dbforge->create_database($dbname)){
  623.                 return true;
  624.             }else{
  625.                 return false;
  626.             }
  627.         }else{
  628.             die('Kesalahan membuat database');
  629.         }
  630.  
  631.     }
  632.  
  633. /**
  634.  * [forgeDropDB menghapus database]
  635.  * @param  string $dbname [Nama Database]
  636.  * @return boolean         [description]
  637.  */
  638.     function delete_database($dbname){
  639.  
  640.         if(!empty($dbname)){
  641.             $this->CI->load->dbforge();
  642.             if($this->CI->dbforge->drop_database($dbname)){
  643.                 return true;
  644.             }else{
  645.                 return false;
  646.             }
  647.         }else{
  648.             $this->noTable();
  649.         }
  650.  
  651.     }
  652.  
  653. /**
  654.  * [forgeDropTable Menghapus table]
  655.  * @param  string $table [Nama Table]
  656.  * @return boolean        [description]
  657.  */
  658.     function delete_table($table){
  659.         if(!empty($table)){
  660.             $this->CI->load->dbforge();
  661.             if($this->CI->dbforge->drop_table($table)){
  662.                 return true;
  663.             }else{
  664.                 return false;
  665.             }
  666.         }else{
  667.             $this->noTable();
  668.         }
  669.     }
  670.  
  671.  
  672. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement