dbTalk Databases Forums  

[Info-Ingres] dbi and float format

comp.databases.ingres comp.databases.ingres


Discuss [Info-Ingres] dbi and float format in the comp.databases.ingres forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Martin Bowes
 
Posts: n/a

Default [Info-Ingres] dbi and float format - 12-21-2010 , 05:01 AM






Hi All,

How do I set the float format when connecting to a database with perl DBI?

I'm looking for smething which would be equivalent ot the terminal monitor command:
sql -f4N16.8 -f8N16.8

Martin Bowes

Reply With Quote
  #2  
Old   
Martin Bowes
 
Posts: n/a

Default Re: [Info-Ingres] dbi and float format - 12-21-2010 , 05:52 AM






Hi All,

FYI...The answer should be... $dbh = DBI -> connect ("dbi:ingres:$dbname[;options]", ....)

Where the options may be a spaces seperated list of SQL options as per the ESQL connect command...as long as they are SQL option flags and not terminal monitor line mode flags.

So in theory the answer seems to be to put the "-f4N16.8 -f8N16.8" options as indicated.

Trouble is that it doesn't work. You get an error indicating the flag options are incorrect.

However, trying either one as a single option the connect will work! This is the same in the ESQL connect. It too permits only one option and not the multiple options indicated by the [options = flag {,flag}] indicated in the command reference guide.

We are raising a bug with IngresCorp...

Marty

From: Martin Bowes [mailto:martin.bowes (AT) ctsu (DOT) ox.ac.uk]
Sent: 21 December 2010 11:01
To: Ingres and related product discussion forum
Subject: [Info-Ingres] dbi and float format

Hi All,

How do I set the float format when connecting to a database with perl DBI?

I'm looking for smething which would be equivalent ot the terminal monitor command:
sql -f4N16.8 -f8N16.8

Martin Bowes

Reply With Quote
  #3  
Old   
Martin Bowes
 
Posts: n/a

Default Re: [Info-Ingres] dbi and float format - 12-21-2010 , 07:17 AM



Hi All again,

This is just getting better all the time....

In ESQL the connect option -f4N16.8 works...but in DBI:Ingres the connect option is accepted but has no effect whatsoever.

Marty

From: Martin Bowes [mailto:martin.bowes (AT) ctsu (DOT) ox.ac.uk]
Sent: 21 December 2010 11:53
To: Ingres and related product discussion forum
Subject: Re: [Info-Ingres] dbi and float format

Hi All,

FYI...The answer should be... $dbh = DBI -> connect ("dbi:ingres:$dbname[;options]", ....)

Where the options may be a spaces seperated list of SQL options as per the ESQL connect command...as long as they are SQL option flags and not terminal monitor line mode flags.

So in theory the answer seems to be to put the "-f4N16.8 -f8N16.8" options as indicated.

Trouble is that it doesn't work. You get an error indicating the flag options are incorrect.

However, trying either one as a single option the connect will work! This is the same in the ESQL connect. It too permits only one option and not the multiple options indicated by the [options = flag {,flag}] indicated in the command reference guide.

We are raising a bug with IngresCorp...

Marty

From: Martin Bowes [mailto:martin.bowes (AT) ctsu (DOT) ox.ac.uk]
Sent: 21 December 2010 11:01
To: Ingres and related product discussion forum
Subject: [Info-Ingres] dbi and float format

Hi All,

How do I set the float format when connecting to a database with perl DBI?

I'm looking for smething which would be equivalent ot the terminal monitor command:
sql -f4N16.8 -f8N16.8

Martin Bowes

Reply With Quote
  #4  
Old   
Ingres Forums
 
Posts: n/a

Default Re: [Info-Ingres] dbi and float format - 12-21-2010 , 08:58 AM



Another quick update on DBI and formatting floats values.

The -f[48]Nx.y options are recognised by DBI:Ingres, but any formatting
specified seems to be lost when a float value is returned to a perl
script - a solution (or at least a workaround) is to cast it to varchar
and return a string instead.
eg "select varchar(float8(10.1))" gives the formatted value whereas
"select float8(10.1)" doesn't.

To get both -f8 and -f4 accepted in a connection string in ESQL,the
trick is to use:
options='-f4Nx.y','-f8Nx,y'
So both options are enclosed by quotes, and separated by a comma.
(Thanks to Ingres support for pointing us in the right direction
there).

All that's left is to figure out how to specify both -f options in a
connect string in a perl script...
GJ


--
geraintjones

Reply With Quote
  #5  
Old   
Chris
 
Posts: n/a

Default Re: dbi and float format - 12-27-2010 , 11:56 AM



On Dec 21, 6:58*am, Ingres Forums <info-
ing... (AT) kettleriverconsulting (DOT) com> wrote:
Quote:
Another quick update on DBI and formatting floats values.

The -f[48]Nx.y options are recognised by DBI:Ingres, but any formatting
specified seems to be lost when a float value is returned to a perl
script - a solution (or at least a workaround) is to cast it to varchar
and return a string instead.
eg "select varchar(float8(10.1))" gives the formatted value whereas
"select float8(10.1)" doesn't.

To get both -f8 and -f4 accepted in a connection string in ESQL,the
trick is to use:
options='-f4Nx.y','-f8Nx,y'
So both options are enclosed by quotes, and separated by a comma.
(Thanks to Ingres support for pointing us in the right direction
there).

All that's left is to figure out how to specify both -f options in a
connect string in a perl script...
The -f flags are Ingres specific flags, in this situation this
controls float -> string formatting in the DBMS as described above.
I.e.varchar cast on floats, e.g.:

select varchar(float4(0.5)) from iidbconstants

If you need control over formatting of floats in Perl, that is your
responsibility as the Perl programmer. Here is a web page that has a
concise table/example for printf: http://www.tutorialspoint.com/perl/perl_printf.htm

Here is a quick demo:

#!perl
use strict;
use warnings;


my $myf = 0.5;

print "$myf\n";

printf "%e\n", $myf;
printf "%E\n", $myf;
printf "%f\n", $myf;
printf "%79.38f\n", $myf;
##### End


And here is a quick demo of varchar type cast with the f4 param using
Ingres DBI (NOTE I personally recommend you consider using the ODBC
DBI driver if you are using Perl with Ingres, I'm not sure what
session connection flags are for -fx though).

use strict;
use warnings;

use DBI;

my $dbname = "iidbdb"; # database name, may include vnode

# Create a database handle.
#my $dbh = DBI->connect( "DBI:Ingres:$dbname")
my $dbh = DBI->connect( "DBI:Ingres:$dbname;-f4F79.38")
or die $DBI::errstr;

# Prepare a statment
my $sth = $dbh->prepare('select varchar(float4(0.5)) from
iidbconstants');

$sth -> execute();

# Get one line of the result as an array.
print join "|", $sth -> fetchrow_array();
print "\n";

# "finishing" the statement is not strictly necessary, but in this
# case we will get a warning if we don't.
$sth -> finish();

$dbh -> disconnect();

There are some perl pages in the wiki, please feel free to add to them
see http://community.ingres.com/wiki/Perl_DBI

Chris

Reply With Quote
  #6  
Old   
nikosv
 
Posts: n/a

Default Re: dbi and float format - 12-28-2010 , 08:49 AM



On Dec 27, 7:56*pm, Chris <Chris.Cl... (AT) ingres (DOT) com> wrote:
Quote:
On Dec 21, 6:58*am, Ingres Forums <info-



ing... (AT) kettleriverconsulting (DOT) com> wrote:
Another quick update on DBI and formatting floats values.

The -f[48]Nx.y options are recognised by DBI:Ingres, but any formatting
specified seems to be lost when a float value is returned to a perl
script - a solution (or at least a workaround) is to cast it to varchar
and return a string instead.
eg "select varchar(float8(10.1))" gives the formatted value whereas
"select float8(10.1)" doesn't.

To get both -f8 and -f4 accepted in a connection string in ESQL,the
trick is to use:
options='-f4Nx.y','-f8Nx,y'
So both options are enclosed by quotes, and separated by a comma.
(Thanks to Ingres support for pointing us in the right direction
there).

All that's left is to figure out how to specify both -f options in a
connect string in a perl script...

The -f flags are Ingres specific flags, in this situation this
controls float -> string formatting in the DBMS as described above.
I.e.varchar cast on floats, e.g.:

* * select varchar(float4(0.5)) from iidbconstants

If you need control over formatting of floats in Perl, that is your
responsibility as the Perl programmer. Here is a web page that has a
concise table/example for printf:http://www.tutorialspoint.com/perl/perl_printf.htm

Here is a quick demo:

#!perl
use strict;
use warnings;

my $myf = 0.5;

print "$myf\n";

printf "%e\n", $myf;
printf "%E\n", $myf;
printf "%f\n", $myf;
printf "%79.38f\n", $myf;
##### End

And here is a quick demo of varchar type cast with the f4 param using
Ingres DBI (NOTE I personally recommend you consider using the ODBC
DBI driver if you are using Perl with Ingres, I'm not sure what
session connection flags are for -fx though).

use strict;
use warnings;

use DBI;

my $dbname = "iidbdb"; * # database name, may include vnode

# Create a database handle.
#my $dbh = DBI->connect( "DBI:Ingres:$dbname")
my $dbh = DBI->connect( "DBI:Ingres:$dbname;-f4F79.38")
or die $DBI::errstr;

# Prepare a statment
my $sth = $dbh->prepare('select varchar(float4(0.5)) from
iidbconstants');

$sth -> execute();

# Get one line of the result as an array.
print join "|", $sth -> fetchrow_array();
print "\n";

# "finishing" the statement is not strictly necessary, but in this
# case we will get a warning if we don't.
$sth -> finish();

$dbh -> disconnect();

There are some perl pages in the wiki, please feel free to add to them
seehttp://community.ingres.com/wiki/Perl_DBI

Chris
I wouldn't worry too much on setting the options in the connection
string since Perl uses doubles internally hence any float4 or float8
would be coerced to float8 or double anyway, with of course no loss
in precision.
DBD::Ingres states that :
"Floats and doubles are returned as numeric values (NVs in perl-
speak)."
and an NV scalar is a double;more accuratelly it can hold a double.
So since you are going to consume the data from within a perl program
you will have to deal with doubles anyway and the formatting would be
up to you, as Chris points out. Can use pritnf for presentation or
sprintf for storing the value
as a string.

Always use Devel::Peek when in doubt :

use Devel::Peek;
$a=0.5;
Dump($a);

##output##
SV = NV(0x1835a7c) at 0x182a6cc
REFCNT = 1
FLAGS = (NOK,pNOK)
NV = 0.5

NV is the internal representation of a double

Reply With Quote
  #7  
Old   
Ingres Forums
 
Posts: n/a

Default Re: [Info-Ingres] dbi and float format - 12-29-2010 , 05:40 AM



Thanks all, printf's it is then.
The -f attempts were due to converting an existing shell script to
perl, and maybe trying to copy it too closely.

Chris makes an interesting comment about DBD::ODBC vs DBD::Ingres too.
We hadn't considered the ODBC option. We will now...


--
geraintjones

Reply With Quote
  #8  
Old   
Ingres Forums
 
Posts: n/a

Default Re: [Info-Ingres] dbi and float format - 01-04-2011 , 05:44 AM



Hi Marty

I'm not sure about the Perl DBI but libq uses the supplied values to
fill in the following structures (see iiingres.c around line 709). Not
sure if that will get you any closer to an answer.

Good luck

Alex


case 4:
IIlbqcb->ii_lq_adf->adf_outarg.ad_f4style =
fstyle_val;
IIlbqcb->ii_lq_adf->adf_outarg.ad_f4width =
fwidth_val;
IIlbqcb->ii_lq_adf->adf_outarg.ad_f4prec =
fprec_val;
def->ii_lq_adf->adf_outarg.ad_f4style =
fstyle_val;
def->ii_lq_adf->adf_outarg.ad_f4width =
fwidth_val;
def->ii_lq_adf->adf_outarg.ad_f4prec =
fprec_val;
f4Set = TRUE;
break;
case 8:
IIlbqcb->ii_lq_adf->adf_outarg.ad_f8style =
fstyle_val;
IIlbqcb->ii_lq_adf->adf_outarg.ad_f8width =
fwidth_val;
IIlbqcb->ii_lq_adf->adf_outarg.ad_f8prec =
fprec_val;
def->ii_lq_adf->adf_outarg.ad_f8style =
fstyle_val;
def->ii_lq_adf->adf_outarg.ad_f8width =
fwidth_val;
def->ii_lq_adf->adf_outarg.ad_f8prec =
fprec_val;
f8Set = TRUE;
break;


--
hanal04

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.