Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table schema support #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/j256/ormlite/stmt/DeleteBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
*/
public class DeleteBuilder<T, ID> extends StatementBuilder<T, ID> {

private String tableSchema;

// NOTE: any fields here should be added to the clear() method below

public DeleteBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao) {
super(databaseType, tableInfo, dao, StatementType.DELETE);
}

public DeleteBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao, String schema) {
super(databaseType, tableInfo, dao, StatementType.DELETE);
this.tableSchema = schema;
}


/**
* Build and return a prepared delete that can be used by {@link Dao#delete(PreparedDelete)} method. If you change
* the where or make other calls you will need to re-call this method to re-prepare the statement for execution.
Expand Down Expand Up @@ -58,6 +66,10 @@ public void reset() {
@Override
protected void appendStatementStart(StringBuilder sb, List<ArgumentHolder> argList) {
sb.append("DELETE FROM ");
if (this.tableSchema != null){
databaseType.appendEscapedEntityName(sb, tableSchema);
sb.append(".");
}
databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
sb.append(' ');
}
Expand All @@ -66,4 +78,13 @@ protected void appendStatementStart(StringBuilder sb, List<ArgumentHolder> argLi
protected void appendStatementEnd(StringBuilder sb, List<ArgumentHolder> argList) {
// noop
}

/**
* Manually force table schema name to this query
* @return
*/
public DeleteBuilder<T, ID> setSchema(String tableSchema) {
this.tableSchema = tableSchema;
return this;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/j256/ormlite/stmt/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class QueryBuilder<T, ID> extends StatementBuilder<T, ID> {
private Long limit;
private Long offset;
private List<JoinInfo> joinList;
private String tableSchema;

// NOTE: anything added here should be added to the clear() method below

Expand All @@ -53,6 +54,13 @@ public QueryBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T
this.idField = tableInfo.getIdField();
this.selectIdColumn = (idField != null);
}

public QueryBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao, String tableSchema) {
super(databaseType, tableInfo, dao, StatementType.SELECT);
this.idField = tableInfo.getIdField();
this.selectIdColumn = (idField != null);
this.tableSchema = tableSchema;
}

/**
* This is used by the internal call structure to note when a query builder is being used as an inner query. This is
Expand Down Expand Up @@ -472,6 +480,10 @@ protected void appendStatementStart(StringBuilder sb, List<ArgumentHolder> argLi
appendSelects(sb);
}
sb.append("FROM ");
if (tableSchema != null){
databaseType.appendEscapedEntityName(sb, tableSchema);
sb.append(".");
}
databaseType.appendEscapedEntityName(sb, tableName);
sb.append(' ');
if (joinList != null) {
Expand Down Expand Up @@ -936,4 +948,13 @@ private JoinWhereOperation(WhereOperation whereOperation) {
this.whereOperation = whereOperation;
}
}

/**
* Manually force table schema name to this query
* @return
*/
public QueryBuilder<T, ID> setSchema(String tableSchema) {
this.tableSchema = tableSchema;
return this;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/j256/ormlite/stmt/UpdateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ public class UpdateBuilder<T, ID> extends StatementBuilder<T, ID> {

private List<Clause> updateClauseList = null;
// NOTE: anything added here should be added to the clear() method below
private String tableSchema;