Add more MySQLQuery options

This commit is contained in:
Timo Westkämper 2013-10-11 14:09:34 +04:00
parent 9adf9d6b0f
commit 7f978e5c69
2 changed files with 102 additions and 56 deletions

View File

@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -30,86 +30,86 @@ import com.mysema.query.sql.SQLTemplates;
/**
* MySQLQuery provides MySQL related extensions to SQLQuery
*
*
* @author tiwe
* @see SQLQuery
*
*/
public class MySQLQuery extends AbstractSQLQuery<MySQLQuery> implements SQLCommonQuery<MySQLQuery> {
private static final String WITH_ROLLUP = "\nwith rollup ";
private static final String STRAIGHT_JOIN = "straight_join ";
private static final String SQL_SMALL_RESULT = "sql_small_result ";
private static final String SQL_NO_CACHE = "sql_no_cache ";
private static final String LOCK_IN_SHARE_MODE = "\nlock in share mode ";
private static final String HIGH_PRIORITY = "high_priority ";
private static final String SQL_CALC_FOUND_ROWS = "sql_calc_found_rows ";
private static final String SQL_CACHE = "sql_cache ";
private static final String SQL_BUFFER_RESULT = "sql_buffer_result ";
private static final String SQL_BIG_RESULT = "sql_big_result ";
private static final Joiner JOINER = Joiner.on(", ");
public MySQLQuery(Connection conn) {
this(conn, new Configuration(new MySQLTemplates()), new DefaultQueryMetadata());
}
public MySQLQuery(Connection conn, SQLTemplates templates) {
this(conn, new Configuration(templates), new DefaultQueryMetadata());
}
public MySQLQuery(Connection conn, Configuration configuration) {
this(conn, configuration, new DefaultQueryMetadata());
}
public MySQLQuery(Connection conn, Configuration configuration, QueryMetadata metadata) {
super(conn, configuration, metadata);
}
/**
* @return
*/
public MySQLQuery bigResult() {
return addFlag(Position.AFTER_SELECT, SQL_BIG_RESULT);
}
/**
* @return
*/
public MySQLQuery bufferResult() {
return addFlag(Position.AFTER_SELECT, SQL_BUFFER_RESULT);
}
/**
* @return
*/
public MySQLQuery cache() {
return addFlag(Position.AFTER_SELECT, SQL_CACHE);
}
/**
* @return
*/
public MySQLQuery calcFoundRows() {
return addFlag(Position.AFTER_SELECT, SQL_CALC_FOUND_ROWS);
}
/**
* @return
*/
public MySQLQuery highPriority() {
return addFlag(Position.AFTER_SELECT, HIGH_PRIORITY);
}
/**
* @param var
* @return
@ -125,7 +125,7 @@ public class MySQLQuery extends AbstractSQLQuery<MySQLQuery> implements SQLCommo
public MySQLQuery intoDumpfile(File file) {
return addFlag(Position.END, "\ninto dumpfile '" + file.getPath() + "'" );
}
/**
* @param file
* @return
@ -133,48 +133,66 @@ public class MySQLQuery extends AbstractSQLQuery<MySQLQuery> implements SQLCommo
public MySQLQuery intoOutfile(File file) {
return addFlag(Position.END, "\ninto outfile '" + file.getPath() + "'" );
}
/**
* @return
*/
public MySQLQuery lockInShareMode() {
return addFlag(Position.END, LOCK_IN_SHARE_MODE);
}
/**
* @return
*/
public MySQLQuery noCache() {
return addFlag(Position.AFTER_SELECT, SQL_NO_CACHE);
}
/**
* @return
*/
public MySQLQuery smallResult() {
return addFlag(Position.AFTER_SELECT, SQL_SMALL_RESULT);
}
/**
* @return
*/
public MySQLQuery straightJoin() {
return addFlag(Position.AFTER_SELECT, STRAIGHT_JOIN);
}
/**
* @param indexes
* @return
*/
public MySQLQuery forceIndex(String... indexes) {
return addJoinFlag(" force index (" + JOINER.join(indexes) + ")", JoinFlag.Position.END);
}
/**
* @param indexes
* @return
*/
public MySQLQuery ignoreIndex(String... indexes) {
return addJoinFlag(" ignore index (" + JOINER.join(indexes) + ")", JoinFlag.Position.END);
}
/**
* @param indexes
* @return
*/
public MySQLQuery useIndex(String... indexes) {
return addJoinFlag(" use_index (" + JOINER.join(indexes) + ")", JoinFlag.Position.END);
return addJoinFlag(" use index (" + JOINER.join(indexes) + ")", JoinFlag.Position.END);
}
/**
* @return
*/
public MySQLQuery withRollup() {
return addFlag(Position.AFTER_GROUP_BY, WITH_ROLLUP);
}
}

View File

@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -25,9 +25,9 @@ import com.mysema.query.sql.domain.QSurvey;
public class MySQLQueryTest {
private MySQLQuery query;
private QSurvey survey = new QSurvey("survey");
@Before
public void setUp() {
query = new MySQLQuery(null, new MySQLTemplates() {{
@ -37,9 +37,9 @@ public class MySQLQueryTest {
query.orderBy(survey.name.asc());
query.getMetadata().addProjection(survey.name);
}
@Test
public void Syntax() {
public void Syntax() {
// SELECT
// [ALL | DISTINCT | DISTINCTROW ]
// [HIGH_PRIORITY]
@ -80,7 +80,35 @@ public class MySQLQueryTest {
query.forUpdate();
query.lockInShareMode();
}
@Test
public void ForceIndex() {
query = new MySQLQuery(null, new MySQLTemplates() {{
newLineToSingleSpace();
}});
query.from(survey);
query.forceIndex("col1_index");
query.orderBy(survey.name.asc());
query.getMetadata().addProjection(survey.name);
assertEquals("select survey.NAME from SURVEY survey force index (col1_index) " +
"order by survey.NAME asc", toString(query));
}
@Test
public void IgnoreIndex() {
query = new MySQLQuery(null, new MySQLTemplates() {{
newLineToSingleSpace();
}});
query.from(survey);
query.ignoreIndex("col1_index");
query.orderBy(survey.name.asc());
query.getMetadata().addProjection(survey.name);
assertEquals("select survey.NAME from SURVEY survey ignore index (col1_index) " +
"order by survey.NAME asc", toString(query));
}
@Test
public void UseIndex() {
query = new MySQLQuery(null, new MySQLTemplates() {{
@ -90,78 +118,78 @@ public class MySQLQueryTest {
query.useIndex("col1_index");
query.orderBy(survey.name.asc());
query.getMetadata().addProjection(survey.name);
assertEquals("select survey.NAME from SURVEY survey use_index (col1_index) " +
assertEquals("select survey.NAME from SURVEY survey use index (col1_index) " +
"order by survey.NAME asc", toString(query));
}
@Test
public void UseIndex2() {
query = new MySQLQuery(null, new MySQLTemplates() {{
newLineToSingleSpace();
}});
query.from(survey);
query.useIndex("col1_index","col2_index");
query.useIndex("col1_index","col2_index");
query.orderBy(survey.name.asc());
query.getMetadata().addProjection(survey.name);
assertEquals("select survey.NAME from SURVEY survey use_index (col1_index, col2_index) " +
assertEquals("select survey.NAME from SURVEY survey use index (col1_index, col2_index) " +
"order by survey.NAME asc", toString(query));
}
@Test
public void HighPriority() {
query.highPriority();
assertEquals("select high_priority survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select high_priority survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void StraightJoin() {
query.straightJoin();
assertEquals("select straight_join survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select straight_join survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void SmallResult() {
query.smallResult();
assertEquals("select sql_small_result survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_small_result survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void BigResult() {
query.bigResult();
assertEquals("select sql_big_result survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_big_result survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void BufferResult() {
query.bufferResult();
assertEquals("select sql_buffer_result survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_buffer_result survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void Cache() {
query.cache();
assertEquals("select sql_cache survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_cache survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void NoCache() {
query.noCache();
assertEquals("select sql_no_cache survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_no_cache survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@Test
public void CalcFoundRows() {
query.calcFoundRows();
assertEquals("select sql_calc_found_rows survey.NAME from SURVEY survey order by survey.NAME asc",
assertEquals("select sql_calc_found_rows survey.NAME from SURVEY survey order by survey.NAME asc",
toString(query));
}
@ -169,7 +197,7 @@ public class MySQLQueryTest {
public void WithRollup() {
query.groupBy(survey.name);
query.withRollup();
assertEquals("select survey.NAME from SURVEY survey group by survey.NAME with rollup order by survey.NAME asc",
assertEquals("select survey.NAME from SURVEY survey group by survey.NAME with rollup order by survey.NAME asc",
toString(query));
}
@ -187,14 +215,14 @@ public class MySQLQueryTest {
assertEquals("select survey.NAME from SURVEY survey order by survey.NAME asc limit ? for update",
toString(query));
}
@Test
public void IntoOutfile() {
query.intoOutfile(new File("target/out"));
assertEquals("select survey.NAME from SURVEY survey " +
"order by survey.NAME asc into outfile 'target" + File.separator + "out'", toString(query));
}
@Test
public void IntoDumpfile() {
query.intoDumpfile(new File("target/out"));
@ -215,7 +243,7 @@ public class MySQLQueryTest {
assertEquals("select survey.NAME from SURVEY survey " +
"order by survey.NAME asc lock in share mode", toString(query));
}
private String toString(MySQLQuery query) {
return query.toString().replace('\n', ' ');
}