Sunday, January 17, 2010

Include a shell file into another one

It is a easy task indeed. Suppose, we have two shell script file in same directory. These are firstShell.sh and secondShell.sh. To include the first script in the second, we just need to write the following statement in the second shell script -

source firstShell.sh

If the fistShell.sh is not in same directory then we need to refer the script with full path -

source /path/firstShell.sh

One more thing is if the included shell script requires some arguments to execute, you can pass the arguments where you refer the script. So if the first shell requires some arguments, you have to write -

source /path/firstShell.sh arg1 arg2 ...

Thursday, December 10, 2009

Building own ORM tool using .net Reflection

All professional .net programmer know about System.Reflection. Through it, you can extract meta information of a class or assembly. Using reflection testing tools, ORM tools etc. can be developed.

Here is a sample how you can develop your own ORM tool using .net Reflectin. It is similar to LINQ and obviously not at all so powerful like LINQ. But it has the superiority over the LINQ in the sense that it can run on any Database. Suppose you have a class Student which has properties like public long Student_Id, public string Student_Name. Like LINQ you must have a table Table Students which has columns student_id number, student_name varchar2(256).

So, if you want to get the list of Student from Database to application you need to use C# generic. Steps are as follows -

1. Use typeof() to get object type.

2. List it properties from the Type object using GetProperties() of type object.

3. Generate sql query - class name maps the table name and property names map the column names.

4. Get the data from database.

5. Invoke set method of the object to set property to the database column values. Example code can be like this -
MethodInfo method = _methodList.FirstOrDefault(m => m.Name == "set_" + property.Name);
method.Invoke(typedObject, paramObjects);
6. Return the list.

This is the high-level algorithm of this simple ORM.

Monday, October 12, 2009

Data validation using awk command

One common use of patterns in awk is for simple data validation task. Specially in ETL process, you can use it for source data cleansing.

In shell scripting, usually =~ operator is used for regular expression matching. There is a nice guide for advance shell script. Point 17 describes regular expression.

But in awk command ~ is used for matching. Suppose we want to filter a file which contains line delimited by "|" and print only those line whose second column contains "*8999*". So the awk command will be -

$ awk -F '|' 'if(($2 ~ "\*8999\*")){print NR,"|",$0;}}' $file

Thus, you can validate data using awk command. This is one of the great feature of awk command.

Saturday, September 12, 2009

Reset password in oracle

At first log in the server where oracle is installed using administrative account of the OS. Then set the ORACLE_SID user variable to the name of database service. So, if your database service name is orcl, then set the variable ORACLE_SID to value orcl. Then -

Log in sqlplus from command prompt of the server using the following command-

sqlplus "/as sysdba"

now you are connected.

Then you can change sys password by the following command-

alter user sys identified by *******;


Thus your password is reset. Now you can connect by any client s/w of oracle.

Sunday, August 30, 2009

protocol configuration of .asmx webservice

The default protocol configuration of .asmx webservice is -
<webServices>
<protocols>
<clear />
<add name="HttpSoap12" />
<add name="HttpSoap" />
<add name="HttpPostLocalhost" />
<add name="Documentation" />
</protocols>
</webServices>

So it supports data & payload in soap envelope and documentation for all; but supports post only for localhost. Thus if we want to test a webservice using post method, we can test it only from localhost.

If we want to allow to invoke the webservice via get & post method we have to add the following under the system.web node -

<webServices>
<protocols>
<clear />
<add name="HttpSoap12" />
<add name="HttpSoap" />
<add name="HttpPostLocalhost" />
<add name="Documentation" />
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

Suppose you want to invoke a webmethod(MethodName which takes a parameter param) of a webservice(SomeService.asmx) using get method, then you need to hit the following url-
http://localhost/WebXmlHttpDemo/SomeService.asmx/MethodName?param=value

Wednesday, July 15, 2009

batch update on a table from another table

CREATE TABLE A
(
NAME VARCHAR2(20 BYTE),
AGE INTEGER
)

CREATE TABLE B
(
NAME VARCHAR2(20 BYTE),
AGE INTEGER
)

Suppose we need to update the age of table B with the age of table A.

For oracle the command will be -

UPDATE tmp_B b
SET age = (SELECT a.age FROM tmp_A a WHERE a.name = b.name)
where b.name in (select a.name from tmp_A a);

Oracle also support n-ordered tuple. So if you want to update all row in a single run, the following command is also valid -

UPDATE tmp_B b
SET (age,name) = (SELECT a.age, a.name FROM tmp_A a WHERE a.name = b.name);

For MS Sql Server the command will be-

update tmp_B
set tmp_B.age=tmp_A.age
from tmp_A,tmp_B where tmp_A.name=tmp_B.name;