Skip to content

Use Erlang Records to construct SQL queries #1

@choptastic

Description

@choptastic

I think the Erlang Record structure would be an interesting way to contruct SQL queries. Each record would represent a type of query (#select, #update, #delete, etc), and the attributes within each would represent the different clauses of the query:

The simple example:

#select{
    fields=[username, email],
    from=user,
    where={userid, Userid},
}

This presents all kinds of potential issues, however.

  • How to represent and or or in where clauses. I'm feeling prefix notation for that: {'or', [{email, Email}, {username, Username}]}
  • How about joins? Listing the tables in from as a list ([table1, table2, table3] then having to use the where clause to join is a bit nasty. Specifying a table as a 2 or 3-tuple as follows seems interesting:
#select{
    fields=[userid, username, email]
    from=[
        user,
        {inner, post, userid},
        {inner, thread, {thread@id, post@threadid}}
    ]
}

Would basically translate to Select userid, username, email from user inner join post using userid inner join thread on thread.id=post.threadid

Subqueries are simple here, using #select records in place of table names in queries, which can add to some interesting and convenient Erlang-based query composition without having to screw around with iolists and concatenation.

Anyway, food for thought!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions