tags:

views:

266

answers:

4

First of all, I'm an Erlang rookie here. I need to interface with a MySQL database and I found the erlang-mysql-driver. I'm trying that out, and am a little confused by some of the syntax.

I can get a row of data from the database with this (greatly oversimplified for brevity here):

Result = mysql:fetch(P1, ["SELECT column1, column2 FROM table1 WHERE column2='", Key, "'"]),  
case Result of  
    {data, Data} ->  
        case mysql:get_result_rows(Data) of  
            [] -> not_found;  
            Res ->  
              %% Now 'Res' has the row

So now here is an example of what `Res' has:

[[<<"value from column1">>, <<"value from column2">>]]

I get that it's a list of records. In this case, the query returned 1 row of 2 columns.

My question is:
What do the << and >> symbols mean? And what is the best (Erlang-recommended) syntax for turning a list like this into a records which I have defined like:

-record(  
    my_record,  
    {   
      column1 = ""  
      ,column2 = ""  
    }  
 ).
+2  A: 

These are bit string comprehensions.

Bit string comprehensions are analogous to List Comprehensions. They are used to generate bit strings efficiently and succinctly.

Bit string comprehensions are written with the following syntax:

<< BitString || Qualifier1,...,QualifierN >>

BitString is a bit string expression, and each Qualifier is either a generator, a bit string generator or a filter.

• A generator is written as:

 Pattern <- ListExpr.

ListExpr must be an expression which evaluates to a list of terms.

• A bit string generator is written as:

 BitstringPattern <= BitStringExpr.

BitStringExpr must be an expression which evaluates to a bitstring.

• A filter is an expression which evaluates to true or false. The variables in the generator patterns shadow variables in the function clause surrounding the bit string comprehensions.

A bit string comprehension returns a bit string, which is created by concatenating the results of evaluating BitString for each combination of bit string generator elements for which all filters are true.

Example:

1> << << (X*2) >> || 
<<X>> <= << 1,2,3 >> >>.
<<2,4,6>>
Robert Harvey
I see. Thanks for the reply. I'm going to try to write a MySQL Row -> Record function now and see what happens...
marcc
And in my case, I don't have any filters or generators, so it looks like I can simply call erlang:bitstring_to_list/1. Thanks very much!
marcc
The most important meaning of the << and >> is to denote binaries itself. There is no sign of bit string comprehension in the question!
gleber
+6  A: 

In your specific example, you can do the conversion by matching on the returned column values, and then creating a new record like this:

case mysql:get_result_rows(Data) of
  [] ->
    not_found;  
  [[Col1, Col2]] ->  
    #my_record{column1 = Col1, column2 = Col2}
end
Zed
+6  A: 

Just a small note: the results are not bit string comprehensions per see, they are just bit strings. However you can use bit string comprehensions to produce a sequence of bit strings (which is described above with the generators and that), much like lists and lists comprehensions.

you can use erlang:binary_to_list/1 and erlang:list_to_binary/1 to convert between binary and strings (lists).

The reason the mysql driver returns bit strings is probably because they are much faster to manipulate.

/Mazen

Mazen Harake
i found bitstring_to_list was working. i'll have to look up binary_to_list also. i did manage to get my code working, but i'm always looking to make it more correct.
marcc
+1  A: 

I hate to see the wrong answer accepted as the correct answer. Mazen Harake is correct on this count.

David Mercer
I should also mention I'd have upvoted you, Mazen, but I need reputation to do that, and my above comment is the first thing I have ever done on Stack Overflow. I can't be bothered figuring out how to gain reputation, since if marcc was happy with the answer, who am I to say otherwise?
David Mercer
Fixed. You are right.
marcc