[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

recommendations for working with PostgreSQL

Chad Perrin

10/16/2007 10:56:00 PM

I have need to work with PostgreSQL via Ruby. What gems/modules do you
recommend? Are there any quickstart-type howtos online to which you
might direct me?

Thanks in advance.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
awj @reddit: "The terms never and always are never always true."

12 Answers

Reid Thompson

10/16/2007 11:09:00 PM

0



Chad Perrin wrote:
> I have need to work with PostgreSQL via Ruby. What gems/modules do you
> recommend? Are there any quickstart-type howtos online to which you
> might direct me?
>
> Thanks in advance.
>
og www.nitroproject.org

$ cat testog.rb
require 'og'

class Comment
property :title, String
property :body, String
property :author, String
property :create_time, Time
end

og_psql = {
:destroy => false,
:store => :psql,
:user => 'rthompso',
:address => '127.0.0.1',
:password => 'rthompso',
:name => 'test'
}

db =Og.setup(og_psql)


t1= Time.now
# save the object in the database
db.store.start
1.upto(10000) { |i|
c = Comment.new
c.title = 'Hello'
c.body = 'World'
c.create_time = Time.now
c.author = 'tml'
c.save
}
db.store.commit
puts Time.now - t1

-----------------this is on cygwin windows ----------------------

$ ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-cygwin]

------------------------------------------------------
cyberhome: /home/rthompso>
$ export RUBYOPT=rubygems
cyberhome: /home/rthompso>
$ ruby testog.rb
INFO: Og uses the Psql store.
NOTICE: CREATE TABLE will create implicit sequence "ogcomment_oid_seq"
for serial column "ogcomment.oid"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"ogcomment_pkey" for table "ogcomment"
INFO: Created table 'ogcomment'.
10.697

cyberhome: /home/rthompso>
$ psql -U rthompso -h 127.0.0.1 -c "select count(*) from ogcomment" test
count
-------
10000
(1 row)



Reid Thompson

10/16/2007 11:22:00 PM

0

Reid Thompson wrote:
>
>
> Chad Perrin wrote:
>> I have need to work with PostgreSQL via Ruby. What gems/modules do you
>> recommend? Are there any quickstart-type howtos online to which you
>> might direct me?
>>
>> Thanks in advance.
>>
> og www.nitroproject.org
>
> $ cat testog.rb
> require 'og'
>
> class Comment
> property :title, String
> property :body, String
> property :author, String
> property :create_time, Time
> end
>
> og_psql = {
> :destroy => false,
> :store => :psql,
> :user => 'rthompso',
> :address => '127.0.0.1',
> :password => 'rthompso',
> :name => 'test'
> }
>
> db =Og.setup(og_psql)
>
>
> t1= Time.now
> # save the object in the database
> db.store.start
> 1.upto(10000) { |i|
> c = Comment.new
> c.title = 'Hello'
> c.body = 'World'
> c.create_time = Time.now
> c.author = 'tml'
> c.save
> }
> db.store.commit
> puts Time.now - t1
>
> -----------------this is on cygwin windows ----------------------
>
> $ ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-cygwin]
>
> ------------------------------------------------------
> cyberhome: /home/rthompso>
> $ export RUBYOPT=rubygems
> cyberhome: /home/rthompso>
> $ ruby testog.rb
> INFO: Og uses the Psql store.
> NOTICE: CREATE TABLE will create implicit sequence "ogcomment_oid_seq"
> for serial column "ogcomment.oid"
> NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
> "ogcomment_pkey" for table "ogcomment"
> INFO: Created table 'ogcomment'.
> 10.697
>
> cyberhome: /home/rthompso>
> $ psql -U rthompso -h 127.0.0.1 -c "select count(*) from ogcomment" test
> count
> -------
> 10000
> (1 row)
>
>
I just updated og...
updated to reflect a few changes...
I'm sure there are others that can give better examples..

require 'og'

class Comment
attr_accessor :title, String
attr_accessor :body, String
attr_accessor :author, String
attr_accessor :create_time, Time
end

og_psql = {
:destroy => false,
:store => :postgresql,
:user => 'rthompso',
:address => '127.0.0.1',
:password => 'rthompso',
:name => 'test'
}

db =Og.setup(og_psql)


t1= Time.now
# save the object in the database
db.store.start
1.upto(10000) { |i|
c = Comment.new
c.title = 'Hello'
c.body = 'World'
c.create_time = Time.now
c.author = 'tml'
c.save
}
db.store.commit
puts Time.now - t1



Ben Bleything

10/17/2007 2:21:00 AM

0

On Wed, Oct 17, 2007, Chad Perrin wrote:
> I have need to work with PostgreSQL via Ruby. What gems/modules do you
> recommend? Are there any quickstart-type howtos online to which you
> might direct me?

If you're looking for an ORM, there is of course ActiveRecord, and
someone else already offered up Og.

As far as the driver goes, the ruby-postgres gem seems to be the way to
go.

Ben

Sharon Rosner

10/17/2007 4:44:00 AM

0

> I have need to work with PostgreSQL via Ruby. What gems/modules do you
> recommend? Are there any quickstart-type howtos online to which you
> might direct me?

First you need to install the postgres driver:

sudo gem install postgres

As far as ORM's go, may I suggest Sequel:

require 'sequel/postgres'

DB = Sequel('postgres://rthompso:rthompso@127.0.0.1/test')

DB.create_table :items do # Create a new table
column :name, :text
column :price, :float
end

items = DB[:items] # Create a dataset

# Populate the table
items << {:name => 'abc', :price => rand * 100}
items << {:name => 'def', :price => rand * 100}
items << {:name => 'ghi', :price => rand * 100}

puts "Count of items: #{items.count}"

puts "All items:"
items.reverse_order(:price).print

puts "The average price is: #{items.avg(:price)}"

puts "All items with price under 50"
items.filter {:price < 50}.print

You can find more information here:

http://code.google.com/p/ru...

Or on sequel-talk:

http://groups.google.com/group/s...

best
Sharon


Chad Perrin

10/17/2007 3:37:00 PM

0

On Wed, Oct 17, 2007 at 11:21:15AM +0900, Ben Bleything wrote:
> On Wed, Oct 17, 2007, Chad Perrin wrote:
> > I have need to work with PostgreSQL via Ruby. What gems/modules do you
> > recommend? Are there any quickstart-type howtos online to which you
> > might direct me?
>
> If you're looking for an ORM, there is of course ActiveRecord, and
> someone else already offered up Og.

I'm not necessarily looking for ORM, though now that I think about it I
can't help but wonder if every single programmatic interface between Ruby
and a relational database might qualify in some manner as ORM (since
"everything is an object" in Ruby). In the Perl world, I'd tend to use
DBI for all things database-y, and was wondering what the preferred
approach would be in the Ruby world especially in the case of PostgreSQL.


>
> As far as the driver goes, the ruby-postgres gem seems to be the way to
> go.

Thanks for confirming that.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
W. Somerset Maugham: "The ability to quote is a serviceable substitute for
wit."

Chad Perrin

10/17/2007 3:38:00 PM

0

On Wed, Oct 17, 2007 at 01:44:05PM +0900, Sharon Rosner wrote:
> > I have need to work with PostgreSQL via Ruby. What gems/modules do you
> > recommend? Are there any quickstart-type howtos online to which you
> > might direct me?
>
> First you need to install the postgres driver:
>
> sudo gem install postgres
>
> As far as ORM's go, may I suggest Sequel:

Thanks. I'll look into it.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Brian K. Reid: "In computer science, we stand on each other's feet."

Chad Perrin

10/17/2007 3:40:00 PM

0

On Wed, Oct 17, 2007 at 08:22:27AM +0900, Reid Thompson wrote:
>
> require 'og'

Thanks. I'll look into it.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Baltasar Gracian: "A wise man gets more from his enemies than a fool from
his friends."

Thomas Adam

10/17/2007 5:38:00 PM

0

On 17/10/2007, Chad Perrin <perrin@apotheon.com> wrote:
> On Wed, Oct 17, 2007 at 11:21:15AM +0900, Ben Bleything wrote:
> > On Wed, Oct 17, 2007, Chad Perrin wrote:
> > > I have need to work with PostgreSQL via Ruby. What gems/modules do you
> > > recommend? Are there any quickstart-type howtos online to which you
> > > might direct me?
> >
> > If you're looking for an ORM, there is of course ActiveRecord, and
> > someone else already offered up Og.
>
> I'm not necessarily looking for ORM, though now that I think about it I
> can't help but wonder if every single programmatic interface between Ruby
> and a relational database might qualify in some manner as ORM (since
> "everything is an object" in Ruby). In the Perl world, I'd tend to use

But then, just because the language you're using means everything is
an object, does not mean to say that when you connect to a database
and start pulling stuff out of it that what you get is ORM. ORM is an
interface your application would define for itself, mapping to the
underlying tables/views/whatever in the database.

Ruby makes this easier in that you could just (at a crude level) map
an object you define to have properties, behaviour and scope of the
underlying tables, etc., in the database. Sometimes there is merit to
that -- and indeed, coming into this are things like patterns (the
factory pattern in particular is useful in ORM scenarios.)

> DBI for all things database-y, and was wondering what the preferred
> approach would be in the Ruby world especially in the case of PostgreSQL.

Having written numerous libraries (well, OK, two) in both perl and
ruby, I can honestly say that whilst it's somewhat "easier" in perl
(you're not forced to go down the abstracted route of ORM design) you
lose a lot of functionality -- the scope of what you can write in Perl
diminishes since the interface subroutines rarely map in an ORM way
[1]. Look into it heavily with Ruby -- it will make life easier.

-- Thomas Adam

[1] Anyone who things writing OO Perl is a good thing for this; think
again. OO perl is just broken.

Chad Perrin

10/17/2007 6:01:00 PM

0

On Thu, Oct 18, 2007 at 02:37:39AM +0900, Thomas Adam wrote:
> On 17/10/2007, Chad Perrin <perrin@apotheon.com> wrote:
> >
> > I'm not necessarily looking for ORM, though now that I think about it I
> > can't help but wonder if every single programmatic interface between Ruby
> > and a relational database might qualify in some manner as ORM (since
> > "everything is an object" in Ruby). In the Perl world, I'd tend to use
>
> But then, just because the language you're using means everything is
> an object, does not mean to say that when you connect to a database
> and start pulling stuff out of it that what you get is ORM. ORM is an
> interface your application would define for itself, mapping to the
> underlying tables/views/whatever in the database.
>
> Ruby makes this easier in that you could just (at a crude level) map
> an object you define to have properties, behaviour and scope of the
> underlying tables, etc., in the database. Sometimes there is merit to
> that -- and indeed, coming into this are things like patterns (the
> factory pattern in particular is useful in ORM scenarios.)

Thus, my "in some manner" qualification. The comment about all
programmatic DB interfaces in Ruby being ORMs was really a throw-away,
not exactly meant to be taken in strictly literal fashion. Sorry if that
wasn't clear.


>
> > DBI for all things database-y, and was wondering what the preferred
> > approach would be in the Ruby world especially in the case of PostgreSQL.
>
> Having written numerous libraries (well, OK, two) in both perl and
> ruby, I can honestly say that whilst it's somewhat "easier" in perl
> (you're not forced to go down the abstracted route of ORM design) you
> lose a lot of functionality -- the scope of what you can write in Perl
> diminishes since the interface subroutines rarely map in an ORM way
> [1]. Look into it heavily with Ruby -- it will make life easier.

Thanks for the advice. I shall (look into it heavily).


>
> [1] Anyone who things writing OO Perl is a good thing for this; think
> again. OO perl is just broken.

I certainly don't disagree much. It's not any more broken, really, than
OOP in languages like C++, but they're pretty broken in that regard too.
Just my opinion, of course. In general, I love Perl -- but in terms of
its OO constructs, I do not love it.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Patrick J. LoPresti: "Emacs has been replaced by a shell script which 1)
Generates a syslog message at level LOG_EMERG; 2) reduces the user's disk
quota by 100K; and 3) RUNS ED!!!!!!"

Chad Perrin

10/17/2007 6:32:00 PM

0

On Thu, Oct 18, 2007 at 12:39:33AM +0900, Chad Perrin wrote:
> On Wed, Oct 17, 2007 at 08:22:27AM +0900, Reid Thompson wrote:
> >
> > require 'og'
>
> Thanks. I'll look into it.

I'm having a difficult time finding anything about Og itself. I find a
lot of stuff in Google that seems to be related to it, but only leads me
to Nitro with little or no mention of Og. What am I missing?

It should be noted that I'm not writing a web app at this time, so if Og
is somehow specific to Nitro, it's probably not what I need.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
McCloctnick the Lucid: "The first rule of magic is simple. Don't waste your
time waving your hands and hopping when a rock or a club will do."