Postgres Firsttime install

Postgres first time install
— sudo -u postgres psql template1
— ALTER USER postgres with encrypted password ‘passswordbaru’;

— sudo vim /etc/postgresql/9.6/main/pg_hba.conf
update as below
# Database administrative login by Unix domain socket
#local all postgres peer
local all postgres md5

—Restart postgres service
sudo /etc/init.d/postgresql restart

—Create a user having the same name as you (to find it, you can type whoami) :

createuser -U postgres -d -e -E -l -P -r -s $USER

The options tell postgresql to create a user that can login, create databases, create new roles, is a superuser &
will have an encrypted password. The really important ones are -P -E, so that you’re asked to type the password that
will be encrypted -d so that you can do a createdb.

First & Last day in Postgresql

–FirstDay
CREATE OR REPLACE FUNCTION fn_GetFirstDayOfMonth(DATE)
RETURNS DATE AS
$$
SELECT (date_trunc(‘MONTH’, $1))::DATE;
$$ LANGUAGE ‘sql’
IMMUTABLE STRICT;

–LastDay
CREATE OR REPLACE FUNCTION fn_GetLastDayOfMonth(DATE)
RETURNS DATE AS
$$
SELECT (date_trunc(‘MONTH’, $1) + INTERVAL ‘1 MONTH – 1 day’)::DATE;
$$ LANGUAGE ‘sql’
IMMUTABLE STRICT;

How to use –>
SELECT * FROM fn_GetLastDayOfMonth(‘2016-07-03’::DATE);
OR
SELECT * FROM fn_GetLastDayOfMonth(‘20160703’);

Partitioning table in PostgreSQL

Share about how to create partition in PosgrestDB.

Master table

CREATE TABLE IF NOT EXISTS stock_trx (
id_tickler varchar(8) NOT NULL,
dt_trx date NOT NULL,
open_prc float DEFAULT NULL,
high_prc float DEFAULT NULL,
low_prc float DEFAULT NULL,
close_prc float DEFAULT NULL,
vol_trx float DEFAULT NULL,
openint float DEFAULT NULL,
PRIMARY KEY (id_tickler, dt_trx)
);

Then create child table

CREATE TABLE stock_trx_2010 (
CHECK (dt_trx >= ‘2010-01-01’ AND dt_trx <= ‘2010-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2011 (
CHECK (dt_trx >= ‘2011-01-01’ AND dt_trx <= ‘2011-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2012 (
CHECK (dt_trx >= ‘2012-01-01’ AND dt_trx <= ‘2012-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2013 (
CHECK (dt_trx >= ‘2013-01-01’ AND dt_trx <= ‘2013-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2014 (
CHECK (dt_trx >= ‘2014-01-01’ AND dt_trx <= ‘2014-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2015 (
CHECK (dt_trx >= ‘2015-01-01’ AND dt_trx <= ‘2015-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2016 (
CHECK (dt_trx >= ‘2016-01-01’ AND dt_trx <= ‘2016-12-31’ )
)inherits(stock_trx);

CREATE TABLE stock_trx_2017 (
CHECK (dt_trx >= ‘2017-01-01’ AND dt_trx <= ‘2017-12-31’ )
)inherits(stock_trx);

Create sequence for each child table

CREATE SEQUENCE stock_trx_2010_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2011_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2012_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2013_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2014_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2015_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2016_seq start with 1 increment by 1 minvalue 1 cache 1;
CREATE SEQUENCE stock_trx_2017_seq start with 1 increment by 1 minvalue 1 cache 1;

Then below are trigger & procedure to handle the partition table

CREATE OR REPLACE FUNCTION stocktrx_insert_trigger()
RETURNS TRIGGER AS $$
DECLARE r stock_trx%rowtype;
BEGIN
IF (NEW.dt_trx >= ‘2010-01-01’ AND NEW.dt_trx <= ‘2010-12-31’ ) THEN
INSERT INTO stock_trx_2010 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2011-01-01’ AND NEW.dt_trx <= ‘2011-12-31’ ) THEN
INSERT INTO stock_trx_2011 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2012-01-01’ AND NEW.dt_trx <= ‘2012-12-31’ ) THEN
INSERT INTO stock_trx_2012 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2013-01-01’ AND NEW.dt_trx <= ‘2013-12-31’ ) THEN
INSERT INTO stock_trx_2013 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2014-01-01’ AND NEW.dt_trx <= ‘2014-12-31’ ) THEN
INSERT INTO stock_trx_2014 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2015-01-01’ AND NEW.dt_trx <= ‘2015-12-31’ ) THEN
INSERT INTO stock_trx_2015 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2016-01-01’ AND NEW.dt_trx <= ‘2016-12-31’ ) THEN
INSERT INTO stock_trx_2016 VALUES (NEW.*) RETURNING * INTO r;
ELSEIF (NEW.dt_trx >= ‘2017-01-01’ AND NEW.dt_trx <= ‘2017-12-31’ ) THEN
INSERT INTO stock_trx_2017 VALUES (NEW.*) RETURNING * INTO r;
ELSE
RAISE EXCEPTION ‘Date out of range. Fix the measurement_insert_trigger() function!’;
END IF;
RETURN r;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER insert_stocktrx_trigger
BEFORE INSERT ON stock_trx
FOR EACH ROW EXECUTE PROCEDURE stocktrx_insert_trigger();

CREATE OR REPLACE FUNCTION stock_trx_delete_master() RETURNS trigger
AS $$
DECLARE
r stock_trx%rowtype;
BEGIN
DELETE FROM ONLY stock_trx where id_tickler = new.id_tickler AND dt_trx = new.dt_trx returning * into r;
RETURN r;
end;
$$
LANGUAGE plpgsql;

— Create the after insert trigger
create trigger after_insert_stock_trx_trigger
after insert on stock_trx
for each row execute procedure stock_trx_delete_master();

Once done you might use for insert single row or batch insert/update.
Enjoy it & hopefully can help you to solve your partition table on postgres.

Regards,
Afif

Basic plugin Laravel4 for me

if you are familiar with laravel, then below step should not be any concern for you. just to the point,
Made some changes on composer.json & add below requirement below require component

“barryvdh/laravel-dompdf”: “dev-master”,
“phpoffice/phpexcel”: “dev-master”,
“bllim/datatables”: “*”,
“chumper/datatable”: “dev-master”,
“edvinaskrucas/notification”: “3.*”

then you need to update your plugin with composer.
composer update && composer self-update && dumpautoload -o

then open your app.php & add this line into your file

in require component

'Bllim\Datatables\DatatablesServiceProvider',
'Chumper\Datatable\DatatableServiceProvider',
'Barryvdh\DomPDF\ServiceProvider',
'Krucas\Notification\NotificationServiceProvider'

in Alias component


'Datatables' => 'Bllim\Datatables\Facade\Datatables',
'Datatable' => 'Chumper\Datatable\Facades\DatatableFacade',
'PDF' => 'Barryvdh\DomPDF\Facade',
'Excel' => 'PHPExcel',
'Notification' => 'Krucas\Notification\Facades\Notification'

update app.php done.

then you need to publish your plugin

php artisan config:publish chumper/datatable
php artisan config:publish bllim/datatables
php artisan config:publish barryvdh/laravel-dompdf
php artisan config:publish edvinaskrucas/notification

Add plugin done (Excel mana?)

Reset router to default

This note is only for ME :p just in case if I forget how to reset Cisco router into default.

en
sh ver
conf t
config-register xxxx
write erase
reload (n)
confirm

Web Spring: Implement HTML template with sitemesh

For those who asking how to create html template in spring frame work, here is my notes 🙂
Need : Having template for all web page in java web portal (using Spring framework)
Answer : Use sitemesh library for templating

Steps:
create web project as normal then follow below steps

*- Download sitemesh library in here
*- Copy/add the jar files into your Libraries directory
*- Create a folder inside WEB-INF (I named as decorators), in this folder then create master jsp/html template, I was created master file (app-theme.jsp) as below

<%-- 
    Document   : app-theme
    Created on : Jan 8, 2013, 11:30:39 AM
    Author     : NUSNAFIF
--%>

<?xml version="1.0" encoding="UTF-8" ?>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <LINK REL="SHORTCUT ICON" href="assets/img/favicon.ico" type="image/x-icon"> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="assets/css/style.css" rel="stylesheet" type="text/css" />
    <title><decorator:title default="Welcome!" /></title>
    
    <style type="text/css">
    label.valid {
      width: 24px;
      height: 24px;
      background: url(assets/img/valid.png) center center no-repeat;
      display: inline-block;
      text-indent: -9999px;
    }
    label.error {
      font-weight: bold;
      color: red;
      padding: 2px 8px;
      margin-top: 2px;
    }

    </style>
    
</head>
<body>
    <!-- Header -->
    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
            <a class="brand" href="">App Portal</a>          
          <div class="nav-collapse">
            <ul class="nav">
              <li><a href="."><i class="icon-home icon-white"></i> Home</a></li>
              <li><a href="${base}/about.htm">About</a></li>
              <li><a href="${base}/contact.htm">Contact</a></li>
            </ul>
              <ul class="nav pull-right">
                  <li class="dropdown">
                      <a class="dropdown-toggle" href="#" data-toggle="dropdown">Sign In <strong class="caret"></strong></a>
                            <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
                                <!-- Login form here -->
                                <form action="" method="post" accept-charset="utf-8">
                                <input style="margin-bottom: 15px;" type="text" id="username" name="username"  placeholder="Username" size="30" />
                                <input style="margin-bottom: 15px;" type="password" id="password" name="password" placeholder="Password" size="30" />                                
                                <input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size: 13px;" type="submit" name="commit" value="Sign In" />
                                </form>
                            </div>
                  </li>
              </ul> <!-- ul Pull right -->
              
          </div><!-- /.nav-collapse -->
        </div><!-- /.container -->
      </div><!-- /navbar-inner -->
    </div><!-- /navbar -->  
        
    <div class="container">
        <decorator:body /> 
        ${body}
    </div>
    <footer class="footer">
      <div class="container">      
        <p class="pull-right"><a href="#">Back to top</a></p>
        <p id="footer">App Portal Version 0.1 &copy;  <a href="${base}">App</a> All Rights Reserved</p>
      </div>
        
        
        <script type="text/javascript" src="${base}/assets/js/jquery-1.8.2.js"></script>
        <script type="text/javascript" src="${base}/assets/js/jquery-1.8.2.min.js"></script>
        <script type="text/javascript" src="${base}/assets/js/jquery-ui.js"></script>
        <script type="text/javascript" src="${base}/assets/js/jquery.validate.min.js"></script>
        <script type="text/javascript" src="${base}/assets/js/bootstrap.js"></script>
        <script type="text/javascript" src="${base}/assets/js/jquery.dataTables.js"></script>
        <script type="text/javascript" src="${base}/assets/js/DT_bootstrap.js"></script>
    </footer>
    
</body>
</html>

dont forget to include your css/img/js files to your project, these files I put into web/assets folder.

*- create decorators.xml as below to determine your html template.

<decorators defaultdir="/WEB-INF/decorators">
 <decorator name="app-theme" page="app-theme.jsp">
    <pattern>*.htm</pattern>
 </decorator>  
</decorators>

*- Then update your web.xml file to tell spring framework that you gonna use HTML template with sitemesh library. just update your bottom web.xml file as below

     <filter>
      <filter-name>sitemesh</filter-name>
         <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
      </filter>    
     <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>              

build & deploy your project then test run your project, Good Luck.
anyway I also host this project into my github
Enjoy it!

Populate select box data with Codeigniter, Ajax, Mysql

I would like to give shot about how to populate data from Database in. In here I used Codeigniter  as PHP Framework, MySQL as DB and Jquery as JS Framework.

I have 1 table (t_unit) with 2 fields (id_unit, unit_name). some how in another form I would like to capture id_unit into another table, then I should populate them into another form, so here are the details

This is my Model

function fetch_unit() {
 
  $this->db->select('id_unit, unit_name');
  $records=$this->db->get('t_unit');
  $data=array();
 
  //$data[0] = 'SELECT';
  foreach ($records->result() as $row)
  {
    $data[$row->id_unit] = $row->unit_name;
  }
  return ($data);
}

from above code, I will set id_unit as value and unit_name as display value.if we use Codeigniter as our framework then we should able to display dataset in view with very easy step.
here are the controller

$data['unit'] = $this->cdd_model->fetch_unit();
$data['title'] = 'Create CDD';
$this->load->view('home/index', $data);

then in your view you just use form_dropdown class to populate it

<div class="control-group">
  <label class="control-label" for="cmpgn_unit" >Business Unit</label>
  <div class="controls"> <?php echo form_dropdown('cmpgn_unit', $unit, 1,'id="cmpgn_bis_unit"'); ?>
  </div>
</div>

Done!. you got data from DB into your select box.

we also able to populate it via jQuery like below (use same model like above, just modify lil bit your controller):

function fetch_unit() {
  $unit = $this->cdd_model->fetch_unit();
  echo json_encode($unit);
}

we use json to send parameter into view file. then from view file write down code like this:

<div class="control-group">
  <label class="control-label" for="cmpgn_bis_unit" >Business Unit</label>
  <div class="controls">
    <select id="sUnit" name="cmpgn_unit"><option value="">Please Select</option></select>
  </div>
</div>

and if you want to call via ajax then you do it like below to call data.

//Unit Select Box
$.post("co/ctrl_cdd/fetch_unit", function(data) {
 var sel = $("#sUnit");
 sel.empty();
 $.each(data, function(i, item){
   $("#sUnit").append(''+ item + '');
  })
 },"json");

its Done!!!, pls take a look the id ‘#sUnit, as this id will get data from DB via ajax. Enjoy the code.

Rgds,
Afif

Hello world!

My First writing with wordpress… 🙂