Archive

Archive for March, 2009

Removing trailing Slashes (‘/’)

March 30, 2009 Leave a comment

Just adding it up to the blog so that it might help with someone looking for the same….

I had to remove the trailing slashes from the directory paths as that was a requirement of a third party program we were using. Ended up doing it like this:

Where “conf->pg_backup” is the original string
and “newstback” is the changed string after removing trailing slashes…

for(i=0, j=0; conf->pg_backup[i]; ++i)
{
newstback[j++] = conf->pg_backup[i];
}

lenback = strlen(newstback) – 1;

for(;newstback[lenback] == ‘/’ || newstback[lenback] == ‘\t’; lenback–)
;
newstback[lenback +1] = ”;


Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: ,

Features Compatibility with Oracle

March 23, 2009 10 comments

A few days back I was given the task to write a document giving the compatibility features in PostgreSQL when comparing with Oracle as a prospective customer wanted to move out of Oracle. They gave us a list of features for which we had to give some sort of compatibility in PostgreSQL.

Here is how I ended up doing it:

Feature

Comments

Procedural Language

Supported using PLPGSQL which is very simiar to PLSQL in Oracle

Windowing Functions

RANK, OVER, PARTITION BY, WITH supported for Recursive queries

Full Text Search

Proper linguistic, searching and ordering support for natural language documents

Table Paritioning

Range and list paritioning is supported

Tablespaces

Oracle like tablespaces support

Database Links

Supported using “dblinks” contribution module

Localization

Provides vast majority of character sets including multi-byte ones.

Transactions

ACID compliant and uses MVCC

GIS Support

Supported with PostGIS which uses GEOS 3.1

Query Optimization

Cost based query optimization

Large Objects Support

Options available for Oracle like BFILE, BLOB and RAW data types using pre-defined large object methods

XML Support

XML data type supported

Application Drivers

Perl, Java, C/C++, PHP .NET, Python, TCL, Delphi, Ruby, ODBC, OLEDB and Openoffice drivers

Embedded SQL

Using ECPG

Process Monitoring

Using tools like ‘ptop’

Replication and Load Balancing

Using Slony with PgPool

Recovery Mecahnism

With PITR (Point In Time Recovery)

Backup and Restore

Standard backup and restore procedures for database using pg_dump and pg_restore

Access Control

Done by controlling a configuration file where Operating System level authentication can also be used

Gateway Product support

Using DBI-Link it is possible to connect to any datasource from within PostgreSQL

Portability

Supported on most of the Unix based platforms (Solaris, RHEL, Ubuntu, SuSe, FreeBSD and many more) and Windows


Shoaib Mir
shoaibmir[@]gmail.com

Bizzare right click behavior in Firfox 3.0.6

March 13, 2009 4 comments

Its been quite a while that I am noticing a weird problem with my firefox since I upgraded to Ubuntu 8.1 where a right click within the browser can at times select one of the random functions from the menu options. Its just very annoying at times, so finally today as I got time I started looking at fixing it.

While searching it on the web I found quite a number of people having the same problem as me but there wasn’t a proper fix available to it. Tried several different things and then finally after installing a Firefox addon “Firegestures” it all started behaving normally.

If anyone having the same problem do give “Firegestures” a shot 🙂


Shoaib Mir
shoaibmir[@]gmail.com

xAjax and asynchronous updates

March 12, 2009 Leave a comment

Most of the libraries available for Ajax requires you to write your own functions in Javascript to process callbacks from the asynchronous requests in order to update the data but xajax gives you its own xajaxResponse object through which you can handle all within PHP. xajaxResponse object is here used to create XML which is returned to the application and that is then parsed by xajax message pump that results in updating the state of the application.

xajaxResponse gives a number of nice to use features like:

Assign –> This sets the specified attribute of an element in your page

Append –> Is used to append data to the end of the specified attribute of an element in your page

Prepend –> Prepends data to the beginning of the specified attribute of an element in your page

Replace –> Which searches for and replaces data in the specified attribute of an element in your page

Script –> This runs the supplied JavaScript code

Alert –> Shows an alert box with the supplied message text


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: , , ,

A quick howto of Ajax with PHP

March 11, 2009 Leave a comment

I have been using Ajax for one of our main applications with PHP. We are using the Xajax PHP class libary for that purpose which gives a very easy to use interface.  I thought of sharing a very simple howto for the xajax library in case anyone wants to have a quick look at it…

//Include the xajax class library
require_once(“xajax_core/xajax.inc.php”);

//Initialize a new Xajax object
$xajax = new xajax();

//Register the name of function that you will be calling from Xajax
$xajax->registerFunction(“myFunction”);

//Now the function that uses xajaxResponse object for returning XML
function test_func($arg)
{
$display_val = “Passed to function: “.$arg;
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id=”v_ElementId” to whatever the new content is
$objResponse->assign(“v_ElementId”,”innerHTML”, $display_val);

//return the  xajaxResponse object
return $objResponse;
}

//Before your script sends any output, use the xajax handle for any requests
$xajax->processRequest();

//Tells ajax to generate the appropriate Javascript
<?php $xajax->printJavascript(); ?>

//Now you can use the function in your HTML code like this
<div id=”v_ElementId”></div>
<button onclick=”xajax_myFunction(‘Hello World!”);”>


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: , ,

Using CASE with SELECT

March 10, 2009 5 comments

Today I came across an interesting example, another team at our office working with a bug tracking software needed to group the data within one of the tables on the basis of a specific component and I was able help them out with the query for doing so. It can be explained in an easier way using the dummy data…

The table and the data can be represented as:

create table testsample (ticketid int, component text, keywords text);

insert into testsample values (1, ‘JTA’, ‘Runtime’);
insert into testsample values (2, ‘JTA’, ‘MSG’);
insert into testsample values (3, ‘JTA’, ‘MSG’);
insert into testsample values (4, ‘JTA’, ‘Deploy’);
insert into testsample values (5, ‘EJB’, ‘Runtime’);
insert into testsample values (6, ‘EJB’, ‘Deploy’);
insert into testsample values (7, ‘J2EE’, ‘Runtime’);

Now we had to represent the data with groups of JTA, EJB and J2EE and then give the counts of MSG, Runtime and Deploy for each component type… SELECT with CASE came handy in this scenario as I ended up doing for a quick solution:

SELECT
component,

CASE WHEN component = ‘JTA’
THEN (select count(1) from testsample where keywords = ‘Runtime’ and component = ‘JTA’)
WHEN component = ‘EJB’
THEN (select count(1) from testsample where keywords = ‘Runtime’ and component = ‘EJB’)
WHEN component = ‘J2EE’
THEN (select count(1) from testsample where keywords = ‘Runtime’ and component = ‘J2EE’)
ELSE 0
END AS Runtime
,
CASE WHEN component = ‘JTA’
THEN (select count(1) from testsample where keywords = ‘Deploy’ and component = ‘JTA’)
WHEN component = ‘EJB’
THEN (select count(1) from testsample where keywords = ‘Deploy’ and component = ‘EJB’)
WHEN component = ‘J2EE’
THEN (select count(1) from testsample where keywords = ‘Deploy’ and component = ‘J2EE’)
ELSE 0
END AS Deploy
,
CASE WHEN component = ‘JTA’
THEN (select count(1) from testsample where keywords = ‘MSG’ and component = ‘JTA’)
WHEN component = ‘EJB’
THEN (select count(1) from testsample where keywords = ‘MSG’ and component = ‘EJB’)
WHEN component = ‘J2EE’
THEN (select count(1) from testsample where keywords = ‘MSG’ and component = ‘J2EE’)
END AS MSG
FROM testsample
group by component

And the output was…

output

Update:

David Fetter came up with a much better and cleaner solution…

SELECT
component,
SUM(CASE keywords WHEN ‘Runtime’ THEN 1 ELSE 0 END) AS Runtime,
SUM(CASE keywords WHEN ‘Deploy’ THEN 1 ELSE 0 END) AS Deploy,
SUM(CASE keywords WHEN ‘MSG’ THEN 1 ELSE 0 END) AS MSG
FROM
testsample
GROUP BY component;


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PostgreSQL Tags: ,

Using cURL for XMLRPC calls

March 10, 2009 Leave a comment

cURL is a project name which came from  ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious it deals with URLs. cURL has basically two products, a command line and a library. We are more concerned with the library as that is used with PHP for cURL calls. LibCurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE, LDAP and LDAPS. libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy tunneling and more…

So before I go with details on the sample program, make sure you got cURL and xmlrpc modules installed for PHP. This can be easily done by compiling the cURL and xmlrpc extensions from PHP source and linking it though the php.ini file. Once that is done make sure
you can see the modules installed using phpinfo function.

Once all looks good when cURL and xmlrpc installations have been verified, you can use the following code in PHP to do the xmlrpc calls…

//Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request($method, array($user, $pass));
$req = curl_init($service_url);

// Using the cURL extension to send it off,  first creating a custom header block
$headers = array();
array_push($headers,”Content-Type: text/xml”);
array_push($headers,”Content-Length: “.strlen($request));
array_push($headers,”\r\n”);

//URL to post to
curl_setopt($req, CURLOPT_URL, $service_url);

//Setting options for a secure SSL based xmlrpc server
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($req, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt( $req, CURLOPT_CUSTOMREQUEST, ‘POST’ );
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($req, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $req, CURLOPT_POSTFIELDS, $request );

//Finally run
$response = curl_exec($req);

//Close the cURL connection
curl_close($req);

//Decoding the response to be displayed
echo xmlrpc_decode($response);


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: , ,