Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
harris2012 committed Oct 6, 2023
1 parent c2dbba0 commit 8f83b38
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.panosen</groupId>
<artifactId>panosen-codedom-clickhouse</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>

<parent>
<groupId>com.panosen</groupId>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/panosen/codedom/clickhouse/Column.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.panosen.codedom.clickhouse;

public class Column {

private String columnName;

private String columnAs;

public String getColumnName() {
return columnName;
}

public void setColumnName(String columnName) {
this.columnName = columnName;
}

public String getColumnAs() {
return columnAs;
}

public void setColumnAs(String columnAs) {
this.columnAs = columnAs;
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/panosen/codedom/clickhouse/SelectSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class SelectSql extends Sql {
private String tableName;

/**
* select ${columnNameList} from table
* select ${columnList} from table
*/
private List<String> columnNameList;
private List<Column> columnList;

/**
* Only useful when limitSize is set.
Expand Down Expand Up @@ -64,12 +64,12 @@ public void setTableName(String tableName) {
this.tableName = tableName;
}

public List<String> getColumnNameList() {
return columnNameList;
public List<Column> getColumnList() {
return columnList;
}

public void setColumnNameList(List<String> columnNameList) {
this.columnNameList = columnNameList;
public void setColumnList(List<Column> columnList) {
this.columnList = columnList;
}

public Integer getLimitFrom() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.panosen.codedom.clickhouse.builder;

import com.panosen.codedom.clickhouse.Column;

public class ColumnBuilder {

private final Column column = new Column();

public Column getColumn() {
return column;
}

public ColumnBuilder name(String name) {
column.setColumnName(name);
return this;
}

public ColumnBuilder as(String as) {
column.setColumnAs(as);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ public SelectSql getSelectSql() {
return selectSql;
}

public SelectSqlBuilder column(String column) {
if (selectSql.getColumnNameList() == null) {
selectSql.setColumnNameList(Lists.newArrayList());
public ColumnBuilder column(String columnName) {
if (selectSql.getColumnList() == null) {
selectSql.setColumnList(Lists.newArrayList());
}
selectSql.getColumnNameList().add(column);
return this;

ColumnBuilder columnBuilder = new ColumnBuilder();
selectSql.getColumnList().add(columnBuilder.getColumn());

columnBuilder.name(columnName);

return columnBuilder;
}

public SelectSqlBuilder columns(String... columns) {
if (selectSql.getColumnNameList() == null) {
selectSql.setColumnNameList(Lists.newArrayList());
public SelectSqlBuilder columns(String... columnNames) {
if (selectSql.getColumnList() == null) {
selectSql.setColumnList(Lists.newArrayList());
}
for (String columnName : columnNames) {
ColumnBuilder columnBuilder = new ColumnBuilder();
selectSql.getColumnList().add(columnBuilder.getColumn());
columnBuilder.name(columnName);
}
selectSql.getColumnNameList().addAll(Lists.newArrayList(columns));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ public void generate(SelectSql selectSql, CodeWriter codeWriter, Parameters para
codeWriter.write(Keywords.SELECT).write(Marks.WHITESPACE);

// columns
if (selectSql.getColumnNameList() != null && !selectSql.getColumnNameList().isEmpty()) {
for (int index = 0, length = selectSql.getColumnNameList().size(); index < length; index++) {
codeWriter.write(Marks.BACKQUOTE).write(selectSql.getColumnNameList().get(index)).write(Marks.BACKQUOTE);
if (selectSql.getColumnList() != null && !selectSql.getColumnList().isEmpty()) {
for (int index = 0, length = selectSql.getColumnList().size(); index < length; index++) {
Column column = selectSql.getColumnList().get(index);
generateColumn(codeWriter, column);

if (index < length - 1) {
codeWriter.write(Marks.COMMA).write(Marks.WHITESPACE);
}
Expand Down Expand Up @@ -79,6 +81,15 @@ public void generate(SelectSql selectSql, CodeWriter codeWriter, Parameters para
codeWriter.write(Marks.SEMICOLON);
}

private static void generateColumn(CodeWriter codeWriter, Column column) {

codeWriter.write(Marks.BACKQUOTE).write(column.getColumnName()).write(Marks.BACKQUOTE);

if (!Strings.isNullOrEmpty(column.getColumnAs())) {
codeWriter.write(Marks.WHITESPACE).write(Keywords.AS).write(Marks.WHITESPACE).write(column.getColumnAs());
}
}

private void generateOrderBy(List<OrderBy> orderByList, CodeWriter codeWriter) {
if (orderByList == null || orderByList.isEmpty()) {
return;
Expand Down

0 comments on commit 8f83b38

Please sign in to comment.