Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Can you expand an array of N to fill N functional arguments?

View previous topic View next topic Go down 
AuthorMessage
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

Can you expand an array of N to fill N functional arguments? Empty
PostSubject: Can you expand an array of N to fill N functional arguments?   Can you expand an array of N to fill N functional arguments? EmptyFri Jul 27, 2012 11:12 pm

I've made this abstract class other classes can implement to make themselves storable in a MPTT database scheme without having knowledge of the left/right juggling. In the new entry part of class, I need to be able to insert an arbitrary length hash (@attrs) into the database (using DBI). I can generate the SQL for N values, but when it comes to executing it the SQL DBI's execute() method will not expand an array to fill the arguments it is expecting.

I tried this first:
Code:

cursor = @dbd.prepare(
      "INSERT INTO #{ @table } (#{ @columns['left'] }, #{ @columns['right'] }, #{ @attrs.keys.join(', ') })
         VALUES (?, ?#{ ', ?' * @attrs.length })")
   
      cursor.execute(l, l + 1, @attrs.values)

But execute() fails for @attrs.length > 1

This works, but it's very hawkish:
Code:

exec_attrs = @attrs.keys.collect { |k| "@attrs['#{ k }']" }
   eval "cursor.execute(l, l + 1, #{ exec_attrs.join(', ') })"

So, any better way to do it? Aside from rewriting execute() to handle arrays the way I want, which I may end up doing.
Back to top Go down
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

Can you expand an array of N to fill N functional arguments? Empty
PostSubject: Re: Can you expand an array of N to fill N functional arguments?   Can you expand an array of N to fill N functional arguments? EmptyFri Jul 27, 2012 11:13 pm

I still don't have a general solution for this but there's a mechanism in DBI that fixes this instance:

Instead of

Code:

cursor.execute(l, l + 1, @attrs.values)

You can do

Code:

cursor.bind_param(1, l)
cursor.bind_param(2, l + 1)
@attrs.each_with_index { |at, idx|
    cursor.bind_param(idx + 3, at[1])
}

Much better.
Back to top Go down
Database
Member
Member
Database

Posts : 318
Join date : 2012-03-04

Can you expand an array of N to fill N functional arguments? Empty
PostSubject: Re: Can you expand an array of N to fill N functional arguments?   Can you expand an array of N to fill N functional arguments? EmptyFri Jul 27, 2012 11:13 pm

ralph l mayo, thanks for reply
code you pasted work fine
Back to top Go down
Sponsored content




Can you expand an array of N to fill N functional arguments? Empty
PostSubject: Re: Can you expand an array of N to fill N functional arguments?   Can you expand an array of N to fill N functional arguments? Empty

Back to top Go down
 

Can you expand an array of N to fill N functional arguments?

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Can you expand an array of N to fill N functional arguments? Edit-trash Useless :: Trash-