Logan Capaldo wrote:
>
> Well after hacking away today for a couple of hours I've come up with
> the beginnings of a rudimentary ORM for KirbyBase. I'll post what I
> have so far, and people can let me know if
That was fast! :-)
> they are interested in a more complete version. It currently looks a
> lot like ActiveRecord, but obviously not as powerful. First it
> doesn't do ActiveRecords pluralization magic, although that should be
> easy to add. Secondly it doesn't have as many variations of find (but
> that's mitigated by KirbyBases's use of ruby syntax to perform
> selections). I've only implemented one kind of relationship
> (one-to-many, or is it many-to- one, some db guy can correct me on my
> terminology) using a ActiveRecord-esque has_many method. Some other
> problems include that it doesn't yet do ruby-esque getters and
> setters (ie. obj.something, and obj.something = value) instead it has
> get_something and set_something methods. I did this since I was just
> hacking away its easily remedied if anyone shows any interest in my
> continuing this. One other thing, it does not make use of KirbyBase's
> Link type yet, although if it did, it would probably be better. Again
> something to add if anyone really wants to use this. I learned a
> couple of things developing this, mostly that @@variables are evil,
> and that class instance vars are better. Without further ado here is
> the code. Its followed by some examples:
> % cat setup_example_db.rb
> require 'kirbybase'
> db = KirbyBase.new
>
> author_tbl = db.create_table(:author, :name, :String)
> book_tbl = db.create_table(:book, :title, :String, :author_id, :Integer)
>
> rn = author_tbl.insert(:name => "John Doe")
> book_tbl.insert(:title => "Ruby for Dummies", :author_id => rn)
> book_tbl.insert(:title => "ORM for REAL Dummies", :author_id => rn)
>
> % ruby setup_example_db.rb
>
> % cat has_many_example.rb
> require 'kirbyrecord'
>
> KirbyRecord::Base.open_database( )
>
> class Author < KirbyRecord::Base
> has_many :book
> end
>
> class Book < KirbyRecord::Base
> end
>
> john_doe = Author.find(:first) { |r| r.name =~ /John Doe/ }
> john_doe.get_book.each do |book|
> puts "#{john_doe.get_name} wrote #{book.get_title}"
> end
>
> % ruby has_many_example.rb
> John Doe wrote Ruby for Dummies
> John Doe wrote ORM for REAL Dummies
>
> And so on.
>
Very cool! I don't know much about ActiveRecord (looks like I have some
reading to do), but I like this. Just as a comparison, here's the way I
would rewrite the has_many example using KirbyBase's new Link_many feature:
author_tbl = db.create_table(:author, :name, :String,
:books, { :DataType => KBResultSet, :Link_many => [:recno, :book,
:author_id] })
book_tbl = db.create_table(:book, :title, :String, :author_id, :Integer)
rn = author_tbl.insert(:name => "John Doe")
book_tbl.insert(:title => "Ruby for Dummies", :author_id => rn)
book_tbl.insert(:title => "ORM for REAL Dummies", :author_id => rn)
john_doe = author_tbl.select { |r| r.name =~ /John Doe/ }.first
john_doe.books.each do |book|
puts "#{john_doe.name} wrote #{book.title}"
end
> Other neat things
>
> % cat setup_db.rb
> require 'kirbybase'
> db = KirbyBase.new
>
> person_tbl = db.create_table(:person, :name, :String, :age, :Integer)
>
>
> % ruby setup_db.rb
>
> % cat person_ex.rb
> require 'kirbyrecord'
>
> KirbyRecord::Base.open_database( )
>
> class Person < KirbyRecord::Base
> end
>
> person1 = Person.new
> person1.set_name "John Doe"
> person1.set_age 25
> person1.save
>
That's very cool!
>
> Other than that the only thing to mention is that
> KirbyRecord::Base.open_database( ) takes the same arguments as
> KirbyBase.new and that you should call it before inheriting from
> KirbyRecord::Base (incidently anyone have any ideas on how to avoid
> having to do this?). Also KirbyRecord::Base#find can take a block
> just like KBTable#select. There are probably lots and lots of bugs.
Very nice work.
Jamey
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.