Sunday, July 18, 2021

React CheckBox

Working with React Bootstrap checkbox


 import React, { PureComponent } from "react";
import Form from "react-bootstrap/Form";

interface CheckBoxState {
  checkedColors: any[];
}

interface CheckBoxProps {
  foo: string;
}

class CheckBox extends PureComponent<CheckBoxProps, CheckBoxState> {
  constructor(props: CheckBoxProps) {
    super(props);
    this.state = {
      checkedColors: [
        { isChecked: false, name: "chk1", label: "blue" },
        { isChecked: false, name: "chk2", label: "red" },
      ],
    };
  }

  render() {
    return (
      <div>
        {this.state.checkedColors.map((item: any) => (
          <Form.Check
            type="checkbox"
            id={item.name}
            inline
            key={item.name}
            checked={item.isChecked}
            onChange={(event: any) => this.handleChange(event)}
            label={item.label}
          />
        ))}
        <button onClick={() => console.log(this.state.checkedColors)}>
          Show checked colors
        </button>

        <hr />
        {this.props.foo}
      </div>
    );
  }

  handleChange(event: any): void {
    const checkedColors = [...this.state.checkedColors];

    checkedColors.map((item: any) => {
      if (item.name === event.target.id) item.isChecked = event.target.checked;
    });

    this.setState({
      checkedColors,
    });
  }
}

export default CheckBox;
Note that this.state.checkedColors can be cloned using spread operator 'cause the array is shallow.

Friday, May 7, 2021

Postgres & Adminer on Docker

Postgres & Adminer on Docker stack.

version: '3'

services:

  postgres:
    image: postgres:13.2-alpine
    restart: always
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_USER: pguser
      POSTGRES_DB: pgdb
    ports:
      - "5432:5432"
    volumes:
      - pg-data:/var/lib/postgresql/data
    networks:
      - pg-network
 

  adminer:
    image: adminer:4.8.0-standalone
    restart: always
    ports:
      - "8080:8080"
    networks:
      - pg-network

networks:
  pg-network:
    driver: bridge

volumes:
  pg-data:
    driver: local
This will set up the two services on the same network and assign a volume for the database.

Thursday, April 15, 2021

TypeScript Question Mark and Exclamation Mark

Here is how to use in combination to determine if property is null or undefined.

interface Product {
    id: number,
    name?: any 
}

let product: Product = {id: 5, name: "some str"};

if(product!.name) console.log("ok")

product = {id: 5}
if(product!.name) console.log("not printed")

product = {id: 5, name: null}
if(product!.name) console.log("not printed")

Second if will not be excecated 'cause property name is not defined. Third if will not be execute 'cause property name is null.

Thursday, February 4, 2021

Oracle Spool

Here is how to create a log file for your script:
SET echo ON
SET feedback ON
SET SERVEROUTPUT ON
SET linesize 500
COL spoolname new_value filename
SELECT TO_CHAR(systimestamp, 'YYYY_MM_DD_HH24_MI_') || 
	(SELECT SYS_CONTEXT ('userenv', 'current_schema') FROM  dual) || 
	'_log_name.log' AS spoolname FROM dual;
SPOOL &filename

-- start main SQL script
INSERT INTO my_table (total) VALUES (25);
-- end main SQL script

SPOOL OFF
EXIT;
This will create a log file with name like this 2019_11_11_23_00_schema_name_log_name.log.

Thursday, January 21, 2021

Oracle Package

Oracle package with procedure, function and global variable

CREATE OR REPLACE PACKAGE my_package AS
    g_fav_num NUMBER := 5;
    PROCEDURE my_proc (val INTEGER);
    FUNCTION  double_func (in_val IN NUMBER) RETURN NUMBER; 
END my_package;
/

CREATE OR REPLACE PACKAGE BODY my_package ASPROCEDURE my_proc (val INTEGER) IS
  BEGIN
      g_fav_num := g_fav_num + val;
      INSERT INTO my_table ( total ) VALUES ( double_func(g_fav_num) );
  END;

  FUNCTION double_func (in_val IN NUMBER) RETURN NUMBER IS 
  BEGIN 
    RETURN in_val*2;
  END;

END my_package;
/

exec my_package.my_proc(1000);

Thursday, December 17, 2020

Logging Table in Oracle

In Oracle 12c  you create table:

CREATE TABLE msg_log (
    id            NUMBER
        GENERATED BY DEFAULT ON NULL AS IDENTITY,
    ts            TIMESTAMP WITH LOCAL TIME ZONE,
    schema_name   VARCHAR2(15),
    message       VARCHAR2(250)
);

 Create procedure:

CREATE OR REPLACE PROCEDURE log_message (
    message IN VARCHAR2
) AS
    PRAGMA autonomous_transaction;
    schema_name VARCHAR2(15);
BEGIN
    SELECT
        sys_context('userenv', 'current_schema')
    INTO schema_name
    FROM
        dual;

    INSERT /*+ APPEND */ INTO msg_log (
        ts,
        schema_name,
        message
    ) VALUES (
        SYSDATE,
        schema_name,
        message
    );

    COMMIT;
END log_message;
Test
EXEC log_message('foo');

SELECT
    id,
    TO_CHAR(ts, 'YYYY-MM-DD HH24:MM:SS') AS log_date,
    message
FROM
    msg_log; -- where ts > '17-DEC-20 05.05.52.000000000 PM';

Wednesday, December 9, 2020

Oracle Stored Procedure

Sample of Oracle stored procedure with in & out params & exceptions.

CREATE OR REPLACE PROCEDURE proc_name (
  p1 IN VARCHAR2, 
  p2 OUT VARCHAR2
) 
AS 
  msg VARCHAR2(30) := 'hello ';
  ex_some_exception EXCEPTION;

BEGIN 
  IF length(p1) < 2 THEN RAISE ex_some_exception;
  END IF;
  p2 := msg || p1;
EXCEPTION 
  WHEN ex_some_exception THEN raise_application_error(-20001, 'name to short');
  WHEN OTHERS THEN raise_application_error(-20002, 'An error was encountered');
END proc_name;
/

Testing in & out params.

SET SERVEROUTPUT ON
 
DECLARE 
  full_hello VARCHAR(50);
BEGIN 
  proc_name('Mike', full_hello);
  DBMS_OUTPUT.put_line(full_hello);
END;
/