funsies/query/yummy

Yummy! Yummy is the main query builder in funsies. It takes an approach I haven’t seen very often: stack-based. Basically, each operation (especially seen in the where function) operates on a stack. Meaning that instead of doing something like: and(equals("id", 2), not(equals("name", "Asher"))) You would do something like: equals(“id”, 2) |> equals(“name”, “Asher”) |> not() |> and() You catch my drift? Basically things like not pop a clause off the stack then wrap it in a Not and append it back. Pretty cool!

Types

Different types of equality.

pub type Eq {
  Equals(col: String, value: Value)
  Not(Eq)
  Or(Eq, Eq)
  And(Eq, Eq)
}

Constructors

  • Equals(col: String, value: Value)
  • Not(Eq)
  • Or(Eq, Eq)
  • And(Eq, Eq)

This is what is passed through thge pipeline to (eventually) create the SQL query

pub type QueryBuilder {
  QueryBuilder(
    table: schema.Table,
    select_columns: List(String),
    where_clauses: List(Eq),
    order_by_clauses: List(String),
    insert_columns: List(String),
    insert_values: List(Value),
  )
}

Constructors

  • QueryBuilder(
      table: schema.Table,
      select_columns: List(String),
      where_clauses: List(Eq),
      order_by_clauses: List(String),
      insert_columns: List(String),
      insert_values: List(Value),
    )

Value type. used for type checking as well as sql making

pub type Value {
  IntValue(Int)
  StringValue(String)
  BoolValue(Bool)
  ErrorValue(String)
}

Constructors

  • IntValue(Int)
  • StringValue(String)
  • BoolValue(Bool)
  • ErrorValue(String)

Passed thorugh the where pipeline

pub type WhereBuilder {
  WhereBuilder(table: schema.Table, clauses: List(Eq))
}

Constructors

  • WhereBuilder(table: schema.Table, clauses: List(Eq))

Functions

pub fn and(builder: WhereBuilder) -> WhereBuilder

Takes the two most recent things and makes an AND clause

pub fn equals(
  builder: WhereBuilder,
  col: String,
  value: a,
) -> WhereBuilder

This adds an Equal value to the stack where col is a string representing a column in your table and value is what it should be equal to. What is kinda cool is value can be anything but because of the types, it will automatically type check it and make sure the the type of v aligns with the type of the column that you specified. So handy!

pub fn insert(
  builder: QueryBuilder,
  values: List(Value),
) -> QueryBuilder

NOT CURRENTLY USED

pub fn new(table: Table) -> QueryBuilder

Initialize a new builder for the yummy.

pub fn not(builder: WhereBuilder) -> WhereBuilder

Pops a value off the stack and wraps it in a Not Used to negate the most recent value Use after like an equals or something

pub fn or(builder: WhereBuilder) -> WhereBuilder
pub fn order_by(
  builder: QueryBuilder,
  column: String,
  direction: String,
) -> QueryBuilder

Order by.

pub fn select(builder: QueryBuilder) -> QueryBuilder

This (currently) selects all columns. Mandatory. Will hopefully eventually allow you to only select some, or maybe select things from other tables

pub fn to_insert_sql(
  builder: QueryBuilder,
) -> Result(String, String)

Build the final SQL query whendoing an insert query.

pub fn to_sql(builder: QueryBuilder) -> Result(String, String)

Build the final sql query if using a select query

pub fn w(v: a) -> Value

Public function to wrap values.

pub fn wb(table: Table) -> WhereBuilder

This stands for where-builder. Makes a new WhereBuilder object

pub fn where(
  builder: QueryBuilder,
  condition: WhereBuilder,
) -> QueryBuilder

This is where you put the wherebuilder.

Search Document