artemave has asked for the wisdom of the Perl Monks concerning the following question:

Hi, all.

I use ActivePerl 5.8.8.817 with the latest Class::DBI::MSSQL, DBD::ODBC on Win2000 sp4. Connecting to MS SQL Server 2000.

Here is the issue:

DBI has very handy 'has_many' relationship. Upon adding new record with add_to_WHAT_WE_HAVE_MANY, 'MakeNewObj' accessor is internally called. This accessor is created using Class::DBI->set_sql(). Which calls Ima::DBI->set_sql() and finally DBI->prepare_cached().

Here is an example of SQL generated this way upon insertion values '2', 'Log', '3', 'artem2' into t_PostParams table (taken from profiler):
declare @P1 int set @P1=8 exec sp_prepexec @P1 output, N'@P1 bigint,@P2 nvarchar(3),@P3 bigint,@ +P4 nvarchar(6)', N'INSERT INTO t_PostParams (po_id, pp_name, pp_id, p +p_value) VALUES (@P1, @P2, @P3, @P4) ', 2, N'Log', 3, N'artem2' select @P1
So far, so good.

Now, I call add_to_post_params() one more time. Another record is being added. But no more statement is generated this time! The above one is used instead with the new data values:
exec sp_execute 8, 2, N'passwd', 4, N'121'
The result is obviously bad. There is (2, pas, 4, 121) record stored instead of (2, passwd, 4, 121). Mind that actual size of pp_name is 50.

My questions are:
1. Isn't it weird, that it generates sql according to the size of values being put instead of real field sizes?
2. If it is not. Shouldn't it cook another statement when sizes differ?
3. If it is not. There is a way to tell set_sql() to use prepare() instead of prepare_cached(), but I didn't find how to control this using CDBI interface. Any hints?

Thanks,
Artem A. Avetisyan.

Replies are listed 'Best First'.
Re: DBI->prepare_cached()
by perrin (Chancellor) on Jul 08, 2006 at 23:57 UTC
    I think it's because Sybase and MSSQL just don't work with prepare_cached. See this entry on the Class::DBI wiki.
Re: DBI->prepare_cached()
by Khen1950fx (Canon) on Jul 09, 2006 at 02:27 UTC
    I have to admit that your questions have given me a different perspective on DBI. Theoretically, some other table is accessing this table, so the answers to the first and second questions would probably be no; however, if you made some declarations with has_a before has_many, it seems to me the answer would be yes. In that case, I would go back and add another argument to see if that would get the desired result. It may also be that there is bug crawling around somewhere. Check out RT: http://rt.cpan.org/Public/Bug/Display.html?id=16493 http://rt.cpan.org/Public/Bug/Display.html?id=3305