diff --git a/README.md b/README.md
index c4ccd98..eb3b120 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
+
# uid.database
pom.xml:
@@ -14,5 +15,5 @@
ua.net.uiduid.database
- 1.0.2
+ 1.0.0
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAO.java b/src/main/java/ua/net/uid/utils/db/dao/DAO.java
new file mode 100644
index 0000000..d938102
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAO.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.dao;
+
+import ua.net.uid.utils.db.Session;
+
+public interface DAO {
+ Session getSession();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAOAbstract.java b/src/main/java/ua/net/uid/utils/db/dao/DAOAbstract.java
new file mode 100644
index 0000000..e171851
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOAbstract.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.dao;
+
+import ua.net.uid.utils.db.Session;
+
+import java.io.Serializable;
+
+public abstract class DAOAbstract, PK extends Serializable> extends DAOCore implements DAOBase {
+ public DAOAbstract(Session session) {
+ super(session);
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAOBase.java b/src/main/java/ua/net/uid/utils/db/dao/DAOBase.java
new file mode 100644
index 0000000..9ec7f02
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOBase.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.dao;
+
+import ua.net.uid.utils.db.Fetcher;
+import ua.net.uid.utils.db.Outcome;
+import ua.net.uid.utils.db.query.Condition;
+import ua.net.uid.utils.db.query.Order;
+import ua.net.uid.utils.db.query.QueryBuilder;
+
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.List;
+import ua.net.uid.utils.db.Processor;
+
+public interface DAOBase, PK extends Serializable> extends DAO {
+ String getTableName();
+
+ Condition getPrimaryCondition(PK key);
+
+ default Order getDefaultOrder() {
+ return null;
+ }
+
+ Fetcher getFetcher();
+
+ default T get(PK key) throws SQLException {
+ return getBy(getPrimaryCondition(key));
+ }
+
+ default T getBy(Condition condition) throws SQLException {
+ return new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(" WHERE ", condition)
+ .append(" LIMIT 1")
+ .on(getSession()).scalar(getFetcher());
+ }
+
+ default long countAll() throws SQLException {
+ return getSession().query("SELECT COUNT(*) FROM " + getTableName()).select(Outcome.FIRST_LONG);
+ }
+
+ default List findAll() throws SQLException {
+ return findAll(getDefaultOrder());
+ }
+
+ default void foreachAll(Processor.Callback callback) throws SQLException {
+ foreachAll(callback, getDefaultOrder());
+ }
+
+ default List findAll(Order order) throws SQLException {
+ return new QueryBuilder().append("SELECT * FROM ").append(getTableName())
+ .append(order).on(getSession()).list(getFetcher());
+ }
+
+ default void foreachAll(Processor.Callback callback, Order order) throws SQLException {
+ new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(order)
+ .on(getSession()).foreach(getFetcher(), callback);
+ }
+
+ default List findAll(long limit, long offset) throws SQLException {
+ return findAll(getDefaultOrder(), limit, offset);
+ }
+
+ default void foreachAll(Processor.Callback callback, long limit, long offset) throws SQLException {
+ foreachAll(callback, getDefaultOrder(), limit, offset);
+ }
+
+ default List findAll(Order order, long limit, long offset) throws SQLException {
+ return new QueryBuilder().append("SELECT * FROM ").append(getTableName())
+ .append(order).on(getSession()).list(getFetcher());
+ }
+
+ default void foreachAll(Processor.Callback callback, Order order, long limit, long offset) throws SQLException {
+ new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(order)
+ .append(" LIMIT ? OFFSET ?", limit, offset)
+ .on(getSession()).foreach(getFetcher(), callback);
+ }
+
+ default long countBy(Condition condition) throws SQLException {
+ return new QueryBuilder()
+ .append("SELECT COUNT(*) FROM ")
+ .append(getTableName())
+ .append(" WHERE ", condition)
+ .on(getSession()).select(Outcome.FIRST_LONG);
+ }
+
+ default List findBy(Condition condition) throws SQLException {
+ return findBy(condition, getDefaultOrder());
+ }
+
+ default void foreachBy(Processor.Callback callback, Condition condition) throws SQLException {
+ foreachBy(callback, condition, getDefaultOrder());
+ }
+
+ default List findBy(Condition condition, Order order) throws SQLException {
+ return new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(" WHERE ", condition)
+ .append(order)
+ .on(getSession()).list(getFetcher());
+ }
+
+ default void foreachBy(Processor.Callback callback, Condition condition, Order order) throws SQLException {
+ new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(" WHERE ", condition)
+ .append(order)
+ .on(getSession()).foreach(getFetcher(), callback);
+ }
+
+ default List findBy(Condition condition, long limit, long offset) throws SQLException {
+ return findBy(condition, getDefaultOrder(), limit, offset);
+ }
+
+ default void foreachBy(Processor.Callback callback, Condition condition, long limit, long offset) throws SQLException {
+ foreachBy(callback, condition, getDefaultOrder(), limit, offset);
+ }
+
+ default List findBy(Condition condition, Order order, long limit, long offset) throws SQLException {
+ return new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(" WHERE ", condition)
+ .append(" LIMIT ? OFFSET ?", limit, offset)
+ .append(order)
+ .on(getSession()).list(getFetcher());
+ }
+
+ default void foreachBy(Processor.Callback callback, Condition condition, Order order, long limit, long offset) throws SQLException {
+ new QueryBuilder()
+ .append("SELECT * FROM ").append(getTableName())
+ .append(" WHERE ", condition)
+ .append(" LIMIT ? OFFSET ?", limit, offset)
+ .append(order)
+ .on(getSession()).foreach(getFetcher(), callback);
+ }
+
+ boolean insert(T item) throws SQLException;
+
+ boolean update(T item, PK key) throws SQLException;
+
+ default boolean update(T item) throws SQLException {
+ return update(item, item.getPrimaryKey());
+ }
+
+ default boolean delete(PK key) throws SQLException {
+ return new QueryBuilder()
+ .append("DELETE FROM ").append(getTableName())
+ .append(" WHERE ", getPrimaryCondition(key))
+ .on(getSession()).update() > 0;
+ }
+
+ default boolean delete(T item) throws SQLException {
+ return delete(item.getPrimaryKey());
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAOCore.java b/src/main/java/ua/net/uid/utils/db/dao/DAOCore.java
new file mode 100644
index 0000000..90e84b7
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOCore.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.dao;
+
+import ua.net.uid.utils.db.Session;
+
+public abstract class DAOCore implements DAO {
+ private final Session session;
+
+ public DAOCore(Session session) {
+ this.session = session;
+ }
+
+ @Override
+ public Session getSession() {
+ return session;
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/Entity.java b/src/main/java/ua/net/uid/utils/db/dao/Entity.java
new file mode 100644
index 0000000..c1eec12
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/Entity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.dao;
+
+import java.io.Serializable;
+
+public interface Entity {
+ PK getPrimaryKey();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/query/Condition.java b/src/main/java/ua/net/uid/utils/db/query/Condition.java
new file mode 100644
index 0000000..8fec961
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/query/Condition.java
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2020 nightfall.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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 ua.net.uid.utils.db.query;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public abstract class Condition implements QueryPart {
+ public static Condition raw(CharSequence condition, Object... params) {
+ if (condition == null)
+ throw new IllegalArgumentException("condition is null");
+ return new Raw(condition, params);
+ }
+
+ public static Condition not(CharSequence condition, Object... params) {
+ return new Not(raw(condition, params));
+ }
+
+ public static Condition not(Condition condition) {
+ if (condition == null) {
+ return null;
+ } else if (condition instanceof Not) {
+ return ((Not) condition).condition;
+ } else {
+ return new Not(condition);
+ }
+ }
+
+ public static Condition and(Condition left, Condition right) {
+ if (left != null) {
+ return right == null ? left : new And(left, right);
+ } else {
+ return right;
+ }
+ }
+
+ public static Condition and(Condition... conditions) {
+ if (conditions == null || conditions.length == 0) return null;
+ return conditions.length == 1 ? conditions[0] : new And(conditions);
+ }
+
+ public static Condition or(Condition... conditions) {
+ if (conditions == null || conditions.length == 0) return null;
+ return conditions.length == 1 ? conditions[0] : new Or(conditions);
+ }
+
+ public static Condition in(CharSequence left, Collection> items) {
+ if (left == null || left.length() == 0)
+ throw new IllegalArgumentException("left side parameter for 'in' condition is null");
+ if (items == null || items.isEmpty()) return null;
+ return new In(left, items.toArray(new Object[items.size()]));
+ }
+
+ public static Condition in(CharSequence left, Object... items) {
+ if (left == null || left.length() == 0)
+ throw new IllegalArgumentException("left side parameter for 'in' condition is null");
+ if (items == null || items.length == 0) return null;
+ return new In(left, items);
+ }
+
+ public Object[] toParams(Object... post) {
+ ArrayList