Get last order number with PostgreSQL trigger

postgresqlIn multi user applications it is not easy to serve a unique order or invoice numbers for each save process. You should check carefully several times on every database insert. But with database triggers you can get last order or invoice number easily as described below.

I have used PostgreSQL 9.3.5 version on Ubuntu for this trigger.

Create order table:

-- order table sequence
DROP SEQUENCE IF EXISTS order_order_id_seq CASCADE;
CREATE SEQUENCE order_order_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

ALTER TABLE public.order_order_id_seq OWNER TO postgres;

-- order table
DROP TABLE IF EXISTS order CASCADE;
CREATE TABLE order (
  order_id bigint DEFAULT nextval('order_order_id_seq'::regclass) NOT NULL,
  order_number bigint,
  order_date timestamp without time zone NOT NULL
  -- add other fields
);

ALTER TABLE public.order OWNER TO postgres;

Create params table. The parameters table has last order number;

-- params table sequence
DROP SEQUENCE IF EXISTS params_params_id_seq CASCADE;
CREATE SEQUENCE params_params_id_seq
  INCREMENT BY 1
  NO MAXVALUE
  NO MINVALUE
  CACHE 1;

ALTER TABLE public.params_params_id_seq OWNER TO postgres;

-- params table
DROP TABLE IF EXISTS params CASCADE;
CREATE TABLE params (
  params_id bigint DEFAULT nextval('params_params_id_seq'::regclass) NOT NULL,
  last_order_number bigint
  -- add other fields
);

ALTER TABLE public.params OWNER TO postgres;

Create trigger:

-- get last order number function
CREATE OR REPLACE FUNCTION get_last_order_number()
    RETURNS TRIGGER AS
$BODY$
DECLARE
    v_last_order_number bigint;
BEGIN
  SELECT last_order_number INTO v_last_order_number FROM params WHERE params_id = 1;
  UPDATE params set last_order_number = v_last_order_number + 1 WHERE params_id = 1;
  NEW.order_number = v_last_order_number + 1;
  RETURN NEW;
END $BODY$
    LANGUAGE plpgsql;

ALTER FUNCTION public.get_last_order_number() OWNER TO postgres;

-- last order number trigger
CREATE TRIGGER last_order_number_trigger BEFORE INSERT ON order
  FOR EACH ROW EXECUTE PROCEDURE get_last_order_number();

Wi-Fi connection problem on Ubuntu 14.04

ubuntu

I have installed Ubuntu 14.04 (Thusty Tahr) and Wi-Fi scans the networks and connects correctly, but Mozilla Firefox or Google Chrome can’t open websites. It works with cable connection trouble-free.

I looked for a solution long time on the Internet. Then I found that there is a bug in Ubuntu Network Manager. Because of this bug Ubuntu Network Manager takes Bit Rate=1 Mb/s for wlan0.

Solution:

iwconfig wlan0 rate 54M

Make this automatically for every system start:

sudo gedit /etc/rc.local
# add the following line before exit 0 line, save and exit
iwconfig wlan0 rate 54M

Reference: askubuntu.com

Install jdk 8 on Ubuntu

java logo

Ubuntu versions:

  • Ubuntu GNU/Linux 14.04 (Trusty Tahr)
  • Ubuntu GNU/Linux 13.10 (Saucy Salamander)
  • Ubuntu GNU/Linux 13.04 (Raring Ringtail)

Installing Oracle JDK 8 on Ubuntu:

  • Download the Oracle JDK 8 tar file from here
  • Extract the tar file: 
tar -xvzf jdk-8-linux-x64.tar.gz
  • Create jvm folder in /usr/lib, if jvm folder not exist
sudo mkdir /usr/lib/jvm
  • Move extracted jdk 8 folder to this location:
sudo mv jdk1.8.0 /usr/lib/jvm/jdk1.8.0
  • Install new java source in system:
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0/bin/java 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.8.0/bin/javaws 1
  • Choose default java:
sudo update-alternatives --config javac
sudo update-alternatives --config java
sudo update-alternatives --config javaws
  • Java version test:
java -version

# java version "1.8.0"
# Java(TM) SE Runtime Environment (build 1.8.0-b132)
# Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
  • Verify the symlinks all point to the new java location:
ls -la /etc/alternatives/java*
  • Enable Java plugin for Mozilla Firefox (even for Chrome)
# for 64-Bit jdk
sudo ln -s /usr/lib/jvm/jdk1.8.0/jre/lib/amd64/libnpjp2.so /usr/lib/mozilla/plugins
# for 32-Bit jdk
sudo ln -s /usr/lib/jvm/jdk1.8.0/jre/lib/i386/libnpjp2.so /usr/lib/mozilla/plugins
  • JAVA_HOME configuration: Some tools require JAVA_HOME variable. You can set JAVA_HOME on Ubuntu either:
    1. With /etc/environment:
    2. sudo gedit /etc/environment
      # add the following line, save and exit
      JAVA_HOME=/usr/lib/jvm/jdk1.8.0
    3. Or in your home directory with .bashrc:
    4. sudo gedit .bashrc
      # add the following lines, save and exit
      export JAVA_HOME=/usr/lib/jvm/jdk1.8.0
      export PATH=$JAVA_HOME/bin:$PATH