mirror of https://github.com/tasks/tasks
Merge remote branch 'stingeraj/dev' into dev
commit
ea10f25814
@ -1,39 +1,39 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<projectDescription>
|
<projectDescription>
|
||||||
<name>astrid</name>
|
<name>astrid</name>
|
||||||
<comment></comment>
|
<comment></comment>
|
||||||
<projects>
|
<projects>
|
||||||
</projects>
|
</projects>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
@ -0,0 +1,156 @@
|
|||||||
|
package com.todoroo.astrid.producteev;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.Spinner;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.AdapterView.OnItemSelectedListener;
|
||||||
|
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.andlib.service.DependencyInjectionService;
|
||||||
|
import com.todoroo.andlib.service.ExceptionService;
|
||||||
|
import com.todoroo.astrid.activity.TaskEditActivity.TaskEditControlSet;
|
||||||
|
import com.todoroo.astrid.model.Metadata;
|
||||||
|
import com.todoroo.astrid.model.StoreObject;
|
||||||
|
import com.todoroo.astrid.model.Task;
|
||||||
|
import com.todoroo.astrid.producteev.sync.ProducteevDashboard;
|
||||||
|
import com.todoroo.astrid.producteev.sync.ProducteevDataService;
|
||||||
|
import com.todoroo.astrid.producteev.sync.ProducteevTask;
|
||||||
|
import com.todoroo.astrid.producteev.sync.ProducteevUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Control Set for managing task/dashboard assignments in Producteev
|
||||||
|
*
|
||||||
|
* @author Arne Jans <arne.jans@gmail.com>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ProducteevControlSet implements TaskEditControlSet {
|
||||||
|
|
||||||
|
// --- instance variables
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ExceptionService exceptionService;
|
||||||
|
|
||||||
|
private final Activity activity;
|
||||||
|
|
||||||
|
private Task myTask;
|
||||||
|
private final Spinner responsibleSelector;
|
||||||
|
private final Spinner dashboardSelector;
|
||||||
|
|
||||||
|
private ArrayList<ProducteevUser> users = null;
|
||||||
|
private ArrayList<ProducteevDashboard> dashboards = null;
|
||||||
|
|
||||||
|
public ProducteevControlSet(final Activity activity, ViewGroup parent) {
|
||||||
|
DependencyInjectionService.getInstance().inject(this);
|
||||||
|
|
||||||
|
this.activity = activity;
|
||||||
|
LayoutInflater.from(activity).inflate(R.layout.producteev_control, parent, true);
|
||||||
|
|
||||||
|
this.responsibleSelector = (Spinner) activity.findViewById(R.id.producteev_TEA_task_assign);
|
||||||
|
this.responsibleSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemSelected(AdapterView<?> parent, View view,
|
||||||
|
int position, long id) {
|
||||||
|
// TODO tim, please set a flag here somewhere, so that producteevinvoker knows
|
||||||
|
// it has to invoke tasks/set_responsible, or otherwise do it your way...
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onNothingSelected(AdapterView<?> parent) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.dashboardSelector = (Spinner) activity.findViewById(R.id.producteev_TEA_dashboard_assign);
|
||||||
|
this.dashboardSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
|
||||||
|
@Override
|
||||||
|
public void onItemSelected(AdapterView<?> parent, View view,
|
||||||
|
int position, long id) {
|
||||||
|
// TODO tim, please set a flag here somewhere, so that producteevinvoker knows
|
||||||
|
// it has to invoke tasks/set_dashboard, or otherwise do it your way...
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onNothingSelected(AdapterView<?> parent) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readFromTask(Task task) {
|
||||||
|
this.myTask = task;
|
||||||
|
Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(myTask.getId());
|
||||||
|
if (metadata != null) {
|
||||||
|
// Fill the dashboard-spinner and set the current dashboard
|
||||||
|
long dashboardId = metadata.getValue(ProducteevTask.DASHBOARD_ID);
|
||||||
|
|
||||||
|
StoreObject[] dashboardsData = ProducteevDataService.getInstance().getDashboards();
|
||||||
|
dashboards = new ArrayList(dashboardsData.length);
|
||||||
|
ProducteevDashboard ownerDashboard = null;
|
||||||
|
int dashboardSpinnerIndex = 0;
|
||||||
|
//dashboard to not sync as first spinner-entry
|
||||||
|
dashboards.add(new ProducteevDashboard(ProducteevUtilities.DASHBOARD_NO_SYNC, activity.getString(R.string.producteev_no_dashboard),null));
|
||||||
|
for (int i=1;i<dashboardsData.length+1;i++) {
|
||||||
|
ProducteevDashboard dashboard = new ProducteevDashboard(dashboardsData[i-1]);
|
||||||
|
dashboards.add(dashboard);
|
||||||
|
if(dashboard.getId() == dashboardId) {
|
||||||
|
ownerDashboard = dashboard;
|
||||||
|
dashboardSpinnerIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayAdapter<String> dashAdapter = new ArrayAdapter(activity,
|
||||||
|
android.R.layout.simple_spinner_item, dashboards);
|
||||||
|
dashAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
|
dashboardSelector.setAdapter(dashAdapter);
|
||||||
|
dashboardSelector.setSelection(dashboardSpinnerIndex);
|
||||||
|
|
||||||
|
if (ownerDashboard == null || ownerDashboard.getId() == ProducteevUtilities.DASHBOARD_NO_SYNC) {
|
||||||
|
responsibleSelector.setEnabled(false);
|
||||||
|
TextView emptyView = new TextView(activity);
|
||||||
|
emptyView.setText(activity.getText(R.string.producteev_no_dashboard));
|
||||||
|
responsibleSelector.setEmptyView(emptyView);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the responsible-spinner and set the current responsible
|
||||||
|
users = ownerDashboard.getUsers();
|
||||||
|
long responsibleId = metadata.getValue(ProducteevTask.RESPONSIBLE_ID);
|
||||||
|
int userSpinnerIndex = 0;
|
||||||
|
|
||||||
|
for (ProducteevUser user : users) {
|
||||||
|
if (user.getId() == responsibleId) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
userSpinnerIndex++;
|
||||||
|
}
|
||||||
|
ArrayAdapter<String> usersAdapter = new ArrayAdapter(activity,
|
||||||
|
android.R.layout.simple_spinner_item, users);
|
||||||
|
responsibleSelector.setAdapter(usersAdapter);
|
||||||
|
responsibleSelector.setSelection(userSpinnerIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
@Override
|
||||||
|
public void writeToModel(Task task) {
|
||||||
|
Metadata metadata = ProducteevDataService.getInstance().getTaskMetadata(task.getId());
|
||||||
|
if (metadata != null) {
|
||||||
|
ProducteevDashboard dashboard = (ProducteevDashboard) dashboardSelector.getSelectedItem();
|
||||||
|
metadata.setValue(ProducteevTask.DASHBOARD_ID, dashboard.getId());
|
||||||
|
|
||||||
|
ProducteevUser responsibleUser = (ProducteevUser) responsibleSelector.getSelectedItem();
|
||||||
|
metadata.setValue(ProducteevTask.RESPONSIBLE_ID, responsibleUser.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.todoroo.astrid.producteev.sync;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Arne Jans <arne.jans@gmail.com>
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
public class ProducteevUser {
|
||||||
|
|
||||||
|
private final long id;
|
||||||
|
|
||||||
|
private final String email;
|
||||||
|
|
||||||
|
private final String firstname;
|
||||||
|
|
||||||
|
private final String lastname;
|
||||||
|
|
||||||
|
public ProducteevUser(long id, String email, String firstname, String lastname) {
|
||||||
|
this.id = id;
|
||||||
|
this.email = email;
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.lastname = lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProducteevUser(JSONObject elt) throws JSONException {
|
||||||
|
this.id = elt.getLong("id");
|
||||||
|
this.email = elt.getString("email");
|
||||||
|
this.firstname = elt.getString("firstname");
|
||||||
|
this.lastname = elt.getString("lastname");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the email
|
||||||
|
*/
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the firstname
|
||||||
|
*/
|
||||||
|
public String getFirstname() {
|
||||||
|
return firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the lastname
|
||||||
|
*/
|
||||||
|
public String getLastname() {
|
||||||
|
return lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the id
|
||||||
|
*/
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String displayString = "";
|
||||||
|
boolean hasFirstname = false;
|
||||||
|
boolean hasLastname = false;
|
||||||
|
if (firstname != null && firstname.length() > 0) {
|
||||||
|
displayString += firstname;
|
||||||
|
hasFirstname = true;
|
||||||
|
}
|
||||||
|
if (lastname != null && lastname.length() > 0)
|
||||||
|
hasLastname = true;
|
||||||
|
if (hasFirstname && hasLastname)
|
||||||
|
displayString += " ";
|
||||||
|
if (hasLastname)
|
||||||
|
displayString += lastname;
|
||||||
|
|
||||||
|
if (!hasFirstname && !hasLastname && email != null && email.length() > 0)
|
||||||
|
displayString += email;
|
||||||
|
return displayString;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- See the file "LICENSE" for the full license governing this code. -->
|
||||||
|
<merge xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<!-- producteev task assignment controlset -->
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/producteev_TEA_task_assign_label"
|
||||||
|
style="@style/TextAppearance.GEN_EditLabel" />
|
||||||
|
|
||||||
|
<Spinner
|
||||||
|
android:id="@+id/producteev_TEA_task_assign"
|
||||||
|
android:prompt="@string/producteev_TEA_task_assign_label"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/producteev_TEA_dashboard_assign_label"
|
||||||
|
style="@style/TextAppearance.GEN_EditLabel" />
|
||||||
|
|
||||||
|
<Spinner
|
||||||
|
android:id="@+id/producteev_TEA_dashboard_assign"
|
||||||
|
android:prompt="@string/producteev_TEA_dashboard_assign_label"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="1dip"
|
||||||
|
android:padding="5dip"
|
||||||
|
android:background="@android:drawable/divider_horizontal_dark" />
|
||||||
|
|
||||||
|
</merge>
|
||||||
|
|
@ -1,25 +1,25 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
public final class Constants {
|
public final class Constants {
|
||||||
static final String SELECT = "SELECT";
|
static final String SELECT = "SELECT";
|
||||||
static final String SPACE = " ";
|
static final String SPACE = " ";
|
||||||
static final String AS = "AS";
|
static final String AS = "AS";
|
||||||
static final String COMMA = ",";
|
static final String COMMA = ",";
|
||||||
static final String FROM = "FROM";
|
static final String FROM = "FROM";
|
||||||
static final String ON = "ON";
|
static final String ON = "ON";
|
||||||
static final String JOIN = "JOIN";
|
static final String JOIN = "JOIN";
|
||||||
static final String ALL = "*";
|
static final String ALL = "*";
|
||||||
static final String LEFT_PARENTHESIS = "(";
|
static final String LEFT_PARENTHESIS = "(";
|
||||||
static final String RIGHT_PARENTHESIS = ")";
|
static final String RIGHT_PARENTHESIS = ")";
|
||||||
static final String AND = "AND";
|
static final String AND = "AND";
|
||||||
static final String BETWEEN = "BETWEEN";
|
static final String BETWEEN = "BETWEEN";
|
||||||
static final String LIKE = "LIKE";
|
static final String LIKE = "LIKE";
|
||||||
static final String OR = "OR";
|
static final String OR = "OR";
|
||||||
static final String ORDER_BY = "ORDER BY";
|
static final String ORDER_BY = "ORDER BY";
|
||||||
static final String GROUP_BY = "GROUP BY";
|
static final String GROUP_BY = "GROUP BY";
|
||||||
static final String WHERE = "WHERE";
|
static final String WHERE = "WHERE";
|
||||||
public static final String EXISTS = "EXISTS";
|
public static final String EXISTS = "EXISTS";
|
||||||
public static final String NOT = "NOT";
|
public static final String NOT = "NOT";
|
||||||
public static final String LIMIT = "LIMIT";
|
public static final String LIMIT = "LIMIT";
|
||||||
}
|
}
|
||||||
|
@ -1,89 +1,89 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.AND;
|
import static com.todoroo.andlib.Constants.AND;
|
||||||
import static com.todoroo.andlib.Constants.EXISTS;
|
import static com.todoroo.andlib.Constants.EXISTS;
|
||||||
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.NOT;
|
import static com.todoroo.andlib.Constants.NOT;
|
||||||
import static com.todoroo.andlib.Constants.OR;
|
import static com.todoroo.andlib.Constants.OR;
|
||||||
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public abstract class Criterion {
|
public abstract class Criterion {
|
||||||
protected final Operator operator;
|
protected final Operator operator;
|
||||||
|
|
||||||
Criterion(Operator operator) {
|
Criterion(Operator operator) {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion all = new Criterion(Operator.exists) {
|
public static Criterion all = new Criterion(Operator.exists) {
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(1);
|
sb.append(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Criterion none = new Criterion(Operator.exists) {
|
public static Criterion none = new Criterion(Operator.exists) {
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(0);
|
sb.append(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Criterion and(final Criterion criterion, final Criterion... criterions) {
|
public static Criterion and(final Criterion criterion, final Criterion... criterions) {
|
||||||
return new Criterion(Operator.and) {
|
return new Criterion(Operator.and) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(criterion);
|
sb.append(criterion);
|
||||||
for (Criterion c : criterions) {
|
for (Criterion c : criterions) {
|
||||||
sb.append(SPACE).append(AND).append(SPACE).append(c);
|
sb.append(SPACE).append(AND).append(SPACE).append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion or(final Criterion criterion, final Criterion... criterions) {
|
public static Criterion or(final Criterion criterion, final Criterion... criterions) {
|
||||||
return new Criterion(Operator.or) {
|
return new Criterion(Operator.or) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(criterion);
|
sb.append(criterion);
|
||||||
for (Criterion c : criterions) {
|
for (Criterion c : criterions) {
|
||||||
sb.append(SPACE).append(OR).append(SPACE).append(c.toString());
|
sb.append(SPACE).append(OR).append(SPACE).append(c.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion exists(final Query query) {
|
public static Criterion exists(final Query query) {
|
||||||
return new Criterion(Operator.exists) {
|
return new Criterion(Operator.exists) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(EXISTS).append(SPACE).append(LEFT_PARENTHESIS).append(query).append(RIGHT_PARENTHESIS);
|
sb.append(EXISTS).append(SPACE).append(LEFT_PARENTHESIS).append(query).append(RIGHT_PARENTHESIS);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion not(final Criterion criterion) {
|
public static Criterion not(final Criterion criterion) {
|
||||||
return new Criterion(Operator.not) {
|
return new Criterion(Operator.not) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(NOT).append(SPACE);
|
sb.append(NOT).append(SPACE);
|
||||||
criterion.populate(sb);
|
criterion.populate(sb);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void populate(StringBuilder sb);
|
protected abstract void populate(StringBuilder sb);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder(LEFT_PARENTHESIS);
|
StringBuilder builder = new StringBuilder(LEFT_PARENTHESIS);
|
||||||
populate(builder);
|
populate(builder);
|
||||||
builder.append(RIGHT_PARENTHESIS);
|
builder.append(RIGHT_PARENTHESIS);
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,68 +1,68 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.AS;
|
import static com.todoroo.andlib.Constants.AS;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public abstract class DBObject<T extends DBObject<?>> implements Cloneable {
|
public abstract class DBObject<T extends DBObject<?>> implements Cloneable {
|
||||||
protected String alias;
|
protected String alias;
|
||||||
protected final String expression;
|
protected final String expression;
|
||||||
|
|
||||||
protected DBObject(String expression){
|
protected DBObject(String expression){
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T as(String newAlias) {
|
public T as(String newAlias) {
|
||||||
try {
|
try {
|
||||||
T clone = (T) clone();
|
T clone = (T) clone();
|
||||||
clone.alias = newAlias;
|
clone.alias = newAlias;
|
||||||
return clone;
|
return clone;
|
||||||
} catch (CloneNotSupportedException e) {
|
} catch (CloneNotSupportedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasAlias() {
|
public boolean hasAlias() {
|
||||||
return alias != null;
|
return alias != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
DBObject<?> dbObject = (DBObject<?>) o;
|
DBObject<?> dbObject = (DBObject<?>) o;
|
||||||
|
|
||||||
if (alias != null ? !alias.equals(dbObject.alias) : dbObject.alias != null) return false;
|
if (alias != null ? !alias.equals(dbObject.alias) : dbObject.alias != null) return false;
|
||||||
if (expression != null ? !expression.equals(dbObject.expression) : dbObject.expression != null) return false;
|
if (expression != null ? !expression.equals(dbObject.expression) : dbObject.expression != null) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = alias != null ? alias.hashCode() : 0;
|
int result = alias != null ? alias.hashCode() : 0;
|
||||||
result = 31 * result + (expression != null ? expression.hashCode() : 0);
|
result = 31 * result + (expression != null ? expression.hashCode() : 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String toString() {
|
public final String toString() {
|
||||||
if (hasAlias()) {
|
if (hasAlias()) {
|
||||||
return alias;
|
return alias;
|
||||||
}
|
}
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String toStringInSelect() {
|
public final String toStringInSelect() {
|
||||||
StringBuilder sb = new StringBuilder(expression);
|
StringBuilder sb = new StringBuilder(expression);
|
||||||
if (hasAlias()) {
|
if (hasAlias()) {
|
||||||
sb.append(SPACE).append(AS).append(SPACE).append(alias);
|
sb.append(SPACE).append(AS).append(SPACE).append(alias);
|
||||||
} else {
|
} else {
|
||||||
int pos = expression.indexOf('.');
|
int pos = expression.indexOf('.');
|
||||||
if(pos > 0)
|
if(pos > 0)
|
||||||
sb.append(SPACE).append(AS).append(SPACE).append(expression.substring(pos + 1));
|
sb.append(SPACE).append(AS).append(SPACE).append(expression.substring(pos + 1));
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
public class EqCriterion extends UnaryCriterion {
|
public class EqCriterion extends UnaryCriterion {
|
||||||
EqCriterion(Field field, Object value) {
|
EqCriterion(Field field, Object value) {
|
||||||
super(field, Operator.eq, value);
|
super(field, Operator.eq, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,90 +1,90 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.AND;
|
import static com.todoroo.andlib.Constants.AND;
|
||||||
import static com.todoroo.andlib.Constants.BETWEEN;
|
import static com.todoroo.andlib.Constants.BETWEEN;
|
||||||
import static com.todoroo.andlib.Constants.COMMA;
|
import static com.todoroo.andlib.Constants.COMMA;
|
||||||
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public class Field extends DBObject<Field> {
|
public class Field extends DBObject<Field> {
|
||||||
|
|
||||||
protected Field(String expression) {
|
protected Field(String expression) {
|
||||||
super(expression);
|
super(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Field field(String expression) {
|
public static Field field(String expression) {
|
||||||
return new Field(expression);
|
return new Field(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion eq(Object value) {
|
public Criterion eq(Object value) {
|
||||||
if(value == null)
|
if(value == null)
|
||||||
return UnaryCriterion.isNull(this);
|
return UnaryCriterion.isNull(this);
|
||||||
return UnaryCriterion.eq(this, value);
|
return UnaryCriterion.eq(this, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion neq(Object value) {
|
public Criterion neq(Object value) {
|
||||||
if(value == null)
|
if(value == null)
|
||||||
return UnaryCriterion.isNotNull(this);
|
return UnaryCriterion.isNotNull(this);
|
||||||
return UnaryCriterion.neq(this, value);
|
return UnaryCriterion.neq(this, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion gt(Object value) {
|
public Criterion gt(Object value) {
|
||||||
return UnaryCriterion.gt(this, value);
|
return UnaryCriterion.gt(this, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion lt(final Object value) {
|
public Criterion lt(final Object value) {
|
||||||
return UnaryCriterion.lt(this, value);
|
return UnaryCriterion.lt(this, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion isNull() {
|
public Criterion isNull() {
|
||||||
return UnaryCriterion.isNull(this);
|
return UnaryCriterion.isNull(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion isNotNull() {
|
public Criterion isNotNull() {
|
||||||
return UnaryCriterion.isNotNull(this);
|
return UnaryCriterion.isNotNull(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion between(final Object lower, final Object upper) {
|
public Criterion between(final Object lower, final Object upper) {
|
||||||
final Field field = this;
|
final Field field = this;
|
||||||
return new Criterion(null) {
|
return new Criterion(null) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(field).append(SPACE).append(BETWEEN).append(SPACE).append(lower).append(SPACE).append(AND)
|
sb.append(field).append(SPACE).append(BETWEEN).append(SPACE).append(lower).append(SPACE).append(AND)
|
||||||
.append(SPACE).append(upper);
|
.append(SPACE).append(upper);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion like(final String value) {
|
public Criterion like(final String value) {
|
||||||
return UnaryCriterion.like(this, value);
|
return UnaryCriterion.like(this, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> Criterion in(final T... value) {
|
public <T> Criterion in(final T... value) {
|
||||||
final Field field = this;
|
final Field field = this;
|
||||||
return new Criterion(Operator.in) {
|
return new Criterion(Operator.in) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(field).append(SPACE).append(Operator.in).append(SPACE).append(LEFT_PARENTHESIS);
|
sb.append(field).append(SPACE).append(Operator.in).append(SPACE).append(LEFT_PARENTHESIS);
|
||||||
for (T t : value) {
|
for (T t : value) {
|
||||||
sb.append(t.toString()).append(COMMA);
|
sb.append(t.toString()).append(COMMA);
|
||||||
}
|
}
|
||||||
sb.deleteCharAt(sb.length() - 1).append(RIGHT_PARENTHESIS);
|
sb.deleteCharAt(sb.length() - 1).append(RIGHT_PARENTHESIS);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion in(final Query query) {
|
public Criterion in(final Query query) {
|
||||||
final Field field = this;
|
final Field field = this;
|
||||||
return new Criterion(Operator.in) {
|
return new Criterion(Operator.in) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
sb.append(field).append(SPACE).append(Operator.in).append(SPACE).append(LEFT_PARENTHESIS).append(query)
|
sb.append(field).append(SPACE).append(Operator.in).append(SPACE).append(LEFT_PARENTHESIS).append(query)
|
||||||
.append(RIGHT_PARENTHESIS);
|
.append(RIGHT_PARENTHESIS);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GroupBy {
|
public class GroupBy {
|
||||||
private List<Field> fields = new ArrayList<Field>();
|
private List<Field> fields = new ArrayList<Field>();
|
||||||
|
|
||||||
public static GroupBy groupBy(Field field) {
|
public static GroupBy groupBy(Field field) {
|
||||||
GroupBy groupBy = new GroupBy();
|
GroupBy groupBy = new GroupBy();
|
||||||
groupBy.fields.add(field);
|
groupBy.fields.add(field);
|
||||||
return groupBy;
|
return groupBy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,43 +1,43 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.JOIN;
|
import static com.todoroo.andlib.Constants.JOIN;
|
||||||
import static com.todoroo.andlib.Constants.ON;
|
import static com.todoroo.andlib.Constants.ON;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public class Join {
|
public class Join {
|
||||||
private final SqlTable joinTable;
|
private final SqlTable joinTable;
|
||||||
private final JoinType joinType;
|
private final JoinType joinType;
|
||||||
private final Criterion[] criterions;
|
private final Criterion[] criterions;
|
||||||
|
|
||||||
private Join(SqlTable table, JoinType joinType, Criterion... criterions) {
|
private Join(SqlTable table, JoinType joinType, Criterion... criterions) {
|
||||||
joinTable = table;
|
joinTable = table;
|
||||||
this.joinType = joinType;
|
this.joinType = joinType;
|
||||||
this.criterions = criterions;
|
this.criterions = criterions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Join inner(SqlTable expression, Criterion... criterions) {
|
public static Join inner(SqlTable expression, Criterion... criterions) {
|
||||||
return new Join(expression, JoinType.INNER, criterions);
|
return new Join(expression, JoinType.INNER, criterions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Join left(SqlTable table, Criterion... criterions) {
|
public static Join left(SqlTable table, Criterion... criterions) {
|
||||||
return new Join(table, JoinType.LEFT, criterions);
|
return new Join(table, JoinType.LEFT, criterions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Join right(SqlTable table, Criterion... criterions) {
|
public static Join right(SqlTable table, Criterion... criterions) {
|
||||||
return new Join(table, JoinType.RIGHT, criterions);
|
return new Join(table, JoinType.RIGHT, criterions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Join out(SqlTable table, Criterion... criterions) {
|
public static Join out(SqlTable table, Criterion... criterions) {
|
||||||
return new Join(table, JoinType.OUT, criterions);
|
return new Join(table, JoinType.OUT, criterions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(joinType).append(SPACE).append(JOIN).append(SPACE).append(joinTable).append(SPACE).append(ON);
|
sb.append(joinType).append(SPACE).append(JOIN).append(SPACE).append(joinTable).append(SPACE).append(ON);
|
||||||
for (Criterion criterion : criterions) {
|
for (Criterion criterion : criterions) {
|
||||||
sb.append(SPACE).append(criterion);
|
sb.append(SPACE).append(criterion);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
public enum JoinType {
|
public enum JoinType {
|
||||||
INNER, LEFT, RIGHT, OUT
|
INNER, LEFT, RIGHT, OUT
|
||||||
}
|
}
|
||||||
|
@ -1,57 +1,57 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
public final class Operator {
|
public final class Operator {
|
||||||
|
|
||||||
private final String operator;
|
private final String operator;
|
||||||
public static final Operator eq = new Operator("=");
|
public static final Operator eq = new Operator("=");
|
||||||
public static final Operator neq = new Operator("<>");
|
public static final Operator neq = new Operator("<>");
|
||||||
public static final Operator isNull = new Operator("IS NULL");
|
public static final Operator isNull = new Operator("IS NULL");
|
||||||
public static final Operator isNotNull = new Operator("IS NOT NULL");
|
public static final Operator isNotNull = new Operator("IS NOT NULL");
|
||||||
public static final Operator gt = new Operator(">");
|
public static final Operator gt = new Operator(">");
|
||||||
public static final Operator lt = new Operator("<");
|
public static final Operator lt = new Operator("<");
|
||||||
public static final Operator gte = new Operator(">=");
|
public static final Operator gte = new Operator(">=");
|
||||||
public static final Operator lte = new Operator("<=");
|
public static final Operator lte = new Operator("<=");
|
||||||
public static final Operator and = new Operator("AND");
|
public static final Operator and = new Operator("AND");
|
||||||
public static final Operator or = new Operator("OR");
|
public static final Operator or = new Operator("OR");
|
||||||
public static final Operator not = new Operator("NOT");
|
public static final Operator not = new Operator("NOT");
|
||||||
public static final Operator exists = new Operator("EXISTS");
|
public static final Operator exists = new Operator("EXISTS");
|
||||||
public static final Operator like = new Operator("LIKE");
|
public static final Operator like = new Operator("LIKE");
|
||||||
public static final Operator in = new Operator("IN");
|
public static final Operator in = new Operator("IN");
|
||||||
|
|
||||||
private static final Map<Operator, Operator> contraryRegistry = new HashMap<Operator, Operator>();
|
private static final Map<Operator, Operator> contraryRegistry = new HashMap<Operator, Operator>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
contraryRegistry.put(eq, neq);
|
contraryRegistry.put(eq, neq);
|
||||||
contraryRegistry.put(neq, eq);
|
contraryRegistry.put(neq, eq);
|
||||||
contraryRegistry.put(isNull, isNotNull);
|
contraryRegistry.put(isNull, isNotNull);
|
||||||
contraryRegistry.put(isNotNull, isNull);
|
contraryRegistry.put(isNotNull, isNull);
|
||||||
contraryRegistry.put(gt, lte);
|
contraryRegistry.put(gt, lte);
|
||||||
contraryRegistry.put(lte, gt);
|
contraryRegistry.put(lte, gt);
|
||||||
contraryRegistry.put(lt, gte);
|
contraryRegistry.put(lt, gte);
|
||||||
contraryRegistry.put(gte, lt);
|
contraryRegistry.put(gte, lt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Operator(String operator) {
|
private Operator(String operator) {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Operator getContrary() {
|
public Operator getContrary() {
|
||||||
if(!contraryRegistry.containsKey(this)){
|
if(!contraryRegistry.containsKey(this)){
|
||||||
Operator opposite = new Operator(not.toString() + SPACE + this.toString());
|
Operator opposite = new Operator(not.toString() + SPACE + this.toString());
|
||||||
contraryRegistry.put(this, opposite);
|
contraryRegistry.put(this, opposite);
|
||||||
contraryRegistry.put(opposite, this);
|
contraryRegistry.put(opposite, this);
|
||||||
}
|
}
|
||||||
return contraryRegistry.get(this);
|
return contraryRegistry.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.operator.toString();
|
return this.operator.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,30 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public class Order {
|
public class Order {
|
||||||
private final Object expression;
|
private final Object expression;
|
||||||
private final OrderType orderType;
|
private final OrderType orderType;
|
||||||
|
|
||||||
private Order(Object expression) {
|
private Order(Object expression) {
|
||||||
this(expression, OrderType.ASC);
|
this(expression, OrderType.ASC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Order(Object expression, OrderType orderType) {
|
private Order(Object expression, OrderType orderType) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.orderType = orderType;
|
this.orderType = orderType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Order asc(Object expression) {
|
public static Order asc(Object expression) {
|
||||||
return new Order(expression);
|
return new Order(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Order desc(Object expression) {
|
public static Order desc(Object expression) {
|
||||||
return new Order(expression, OrderType.DESC);
|
return new Order(expression, OrderType.DESC);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return expression + SPACE + orderType;
|
return expression + SPACE + orderType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
public enum OrderType {
|
public enum OrderType {
|
||||||
DESC, ASC
|
DESC, ASC
|
||||||
}
|
}
|
||||||
|
@ -1,192 +1,192 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.ALL;
|
import static com.todoroo.andlib.Constants.ALL;
|
||||||
import static com.todoroo.andlib.Constants.COMMA;
|
import static com.todoroo.andlib.Constants.COMMA;
|
||||||
import static com.todoroo.andlib.Constants.FROM;
|
import static com.todoroo.andlib.Constants.FROM;
|
||||||
import static com.todoroo.andlib.Constants.GROUP_BY;
|
import static com.todoroo.andlib.Constants.GROUP_BY;
|
||||||
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.LEFT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.ORDER_BY;
|
import static com.todoroo.andlib.Constants.ORDER_BY;
|
||||||
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
import static com.todoroo.andlib.Constants.RIGHT_PARENTHESIS;
|
||||||
import static com.todoroo.andlib.Constants.SELECT;
|
import static com.todoroo.andlib.Constants.SELECT;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
import static com.todoroo.andlib.Constants.WHERE;
|
import static com.todoroo.andlib.Constants.WHERE;
|
||||||
import static com.todoroo.andlib.SqlTable.table;
|
import static com.todoroo.andlib.SqlTable.table;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import com.todoroo.astrid.data.Property;
|
import com.todoroo.astrid.data.Property;
|
||||||
|
|
||||||
public final class Query {
|
public final class Query {
|
||||||
|
|
||||||
private SqlTable table;
|
private SqlTable table;
|
||||||
private String queryTemplate = null;
|
private String queryTemplate = null;
|
||||||
private final ArrayList<Criterion> criterions = new ArrayList<Criterion>();
|
private final ArrayList<Criterion> criterions = new ArrayList<Criterion>();
|
||||||
private final ArrayList<Field> fields = new ArrayList<Field>();
|
private final ArrayList<Field> fields = new ArrayList<Field>();
|
||||||
private final ArrayList<Join> joins = new ArrayList<Join>();
|
private final ArrayList<Join> joins = new ArrayList<Join>();
|
||||||
private final ArrayList<Field> groupBies = new ArrayList<Field>();
|
private final ArrayList<Field> groupBies = new ArrayList<Field>();
|
||||||
private final ArrayList<Order> orders = new ArrayList<Order>();
|
private final ArrayList<Order> orders = new ArrayList<Order>();
|
||||||
private final ArrayList<Criterion> havings = new ArrayList<Criterion>();
|
private final ArrayList<Criterion> havings = new ArrayList<Criterion>();
|
||||||
|
|
||||||
private Query(Field... fields) {
|
private Query(Field... fields) {
|
||||||
this.fields.addAll(asList(fields));
|
this.fields.addAll(asList(fields));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Query select(Field... fields) {
|
public static Query select(Field... fields) {
|
||||||
return new Query(fields);
|
return new Query(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query from(SqlTable fromTable) {
|
public Query from(SqlTable fromTable) {
|
||||||
this.table = fromTable;
|
this.table = fromTable;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query join(Join... join) {
|
public Query join(Join... join) {
|
||||||
joins.addAll(asList(join));
|
joins.addAll(asList(join));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query where(Criterion criterion) {
|
public Query where(Criterion criterion) {
|
||||||
criterions.add(criterion);
|
criterions.add(criterion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query groupBy(Field... groupBy) {
|
public Query groupBy(Field... groupBy) {
|
||||||
groupBies.addAll(asList(groupBy));
|
groupBies.addAll(asList(groupBy));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query orderBy(Order... order) {
|
public Query orderBy(Order... order) {
|
||||||
orders.addAll(asList(order));
|
orders.addAll(asList(order));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query appendSelectFields(Property<?>... selectFields) {
|
public Query appendSelectFields(Property<?>... selectFields) {
|
||||||
this.fields.addAll(asList(selectFields));
|
this.fields.addAll(asList(selectFields));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
return this == o || !(o == null || getClass() != o.getClass()) && this.toString().equals(o.toString());
|
return this == o || !(o == null || getClass() != o.getClass()) && this.toString().equals(o.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return toString().hashCode();
|
return toString().hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
visitSelectClause(sql);
|
visitSelectClause(sql);
|
||||||
visitFromClause(sql);
|
visitFromClause(sql);
|
||||||
|
|
||||||
if(queryTemplate == null) {
|
if(queryTemplate == null) {
|
||||||
visitJoinClause(sql);
|
visitJoinClause(sql);
|
||||||
visitWhereClause(sql);
|
visitWhereClause(sql);
|
||||||
visitGroupByClause(sql);
|
visitGroupByClause(sql);
|
||||||
visitOrderByClause(sql);
|
visitOrderByClause(sql);
|
||||||
} else {
|
} else {
|
||||||
if(joins.size() > 0 || groupBies.size() > 0 || orders.size() > 0 ||
|
if(joins.size() > 0 || groupBies.size() > 0 || orders.size() > 0 ||
|
||||||
havings.size() > 0)
|
havings.size() > 0)
|
||||||
throw new IllegalStateException("Can't have extras AND query template"); //$NON-NLS-1$
|
throw new IllegalStateException("Can't have extras AND query template"); //$NON-NLS-1$
|
||||||
sql.append(queryTemplate);
|
sql.append(queryTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitOrderByClause(StringBuilder sql) {
|
private void visitOrderByClause(StringBuilder sql) {
|
||||||
if (orders.isEmpty()) {
|
if (orders.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(ORDER_BY);
|
sql.append(ORDER_BY);
|
||||||
for (Order order : orders) {
|
for (Order order : orders) {
|
||||||
sql.append(SPACE).append(order).append(COMMA);
|
sql.append(SPACE).append(order).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
private void visitGroupByClause(StringBuilder sql) {
|
private void visitGroupByClause(StringBuilder sql) {
|
||||||
if (groupBies.isEmpty()) {
|
if (groupBies.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(GROUP_BY);
|
sql.append(GROUP_BY);
|
||||||
for (Field groupBy : groupBies) {
|
for (Field groupBy : groupBies) {
|
||||||
sql.append(SPACE).append(groupBy).append(COMMA);
|
sql.append(SPACE).append(groupBy).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
if (havings.isEmpty()) {
|
if (havings.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append("HAVING");
|
sql.append("HAVING");
|
||||||
for (Criterion havingCriterion : havings) {
|
for (Criterion havingCriterion : havings) {
|
||||||
sql.append(SPACE).append(havingCriterion).append(COMMA);
|
sql.append(SPACE).append(havingCriterion).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitWhereClause(StringBuilder sql) {
|
private void visitWhereClause(StringBuilder sql) {
|
||||||
if (criterions.isEmpty()) {
|
if (criterions.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(WHERE);
|
sql.append(WHERE);
|
||||||
for (Criterion criterion : criterions) {
|
for (Criterion criterion : criterions) {
|
||||||
sql.append(SPACE).append(criterion).append(SPACE);
|
sql.append(SPACE).append(criterion).append(SPACE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitJoinClause(StringBuilder sql) {
|
private void visitJoinClause(StringBuilder sql) {
|
||||||
for (Join join : joins) {
|
for (Join join : joins) {
|
||||||
sql.append(join).append(SPACE);
|
sql.append(join).append(SPACE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitFromClause(StringBuilder sql) {
|
private void visitFromClause(StringBuilder sql) {
|
||||||
if (table == null) {
|
if (table == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(FROM).append(SPACE).append(table).append(SPACE);
|
sql.append(FROM).append(SPACE).append(table).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitSelectClause(StringBuilder sql) {
|
private void visitSelectClause(StringBuilder sql) {
|
||||||
sql.append(SELECT).append(SPACE);
|
sql.append(SELECT).append(SPACE);
|
||||||
if (fields.isEmpty()) {
|
if (fields.isEmpty()) {
|
||||||
sql.append(ALL).append(SPACE);
|
sql.append(ALL).append(SPACE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
sql.append(field.toStringInSelect()).append(COMMA);
|
sql.append(field.toStringInSelect()).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SqlTable as(String alias) {
|
public SqlTable as(String alias) {
|
||||||
return table(LEFT_PARENTHESIS + this.toString() + RIGHT_PARENTHESIS).as(alias);
|
return table(LEFT_PARENTHESIS + this.toString() + RIGHT_PARENTHESIS).as(alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query having(Criterion criterion) {
|
public Query having(Criterion criterion) {
|
||||||
this.havings.add(criterion);
|
this.havings.add(criterion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of fields returned by this query
|
* Gets a list of fields returned by this query
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Property<?>[] getFields() {
|
public Property<?>[] getFields() {
|
||||||
return fields.toArray(new Property<?>[fields.size()]);
|
return fields.toArray(new Property<?>[fields.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the SQL query template (comes after the "from")
|
* Add the SQL query template (comes after the "from")
|
||||||
* @param sqlQuery
|
* @param sqlQuery
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Query withQueryTemplate(String template) {
|
public Query withQueryTemplate(String template) {
|
||||||
queryTemplate = template;
|
queryTemplate = template;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,117 +1,117 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.COMMA;
|
import static com.todoroo.andlib.Constants.COMMA;
|
||||||
import static com.todoroo.andlib.Constants.GROUP_BY;
|
import static com.todoroo.andlib.Constants.GROUP_BY;
|
||||||
import static com.todoroo.andlib.Constants.LIMIT;
|
import static com.todoroo.andlib.Constants.LIMIT;
|
||||||
import static com.todoroo.andlib.Constants.ORDER_BY;
|
import static com.todoroo.andlib.Constants.ORDER_BY;
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
import static com.todoroo.andlib.Constants.WHERE;
|
import static com.todoroo.andlib.Constants.WHERE;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query Template returns a bunch of criteria that allows a query to be
|
* Query Template returns a bunch of criteria that allows a query to be
|
||||||
* constructed
|
* constructed
|
||||||
*
|
*
|
||||||
* @author Tim Su <tim@todoroo.com>
|
* @author Tim Su <tim@todoroo.com>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final class QueryTemplate {
|
public final class QueryTemplate {
|
||||||
|
|
||||||
private final ArrayList<Criterion> criterions = new ArrayList<Criterion>();
|
private final ArrayList<Criterion> criterions = new ArrayList<Criterion>();
|
||||||
private final ArrayList<Join> joins = new ArrayList<Join>();
|
private final ArrayList<Join> joins = new ArrayList<Join>();
|
||||||
private final ArrayList<Field> groupBies = new ArrayList<Field>();
|
private final ArrayList<Field> groupBies = new ArrayList<Field>();
|
||||||
private final ArrayList<Order> orders = new ArrayList<Order>();
|
private final ArrayList<Order> orders = new ArrayList<Order>();
|
||||||
private final ArrayList<Criterion> havings = new ArrayList<Criterion>();
|
private final ArrayList<Criterion> havings = new ArrayList<Criterion>();
|
||||||
private Integer limit = null;
|
private Integer limit = null;
|
||||||
|
|
||||||
public QueryTemplate join(Join... join) {
|
public QueryTemplate join(Join... join) {
|
||||||
joins.addAll(asList(join));
|
joins.addAll(asList(join));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTemplate where(Criterion criterion) {
|
public QueryTemplate where(Criterion criterion) {
|
||||||
criterions.add(criterion);
|
criterions.add(criterion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTemplate groupBy(Field... groupBy) {
|
public QueryTemplate groupBy(Field... groupBy) {
|
||||||
groupBies.addAll(asList(groupBy));
|
groupBies.addAll(asList(groupBy));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTemplate orderBy(Order... order) {
|
public QueryTemplate orderBy(Order... order) {
|
||||||
orders.addAll(asList(order));
|
orders.addAll(asList(order));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
visitJoinClause(sql);
|
visitJoinClause(sql);
|
||||||
visitWhereClause(sql);
|
visitWhereClause(sql);
|
||||||
visitGroupByClause(sql);
|
visitGroupByClause(sql);
|
||||||
visitOrderByClause(sql);
|
visitOrderByClause(sql);
|
||||||
if(limit != null)
|
if(limit != null)
|
||||||
sql.append(LIMIT).append(SPACE).append(limit);
|
sql.append(LIMIT).append(SPACE).append(limit);
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitOrderByClause(StringBuilder sql) {
|
private void visitOrderByClause(StringBuilder sql) {
|
||||||
if (orders.isEmpty()) {
|
if (orders.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(ORDER_BY);
|
sql.append(ORDER_BY);
|
||||||
for (Order order : orders) {
|
for (Order order : orders) {
|
||||||
sql.append(SPACE).append(order).append(COMMA);
|
sql.append(SPACE).append(order).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
private void visitGroupByClause(StringBuilder sql) {
|
private void visitGroupByClause(StringBuilder sql) {
|
||||||
if (groupBies.isEmpty()) {
|
if (groupBies.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(GROUP_BY);
|
sql.append(GROUP_BY);
|
||||||
for (Field groupBy : groupBies) {
|
for (Field groupBy : groupBies) {
|
||||||
sql.append(SPACE).append(groupBy).append(COMMA);
|
sql.append(SPACE).append(groupBy).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
if (havings.isEmpty()) {
|
if (havings.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append("HAVING");
|
sql.append("HAVING");
|
||||||
for (Criterion havingCriterion : havings) {
|
for (Criterion havingCriterion : havings) {
|
||||||
sql.append(SPACE).append(havingCriterion).append(COMMA);
|
sql.append(SPACE).append(havingCriterion).append(COMMA);
|
||||||
}
|
}
|
||||||
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
sql.deleteCharAt(sql.length() - 1).append(SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitWhereClause(StringBuilder sql) {
|
private void visitWhereClause(StringBuilder sql) {
|
||||||
if (criterions.isEmpty()) {
|
if (criterions.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sql.append(WHERE);
|
sql.append(WHERE);
|
||||||
for (Criterion criterion : criterions) {
|
for (Criterion criterion : criterions) {
|
||||||
sql.append(SPACE).append(criterion).append(SPACE);
|
sql.append(SPACE).append(criterion).append(SPACE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void visitJoinClause(StringBuilder sql) {
|
private void visitJoinClause(StringBuilder sql) {
|
||||||
for (Join join : joins) {
|
for (Join join : joins) {
|
||||||
sql.append(join).append(SPACE);
|
sql.append(join).append(SPACE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTemplate having(Criterion criterion) {
|
public QueryTemplate having(Criterion criterion) {
|
||||||
this.havings.add(criterion);
|
this.havings.add(criterion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryTemplate limit(int limitValue) {
|
public QueryTemplate limit(int limitValue) {
|
||||||
this.limit = limitValue;
|
this.limit = limitValue;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
public class SqlTable extends DBObject<SqlTable> {
|
public class SqlTable extends DBObject<SqlTable> {
|
||||||
|
|
||||||
protected SqlTable(String expression) {
|
protected SqlTable(String expression) {
|
||||||
super(expression);
|
super(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SqlTable table(String table) {
|
public static SqlTable table(String table) {
|
||||||
return new SqlTable(table);
|
return new SqlTable(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
protected String fieldExpression(String fieldName) {
|
protected String fieldExpression(String fieldName) {
|
||||||
if (hasAlias()) {
|
if (hasAlias()) {
|
||||||
return alias + "." + fieldName;
|
return alias + "." + fieldName;
|
||||||
}
|
}
|
||||||
return expression+"."+fieldName;
|
return expression+"."+fieldName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,92 +1,92 @@
|
|||||||
package com.todoroo.andlib;
|
package com.todoroo.andlib;
|
||||||
|
|
||||||
import static com.todoroo.andlib.Constants.SPACE;
|
import static com.todoroo.andlib.Constants.SPACE;
|
||||||
|
|
||||||
public class UnaryCriterion extends Criterion {
|
public class UnaryCriterion extends Criterion {
|
||||||
protected final Field expression;
|
protected final Field expression;
|
||||||
protected final Object value;
|
protected final Object value;
|
||||||
|
|
||||||
UnaryCriterion(Field expression, Operator operator, Object value) {
|
UnaryCriterion(Field expression, Operator operator, Object value) {
|
||||||
super(operator);
|
super(operator);
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void populate(StringBuilder sb) {
|
protected void populate(StringBuilder sb) {
|
||||||
beforePopulateOperator(sb);
|
beforePopulateOperator(sb);
|
||||||
populateOperator(sb);
|
populateOperator(sb);
|
||||||
afterPopulateOperator(sb);
|
afterPopulateOperator(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion eq(Field expression, Object value) {
|
public static Criterion eq(Field expression, Object value) {
|
||||||
return new UnaryCriterion(expression, Operator.eq, value);
|
return new UnaryCriterion(expression, Operator.eq, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void beforePopulateOperator(StringBuilder sb) {
|
protected void beforePopulateOperator(StringBuilder sb) {
|
||||||
sb.append(expression);
|
sb.append(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void populateOperator(StringBuilder sb) {
|
protected void populateOperator(StringBuilder sb) {
|
||||||
sb.append(operator);
|
sb.append(operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
protected void afterPopulateOperator(StringBuilder sb) {
|
protected void afterPopulateOperator(StringBuilder sb) {
|
||||||
if(value == null)
|
if(value == null)
|
||||||
return;
|
return;
|
||||||
else if(value instanceof String)
|
else if(value instanceof String)
|
||||||
sb.append("'").append(sanitize((String) value)).append("'");
|
sb.append("'").append(sanitize((String) value)).append("'");
|
||||||
else
|
else
|
||||||
sb.append(value);
|
sb.append(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitize the given input for SQL
|
* Sanitize the given input for SQL
|
||||||
* @param input
|
* @param input
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("nls")
|
@SuppressWarnings("nls")
|
||||||
public static String sanitize(String input) {
|
public static String sanitize(String input) {
|
||||||
return input.replace("\\", "\\\\").replace("'", "\\'");
|
return input.replace("\\", "\\\\").replace("'", "\\'");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion neq(Field field, Object value) {
|
public static Criterion neq(Field field, Object value) {
|
||||||
return new UnaryCriterion(field, Operator.neq, value);
|
return new UnaryCriterion(field, Operator.neq, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion gt(Field field, Object value) {
|
public static Criterion gt(Field field, Object value) {
|
||||||
return new UnaryCriterion(field, Operator.gt, value);
|
return new UnaryCriterion(field, Operator.gt, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion lt(Field field, Object value) {
|
public static Criterion lt(Field field, Object value) {
|
||||||
return new UnaryCriterion(field, Operator.lt, value);
|
return new UnaryCriterion(field, Operator.lt, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion isNull(Field field) {
|
public static Criterion isNull(Field field) {
|
||||||
return new UnaryCriterion(field, Operator.isNull, null) {
|
return new UnaryCriterion(field, Operator.isNull, null) {
|
||||||
@Override
|
@Override
|
||||||
protected void populateOperator(StringBuilder sb) {
|
protected void populateOperator(StringBuilder sb) {
|
||||||
sb.append(SPACE).append(operator);
|
sb.append(SPACE).append(operator);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion isNotNull(Field field) {
|
public static Criterion isNotNull(Field field) {
|
||||||
return new UnaryCriterion(field, Operator.isNotNull, null) {
|
return new UnaryCriterion(field, Operator.isNotNull, null) {
|
||||||
@Override
|
@Override
|
||||||
protected void populateOperator(StringBuilder sb) {
|
protected void populateOperator(StringBuilder sb) {
|
||||||
sb.append(SPACE).append(operator);
|
sb.append(SPACE).append(operator);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Criterion like(Field field, String value) {
|
public static Criterion like(Field field, String value) {
|
||||||
return new UnaryCriterion(field, Operator.like, value) {
|
return new UnaryCriterion(field, Operator.like, value) {
|
||||||
@Override
|
@Override
|
||||||
protected void populateOperator(StringBuilder sb) {
|
protected void populateOperator(StringBuilder sb) {
|
||||||
sb.append(SPACE).append(operator).append(SPACE);
|
sb.append(SPACE).append(operator).append(SPACE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue