LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 September 30 2010

arithma
Member

[Exercise] SQL-Like Interpreter

This is a long term and open ended exercise. We need to implement a parser and interpreter of an SQL-like language that works over a set of tables or mapped arrays. Use the language of your own choice.

Offline

#2 September 30 2010

saeidw
Member

Re: [Exercise] SQL-Like Interpreter

I'd definitely like to work on this one! I've done parsing and interpretation before but it should be interesting with the SQL syntax

Offline

#3 September 30 2010

arithma
Member

Re: [Exercise] SQL-Like Interpreter

As xterm was suggesting, I should have limited the use of language features that support SQL and the sort, also a constraint on how much cross platform, cross compiler portability is needed.

The most extreme form of the exercise would be to implement it in a native language based on string processing, with extreme cross platform and cross compiler portability.

Offline

#4 September 30 2010

xterm
Moderator

Re: [Exercise] SQL-Like Interpreter

Another suggestion would be going for/implementing an internal DSL, or an external CL interpreter/parser.

Offline

#5 September 30 2010

rolf
Member

Re: [Exercise] SQL-Like Interpreter

I like this exercise.

Offline

#6 September 30 2010

mrmat
Member

Re: [Exercise] SQL-Like Interpreter

Yep, this one is interesting, i'll probably give it a go.

Offline

#7 September 30 2010

arithma
Member

Re: [Exercise] SQL-Like Interpreter

Let's all work on two solutions in parallel. One of our own, and one which we'll discuss here.
How does C# sound for a language of choice? Can we design a solution that is line-by-line portable to Java? How about C++. Where could C stand from this portability.
We need what is usually called in RDBMS a schema for our database. It may simply be integer indexes into different arrays, or string keys into a dictionary/map/(whatever your language calls it).
There's lex and yacc, flex and bison for grammar parsing, but I am leaning towards a backtracking recursive parser based on a language definition written on paper.

Offline

#8 September 30 2010

saeidw
Member

Re: [Exercise] SQL-Like Interpreter

I think a nice recursive descent parser would be fun and educational if we specify the language in a simple way, that way we can just turn the BNF into code.

C#/Java/C++ is fine with me. I wrote my last one in C and it was simple but you have to REALLY pay attention to your pointers.

As for the database, what do you think about flat text files? Each table could be represented by a text file, and the SQL queries translate into various string processing routines on the files? I don't think that's the best way but I'm just putting it out there and hopefully one of you guys can improve/replace it.

Offline

#9 September 30 2010

Joe
Member

Re: [Exercise] SQL-Like Interpreter

I agree with text files for storage, and we could even organize the data as XML or (even better) YAML files. YAML is particularly easy to parse (though maybe not very fast, I think).

As for the language, I would suggest a scripting language, preferably Python.

Offline

#10 December 22 2011

xterm
Moderator

Re: [Exercise] SQL-Like Interpreter

Although not a direct answer/solution to the exercise. I'm bringing this old code block from a different topic simply to because this thread has no code whatsoever.

-

Here we go, an initial pass at an internal SQL DSL using Expression Based DSL in Groovy 1.8+ (GEP3)

You can run it here.

class SQL {
    def tables = [:]
   
    def add(table){
        [with: {content -> tables[table] = content}]   
    }
   
    def describe(table){
        println tables[table][0]   
    }
   
    def update(table){
        [set: { map ->     map.keySet().each { key ->
                              def col_idx = tables[table][0].indexOf(key);
                              tables[table][1..-1].eachWithIndex {
                                 item, idx-> item[col_idx] =
                                     (map."$key" instanceof Closure)?map."$key"(item[col_idx]):map."$key"
                              }}}]
    }
   
    def show(table){
        println tables[table]
    }
}

users = [
    ["id", "name", "password", "created"],
    [1,    "john", "doe",      new Date()],
    [2,    "jane", "smith",    new Date()],
    [3,    "foo",  "bar",      new Date()]
]

sql = new SQL()

sql.with {
    add "users" with users
    describe "users"
    update "users" set name: "baba"
    show "users"
    update "users" set id : {x -> x * 10}, name : "xterm"
    show "users"
}

Visualization (Can't be tested on Web Console)

Offline

Board footer