Select and From

Step 1 - Select and From

A Builder assembles a complex object step by step through a fluent chain of method calls. Each method records one piece of the final result and returns the builder itself, so calls can be chained. Here you build a SQL SELECT query.

The QueryBuilder starts empty. Two setters are already written for you:

  • *select(cols) stores the chosen columns and returns self
  • from_(table) stores the table name and returns self

Implement build(). Join the columns with a comma and a space. If no columns were chosen, use a single star instead. Then return the string SELECT <cols> FROM <table>.

Hints
Starter code
class QueryBuilder:
    def __init__(self):
        self.columns = []
        self.table = None
        self.wheres = []
        self.order = None
        self._limit = None

    def select(self, *cols):
        self.columns = list(cols)
        return self

    def from_(self, table):
        self.table = table
        return self

    def build(self):
        """Return the SQL string: SELECT <cols> FROM <table>."""
        # TODO: turn the stored self.columns into one comma-separated piece of
        #   text, falling back to a single star when nothing was selected. Hand
        #   back a string made of the SELECT keyword, those columns, the FROM
        #   keyword and self.table, without changing any state along the way.
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.