Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Apr 14, 2024
1 parent ff8e1fd commit 1e9eb31
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/main/java/typewriter/maria/MariaDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public void createDatabase(String url) {
* {@inheritDoc}
*/
@Override
public void commandLimitAndOffset(SQL builder, long limit, long offset) {
if (0 < limit) builder.write("LIMIT").write(limit);
public void commandLimitAndOffset(SQL sql, long limit, long offset) {
sql.limit(limit);
if (0 < offset) {
if (limit <= 0) builder.write("LIMIT 18446744073709551615");
builder.write(" OFFSET ").write(offset);
if (limit <= 0) sql.limit(Long.MAX_VALUE);
sql.offset(offset);
}
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/typewriter/query/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2023 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package typewriter.query;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import typewriter.api.Identifiable;
import typewriter.api.Specifier;

public abstract class Query<M extends Identifiable> {

private final List<String> select = new ArrayList();

/**
* Declare SELECT statement.
*
* @param names
*/
protected final void SELECT(Specifier<M, ?>... names) {
SELECT(Stream.of(names).map(Specifier::propertyName).toList());
}

/**
* Declare SELECT statement.
*
* @param names
*/
protected final void SELECT(String... names) {
SELECT(List.of(names));
}

/**
* Declare SELECT statement.
*
* @param names
*/
protected final void SELECT(List<String> names) {
select.addAll(names);
}

/**
* Test query.
*
* @param query
* @return
*/
public boolean is(String query) {
return query.equals(toString());
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

// SELECT
builder.append("SELECT ").append(select.stream().collect(Collectors.joining(", ")));

return builder.toString();
}
}
3 changes: 1 addition & 2 deletions src/main/java/typewriter/rdb/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public String commandUpdate() {
* @param offset
*/
public void commandLimitAndOffset(SQL sql, long limit, long offset) {
if (0 < limit) sql.write("LIMIT").write(limit);
if (0 < offset) sql.write("OFFSET").write(offset);
sql.limit(limit).offset(offset);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/typewriter/rdb/RDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected RDBQuery<M> createQueryable() {
*/
@Override
public long count() {
return new SQL<>(this).write("SELECT count(*) N").from(tableName).qurey().map(result -> result.getLong("N")).to().exact();
return new SQL<>(this).select("count(*)").from(tableName).qurey().map(result -> result.getLong(1)).to().exact();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/typewriter/rdb/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ public SQL<M> orderBy(Specifier<M, ?> specifier1, boolean ascending1, Specifier<
return this;
}

/**
* Write SELECT statement.
*
* @return
*/
public SQL<M> distinct(String... specifiers) {
text.append(" SELECT DISTINCT ").append(Stream.of(specifiers).collect(Collectors.joining(", ")));
return this;
}

/**
* Write SELECT statement.
*
* @return
*/
public SQL<M> select(String... specifiers) {
text.append(" SELECT ").append(Stream.of(specifiers).collect(Collectors.joining(", ")));
return this;
}

/**
* Write SELECT statement.
*
Expand All @@ -321,6 +341,16 @@ public SQL<M> avg(Specifier<M, ?> specifier, int windowSize) {
return this;
}

public SQL<M> limit(long size) {
if (0 < size) text.append(" LIMIT ").append(size);
return this;
}

public SQL<M> offset(long size) {
if (0 < size) text.append(" OFFSET ").append(size);
return this;
}

/**
* Write WHERE statement.
*
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/typewriter/query/QueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (C) 2023 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package typewriter.query;

import org.junit.jupiter.api.Test;

import typewriter.api.model.IdentifiableModel;

public class QueryTest {

@Test
void selectName() {
Query query = new Query() {
{
SELECT("test");
}
};

assert query.is("SELECT test");
}

@Test
void selectNames() {
Query query = new Query() {
{
SELECT("first", "second");
}
};

assert query.is("SELECT first, second");
}

@Test
void selectSpecifier() {
Query query = new Query<Person>() {
{
SELECT(Person::getName);
}
};

assert query.is("SELECT name");
}

@Test
void selectSpecifiers() {
Query query = new Query() {
{
SELECT("first", "second");
}
};

assert query.is("SELECT first, second");
}

/**
*
*/
static class Person extends IdentifiableModel {

public String name;

public int age;

/**
* Create empty model.
*/
public Person() {
}

/**
* @param name
* @param age
*/
public Person(String name, int age) {
this.name = name;
this.age = age;
}

/**
* Get the name property of this {@link Person}.
*
* @return The name property.
*/
public String getName() {
return name;
}

/**
* Get the age property of this {@link Person}.
*
* @return The age property.
*/
public int getAge() {
return age;
}
}
}

0 comments on commit 1e9eb31

Please sign in to comment.