ActiveRecord outside of rails

I have a few questions regarding using activerecord outside of rails.

Usage is with legacy databases, which gives rise to some of the
problems.

Probably the first, and most simple question though, is how to capture
the SQL log, with which I can hopefully figure the other stuff out.

I’m currently trying

ActiveRecord::Base.logger = Logger.new STDOUT

Based on random googles, but it doesn’t seem to give me anything.

Charles L. wrote:

I have a few questions regarding using activerecord outside of rails.

Usage is with legacy databases, which gives rise to some of the
problems.

Probably the first, and most simple question though, is how to capture
the SQL log, with which I can hopefully figure the other stuff out.

I’m currently trying

ActiveRecord::Base.logger = Logger.new STDOUT

Based on random googles, but it doesn’t seem to give me anything.

Is there a different list I could perhaps put this question too?

On Dec 15, 2006, at 11:00 PM, Guest wrote:

I’m currently trying

ActiveRecord::Base.logger = Logger.new STDOUT

Based on random googles, but it doesn’t seem to give me anything.

Is there a different list I could perhaps put this question too?

I used this successfully for a script recently:

ActiveRecord::Base.logger = Logger.new( “my_app.log” )

Does that help?

Craig

ActiveRecord::Base.logger is passed into your connection adapter at the
time of instantiation. If you change your class “logger” object, it is
not reflected in the adapter. This is a bug in rails IMO.

For a temporary workaround you can use:
ModelClass.connection.instance_eval( “@logger=Logger.new( STDOUT )” )

I will submit ticket and patch tomorrow.

Zach

Does this mean if I write
ActiveRecord::Base.logger = …

before I define my class:
class Document < ActiveRecord::Base

That it should work also?
At any rate, trying that, and what you suggested doesn’t give me
anything.
Maybe I’m mis-understanding it:

$log = Logger.new STDOUT
$log.level # => 0
$log.warn ‘hello world’ # => hello world
ActiveRecord::Base.logger = $log
Document.connection.instance_eval(’@logger = $log’)

test some more

Document.connection.instance_eval(’@logger’).warn ‘hello world’ # =>
hello world

now this should output some sql no?

Document.find :first

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guest wrote:

ActiveRecord::Base.logger = Logger.new STDOUT

Based on random googles, but it doesn’t seem to give me anything.

Is there a different list I could perhaps put this question too?

ActiveRecord::Base.logger is passed into your connection adapter at the
time of instantiation. If you change your class “logger” object, it is
not reflected in the adapter. This is a bug in rails IMO.

For a temporary workaround you can use:
ModelClass.connection.instance_eval( “@logger=Logger.new( STDOUT )” )

I will submit ticket and patch tomorrow.

Zach

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFg4mKMyx0fW1d8G0RAoKyAJ48As1jEWD8ftitmmLeXRjNxfjTTwCcCDyG
uLkgNwkzXzs8VpCBI8tU9Cw=
=RYig
-----END PGP SIGNATURE-----

First of all, thanks for your help, I’ve been trying to figure this out
for about a week with little success. Seems like it (AR) could be a
really nice abstraction, I just haven’t got it working quite right
yet…

zdennis wrote:

ActiveRecord::VERSION::STRING
=> “1.14.4”

What version of ActiveRecord are you using?

Zach

For me,
ActiveRecord::VERSION::STRING
=> “1.14.4”

FWIW, i’m using ruby 1.8.4, on cygwin.
I installed AR using rubygems, then installed OpenLink’s ODBC connection
adapter (is it the adapter’s job to send the SQL to the logger?) and
finally monkey-patched it a bit, in a similar fashion to dbi’s ODBC
class, to allow it to use odbc_utf8, and to allow the use of simple dsns
in string form.

I connect using:
ActiveRecord::Base.establish_connection :adapter => ‘odbc’,
:dsn => ‘driver=Microsoft Access Driver (*.mdb); dbq=’ +
mdb_filename
(All this is in aid of writing a script that works on someone else’s
Access database.)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles L. wrote:

Does this mean if I write
ActiveRecord::Base.logger = …

before I define my class:
class Document < ActiveRecord::Base

That it should work also?

Yes. It should also work if you define it on
ActiveRecord::Base.connection after you define your models as well.

Document.connection.instance_eval(‘@logger’).warn ‘hello world’ # =>
hello world

now this should output some sql no?

Document.find :first

Yes it should.

ActiveRecord::Base.logger = Logger.new( STDOUT )
=> #<Logger:0xb7754e70 @formatter=nil,
@default_formatter=#<Logger::Formatter:0xb7754e48 @datetime_format=nil>,
level0,
prognamenil, logdev#<Logger::LogDevice:0xb7754e20 @shift_age=nil,
@filename=nil,
@mutex=#<Logger::LogDevice::LogDeviceMutex:0xb7754df8
@mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0,
@mon_owner=nil>, dev#<IO:0xb7cce02c, @shift_size=nil>
class Developer < ActiveRecord::Base ; end
=> nil
Developer.count
SQL (0.058811) SET NAMES ‘utf8’
Developer Columns (0.183238) SHOW FIELDS FROM developers
SQL (0.001609) SELECT count(*) AS count_all FROM developers
=> 3
Developer.find( :all )
Developer Load (0.024063) SELECT * FROM developers
=> [#<Developer:0xb7744390 @attributes={“name”=>“Zach D.”,
“updated_at”=>nil, “salary”=>“1”, “id”=>“1”, “created_at”=>nil},
#<Developer:0xb7744354 @attributes={“name”=>“John D.”,
“updated_at”=>nil, “salary”=>“2”, “id”=>“2”, “created_at”=>nil},
#<Developer:0xb7744318 @attributes={“name”=>nil, “updated_at”=>nil,
“salary”=>“3”, “id”=>“3”, “created_at”=>nil}]
ActiveRecord::VERSION::STRING
=> “1.14.4”

What version of ActiveRecord are you using?

Zach

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFhoc2Myx0fW1d8G0RAmanAJ4j+CczQ5zhBo5SwQ+1B1ta4jOZLQCdHx3O
3NVayDtqlqqlW9Qpa9pO3ow=
=YOG4
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charles L. wrote:

For me,
ActiveRecord::VERSION::STRING
=> “1.14.4”

FWIW, i’m using ruby 1.8.4, on cygwin.
I installed AR using rubygems, then installed OpenLink’s ODBC connection
adapter (is it the adapter’s job to send the SQL to the logger?)

Yes it is the adapters responsbility to log the SQL. This usually
occurs in an ‘execute’ method on the implemented adapter.

Can you send me your adapter code ?

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFhyK/Myx0fW1d8G0RAnqvAJ0QGW4u66eBk/6YJfFF99tkNhzraQCfULZc
D5Id7mZrTRyLBGKg7YdKe98=
=PecN
-----END PGP SIGNATURE-----

Hi,

I saw your Dec. 2006 post that mentioned connecting via ‘ODBC’ to a
Microsoft Access db. I know it was a while ago but I am new to Rails
and would like to connect to a legacy systems Access db and could use
some help. I have installed the adapters but I am unsure of what
other things need to be done. Does the “establish_connection” go in a
model named for the Access db? What if anything goes in the database
yml?
Thanks.

Lewis

On Dec 18 2006, 6:41 pm, Charles L. <rails-mailing-l…@andreas-

zdennis wrote:

Yes it is the adapters responsbility to log the SQL. This usually
occurs in an ‘execute’ method on the implemented adapter.

Can you send me your adapter code ?

Zach

Ahh, so that’d be the problem then. I see the SQLite adapter is doing
some definite logging:

sqlite:

def execute(sql, name = nil) #:nodoc:
catch_schema_changes { log(sql, name) { @connection.execute(sql) } }
end

odbc:

def execute(sql, name = nil)
@logger.unknown(“ODBCAdapter#execute>”) if @@trace
@logger.unknown(“args=[#{sql}|#{name}]”) if @@trace
if sql =~ /^\s*INSERT/i && [:microsoftsqlserver,
:virtuoso].include?(@dbmsName)
# Guard against IDENTITY insert problems caused by explicit inserts
# into autoincrementing id column.
insert(sql, name)
else
begin
@connection.do(sql)
rescue Exception => e
@logger.unknown(“exception=#{e}”) if @@trace
raise StatementInvalid, e.message
end
end
end

Further more, while the sqlite functions such as #select_all, go through
execute, the odbc version doesn’t.
I suppose I can set @@trace variable…
Is there an official odbc connector?