martes, 29 de septiembre de 2009

Simple oracle cursor loop example

Intro
The script will create all the needed objects for this example, I assume you have a user with the proper permisions to execute the example.

Script
/*This anonymous pl/sql block is to drop the table, so anytime we want to run the script it will run without problems*/
BEGIN
--drop the table
EXECUTE IMMEDIATE ('DROP TABLE number_text_pair');
--if there is some problems like it is the first time so there is no table to drop just do nothing
EXCEPTION WHEN OTHERS THEN NULL;
END;
/

/*create the table*/
CREATE TABLE number_text_pair
(
number_value integer,
text_value varchar2(4000)
);


/*create some test data*/
INSERT INTO number_text_pair (number_value, text_value) VALUES (1,'one');
INSERT INTO number_text_pair (number_value, text_value) VALUES (2,'two');
INSERT INTO number_text_pair (number_value, text_value) VALUES (3,'tree');

/*anonymous pl/sql block for iterating number_text_pair's data with a cursor*/
DECLARE
--declare the cursor
CURSOR cur_example is
SELECT *
FROM number_text_pair;
/*create a record to store the data of the table,
note the datatype of the record as row type of cur_example*/
rec_example cur_example%ROWTYPE;
BEGIN
--open the cursor to iterate it
OPEN cur_example;
--loop until there is a condition to stop
LOOP
--fetch a row data from the cursor and store it in rec_example
FETCH cur_example INTO rec_example;
--if there is no more data in the cursor then exit
EXIT WHEN cur_example%NOTFOUND;
/*output the values of the cursor to the console
note the use of . to access the fields of the record*/
DBMS_OUTPUT.put_line('Number value is: ' || rec_example.number_value || ' text value is: ' || rec_example.text_value);
END LOOP;
--close the cursor
CLOSE cur_example;
END;
/
/*the slash is to instruct the program running the script to continue after a pl/sql block,
it is not necessary when you are executing a DML statement*/
Script output
After executing the script you should see

Number value is: 1 text value is: one
Number value is: 2 text value is: two
Number value is: 3 text value is: tree
A simpler and more robust version
There is another construction to iterate over cursors, it is the for construction, a simpler more robust pl/sql to iterate the cursor is:
/*anonymous pl/sql block for iterating number_text_pair's data with a cursor*/
DECLARE
--declare the cursor
CURSOR cur_example is
SELECT *
FROM number_text_pair;
BEGIN
/*note the simpler construction, there is no need to open and close the cursor,
also there is no need to declare the record*/
FOR rec_example IN cur_example LOOP
DBMS_OUTPUT.put_line('Number value is: ' || rec_example.number_value || ' text value is: ' || rec_example.text_value);
END LOOP;
END;
/

sábado, 26 de septiembre de 2009

Replace on file with another in other directory in linux

Story

It's a long story, the thing is that my documents filesystem became corrupted and there were a lot of song that were not able to be reproduced.
I didn't know which songs were until I played them so do it by hand was not practical.
I get to create to scripts that allow me to replace all my favorites songs in my music folder with a backup of the song in a different folder.
There were some restrictions
  • The songs had to have the same name
  • The song to be replaced and the copy can be in diferent folders
The scripts

the solution is composed of two parts, a command that I run in console and a script, the command is

find -type f -exec ./replaceFiles "{}" \;

the script with name replaceFiles is

cp "$(find /another/directory/ | grep $basename "$1")" "$1" -v

How they work

When you first execute the command, for every file in the directory it is runned in it calls the script replaceFiles passing as parameter a file in the directory, the script search for the file within another directory by its name (basename "$1"), if it founds it then it copies over the original file "$1".

miércoles, 23 de septiembre de 2009

The future of cell phone

Is taking some time to people to realize that the social networking applications are not adds to their cellphones and computers but that the latter are the medium to former.
And as computers (that is a cellphone) continue to shrink the medium will become invisible and everybody will be connected.
An universally society universally connected, always...

Intel i7 turbo boost

People at marketing are genius or the average buyer is stupid.
So instead of selling a 3ghz processor that can run at 2ghz to save power they sell a 2ghz processor that can operate in turbo mode at 3ghz and everybody drops their jaws.
Tell me is just me.