From gerardo.santana at gmail.com Sun Mar 23 17:43:55 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Sun, 23 Mar 2008 15:43:55 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype Message-ID: Hello everybody, I have given the INTERVAL datatype a thought for implementing it in Ruby/Informix. One of the reasons I hadn't done it before is because I wanted to get it right. The other reasons were I hadn't used it and nobody asked for it, so it was difficult to get the motivation. But I'm on track again and wanted to share with you what would be the behavior of Informix::Interval. As you already know, there are two main classes of INTERVAL: YEAR TO MONTH and DAY TO FRACTION. You can't mixed them, you can specify a subset of them and you can make basic operations with DATE, DATETIME, numbers and other INTERVALs. According to Ruby, there are three date and time classes: Date, DateTime and Time. I'm using Date and Time only for binding input and output parameters. DateTime is never used. Informix::Interval, depending on the qualifier would be able to work with a Date or a Time variable. DateTime is the exception, where the qualifier doesn't matter. Here's a sample program to explain this: ---------- 8< ---------- include Informix # initializing date and time variables today = Date.today now = Time.now dtnow = DateTime.now # initializing interval variables ym = Interval.year_to_month(13) # 13 months df = Interval.day_to_fraction(Rational(615,10)) # 61.5 seconds # Adding intervals to dates and times puts "#{today} + #{ym} == #{ym + today}" #Interval(YEAR TO MONTH)+Date puts "#{now} + #{df} == #{df + now}" # Interval(DAY TO FRACTION) + Time puts "#{dtnow} + #{ym} == #{ym + dtnow}" # Interval(YEAR TO MONTH) + DateTime puts "#{dtnow} + #{df} == #{df + dtnow}" # Interval(DAY TO FRACTION) + DateTime ---------- >8 ---------- The output in my system of the above program is: ---------- 8< ---------- 2008-03-23 + 1 year, 1 month == 2009-04-23 Sun Mar 23 15:26:35 -0600 2008 + 0 days, 0 hours, 1 minute, 1.5 seconds == Sun Mar 23 15:27:36 -0600 2008 2008-03-23T15:26:35-06:00 + 1 year, 1 month == 2009-04-23T15:26:35-06:00 2008-03-23T15:26:35-06:00 + 0 days, 0 hours, 1 minute, 1.5 seconds == 2008-03-23T15:27:36-06:00 ---------- >8 ---------- If you have any questions or suggestions I'm all ears. -- Gerardo Santana From gerardo.santana at gmail.com Mon Mar 24 01:26:10 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Sun, 23 Mar 2008 23:26:10 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: Message-ID: On 3/23/08, Gerardo Santana G?mez Garrido wrote: > ym = Interval.year_to_month(13) # 13 months > df = Interval.day_to_fraction(Rational(615,10)) # 61.5 seconds or would this look better/more practical/more verbose? ym = Interval.year_to_month(1, 1) df = Interval.day_to_fraction(0, 0, 1, 1, Rational(1, 2)) probably setting the last parameters to zero when ommitted? I'm not sure. The first approach is shorter to type; the second one may be clearer when we have an interval in mind in different units than months or seconds. -- Gerardo Santana From gerardo.santana at gmail.com Mon Mar 24 22:44:32 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Mon, 24 Mar 2008 20:44:32 -0600 Subject: [Ruby-informix-misc] INTERVAL datatype support Message-ID: I've just committed the code for supporting the INTERVAL datata type. For your information, here's the commit message: * Informix::Interval class for binding input and output INTERVAL parameters * year_to_month, day_to_fraction, from_months and from_seconds class methods for creating Interval objects * Basic operations (+, -, *, /) available with Integer, Rational, Date, Time and DateTime * #to_a method for returning each field of an Interval object (years, months, days, hours, minutes, seconds, fraction) * #to_s method for displaying an Interval object as an string according to ANSI SQL standards * Includes the Comparable module, for comparing with other Interval objects It already works right now. Please feel free to try it and criticize it. I can provide you a rubygem if anyone wants to try it. I would appreciate any comments. Thanks -- Gerardo Santana From jonathan.leffler at gmail.com Wed Mar 26 10:31:31 2008 From: jonathan.leffler at gmail.com (Jonathan Leffler) Date: Wed, 26 Mar 2008 07:31:31 -0700 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: Message-ID: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> On Sun, Mar 23, 2008 at 10:26 PM, Gerardo Santana G?mez Garrido < gerardo.santana at gmail.com> wrote: > On 3/23/08, Gerardo Santana G?mez Garrido > wrote: > > ym = Interval.year_to_month(13) # 13 months > > df = Interval.day_to_fraction(Rational(615,10)) # 61.5 seconds > > or would this look better/more practical/more verbose? > > ym = Interval.year_to_month(1, 1) > df = Interval.day_to_fraction(0, 0, 1, 1, Rational(1, 2)) > > probably setting the last parameters to zero when ommitted? > > I'm not sure. The first approach is shorter to type; the second one > may be clearer when we have an interval in mind in different units > than months or seconds. [Omit one of the m's in 'ommitted' :)] Both have their merits. Without being sure of Ruby syntax, it would be nice to be able to create intervals of the DS (day-second/fraction) class with any combination of initializers, such as: (Aaargh - don't shoot me - I'm only the messenger. Beware: Perl syntax, more or less.) df = Interval::DayToSecond({ day => 1.3, hour => Rational(14,10), second => 1}); That is, you get to specify each of the 'bits' - or not - and the values can be any numeric type, integer or decimal or variations thereupon. The net result is well defined - though the particular initializer above is unduly contorted because you'd normally have just one component with fractions, the smaller values would be omitted and treated as zero, and larger components would be integer values. So, more sanely: df = Interval::DayToSecond({hour => 12, minute => 21.34}); That is, 0 days, 12 hours, 21 minutes, and 20.4 seconds. FWIW: I would follow the SQL standard and not use Fraction but just Second as the last field, with fractions being treated like INTERVAL HOUR TO SECOND(4) ==> INTERVAL HOUR TO FRACTION(4). The tricky bit is the standard notation INTERVAL SECOND(9) is like INTERVAL SECOND(9) TO SECOND; the standard notation INTERVAL SECOND(9,5) is like INTERVAL SECOND(9) TO FRACTION(5). -- Jonathan Leffler #include Guardian of DBD::Informix - v2008.0229 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ruby-informix-misc/attachments/20080326/324825d1/attachment.html From gerardo.santana at gmail.com Wed Mar 26 13:20:10 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Wed, 26 Mar 2008 11:20:10 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: On 3/26/08, Jonathan Leffler wrote: > > > > On Sun, Mar 23, 2008 at 10:26 PM, Gerardo Santana G?mez Garrido > wrote: > > > > On 3/23/08, Gerardo Santana G?mez Garrido > wrote: > > > ym = Interval.year_to_month(13) # 13 months > > > df = Interval.day_to_fraction(Rational(615,10)) # 61.5 seconds > > > > or would this look better/more practical/more verbose? > > > > ym = Interval.year_to_month(1, 1) > > df = Interval.day_to_fraction(0, 0, 1, 1, Rational(1, 2)) > > > > probably setting the last parameters to zero when ommitted? > > > > I'm not sure. The first approach is shorter to type; the second one > > may be clearer when we have an interval in mind in different units > > than months or seconds. > > [Omit one of the m's in 'ommitted' :)] Thanks, I will omit it :-) > > Both have their merits. Without being sure of Ruby syntax, it would be nice > to be able to create intervals of the DS (day-second/fraction) class with > any combination of initializers, such as: > > (Aaargh - don't shoot me - I'm only the messenger. Beware: Perl syntax, > more or less.) > > df = Interval::DayToSecond({ day => 1.3, hour => Rational(14,10), second => > 1}); > > That is, you get to specify each of the 'bits' - or not - and the values can > be any numeric type, integer or decimal or variations thereupon. The net > result is well defined - though the particular initializer above is unduly > contorted because you'd normally have just one component with fractions, the > smaller values would be omitted and treated as zero, and larger components > would be integer values. So, more sanely: > > df = Interval::DayToSecond({hour => 12, minute => 21.34}); > > That is, 0 days, 12 hours, 21 minutes, and 20.4 seconds. > Right, I'll make the constructors to accept a hash It would look like this: df = Interval.day_to_fraction(:hours => 12, :minutes => 21, :seconds => 20, :fraction => 4)) > FWIW: I would follow the SQL standard and not use Fraction but just Second > as the last field, Do you mean to rename Interval.day_to_fraction to Interval.day_to_second, and include the "fractions" in the "seconds" parameter? Can you give me a reference to that part of the standard? > with fractions being treated like INTERVAL HOUR TO > SECOND(4) ==> INTERVAL HOUR TO FRACTION(4). The tricky bit is the standard > notation INTERVAL SECOND(9) is like INTERVAL SECOND(9) TO SECOND; the > standard notation INTERVAL SECOND(9,5) is like INTERVAL SECOND(9) TO > FRACTION(5). When retrieving INTERVALs or creating Interval objects, I'm using YEAR TO MONTH or DAY TO FRACTION only, for simplicity. -- Gerardo Santana From gerardo.santana at gmail.com Wed Mar 26 15:07:44 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Wed, 26 Mar 2008 13:07:44 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: On 3/26/08, Gerardo Santana G?mez Garrido wrote: > On 3/26/08, Jonathan Leffler wrote: > > > > Both have their merits. Without being sure of Ruby syntax, it would be nice > > to be able to create intervals of the DS (day-second/fraction) class with > > any combination of initializers, such as: > > > > (Aaargh - don't shoot me - I'm only the messenger. Beware: Perl syntax, > > more or less.) > > > > df = Interval::DayToSecond({ day => 1.3, hour => Rational(14,10), second => > > 1}); > > > > That is, you get to specify each of the 'bits' - or not - and the values can > > be any numeric type, integer or decimal or variations thereupon. The net > > result is well defined - though the particular initializer above is unduly > > contorted because you'd normally have just one component with fractions, the > > smaller values would be omitted and treated as zero, and larger components > > would be integer values. So, more sanely: > > > > df = Interval::DayToSecond({hour => 12, minute => 21.34}); > > > > That is, 0 days, 12 hours, 21 minutes, and 20.4 seconds. > > > > > > Right, I'll make the constructors to accept a hash > > It would look like this: > > df = Interval.day_to_fraction(:hours => 12, :minutes => 21, :seconds > => 20, :fraction => 4)) Ok, I've got something now. Here's the documentation explaining the new behavior. Interval.year_to_month: # Interval.year_to_month(years, months = 0) => interval # Interval.year_to_month(:years => years, :months =>months) => interval # # Creates an Interval object in the year-to-month scope. # # Interval.year_to_month(5) => '5-0' # Interval.year_to_month(0, 3) => '0-3' # Interval.year_to_month(5, 3) => '5-3' # Interval.year_to_month(:years => 5) => '5-0' # Interval.year_to_month(:months =>3) => '0-3' # Interval.year_to_month(:years => 5, :months =>3) => '5-3' Interval.day_to_fraction: # Interval.day_to_fraction(days, hours = 0, mins = 0, # secs = 0, frac = 0) =>interval # Interval.day_to_fraction(:days => dd, :hours => hh, # :mins => mm, :secs => ss, # :frac => f) =>interval # # Creates an Interval in the day-to-fraction scope. # Use a Rational if you want to specify fraction of seconds. # # Interval.day_to_fraction(5, 3) # => '5 3:00:00.00' # Interval.day_to_fraction(0, 2, 0, 30) # => '0 2:00:30.00 # Interval.day_to_fraction(:hours=>2, :seconds=>30) # => '0 2:00:30.00' # Interval.day_to_fraction(:seconds=>10, # :fraction=>Rational(16,100)) # => '0 0:00:10.16' -- Gerardo Santana From gerardo.santana at gmail.com Wed Mar 26 15:17:56 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Wed, 26 Mar 2008 13:17:56 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: On 3/26/08, Gerardo Santana G?mez Garrido wrote: > Interval.day_to_fraction: > > # Interval.day_to_fraction(days, hours = 0, mins = 0, > # secs = 0, frac = 0) =>interval > # Interval.day_to_fraction(:days => dd, :hours => hh, > # :mins => mm, :secs => ss, > # :frac => f) =>interval There is an error here. It must say :minutes, :seconds and :fraction, instead of :mins, :secs, :frac -- Gerardo Santana From jonathan.leffler at gmail.com Wed Mar 26 18:13:51 2008 From: jonathan.leffler at gmail.com (Jonathan Leffler) Date: Wed, 26 Mar 2008 15:13:51 -0700 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: <844b8e1c0803261513q7ad5f919y4375a80a705abaeb@mail.gmail.com> On Wed, Mar 26, 2008 at 10:20 AM, Gerardo Santana G?mez Garrido < gerardo.santana at gmail.com> wrote: > On 3/26/08, Jonathan Leffler wrote: > > FWIW: I would follow the SQL standard and not use Fraction but just > Second > > as the last field, > > Do you mean to rename Interval.day_to_fraction to > Interval.day_to_second, and include the "fractions" in the "seconds" > parameter? > > Can you give me a reference to that part of the standard? > http://savage.net.au/SQL/ > When retrieving INTERVALs or creating Interval objects, I'm using YEAR > TO MONTH or DAY TO FRACTION only, for simplicity. > OK, but sometimes people want to measure the time interval in hours and decimals of an hour, even though the data is stored in DAY TO FRACTION - see ivconv.ec. -- Jonathan Leffler #include Guardian of DBD::Informix - v2008.0229 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ruby-informix-misc/attachments/20080326/24fe9f3f/attachment-0001.html From gerardo.santana at gmail.com Wed Mar 26 18:35:44 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Wed, 26 Mar 2008 16:35:44 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803261513q7ad5f919y4375a80a705abaeb@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> <844b8e1c0803261513q7ad5f919y4375a80a705abaeb@mail.gmail.com> Message-ID: On 3/26/08, Jonathan Leffler wrote: > > > On Wed, Mar 26, 2008 at 10:20 AM, Gerardo Santana G?mez Garrido > wrote: > > > > On 3/26/08, Jonathan Leffler wrote: > > > FWIW: I would follow the SQL standard and not use Fraction but just > Second > > > as the last field, > > > > Do you mean to rename Interval.day_to_fraction to > > Interval.day_to_second, and include the "fractions" in the "seconds" > > parameter? > > > > Can you give me a reference to that part of the standard? > > > > > > http://savage.net.au/SQL/ I see 'fraction' there. Or where should I look? > > > > > When retrieving INTERVALs or creating Interval objects, I'm using YEAR > > TO MONTH or DAY TO FRACTION only, for simplicity. > > > > OK, but sometimes people want to measure the time interval in hours and > decimals of an hour, even though the data is stored in DAY TO FRACTION - see > ivconv.ec. I see. the YTM and DTF convention is just an implementation detail. The user will specify an INTERVAL as he/she wishes. -- Gerardo Santana From gerardo.santana at gmail.com Thu Mar 27 03:04:32 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Thu, 27 Mar 2008 01:04:32 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: On 3/26/08, Jonathan Leffler wrote: > > > > On Sun, Mar 23, 2008 at 10:26 PM, Gerardo Santana G?mez Garrido > wrote: > > > > On 3/23/08, Gerardo Santana G?mez Garrido > wrote: > > > ym = Interval.year_to_month(13) # 13 months > > > df = Interval.day_to_fraction(Rational(615,10)) # 61.5 seconds > > > > or would this look better/more practical/more verbose? > > > > ym = Interval.year_to_month(1, 1) > > df = Interval.day_to_fraction(0, 0, 1, 1, Rational(1, 2)) > > > > probably setting the last parameters to zero when ommitted? > > > > I'm not sure. The first approach is shorter to type; the second one > > may be clearer when we have an interval in mind in different units > > than months or seconds. > > [Omit one of the m's in 'ommitted' :)] > > Both have their merits. Without being sure of Ruby syntax, it would be nice > to be able to create intervals of the DS (day-second/fraction) class with > any combination of initializers, such as: > > (Aaargh - don't shoot me - I'm only the messenger. Beware: Perl syntax, > more or less.) > > df = Interval::DayToSecond({ day => 1.3, hour => Rational(14,10), second => > 1}); > > That is, you get to specify each of the 'bits' - or not - and the values can > be any numeric type, integer or decimal or variations thereupon. The net > result is well defined - though the particular initializer above is unduly > contorted because you'd normally have just one component with fractions, the > smaller values would be omitted and treated as zero, and larger components > would be integer values. So, more sanely: > > df = Interval::DayToSecond({hour => 12, minute => 21.34}); > > That is, 0 days, 12 hours, 21 minutes, and 20.4 seconds. > > FWIW: I would follow the SQL standard and not use Fraction but just Second > as the last field, with fractions being treated like INTERVAL HOUR TO > SECOND(4) ==> INTERVAL HOUR TO FRACTION(4). The tricky bit is the standard > notation INTERVAL SECOND(9) is like INTERVAL SECOND(9) TO SECOND; the > standard notation INTERVAL SECOND(9,5) is like INTERVAL SECOND(9) TO > FRACTION(5). I've been slow today... I understand you now. I will allow any kind of numbers (well, real numbers :-) and rename day_to_fraction to day_to_second. I hope Informix users don't get confused (it will be documented of course) -- Gerardo Santana From gerardo.santana at gmail.com Thu Mar 27 05:04:36 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Thu, 27 Mar 2008 03:04:36 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: Here's another iteration of the interface to create an Interval object. year_to_month: # Interval.year_to_month(years = 0, months = 0) => interval # Interval.year_to_month(:years => yy, :months => mm) => interval # # Creates an Interval object in the year-to-month scope. # # Interval.year_to_month(5) => '5-00' # Interval.year_to_month(0, 3) => '0-03' # Interval.year_to_month(5, 3) => '5-03' # Interval.year_to_month(:years => 5.5) => '5-06' # Interval.year_to_month(:months =>3) => '0-03' # Interval.year_to_month(:years => 5.5, :months =>5) => '5-11' day_to_second: # Interval.day_to_second(days = 0, hours = 0, # minutes = 0, seconds = 0) => interval # Interval.day_to_second(:days => dd, :hours => hh, # :minutes => mm, :seconds => ss) => interval # # Creates an Interval object in the day-to-second scope. # # Interval.day_to_second(5, 3) # => '5 3:00:00.00000' # Interval.day_to_second(0, 2, 0, 30) # => '0 2:00:30.00000' # Interval.day_to_second(:hours=>2.5) # => '0 2:30:00.00000' # Interval.day_to_second(:seconds=> 10.16) # => '0 0:00:10.16000' # Interval.day_to_second(:days=>1.5, :hours=>2) # => '1 14:00:0.00000' -- Gerardo Santana From jonathan.leffler at gmail.com Thu Mar 27 09:58:31 2008 From: jonathan.leffler at gmail.com (Jonathan Leffler) Date: Thu, 27 Mar 2008 06:58:31 -0700 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> Message-ID: <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> Looking good. On Thu, Mar 27, 2008 at 2:04 AM, Gerardo Santana G?mez Garrido < gerardo.santana at gmail.com> wrote: > Here's another iteration of the interface to create an Interval object. > > year_to_month: > > # Interval.year_to_month(years = 0, months = 0) => interval > # Interval.year_to_month(:years => yy, :months => mm) => interval > # > # Creates an Interval object in the year-to-month scope. > # > # Interval.year_to_month(5) => '5-00' > # Interval.year_to_month(0, 3) => '0-03' > # Interval.year_to_month(5, 3) => '5-03' > # Interval.year_to_month(:years => 5.5) => '5-06' > # Interval.year_to_month(:months =>3) => '0-03' > # Interval.year_to_month(:years => 5.5, :months =>5) => '5-11' > > > day_to_second: > > # Interval.day_to_second(days = 0, hours = 0, > # minutes = 0, seconds = 0) => interval > # Interval.day_to_second(:days => dd, :hours => hh, > # :minutes => mm, :seconds => ss) => interval > # > # Creates an Interval object in the day-to-second scope. > # > # Interval.day_to_second(5, 3) # => '5 3:00: > 00.00000' > # Interval.day_to_second(0, 2, 0, 30) # => '0 2:00: > 30.00000' > # Interval.day_to_second(:hours=>2.5) # => '0 2:30: > 00.00000' > # Interval.day_to_second(:seconds=> 10.16) # => '0 0:00: > 10.16000' > # Interval.day_to_second(:days=>1.5, :hours=>2) # => '1 14:00: > 0.00000' > I'm wicked... Interval.day_to_second(:days => -1.5, :hours => 12) # => '-1 00:00: 00.00000'? Basically, intervals can be negative, and what happens with negatives, with some fields negative and some not negative? What about: Interval.day_to_second(:days => 1.5, :hours => 12, :sign => '-') # => '-2 00:00:00.00000'? An explicit sign, in other words. -- Jonathan Leffler #include Guardian of DBD::Informix - v2008.0229 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ruby-informix-misc/attachments/20080327/7b1fb6b8/attachment.html From gerardo.santana at gmail.com Thu Mar 27 13:27:53 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Thu, 27 Mar 2008 11:27:53 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> Message-ID: On 3/27/08, Jonathan Leffler wrote: > Looking good. > > > On Thu, Mar 27, 2008 at 2:04 AM, Gerardo Santana G?mez Garrido > wrote: > > Here's another iteration of the interface to create an Interval object. > > > > year_to_month: > > > > # Interval.year_to_month(years = 0, months = 0) => interval > > # Interval.year_to_month(:years => yy, :months => mm) => interval > > > > # > > # Creates an Interval object in the year-to-month scope. > > # > > # Interval.year_to_month(5) => '5-00' > > # Interval.year_to_month(0, 3) => '0-03' > > # Interval.year_to_month(5, 3) => '5-03' > > # Interval.year_to_month(:years => 5.5) => '5-06' > > # Interval.year_to_month(:months =>3) => '0-03' > > # Interval.year_to_month(:years => 5.5, :months =>5) => '5-11' > > > > > > day_to_second: > > > > # Interval.day_to_second(days = 0, hours = 0, > > # minutes = 0, seconds = 0) => interval > > # Interval.day_to_second(:days => dd, :hours => hh, > > # :minutes => mm, :seconds => ss) => interval > > # > > # Creates an Interval object in the day-to-second scope. > > # > > # Interval.day_to_second(5, 3) # => '5 > 3:00:00.00000' > > # Interval.day_to_second(0, 2, 0, 30) # => '0 > 2:00:30.00000' > > # Interval.day_to_second(:hours=>2.5) # => '0 > 2:30:00.00000' > > # Interval.day_to_second(:seconds=> 10.16) # => '0 > 0:00:10.16000' > > # Interval.day_to_second(:days=>1.5, :hours=>2) # => '1 > 14:00:0.00000' > > > > > I'm wicked... > > Interval.day_to_second(:days => -1.5, :hours => 12) # => '-1 > 00:00:00.00000'? > > Basically, intervals can be negative, and what happens with negatives, with > some fields negative and some not negative? It happens exactly what you show. I think of that as a feature actually :) But, hey, I don't work with INTERVALs that much. Do you think the programmer must be restricted from making those kinds of operations? > > What about: > > Interval.day_to_second(:days => 1.5, :hours => 12, :sign => '-') # => '-2 > 00:00:00.00000'? > > An explicit sign, in other words. What about this: -Interval.day_to_second(:days => 1.5, :hours => 12) It already works, and looks better. By the way, I'm thinking of adding methods for retrieving just one field (#to_a returns all of them) -- Gerardo Santana From gerardo.santana at gmail.com Thu Mar 27 18:08:36 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Thu, 27 Mar 2008 16:08:36 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803271151j67381d41ie1a11158f3c8befc@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> <844b8e1c0803271151j67381d41ie1a11158f3c8befc@mail.gmail.com> Message-ID: On 3/27/08, Jonathan Leffler wrote: > > > > On Thu, Mar 27, 2008 at 10:27 AM, Gerardo Santana G?mez Garrido > wrote: > > > > It happens exactly what you show. I think of that as a feature > > actually :) But, hey, I don't work with INTERVALs that much. Do you > > think the programmer must be restricted from making those kinds of > > operations? > > > > > > It is a reasonable model - it is at least explainable, which is the > important thing. > > When dealing with segmented signed quantities (for example, degrees, > minutes, seconds, or struct decimal, or intervals, or ... (pounds, shillings > and pence, if you ever knew about the pre-decimalized English currency)), > then you need to keep the sign separate from the numerics, eventually. Of > course, ESQL/C (intrvl_t) does that for you. During the construction, it is > often good to keep the option of the sign separate from the rest of the > information. (There's a design document about this somewhere on the > web...but I can't find it quickly by Google.) I see. I will restrict the arguments to positive real numbers, or zero. If the user wants a negative interval, he/she can use the minus unary operator, or use from_secods or from_months, which accept a single real number. > > By the way, I'm thinking of adding methods for retrieving just one > > field (#to_a returns all of them) > > > > There are two separate sets of operations possible there. > > One is to obtain the hour component of the interval (say). > > The other is to obtain the interval as a number of hours (with fractional > hours if there are non-zero minutes or seconds components, and the value > could be much bigger than 24 (or less than -24) if the interval has a > non-zero number of days). > > Both are useful - they just have different purposes. Aha. What about this. Lets the first behavior be called as invl.hours while the second one would be invoked as invl.to_hours Is that useful? Make sense? Does anybody plan to use INTERVAL? -- Gerardo Santana From jonathan.leffler at gmail.com Thu Mar 27 18:50:25 2008 From: jonathan.leffler at gmail.com (Jonathan Leffler) Date: Thu, 27 Mar 2008 15:50:25 -0700 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> <844b8e1c0803271151j67381d41ie1a11158f3c8befc@mail.gmail.com> Message-ID: <844b8e1c0803271550k1a841c9fwf8098e755968f663@mail.gmail.com> On Thu, Mar 27, 2008 at 3:08 PM, Gerardo Santana G?mez Garrido < gerardo.santana at gmail.com> wrote: > On 3/27/08, Jonathan Leffler wrote: > > On Thu, Mar 27, 2008 at 10:27 AM, Gerardo Santana G?mez Garrido > > wrote: > > > It happens exactly what you show. I think of that as a feature > > > actually :) But, hey, I don't work with INTERVALs that much. Do you > > > think the programmer must be restricted from making those kinds of > > > operations? > > > > It is a reasonable model - it is at least explainable, which is the > > important thing. > > > > When dealing with segmented signed quantities (for example, degrees, > > minutes, seconds, or struct decimal, or intervals, or ... (pounds, > shillings > > and pence, if you ever knew about the pre-decimalized English > currency)), > > then you need to keep the sign separate from the numerics, eventually. > Of > > course, ESQL/C (intrvl_t) does that for you. During the construction, > it is > > often good to keep the option of the sign separate from the rest of the > > information. (There's a design document about this somewhere on the > > web...but I can't find it quickly by Google.) > > > I see. I will restrict the arguments to positive real numbers, or > zero. If the user wants a negative interval, he/she can use the minus > unary operator, or use from_secods or from_months, which accept a > single real number. > Fair enough. Most people won't need from_minutes, from_hours, from_days, from_years variants, and they can be trivially implemented in terms of the from_seconds or from_months which you do provide. > > > By the way, I'm thinking of adding methods for retrieving just one > > > field (#to_a returns all of them) > > > > There are two separate sets of operations possible there. > > > > One is to obtain the hour component of the interval (say). > > > > The other is to obtain the interval as a number of hours (with > fractional > > hours if there are non-zero minutes or seconds components, and the value > > could be much bigger than 24 (or less than -24) if the interval has a > > non-zero number of days). > > > > Both are useful - they just have different purposes. > > Aha. > > What about this. Lets the first behavior be called as > > invl.hours > > while the second one would be invoked as > > invl.to_hours > > Is that useful? Make sense? > Yes, and yes. What will invl.hours return for a negative interval? The positive value? How will the user detect that the interval value was negative? I'm assuming that both methods return a number rather than an interval per se. > Does anybody plan to use INTERVAL? > The horrid question - if I provide this feature, will they use it? -- Jonathan Leffler #include Guardian of DBD::Informix - v2008.0229 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ruby-informix-misc/attachments/20080327/574aa535/attachment-0001.html From gerardo.santana at gmail.com Thu Mar 27 21:54:52 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Thu, 27 Mar 2008 19:54:52 -0600 Subject: [Ruby-informix-misc] Starting to implement INTERVAL datatype In-Reply-To: <844b8e1c0803271550k1a841c9fwf8098e755968f663@mail.gmail.com> References: <844b8e1c0803260731g21f38b48mac36dcba719a3370@mail.gmail.com> <844b8e1c0803270658m21c70849ke1b86305185f755f@mail.gmail.com> <844b8e1c0803271151j67381d41ie1a11158f3c8befc@mail.gmail.com> <844b8e1c0803271550k1a841c9fwf8098e755968f663@mail.gmail.com> Message-ID: On 3/27/08, Jonathan Leffler wrote: > > > On Thu, Mar 27, 2008 at 3:08 PM, Gerardo Santana G?mez Garrido > wrote: > > What about this. Lets the first behavior be called as > > > > invl.hours > > > > while the second one would be invoked as > > > > invl.to_hours > > > > Is that useful? Make sense? > > > > Yes, and yes. What will invl.hours return for a negative interval? The > positive value? How will the user detect that the interval value was > negative? invl.hours would return a negative value for a negative Interval. That's what it means actually: -1 02:00:00.00 means -1 day -2 hours, just like -1 1/2 means -1 -1/2. > > I'm assuming that both methods return a number rather than an interval per > se. That's right. -- Gerardo Santana From gerardo.santana at gmail.com Sat Mar 29 23:47:56 2008 From: gerardo.santana at gmail.com (=?ISO-8859-1?Q?Gerardo_Santana_G=F3mez_Garrido?=) Date: Sat, 29 Mar 2008 21:47:56 -0600 Subject: [Ruby-informix-misc] shortcut for prepare + execute Message-ID: Hello everybody, There in CVS a shorcut to declare + open + iterate a cursor, contributed by Reid Morrison: Database#each and Database#each_hash. Here's an example: query = "select * from sysusers" Informix.connect(dbname) do |db| db.each(query) { |row| puts row.join('|') } # do something with row end Now I'm thinking of adding another shortcut, for prepare + execute. Right now, we have to do this: query = "update test set field = ? where id = ?" Informix.connect(dbname) do |db| db.prepare(query) { |st| st.execute(field, id) } end The proposal is to do something like this. query = "update test set field = ? where id = ?" Informix.connect(dbname) do |db| db.execute(query, field, id) # prepare + execute in a single step end Even though Database#execute is already an alias for immediate, I don't see any problem giving Database#execute this new behavior, because any previous script that used Database#execute thinking of it as Database#immediate, will still work: db.execute("create table test(id serial, desc varchar(30))") will still do the same. The drawback is that those scripts will lack whatever the optimization Informix does for EXEC SQL immediate versus EXEC SQL PREPARE + EXEC SQL EXECUTE. Anybody? -- Gerardo Santana