Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 12, 2016
1 parent 7dd913d commit 4e4d51f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bladejava</groupId>
<artifactId>blade-jdbc</artifactId>
<version>0.1.2</version>
<version>0.1.3-beta</version>
<packaging>jar</packaging>

<name>blade-jdbc</name>
Expand Down Expand Up @@ -70,7 +70,7 @@
<version>${slf4j-log4j12.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
45 changes: 32 additions & 13 deletions src/main/java/com/blade/jdbc/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Model extends HashMap<String, Object> {

private Map<ParamKey, Object> params = new TreeMap<ParamKey, Object>();

private PageRow pageRow;

private String sql;

private String order;
Expand Down Expand Up @@ -240,25 +242,21 @@ private <T extends Model> List<T> list(String sql) {
query.withParams(paramValues);
LOGGER.debug("Parameters\t=> {}", Arrays.toString(paramValues));
}
return (List<T>) query.executeAndFetchModels(clazz);

List<T> list = (List<T>) query.executeAndFetchModels(clazz);
this.clear();
return list;
}

public <T extends Model> Paginator<T> page(int page, int limit) {

// query count
long total = this.count();
long total = this.count(false);
Paginator<T> pager = new Paginator<T>(total, page, limit);

int offset = (pager.getPageNum() - 1) * limit;

String sql = dialect.getQueryPageSql(null, this);

int index = params.size() + 1;
this.params.put(new ParamKey(index, "offset"), offset);
this.params.put(new ParamKey(index + 1, "limit"), limit);

List<T> result = this.list(sql);
pageRow = new PageRow(offset, limit);
List<T> result = this.list(null);
pager.setList(result);
pageRow = null;
return pager;
}

Expand All @@ -280,13 +278,20 @@ public <T extends Model> T findOne() {
}

List<T> models = (List<T>) query.executeAndFetchTable().asModel(clazz);

this.clear();

if(null != models && !models.isEmpty()){
return models.get(0);
}
return null;
}

public int count(){
return this.count(true);
}

public int count(boolean clear){
String sql = dialect.getQueryCountSql(this.sql, this);
LOGGER.debug("Preparing\t=> {}", sql);
Query query = Base.sql2o.open().createQuery(sql);
Expand All @@ -296,7 +301,11 @@ public int count(){
query.withParams(paramValues);
LOGGER.debug("Parameters\t=> {}", Arrays.toString(paramValues));
}
return query.executeScalar(Integer.class);
int count = query.executeScalar(Integer.class);
if(clear){
this.clear();
}
return count;
}

private void close(Connection connection){
Expand All @@ -322,7 +331,17 @@ public Map<ParamKey, Object> params() {
return this.params;
}

public PageRow getPageRow() {
return pageRow;
}

public String getOrder(){
return this.order;
}

@Override
public void clear() {
super.clear();
this.params.clear();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/blade/jdbc/PageRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.blade.jdbc;

import java.io.Serializable;

public class PageRow implements Serializable {

private static final long serialVersionUID = -1221241722858484846L;
private int offset;
private int limit;

public PageRow(int offset, int limit) {
this.offset = offset;
this.limit = limit;
}

public int getOffset() {
return offset;
}

public int getLimit() {
return limit;
}

}
32 changes: 21 additions & 11 deletions src/main/java/com/blade/jdbc/dialect/DefaultDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;

import com.blade.jdbc.Model;
import com.blade.jdbc.PageRow;
import com.blade.jdbc.ParamKey;

public class DefaultDialect implements Dialect {
Expand Down Expand Up @@ -77,6 +78,13 @@ public String getQuerySql(String sql, Model model) {
if(null != order){
realSql += "order by " + order;
}
PageRow pageRow = model.getPageRow();
if(null != pageRow){
int index = model.params().size() + 1;
model.params().put(new ParamKey(index, "offset"), pageRow.getOffset());
model.params().put(new ParamKey(index + 1, "limit"), pageRow.getLimit());
realSql += " limit :p" + index + ", :p" + (index + 1);
}
return realSql;
}

Expand Down Expand Up @@ -122,24 +130,26 @@ public String getQueryOneSql(String sql, Model model) {
public String getQueryCountSql(String sql, Model model) {
StringBuffer sqlBuf = new StringBuffer();
if(null != sql){
sqlBuf.append(sql);
sqlBuf.append("select count(1) from");
int pos = sql.indexOf("from") + 4;
int w = sql.indexOf("where");
int o = sql.indexOf("order by");

if(w != -1){
sqlBuf.append(sql.substring(pos, w));
} else if(o != -1){
sqlBuf.append(sql.substring(pos, 0));
} else{
sqlBuf.append(sql.substring(pos));
}
} else {
sqlBuf.append("select count(")
.append(model.pkName())
.append(") from ")
.append(model.table());
}
sqlBuf.append(' ').append(this.whereSql(1, model));
return sql.toString();
return sqlBuf.toString();
}

@Override
public String getQueryPageSql(String sql, Model model) {
String realSql = this.getQuerySql(sql, model);
Map<ParamKey, Object> where = model.params();
int whereSize = where.size();
realSql += " limit :p" + (whereSize+1) + ", :p" + (whereSize+2);
return realSql;
}

}
14 changes: 6 additions & 8 deletions src/main/java/com/blade/jdbc/dialect/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import com.blade.jdbc.Model;

public interface Dialect {

String getSaveSql(Model model);

String getUpdateSql(Model model);

String getDeleteSql(Model model);

String getQuerySql(String sql, Model model);

String getQueryOneSql(String sql, Model model);

String getQueryCountSql(String sql, Model model);

String getQueryPageSql(String sql, Model model);

}
}
8 changes: 7 additions & 1 deletion src/test/java/com/blade/jdbc/test/CRUDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,11 @@ public void testQuery9() {
System.out.println(count);
}


@Test
public void testQuery10() {
Person p = new Person();
Paginator<Person> paginator = p.sql("select id, name from person").where("id > ?", 5).order("id desc").page(1, 10);
System.out.println(paginator);
}

}

0 comments on commit 4e4d51f

Please sign in to comment.