Skip to content

Commit

Permalink
Light refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Osolovskiy committed Jul 26, 2015
1 parent 0a920a5 commit 63c104d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
15 changes: 12 additions & 3 deletions src/main/java/ftldb/ext/sql/ArrayModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.*;

import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;


/**
Expand Down Expand Up @@ -65,4 +64,14 @@ public int size() {
}


/**
* Returns the empty list. Iteration through the {@code super.values()} list causes an exception.
*
* @return the empty list
*/
public TemplateCollectionModel values() {
return new SimpleCollection(new ArrayList(0), wrapper);
}


}
23 changes: 23 additions & 0 deletions src/main/java/ftldb/ext/sql/ClobModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.SimpleCollection;
import freemarker.template.TemplateCollectionModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;


/**
Expand Down Expand Up @@ -54,4 +57,24 @@ public String getAsString() throws TemplateModelException {
}


/**
* Returns the clob size.
*
* @return the number of characters
*/
public int size() {
return string.length();
}


/**
* Returns the empty list. Iteration through the {@code super.values()} list causes an exception.
*
* @return the empty list
*/
public TemplateCollectionModel values() {
return new SimpleCollection(new ArrayList(0), wrapper);
}


}
51 changes: 49 additions & 2 deletions src/main/java/ftldb/ext/sql/FetchedResultSetModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
package ftldb.ext.sql;


import freemarker.core.CollectionAndSequence;
import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
* This class wraps {@link FetchedResultSet} and adapts it for using in FTL both as a sequence of rows and as a bean.
Expand Down Expand Up @@ -58,19 +63,61 @@ public int size() {
}


private final static String TRANSPOSE_METHOD_NAME = "transpose";


/**
* Get the specified property or method's result.
*
* @return the result of evaluation
*/
public TemplateModel get(String key) throws TemplateModelException {
if (key.equals("transpose")) {
return new FetchedResultSetTransposedModel(frs, wrapper);
if (key.equals(TRANSPOSE_METHOD_NAME)) {
return transpose();
}
return super.get(key);
}


/**
* Returns the transposed result set as a {@link FetchedResultSetTransposedModel}.
*
* @return the transposed result set
*/
public TemplateModel transpose() throws TemplateModelException {
return new TemplateMethodModelEx() {
public Object exec(List args) throws TemplateModelException {
if (args.size() != 0) {
throw new TemplateModelException("No arguments needed");
}
return new FetchedResultSetTransposedModel(frs, wrapper);
}
};
}


/**
* Returns the list of available methods and properties, extended by own methods.
*
* @return the collection of methods and properties
*/
public TemplateCollectionModel keys() {
Set keySetEx = super.keySet();
keySetEx.add(TRANSPOSE_METHOD_NAME);
return new CollectionAndSequence(new SimpleSequence(keySetEx, wrapper));
}


/**
* Returns the empty list. Iteration through the {@code super.values()} list causes an exception.
*
* @return the empty list
*/
public TemplateCollectionModel values() {
return new SimpleCollection(new ArrayList(0), wrapper);
}


/**
* Returns the result set as a text table with column headers. This method should be used for debugging only.
* Usage example: {@code ${my_result}}.
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/ftldb/ext/sql/FetchedResultSetTransposedModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* This class wraps {@link java.sql.ResultSet} and adapts it for using in FTL both as a sequence and a hash of columns.
*/
public class FetchedResultSetTransposedModel extends WrappingTemplateModel implements TemplateSequenceModel,
TemplateHashModelEx, TemplateMethodModelEx {
TemplateHashModelEx {


public final FetchedResultSet resultSet;
Expand Down Expand Up @@ -108,18 +108,4 @@ public boolean isEmpty() throws TemplateModelException {
}


/**
* Evaluates the {@code transpose()} method of {@link FetchedResultSetModel} and returns this object.
*
* @param args method arguments
* @return this model
*/
public Object exec(List args) throws TemplateModelException {
if (args.size() != 0) {
throw new TemplateModelException("No arguments needed");
}
return this;
}


}
15 changes: 12 additions & 3 deletions src/main/java/ftldb/ext/sql/StructModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.*;

import java.sql.SQLException;
import java.sql.Struct;
import java.util.ArrayList;


/**
Expand Down Expand Up @@ -65,4 +64,14 @@ public int size() {
}


/**
* Returns the empty list. Iteration through the {@code super.values()} list causes an exception.
*
* @return the empty list
*/
public TemplateCollectionModel values() {
return new SimpleCollection(new ArrayList(0), wrapper);
}


}

0 comments on commit 63c104d

Please sign in to comment.