All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tonivade.puredbc.sql.SQL1 Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2024, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.puredbc.sql;

import static com.github.tonivade.purefun.core.Precondition.checkNonEmpty;
import static com.github.tonivade.purefun.data.Sequence.arrayOf;

public final class SQL1 {

  private final String query;

  protected SQL1(String query) {
    this.query = checkNonEmpty(query);
  }

  public SQL bind(A a) {
    return new SQL(query, arrayOf(a));
  }

  public SQL1 from(SQL other) {
    return new SQL1<>(other.getQuery() + " from (" + query + ")");
  }

  public  SQL2 and(Condition condition) {
    return new SQL2<>(query + " and " + condition.expression());
  }

  public  SQL2 where(Condition condition) {
    return new SQL2<>(query + " where " + condition.expression());
  }

  public  SQL1 groupBy(Field field) {
    return new SQL1<>(query + " group by " + field.render());
  }

  public  SQL1 orderBy(Field field) {
    return new SQL1<>(query + " order by " + field.render());
  }

  public SQL1 asc() {
    return new SQL1<>(query + " asc");
  }

  public SQL1 desc() {
    return new SQL1<>(query + " desc");
  }

  public SQL1 limit(int limit) {
    return new SQL1<>(query + " limit " + limit);
  }

  public SQL1 offset(int offset) {
    return new SQL1<>(query + " offset " + offset);
  }

  @Override
  public String toString() {
    return String.format("SQL1{query='%s'}", query);
  }
}