Thursday, July 9, 2015

PostgreSQL: copy table

select * into archive.radius_cdr_pdp_p2015_07_09 from ods_db.radiucdr_pdp_p2015_07_09;

Monday, June 29, 2015

PostgreSQL: get function source code

select pg_get_functiondef(pg_proc.oid) from pg_proc, pg_namespace  where proname = 'add_cdr_req' and pg_namespace.oid = pg_proc.pronamespace and pg_namespace.nspname = 'crm_db';

Displaying current path in the window title with byobu on centos 6

The problem with CentOS 6, is that the tmux version that comes from the standard repo. is v1.6, and we need at least 1.8 to achieve the result.
So install tmux from the latest sources like explained here:
# download latest libevent2 and tmux sources, and extract them somewhere
# (thx bluejedi for tip on latest tmux URL)
#
# at the time of writing:
# https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
# http://sourceforge.net/projects/tmux/files/latest/download?source=files
# 
 
# install deps
yum install gcc kernel-devel make ncurses-devel
 
# cd to libevent2 src
./configure --prefix=/usr/local
make && make install
 
# cd to tmux src
LDFLAGS="-L/usr/local/lib -Wl,-rpath=/usr/local/lib" ./configure --prefix=/usr/local
make && make install
 
# you're good to go, for a sample ~/.tmux.conf check out
# https://github.com/sturadnidge/misc/blob/master/.tmux.conf

NB: Kill old tmux sessions before running byobu again.
And then use the following setting in your $HOME/.byobu/.tmux.conf

set -g status-left '#{pane_current_path} '

Monday, May 18, 2015

Tuesday, April 28, 2015

byobu: Show pwd in prompt

Enable prompt
byobu-enable-prompt
Disable prompt
byobu-disable-prompt

Friday, April 24, 2015

mod_perl: How to send a custom content type along with custom_response

By default, mod_perl Apache2::Response::custom_response will use the following content-type: text/html; charset=iso-8859-1.
But, what if we want to use e.g. application/x-www-form-urlencoded; charset=utf-8 instead?

Main handler:
sub handler {
    my ($i_request_rec) = @_;

    @{$i_request_rec->pnotes}{'err_txt', 'err_content_type'} = ('appid=45465&status=-42', 'application/x-www-form-urlencoded; charset=utf-8');
    $i_request_rec->custom_response(Apache2::Const::SERVER_ERROR, '/error_vas/');
    return Apache2::Const::SERVER_ERROR;
}
Apache conf.:
<Location /error_vas/>
        SetHandler perl-script
        PerlResponseHandler Toto::ErrorVas
</Location>
ErrorVas.pm:
package Toto::ErrorVas;

use strict;
use warnings;
use Apache2::Const -compile => qw(:common :log);

sub handler {
    my ($i_request_rec) = @_;

    return Apache2::Const::NOT_FOUND unless $i_request_rec->prev;

    $i_request_rec->content_type($i_request_rec->prev->pnotes->{err_content_type});
    $i_request_rec->print($i_request_rec->prev->pnotes->{err_txt});

    return Apache2::Const::OK;
}
source: http://foertsch.name/ModPerl-Tricks/custom-content_type-with-custom_response.shtml

perl: setting multiple hash values at once

perl -MData::Dumper -e '@{$h}{qw/toto tata/} = (34, 56); print Dumper $h;'              
$VAR1 = {
          'tata' => 56,
          'toto' => 34
        };

Thursday, April 23, 2015

perl: posting to multihomed hostname

What if you want to post to a hostname resolving to multiple IP addresses?
    local @LWP::Protocol::http::EXTRA_SOCK_OPTS = (
                                                   PeerAddr   => $my_hostname,
                                                   MultiHomed => 1,
                                                  );
    my $ua = LWP::UserAgent->new;
    my $response = $ua->post("https://$my_hostname/action",
                             Content_Type => 'application/x-www-form-urlencoded; charset=utf-8',
                             Content      => {
                                              param1 => 'value1',
                                              param2 => 'value2',
                                             });

php/oci-extension: missing leading zero

Reading numbers from Oracle in PHP results in missing leading 0s if -1 < value < 1.
The reason for this behavior is because Oracle OCI omits 0s in results and PHP converts all results from OCI to strings without performing any casting depending on column datatype.

A simple workaroud consists in converting to float:
$a = '.63';
echo (float) $a;
source: http://stackoverflow.com/questions/4284571/php-oci-oracle-and-default-number-format

Oracle alternative:
REGEXP_REPLACE(TO_CHAR(x), '^\.', '0.')
or if we handle negative numbers
REGEXP_REPLACE(TO_CHAR(x), '(-?)^\.', '\10.')

Wednesday, April 22, 2015

perl: print module version

perl -MLWP::UserAgent -e 'print $LWP::UserAgent::VERSION'

Thursday, April 9, 2015

HTML: POST array not showing unchecked checkbox

<td>
  <input type="checkbox" name="cb[]"/>
  <input type="checkbox" name="cb[]"/>
</td>
  • Solution 1) Explicit index + hidden trick
  • <td>
      <input type="hidden" name="cb[0]" value=""/>
      <input type="checkbox" name="cb[0]"/>
      <input type="hidden" name="cb[1]" value=""/>
      <input type="checkbox" name="cb[1]"/>
    </td>
  • Solution 2) Explicit index + fill in the blanks (e.g. with PHP)
  • <td>
      <input type="checkbox" name="cb[0]"/>
      <input type="checkbox" name="cb[1]"/>
    </td>
    $post_array = $_POST['cb'];
    $defaults = array_fill(0, 2, '0');
    $post_array = $post_array + $defaults;

Friday, March 27, 2015

PostgreSQL: perl like join

db=# select array_to_string(array['a', 'b', 'c'], '-');                                                                     
 array_to_string 
-----------------
 a-b-c
(1 row)

PostgreSQL: date to epoch

select extract(epoch from localtimestamp(0));

Thursday, March 26, 2015

PostgreSQL: cursor example

create or replace function test_cursor() returns void as
$$
declare
    _my_cursor(p_co_id text) for
        select * from crm_db.ht_co_addon where co_id = p_co_id;
    _idx int := 0;
    _row record;
begin
    -- Example 1
    for _row in _my_cursor('000000000666') loop
        _row.co_addon_id := _idx;
        _idx := _idx + 1;
    end loop;

    -- Example 2
    open _my_cursor('000000000666')
    loop
        fetch _my_cursor into _row
        exit when not found
        raise notice 'co_addon_id=%', _row.co_addon_id
    end loop
    close _my_cursor
end;
$$ language plpgsql security definer;

PostgreSQL: hstore or a perl like hash table

create or replace function test_hstore() returns void as
$$
declare
    _my_cursor(p_co_id text) for
        select * from crm_db.ht_co_addon where co_id = p_co_id;
    _row record;
    _addon_id2co_addon_id_h hstore;
    _sms_ao text := 'SMS_AO';
begin
    for _row in _my_cursor('000000000666') loop
        if _addon_id2co_addon_id_h is null then
            _addon_id2co_addon_id_h := (_row.addon_id||'=>'||_row.co_addon_id)::hstore;
        else
            _addon_id2co_addon_id_h := _addon_id2co_addon_id_h || (_row.addon_id||'=>'||_row.co_addon_id)::hstore;
        end if;
    end loop;

    raise notice 'SMS_AO: %', _addon_id2co_addon_id_h->_sms_ao;

    for _row in select * from each(_addon_id2co_addon_id_h) loop
        raise notice 'h: %=>%', _row.key, _row.value;
    end loop;
end;
$$ language plpgsql security definer;

Friday, March 20, 2015

Linux: remove files older than x days

E.g. Remove files older than 90 days
find . -mtime +90 -exec rm {} \;

Thursday, March 19, 2015

Git merge master into feature branch

Source: http://stackoverflow.com/questions/16955980/git-merge-master-into-feature-branch

You should be able to rebase your branch on master:
git checkout feature1
git rebase master
Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with
git rebase --skip
If you perform a git log on your feature branch, you'll see the bugfix commit appear only once, and in the master portion.

git: change remote repository

Context: I have used bitbucket as a remote git repository so far, and now I want to switch to a corporate git server.

Following the instructions detailled here: http://www.smashingmagazine.com/2014/05/19/moving-git-repository-new-server/
Here are the steps eventually run:
git fetch origin
git remote add new-origin ssh://john.smith@192.168.2.3/volume1/git-server/is.git
git pull new-origin master
git push --all new-origin
git push --tags new-origin
git remote rm origin
git remote rename new-origin origin