You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ke...@apache.org on 2012/10/12 04:25:49 UTC
[1/50] git commit: Merge DB query builders from master
Updated Branches:
refs/heads/javelin 387c6fc13 -> 5d7509e96
Merge DB query builders from master
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/5d7509e9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/5d7509e9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/5d7509e9
Branch: refs/heads/javelin
Commit: 5d7509e9642f2d642f4346eab468a5ef2ed478d1
Parents: 5fee891
Author: Kelven Yang <ke...@citrix.com>
Authored: Thu Oct 11 18:24:33 2012 -0700
Committer: Kelven Yang <ke...@citrix.com>
Committed: Thu Oct 11 18:24:33 2012 -0700
----------------------------------------------------------------------
server/src/com/cloud/storage/TemplateProfile.java | 255 +++++++++++++++
utils/src/com/cloud/utils/db/Condition.java | 100 ++++++
utils/src/com/cloud/utils/db/FirstWhere.java | 29 ++
utils/src/com/cloud/utils/db/JoinQueryBuilder.java | 31 ++
utils/src/com/cloud/utils/db/NextWhere.java | 27 ++
utils/src/com/cloud/utils/db/On.java | 21 ++
utils/src/com/cloud/utils/db/QueryBuilder.java | 194 +++++++++++
utils/src/com/cloud/utils/db/Select.java | 52 +++
.../src/com/cloud/utils/db/SelectQueryBuilder.java | 28 ++
.../src/com/cloud/utils/db/SimpleQueryBuilder.java | 47 +++
utils/src/com/cloud/utils/db/Where.java | 81 +++++
11 files changed, 865 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/server/src/com/cloud/storage/TemplateProfile.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/TemplateProfile.java b/server/src/com/cloud/storage/TemplateProfile.java
new file mode 100755
index 0000000..1d8b6bf
--- /dev/null
+++ b/server/src/com/cloud/storage/TemplateProfile.java
@@ -0,0 +1,255 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.storage;
+
+import java.util.Map;
+
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.storage.Storage.ImageFormat;
+import com.cloud.storage.VMTemplateVO;
+
+public class TemplateProfile {
+ Long userId;
+ String name;
+ String displayText;
+ Integer bits;
+ Boolean passwordEnabled;
+ Boolean sshKeyEnbaled;
+ Boolean requiresHvm;
+ String url;
+ Boolean isPublic;
+ Boolean featured;
+ Boolean isExtractable;
+ ImageFormat format;
+ Long guestOsId;
+ Long zoneId;
+ HypervisorType hypervisorType;
+ String accountName;
+ Long domainId;
+ Long accountId;
+ String chksum;
+ Boolean bootable;
+ Long templateId;
+ VMTemplateVO template;
+ String templateTag;
+ Map details;
+
+ public TemplateProfile(Long templateId, Long userId, String name, String displayText, Integer bits, Boolean passwordEnabled, Boolean requiresHvm,
+ String url, Boolean isPublic, Boolean featured, Boolean isExtractable, ImageFormat format, Long guestOsId, Long zoneId,
+ HypervisorType hypervisorType, String accountName, Long domainId, Long accountId, String chksum, Boolean bootable, Map details, Boolean sshKeyEnabled) {
+ this.templateId = templateId;
+ this.userId = userId;
+ this.name = name;
+ this.displayText = displayText;
+ this.bits = bits;
+ this.passwordEnabled = passwordEnabled;
+ this.requiresHvm = requiresHvm;
+ this.url = url;
+ this.isPublic = isPublic;
+ this.featured = featured;
+ this.isExtractable = isExtractable;
+ this.format = format;
+ this.guestOsId = guestOsId;
+ this.zoneId = zoneId;
+ this.hypervisorType = hypervisorType;
+ this.accountName = accountName;
+ this.domainId = domainId;
+ this.accountId = accountId;
+ this.chksum = chksum;
+ this.bootable = bootable;
+ this.details = details;
+ this.sshKeyEnbaled = sshKeyEnabled;
+ }
+
+ public TemplateProfile(Long userId, VMTemplateVO template, Long zoneId) {
+ this.userId = userId;
+ this.template = template;
+ this.zoneId = zoneId;
+ }
+
+ public TemplateProfile(Long templateId, Long userId, String name, String displayText, Integer bits, Boolean passwordEnabled, Boolean requiresHvm,
+ String url, Boolean isPublic, Boolean featured, Boolean isExtractable, ImageFormat format, Long guestOsId, Long zoneId,
+ HypervisorType hypervisorType, String accountName, Long domainId, Long accountId, String chksum, Boolean bootable, String templateTag, Map details, Boolean sshKeyEnabled) {
+ this(templateId, userId, name, displayText, bits, passwordEnabled, requiresHvm, url, isPublic, featured, isExtractable, format, guestOsId, zoneId,
+ hypervisorType, accountName, domainId, accountId, chksum, bootable, details, sshKeyEnabled);
+ this.templateTag = templateTag;
+ }
+
+ public Long getTemplateId() {
+ return templateId;
+ }
+ public void setTemplateId(Long id) {
+ this.templateId = id;
+ }
+
+ public Long getUserId() {
+ return userId;
+ }
+ public void setUserId(Long userId) {
+ this.userId = userId;
+ }
+
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDisplayText() {
+ return displayText;
+ }
+ public void setDisplayText(String text) {
+ this.displayText = text;
+ }
+
+ public Integer getBits() {
+ return bits;
+ }
+ public void setBits(Integer bits) {
+ this.bits = bits;
+ }
+
+ public Boolean getPasswordEnabled() {
+ return passwordEnabled;
+ }
+ public void setPasswordEnabled(Boolean enabled) {
+ this.passwordEnabled = enabled;
+ }
+
+ public Boolean getRequiresHVM() {
+ return requiresHvm;
+ }
+ public void setRequiresHVM(Boolean hvm) {
+ this.requiresHvm = hvm;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public Boolean getIsPublic() {
+ return isPublic;
+ }
+ public void setIsPublic(Boolean is) {
+ this.isPublic = is;
+ }
+
+ public Boolean getFeatured() {
+ return featured;
+ }
+ public void setFeatured(Boolean featured) {
+ this.featured = featured;
+ }
+
+ public Boolean getIsExtractable() {
+ return isExtractable;
+ }
+ public void setIsExtractable(Boolean is) {
+ this.isExtractable = is;
+ }
+
+ public ImageFormat getFormat() {
+ return format;
+ }
+ public void setFormat(ImageFormat format) {
+ this.format = format;
+ }
+
+ public Long getGuestOsId() {
+ return guestOsId;
+ }
+ public void setGuestOsId(Long id) {
+ this.guestOsId = id;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+ public void setZoneId(Long id) {
+ this.zoneId = id;
+ }
+
+ public HypervisorType getHypervisorType() {
+ return hypervisorType;
+ }
+ public void setHypervisorType(HypervisorType type) {
+ this.hypervisorType = type;
+ }
+
+ public Long getDomainId() {
+ return domainId;
+ }
+ public void setDomainId(Long id) {
+ this.domainId = id;
+ }
+
+ public Long getAccountId() {
+ return accountId;
+ }
+ public void setAccountId(Long id) {
+ this.accountId = id;
+ }
+
+ public String getCheckSum() {
+ return chksum;
+ }
+ public void setCheckSum(String chksum) {
+ this.chksum = chksum;
+ }
+
+ public Boolean getBootable() {
+ return this.bootable;
+ }
+ public void setBootable(Boolean bootable) {
+ this.bootable = bootable;
+ }
+
+ public VMTemplateVO getTemplate() {
+ return template;
+ }
+ public void setTemplate(VMTemplateVO template) {
+ this.template = template;
+ }
+
+ public String getTemplateTag() {
+ return templateTag;
+ }
+
+ public void setTemplateTag(String templateTag) {
+ this.templateTag = templateTag;
+ }
+
+ public Map getDetails() {
+ return this.details;
+ }
+
+ public void setDetails(Map details) {
+ this.details = details;
+ }
+
+ public void setSshKeyEnabled(Boolean enabled) {
+ this.sshKeyEnbaled = enabled;
+ }
+
+ public Boolean getSshKeyEnabled() {
+ return this.sshKeyEnbaled;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/Condition.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/Condition.java b/utils/src/com/cloud/utils/db/Condition.java
new file mode 100644
index 0000000..55b19d8
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/Condition.java
@@ -0,0 +1,100 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+import java.util.HashMap;
+
+import com.cloud.utils.db.SearchCriteria.Op;
+
+public class Condition<T, K> {
+ Where<T, K> _where;
+ Attribute _attr;
+ String _as;
+ SearchCriteria.Op _op;
+ String _paramName;
+
+ protected Condition(Where<T, K> where, Attribute attr, String as) {
+ assert (where != null) : "What am I going to return to the user when Where is null?";
+ assert (attr != null) : "What's the point of giving me a null attribute?";
+ _where = where;
+ _attr = attr;
+ _as = as;
+ }
+
+ protected NextWhere<T, K> set(Op op, String paramName) {
+ _op = op;
+ _paramName = paramName;
+ Where<T, K> where = _where;
+ _where = null;
+ return where;
+ }
+
+ public NextWhere<T, K> eq(String paramName) {
+ return set(Op.EQ, paramName);
+ }
+
+ public NextWhere<T, K> lt(String paramName) {
+ return set(Op.LT, paramName);
+ }
+
+ public NextWhere<T, K> lteq(String paramName) {
+ return set(Op.LTEQ, paramName);
+ }
+
+ public NextWhere<T, K> gt(String paramName) {
+ return set(Op.GT, paramName);
+ }
+
+ public NextWhere<T, K> isNull() {
+ return set(Op.NULL, null);
+ }
+
+ public NextWhere<T, K> isNotNull() {
+ return set(Op.NNULL, null);
+ }
+
+ public NextWhere<T, K> in(String paramName) {
+ _op = Op.IN;
+ _paramName = paramName;
+ return _where;
+ }
+
+ protected String getParamName() {
+ assert (_paramName instanceof String) : "Well, how can we get back a parameter name if it was not assigned one?";
+ return _paramName;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return _paramName.equals(obj);
+ }
+
+ @Override
+ public int hashCode() {
+ return _paramName.hashCode();
+ }
+
+ public void toSql(StringBuilder builder, HashMap<String, Object[]> values) {
+ if (_as != null) {
+ builder.append(_as);
+ } else {
+ builder.append(_attr.table);
+ }
+ builder.append(".").append(_attr.columnName);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/FirstWhere.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/FirstWhere.java b/utils/src/com/cloud/utils/db/FirstWhere.java
new file mode 100644
index 0000000..46551e9
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/FirstWhere.java
@@ -0,0 +1,29 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+public interface FirstWhere<T, K> {
+ Condition<T, K> field(Object field);
+
+ Condition<T, K> field(Object field, String as);
+
+ NextWhere<T, K> text(String text, String... paramNames);
+
+ FirstWhere<T, K> op();
+
+ void done();
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/JoinQueryBuilder.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/JoinQueryBuilder.java b/utils/src/com/cloud/utils/db/JoinQueryBuilder.java
new file mode 100644
index 0000000..090a1d1
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/JoinQueryBuilder.java
@@ -0,0 +1,31 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+/**
+ * JoinQueryBuilder builds queries for joins between multiple tables.
+ *
+ */
+public interface JoinQueryBuilder<S, T> {
+ Select<S, T> selectField(Object column);
+
+ <J> On<S, J, T> innerJoin(Class<J> entityClazz);
+
+ <J> J entity(Class<J> entityClazz);
+
+ FirstWhere<S, T> where();
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/NextWhere.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/NextWhere.java b/utils/src/com/cloud/utils/db/NextWhere.java
new file mode 100644
index 0000000..0d65ae4
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/NextWhere.java
@@ -0,0 +1,27 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+public interface NextWhere<T, K> extends FirstWhere<T, K> {
+ NextWhere<T, K> and();
+ NextWhere<T, K> or();
+ NextWhere<T, K> not();
+
+ @Override
+ void done();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/On.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/On.java b/utils/src/com/cloud/utils/db/On.java
new file mode 100644
index 0000000..6965f4d
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/On.java
@@ -0,0 +1,21 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+public interface On<S, J, T> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/QueryBuilder.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/QueryBuilder.java b/utils/src/com/cloud/utils/db/QueryBuilder.java
new file mode 100644
index 0000000..2eb3658
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/QueryBuilder.java
@@ -0,0 +1,194 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+
+import com.cloud.utils.Pair;
+
+public class QueryBuilder<S, T> implements MethodInterceptor, SimpleQueryBuilder<S>, SelectQueryBuilder<S, T>, JoinQueryBuilder<S, T> {
+ public enum Func {
+ NATIVE("@", 1),
+ MAX("MAX(@)", 1),
+ MIN("MIN(@)", 1),
+ FIRST("FIRST(@)", 1),
+ LAST("LAST(@)", 1),
+ SUM("SUM(@)", 1),
+ COUNT("COUNT(@)", 1),
+ DISTINCT("DISTINCT(@)", 1);
+
+ private String func;
+ private int count;
+
+ Func(String func, int params) {
+ this.func = func;
+ this.count = params;
+ }
+
+ @Override
+ public String toString() {
+ return func;
+ }
+
+ public int getCount() {
+ return count;
+ }
+ }
+
+ protected HashMap<Class<?>, Pair<GenericDaoBase<?,?>, Object>> _entities;
+ protected ArrayList<Attribute> _specifiedAttrs = new ArrayList<Attribute>();
+ protected T _resultSetClass;
+ protected ArrayList<Select<S, T>> _selects;
+
+ public QueryBuilder(Class<T> resultSetClass, Class<?>... clazzes) {
+ _entities = new HashMap<Class<?>, Pair<GenericDaoBase<?,?>, Object>>(clazzes.length);
+ for (Class<?> clazz : clazzes) {
+ GenericDaoBase<?,?> dao = GenericDaoBase.getDao(clazz);
+ Enhancer searchEnhancer = new Enhancer();
+ searchEnhancer.setSuperclass(clazz);
+ searchEnhancer.setCallback(this);
+ Object entity = searchEnhancer.create();
+
+ _entities.put(clazz, new Pair<GenericDaoBase<?, ?>, Object>(dao, entity));
+ }
+ }
+
+ protected void clean() {
+ _specifiedAttrs = null;
+ _entities = null;
+ }
+
+ /**
+ * Constructor for SelectQueryBuilder interface. Must specify the
+ * table to be performing the query on and the result class to place it in.
+ * @param entityClass entity class to do the query on.
+ * @param resultSetClass result class to put the result set in.
+ */
+ public QueryBuilder(Class<S> entityClass, Class<T> resultSetClass) {
+ _entities = new HashMap<Class<?>, Pair<GenericDaoBase<?,?>, Object>>(1);
+ GenericDaoBase<?,?> dao = GenericDaoBase.getDao(entityClass);
+ Enhancer searchEnhancer = new Enhancer();
+ searchEnhancer.setSuperclass(entityClass);
+ searchEnhancer.setCallback(this);
+ Object entity = searchEnhancer.create();
+
+ _entities.put(entityClass, new Pair<GenericDaoBase<?, ?>, Object>(dao, entity));
+ }
+
+ @Override
+ public SimpleQueryBuilder<S> selectFields(Object... fields) {
+ assert _entities != null && _entities.size() == 1 : "Now you've done it....Stop casting interfaces on the QueryBuilder";
+ assert _specifiedAttrs.size() > 0 : "You didn't specify any attributes";
+
+ if (_selects == null) {
+ _selects = new ArrayList<Select<S, T>>(fields.length);
+ }
+
+ for (Attribute attr : _specifiedAttrs) {
+ _selects.add(new Select<S, T>(this, null, attr));
+ }
+
+ _specifiedAttrs.clear();
+
+ return this;
+ }
+
+ protected void set(GenericDaoBase<?, ?> dao , String name) {
+ Attribute attr = dao.getAllAttributes().get(name);
+ assert (attr != null) : "Searching for a field that's not there: " + name;
+ _specifiedAttrs.add(attr);
+ }
+
+
+ @Override
+ public Object intercept(Object entity, Method method, Object[] args, MethodProxy proxy) throws Throwable {
+ Class<?> entityClass = entity.getClass().getSuperclass();
+
+ Pair<GenericDaoBase<?,?>, Object> daoInfo = _entities.get(entityClass);
+ assert (daoInfo != null) : "You need to specify " + entityClass + " as one of the entities in the Query";
+ GenericDaoBase<?,?> dao = daoInfo.first();
+
+ String name = method.getName();
+ if (name.startsWith("get")) {
+ String fieldName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
+ set(dao, fieldName);
+ return null;
+ } else if (name.startsWith("is")) {
+ String fieldName = Character.toLowerCase(name.charAt(2)) + name.substring(3);
+ set(dao, fieldName);
+ return null;
+ } else {
+ assert false : "Perhaps you need to make the method start with get or is?";
+ }
+ return proxy.invokeSuper(entity, args);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <E> E entity(Class<E> clazz) {
+ return (E)_entities.get(clazz).second();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public S entity() {
+ return (S)_entities.values().iterator().next().second();
+ }
+
+ @Override
+ public FirstWhere<S, T> where() {
+ return new Where<S, T>(this);
+ }
+
+ @Override
+ public SimpleQueryBuilder<S> selectAll() {
+ return this;
+ }
+
+ public List<Attribute> getSpecifiedAttributes() {
+ return _specifiedAttrs;
+ }
+
+ public Attribute getSpecifiedAttribute() {
+ assert _specifiedAttrs.size() == 1 : "You can only specify one attribute";
+ return _specifiedAttrs.get(0);
+ }
+
+ @Override
+ public Select<S, T> selectColumn(Object column) {
+ return null;
+ }
+
+ @Override
+ public Select<S, T> selectField(Object column) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public <J> On<S, J, T> innerJoin(Class<J> entityClazz) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/Select.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/Select.java b/utils/src/com/cloud/utils/db/Select.java
new file mode 100644
index 0000000..d82d1fa
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/Select.java
@@ -0,0 +1,52 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+import java.lang.reflect.Field;
+
+public class Select<S, T> {
+ QueryBuilder<S,T> _builder;
+ Class<T> _clazz;
+ Attribute _attr;
+ String _as;
+ Field _field;
+
+ protected Select(QueryBuilder<S, T> builder, Class<T> clazz, Attribute attr) {
+ _builder = builder;
+ _clazz = clazz;
+ _attr = attr;
+ }
+
+ public QueryBuilder<S, T> into(String fieldName) {
+ if (fieldName != null) {
+ try {
+ _field = _clazz.getDeclaredField(fieldName);
+ _field.setAccessible(true);
+ } catch (SecurityException e) {
+ throw new RuntimeException("Unable to find " + fieldName + " in " + _clazz.getName(), e);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException("Unable to find " + fieldName + " in " + _clazz.getName(), e);
+ }
+ }
+ return _builder;
+ }
+
+ public QueryBuilder<S, T> as(String as) {
+ _as = as;
+ return _builder;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/SelectQueryBuilder.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/SelectQueryBuilder.java b/utils/src/com/cloud/utils/db/SelectQueryBuilder.java
new file mode 100644
index 0000000..82ba134
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/SelectQueryBuilder.java
@@ -0,0 +1,28 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+/**
+ * is defined.
+ */
+public interface SelectQueryBuilder<T, S> {
+ Select<T, S> selectColumn(Object column);
+
+ T entity();
+
+ FirstWhere<T, S> where();
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/SimpleQueryBuilder.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/SimpleQueryBuilder.java b/utils/src/com/cloud/utils/db/SimpleQueryBuilder.java
new file mode 100644
index 0000000..9e446d2
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/SimpleQueryBuilder.java
@@ -0,0 +1,47 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+/**
+ * SimpleQueryBuilder builds queries against a single table. The
+ *
+ */
+public interface SimpleQueryBuilder<S> {
+ /**
+ * Select all of the columns in the entity object. This is default so
+ * it's not necessary to make this method call at all.
+ */
+ SimpleQueryBuilder<S> selectAll();
+
+ /**
+ * Select the following columns
+ * @param columns array of columsn to select.
+ */
+ SimpleQueryBuilder<S> selectFields(Object... columns);
+
+ /**
+ * @return the entity object we're building this query for. By using this
+ * entity object, you can specify which column to select or form
+ */
+ S entity();
+
+ /**
+ * Starts the query conditionals.
+ * @return
+ */
+ FirstWhere<S, ?> where();
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5d7509e9/utils/src/com/cloud/utils/db/Where.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/Where.java b/utils/src/com/cloud/utils/db/Where.java
new file mode 100644
index 0000000..6815f24
--- /dev/null
+++ b/utils/src/com/cloud/utils/db/Where.java
@@ -0,0 +1,81 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.utils.db;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Where implements any list of search conditions.
+ *
+ */
+public class Where<T, K> implements FirstWhere<T, K>, NextWhere<T, K> {
+ QueryBuilder<T, K> _builder;
+ List<Object> _conditions = new ArrayList<Object>();
+
+ protected Where(QueryBuilder<T, K> builder) {
+ _builder = builder;
+ }
+
+ @Override
+ public Condition<T, K> field(Object useless, String as) {
+ Attribute attr = _builder.getSpecifiedAttribute();
+ Condition<T, K> cond = new Condition<T, K>(this, attr, as);
+ _conditions.add(cond);
+ return cond;
+ }
+
+ @Override
+ public Where<T, K> and() {
+ _conditions.add(" (");
+ return this;
+ }
+
+ @Override
+ public Where<T, K> or() {
+ _conditions.add(" OR ");
+ return this;
+ }
+
+ @Override
+ public NextWhere<T, K> not() {
+ _conditions.add(" NOT ");
+ return this;
+ }
+
+ @Override
+ public NextWhere<T, K> text(String text, String... paramNames) {
+ assert ((paramNames.length == 0 && !text.contains("?")) || (text.matches("\\?.*{" + paramNames.length + "}")));
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Condition<T, K> field(Object useless) {
+ return field(useless, null);
+ }
+
+ @Override
+ public FirstWhere<T, K> op() {
+ _conditions.add("(");
+ return this;
+ }
+
+ @Override
+ public void done() {
+ }
+}