C# – Make your own query language

59101739

Well, most probably the answer depends on the query language 🙂

Here is the task:

Problem 3 – A query language over a CSV file

A CSV files (comma separated values) can be represented as a table.

For example this CSV file:

Can be represented by this table:

id name course
1 Rado Haskell

We are going to use CSV files as tables and make a simple query language for fetching data.

In a language of your choice, make a program that:

  • Reads a CSV file.
  • Gives the user an interactive input for queries.
  • Answers the queries with data from the CSV file.

The queries, that should be supported are:

  • SELECT [columns] LIMIT X – where you can SELECT without giving the LIMIT. Then this will fetch all rows.
  • SUM [column] – returns the sum of all integers in the given column.
  • SHOW – returns a list of all column names in your data.
  • FIND X – returns all rows, that has X in some of their cells (Match X with at least one of the columns). If X is a string, search for every string, that contains X as a substring.

Here are examples of all queries.

Consider that we have loaded the following CSV:

Now, let’s make queries:

Features

  • If the query is non-valid – say it. Don’t crash the program.
  • As you see in the examples, the results should be displayed in visual, tabular way. This is up to you. You don’t have to follow the styles of the example.

What I did? Check by yourself:

query2

And my favourite – here comes the code 🙂

Enjoy it.

Tagged with: , , , ,