diff --git a/pom.xml b/pom.xml
index 6ef6eb5..e57297b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,10 +45,13 @@
test
-
+
+ src/main/resources
+
+ .LICENSE
@@ -57,17 +60,35 @@
-
org.apache.maven.pluginsmaven-compiler-plugin3.8.0
- 1.8
- 1.8
+ 17
+ 17true
+
+
+ default-compile
+
+ -proc:none
+
+ ua/net/uid/utils/db/orm/Processor.java
+
+
+
+
+
+ compile-project
+ compile
+
+ compile
+
+
+
@@ -124,8 +145,8 @@
UTF-8
- 1.8
- 1.8
+ 17
+ 17uid-releases
diff --git a/src/main/java/ua/net/uid/utils/db/annotation/Column.java b/src/main/java/ua/net/uid/utils/db/annotation/Column.java
new file mode 100644
index 0000000..de4ff31
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/annotation/Column.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2022 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.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specifies the mapped column for a persistent property or field.
+ */
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.SOURCE)
+public @interface Column {
+ /**
+ * The name of the column.Defaults to the property or field name.
+ * @return
+ */
+ String name() default "";
+
+ boolean autoincrement() default false;
+
+ boolean nullable() default true;
+}
diff --git a/src/main/java/ua/net/uid/utils/db/annotation/Composite.java b/src/main/java/ua/net/uid/utils/db/annotation/Composite.java
new file mode 100644
index 0000000..a1db483
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/annotation/Composite.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2022 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.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.SOURCE)
+public @interface Composite {
+}
diff --git a/src/main/java/ua/net/uid/utils/db/annotation/Entity.java b/src/main/java/ua/net/uid/utils/db/annotation/Entity.java
new file mode 100644
index 0000000..4f2b3ea
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/annotation/Entity.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2022 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.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specifies that the class is an entity.
+ */
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.TYPE)
+public @interface Entity {
+ /**
+ * The name of the table.Defaults to the entity name.
+ * @return
+ */
+ String name() default "";
+ /**
+ * The schema of the table.Defaults to the default schema for user.
+ * @return
+ */
+ String schema() default "";
+}
diff --git a/src/main/java/ua/net/uid/utils/db/annotation/PrimaryKey.java b/src/main/java/ua/net/uid/utils/db/annotation/PrimaryKey.java
new file mode 100644
index 0000000..5c3b06c
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/annotation/PrimaryKey.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2022 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.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.SOURCE)
+public @interface PrimaryKey {
+}
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
deleted file mode 100644
index d938102..0000000
--- a/src/main/java/ua/net/uid/utils/db/dao/DAO.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * 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
deleted file mode 100644
index e171851..0000000
--- a/src/main/java/ua/net/uid/utils/db/dao/DAOAbstract.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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
deleted file mode 100644
index 9ec7f02..0000000
--- a/src/main/java/ua/net/uid/utils/db/dao/DAOBase.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * 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
deleted file mode 100644
index 90e84b7..0000000
--- a/src/main/java/ua/net/uid/utils/db/dao/DAOCore.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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/DAOInterface.java b/src/main/java/ua/net/uid/utils/db/dao/DAOInterface.java
new file mode 100644
index 0000000..4322ee3
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOInterface.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2022 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 DAOInterface extends DAOReaderInterface, DAOWriterInterface
+{
+
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAOReaderInterface.java b/src/main/java/ua/net/uid/utils/db/dao/DAOReaderInterface.java
new file mode 100644
index 0000000..86a0359
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOReaderInterface.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2022 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 DAOReaderInterface extends SessionObjectInterface {
+
+
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/DAOWriterInterface.java b/src/main/java/ua/net/uid/utils/db/dao/DAOWriterInterface.java
new file mode 100644
index 0000000..891df30
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/DAOWriterInterface.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2022 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;
+import java.sql.SQLException;
+
+public interface DAOWriterInterface extends SessionObjectInterface {
+ boolean insert(T item) throws SQLException;
+ boolean update(T item, PK key) throws SQLException;
+ boolean update(T item) throws SQLException;
+ boolean delete(PK key) throws SQLException;
+ boolean delete(T item) throws SQLException;
+}
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
deleted file mode 100644
index c1eec12..0000000
--- a/src/main/java/ua/net/uid/utils/db/dao/Entity.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * 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/dao/EntityInterface.java b/src/main/java/ua/net/uid/utils/db/dao/EntityInterface.java
new file mode 100644
index 0000000..8596660
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/EntityInterface.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2022 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 EntityInterface {
+ public PK primaryKey();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/SessionObject.java b/src/main/java/ua/net/uid/utils/db/dao/SessionObject.java
new file mode 100644
index 0000000..df1dc16
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/SessionObject.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2022 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 class SessionObject implements SessionObjectInterface {
+ private final Session session;
+
+ public SessionObject(Session session) {
+ this.session = session;
+ }
+
+ @Override
+ public Session getSession() {
+ return session;
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/dao/SessionObjectInterface.java b/src/main/java/ua/net/uid/utils/db/dao/SessionObjectInterface.java
new file mode 100644
index 0000000..f12e800
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/dao/SessionObjectInterface.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2022 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 SessionObjectInterface {
+ Session getSession();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Column.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Column.java
new file mode 100644
index 0000000..867d68b
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Column.java
@@ -0,0 +1,34 @@
+/*
+ * 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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specifies the mapped column for a persistent property or field.
+ */
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.FIELD)
+public @interface Column {
+ /**
+ * The name of the column.
+ */
+ String name() default "";
+ //boolean auto() default false;
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Columns.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Columns.java
new file mode 100644
index 0000000..a98c188
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Columns.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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.FIELD)
+public @interface Columns {
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Entity.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Entity.java
new file mode 100644
index 0000000..16788c5
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Entity.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2022 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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.FIELD)
+public @interface Entity {
+ String name() default "";
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/PrimaryKey.java b/src/main/java/ua/net/uid/utils/db/old/annotation/PrimaryKey.java
new file mode 100644
index 0000000..1f49aea
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/PrimaryKey.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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.FIELD)
+public @interface PrimaryKey {
+}
\ No newline at end of file
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Select.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Select.java
new file mode 100644
index 0000000..cf3750c
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Select.java
@@ -0,0 +1,29 @@
+/*
+ * 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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import ua.net.uid.utils.db.Fetcher;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.METHOD)
+public @interface Select {
+ String sql();
+ Class extends Fetcher> fetcher() default Fetcher.class;
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Table.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Table.java
new file mode 100644
index 0000000..4adfa26
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Table.java
@@ -0,0 +1,27 @@
+/*
+ * 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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.TYPE)
+public @interface Table {
+ String value();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/annotation/Update.java b/src/main/java/ua/net/uid/utils/db/old/annotation/Update.java
new file mode 100644
index 0000000..024f6d0
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/annotation/Update.java
@@ -0,0 +1,28 @@
+/*
+ * 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.old.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.SOURCE)
+@Target(ElementType.METHOD)
+public @interface Update {
+ String sql();
+ boolean returnAuto() default false;
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/dao/DAO.java b/src/main/java/ua/net/uid/utils/db/old/dao/DAO.java
new file mode 100644
index 0000000..317c69f
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.dao;
+
+import ua.net.uid.utils.db.Session;
+
+public interface DAO {
+ Session getSession();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/dao/DAOAbstract.java b/src/main/java/ua/net/uid/utils/db/old/dao/DAOAbstract.java
new file mode 100644
index 0000000..7814f71
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.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/old/dao/DAOBase.java b/src/main/java/ua/net/uid/utils/db/old/dao/DAOBase.java
new file mode 100644
index 0000000..a06b53c
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.dao;
+
+import ua.net.uid.utils.db.Fetcher;
+import ua.net.uid.utils.db.Outcome;
+import ua.net.uid.utils.db.old.query.Condition;
+import ua.net.uid.utils.db.old.query.Order;
+import ua.net.uid.utils.db.old.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/old/dao/DAOCore.java b/src/main/java/ua/net/uid/utils/db/old/dao/DAOCore.java
new file mode 100644
index 0000000..610f95c
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.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/old/dao/Entity.java b/src/main/java/ua/net/uid/utils/db/old/dao/Entity.java
new file mode 100644
index 0000000..517a0cd
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.dao;
+
+import java.io.Serializable;
+
+public interface Entity {
+ PK getPrimaryKey();
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/orm/EntityColumn.java b/src/main/java/ua/net/uid/utils/db/old/orm/EntityColumn.java
new file mode 100644
index 0000000..d52d151
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/orm/EntityColumn.java
@@ -0,0 +1,132 @@
+/*
+ * 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.old.orm;
+
+import ua.net.uid.utils.db.orm.Utils;
+import java.io.IOException;
+import java.util.Date;
+import java.util.TimeZone;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.ArrayType;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import ua.net.uid.utils.db.old.annotation.Column;
+import ua.net.uid.utils.db.old.annotation.PrimaryKey;
+
+public class EntityColumn extends EntityField {
+
+ private final String name;
+ private final boolean primary;
+
+ public EntityColumn(VariableElement variableElement, EntityField parentField) {
+ super(variableElement, parentField);
+ Column column = variableElement.getAnnotation(Column.class);
+ name = "".equals(column.name()) ? variableElement.getSimpleName().toString() : column.name();
+ primary = variableElement.getAnnotation(PrimaryKey.class) != null;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean hasPrimary() {
+ return primary;
+ }
+
+ @Override
+ public boolean isPrimaryKey() {
+ return primary;
+ }
+
+ @Override
+ public EntityField getPrimaryKey() {
+ return primary ? this : null;
+ }
+
+ @Override
+ public void writeFetch(Appendable builder, String item) throws IOException {
+ builder.append(" ").append(item).append('.');
+ final boolean dirrect = !writeSetter(builder);
+ builder.append(dirrect ? " = " : "(");
+ final TypeMirror typeMirror = getVariableElement().asType();
+ switch (typeMirror.getKind()) {
+ case ARRAY:
+ final TypeMirror componentType = ((ArrayType) typeMirror).getComponentType();
+ if (componentType.getKind() == TypeKind.BYTE) {
+ builder.append("result.getBytes(\"").append(name).append("\")");
+ } else {
+ builder.append("result.getArray(\"").append(name).append("\").getArray()");
+ }
+ break;
+ case DECLARED:
+ final TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
+ if (Utils.isAssignableFrom(typeElement, TimeZone.class)) {
+ builder.append("java.util.TimeZone.getTimeZone(result.getString(\"").append(name).append("\"))");
+ } else if (Utils.isAssignableFrom(typeElement, Date.class)) {
+ builder.append("result.getTimestamp(\"").append(name).append("\")");
+ } else if (Utils.isAssignableFrom(typeElement, String.class)) {
+ builder.append("result.getString(\"").append(name).append("\")");
+ } else if (typeElement.getKind() == ElementKind.ENUM) {
+ builder.append(typeElement.getQualifiedName()).append(".valueOf(result.getString(\"").append(name).append("\"))");
+ } else {
+ builder.append('(').append(typeElement.getQualifiedName()).append(")result.getObject(\"").append(name).append("\")");
+ }
+ break;
+ default:
+ switch (typeMirror.getKind()) {
+ case BOOLEAN:
+ builder.append("result.getBoolean(\"").append(name).append("\")");
+ break;
+ case BYTE:
+ builder.append("result.getByte(\"").append(name).append("\")");
+ break;
+ case CHAR:
+ builder.append("result.getString(\"").append(name).append("\").charAt(0)");
+ break;
+ case SHORT:
+ builder.append("result.getShort(\"").append(name).append("\")");
+ break;
+ case INT:
+ builder.append("result.getInt(\"").append(name).append("\")");
+ break;
+ case LONG:
+ builder.append("result.getLong(\"").append(name).append("\")");
+ break;
+ case FLOAT:
+ builder.append("result.getFloat(\"").append(name).append("\")");
+ break;
+ case DOUBLE:
+ builder.append("result.getDouble(\"").append(name).append("\")");
+ break;
+ default:
+ builder.append("result.getObject(\"").append(name).append("\")");
+ break;
+ }
+ break;
+ }
+ builder.append(dirrect ? ";\n" : ");\n");
+ }
+
+ @Override
+ public void writeColums(Appendable builder, String postfix, String div) throws IOException {
+ builder.append('"').append(name).append('"').append(postfix);
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/orm/EntityField.java b/src/main/java/ua/net/uid/utils/db/old/orm/EntityField.java
new file mode 100644
index 0000000..4380176
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/orm/EntityField.java
@@ -0,0 +1,59 @@
+/*
+ * 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.old.orm;
+
+
+import ua.net.uid.utils.db.orm.Utils;
+import java.io.IOException;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+
+public abstract class EntityField {
+ private final EntityField parentField;
+ private final VariableElement variableElement;
+
+ public EntityField(VariableElement variableElement, EntityField parentField) {
+ this.parentField = parentField;
+ this.variableElement = variableElement;
+ }
+ public EntityField getParent() { return parentField; }
+ public VariableElement getVariableElement() { return variableElement; }
+ public String getName() { return variableElement.toString(); }
+
+ public abstract boolean hasPrimary();
+ public abstract boolean isPrimaryKey();
+ public abstract EntityField getPrimaryKey();
+
+ public abstract void writeFetch(Appendable builder, String item) throws IOException;
+
+ public boolean writeSetter(Appendable builder) throws IOException {
+ final TypeElement typeElement = (TypeElement) variableElement.getEnclosingElement();
+ final ExecutableElement setterElement = Utils.findSetter(getName(), typeElement);
+ if (setterElement != null) {
+ builder.append(setterElement.getSimpleName().toString());
+ return true;
+ } else if (!variableElement.getModifiers().contains(Modifier.PRIVATE)) {
+ builder.append(variableElement.toString());
+ return false;
+ } else {
+ throw new IOException("can`t find setter for " + variableElement.toString());
+ }
+ }
+
+ public abstract void writeColums(Appendable builder, String postfix, String div) throws IOException;
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/orm/EntityMeta.java b/src/main/java/ua/net/uid/utils/db/old/orm/EntityMeta.java
new file mode 100644
index 0000000..8df9c80
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/orm/EntityMeta.java
@@ -0,0 +1,139 @@
+/*
+ * 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.old.orm;
+
+import ua.net.uid.utils.db.orm.Utils;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.processing.Filer;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.tools.JavaFileObject;
+import ua.net.uid.utils.db.old.annotation.Column;
+import ua.net.uid.utils.db.old.annotation.Columns;
+import ua.net.uid.utils.db.old.annotation.PrimaryKey;
+import ua.net.uid.utils.db.old.annotation.Table;
+
+public class EntityMeta {
+ private final Processor processor;
+ private final TypeElement typeElement;
+ private final PackageElement packageElement;
+ private final String tableName;
+ private final Map columns = new LinkedHashMap<>();
+ private final List fields = new ArrayList<>();
+ private final EntityField primaryField;
+ //private final EntityColumn auto;
+ private final String fetcherSimpleName;
+ private final String fetcherQualifiedName;
+ private final boolean fetcherFromResulSet;
+
+ public EntityMeta(Processor processor, TypeElement typeElement) {
+ this.processor = processor;
+ this.typeElement = typeElement;
+ packageElement = Utils.getPackageElement(typeElement);
+
+ final Table table = typeElement.getAnnotation(Table.class);
+ tableName = table == null ? typeElement.getSimpleName().toString() : table.value();
+
+ boolean simple = false;
+ EntityField primary = null;
+ for (Element element : typeElement.getEnclosedElements()) {
+ if (element.getKind() == ElementKind.CONSTRUCTOR) {
+ final ExecutableElement executable = (ExecutableElement) element;
+ if (executable.getParameters().size() == 1 && Utils.RESULTSET_CLASS.equals(executable.getParameters().get(0).asType().toString())) {
+ simple = true;
+ }
+ } else if (element.getKind() == ElementKind.FIELD) {
+ final VariableElement variable = (VariableElement) element;
+ EntityField field = null;
+ if (variable.getAnnotation(Column.class) != null) {
+ field = new EntityColumn(variable, null);
+ columns.put(field.getName(), (EntityColumn) field);
+ } else if (variable.getAnnotation(PrimaryKey.class) != null || variable.getAnnotation(Columns.class) != null) {
+ field = new EntityObject(variable, columns, null);
+ }
+ if (field != null) {
+ fields.add(field);
+ if (field.hasPrimary()) primary = field.getPrimaryKey();
+ //if (_auto == null && field.isAuto()) _auto = field.getAuto();
+ }
+ }
+ }
+ primaryField = primary;
+ fetcherSimpleName = typeElement.getQualifiedName().toString()
+ .substring(packageElement.getQualifiedName().length() + 1)
+ .replace('.', '_') + Utils.FETCHER_SUFFIX;
+ fetcherQualifiedName = packageElement.getQualifiedName() + "." + fetcherSimpleName;
+ System.err.println("\t" + fetcherQualifiedName);
+ fetcherFromResulSet = simple;
+ }
+
+ public Processor getProcessor() { return processor; }
+ public TypeElement getTypeElement() { return typeElement; }
+ public PackageElement getPackageElement() { return packageElement; }
+ public String getTableName() { return tableName; }
+ public String getFetcherSimpleName() { return fetcherSimpleName; }
+ public String getFetcherQualifiedName() { return fetcherQualifiedName; }
+ public EntityField getPrimaryKey() { return primaryField; }
+
+ public void generateFetcher(Filer filer) throws IOException {
+ JavaFileObject fileObject = filer.createSourceFile(fetcherQualifiedName, packageElement, typeElement);
+ try (final Writer writer = fileObject.openWriter()) {
+ final BufferedWriter builder = new BufferedWriter(writer);
+ writeFetcher(builder);
+ builder.flush();
+ }
+ }
+
+ private void writeFetcher(Appendable builder) throws IOException {
+ String simpleName = typeElement.getSimpleName().toString();
+ builder.append("package ").append(packageElement.getQualifiedName())
+ .append(";\npublic class ").append(fetcherSimpleName)
+ .append(" implements ").append(Utils.FETCHER_CLASS)
+ .append('<').append(simpleName).append("> {\n @Override\n public ")
+ .append(simpleName).append(" fetch(final ").append(Utils.RESULTSET_CLASS)
+ .append(" result) throws ").append(Utils.SQLEXCEPTION_CLASS).append(" {\n");
+ if (fetcherFromResulSet) {
+ builder.append(" return new ").append(simpleName).append("(result);\n");
+ } else {
+ builder.append(" ").append(simpleName).append(" item = new ").append(simpleName).append("();\n");
+ for (EntityField field : fields) {
+ field.writeFetch(builder, "item");
+ }
+ builder.append(" return item;\n");
+ }
+ builder.append(" }\n public static ").append(fetcherSimpleName)
+ .append(" getInstance() { return Holder.INSTANCE; }\n private ")
+ .append(fetcherSimpleName)
+ .append("() {}\n private static class Holder {\n private static final ")
+ .append(fetcherSimpleName)
+ .append(" INSTANCE = new ").append(fetcherSimpleName).append("();\n }\n}\n");
+ }
+
+ /*public void writeDelete(Appendable builder, String param) throws IOException {
+ builder.append("\"DELETE FROM \\\"").append(tableName).
+ }*/
+
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/orm/EntityObject.java b/src/main/java/ua/net/uid/utils/db/old/orm/EntityObject.java
new file mode 100644
index 0000000..803e820
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/orm/EntityObject.java
@@ -0,0 +1,97 @@
+/*
+ * 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.old.orm;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.DeclaredType;
+import ua.net.uid.utils.db.old.annotation.Column;
+import ua.net.uid.utils.db.old.annotation.Columns;
+import ua.net.uid.utils.db.old.annotation.PrimaryKey;
+
+public class EntityObject extends EntityField {
+ private final TypeElement typeElement;
+ private final List fields = new ArrayList<>();
+ private final boolean isPrimary;
+ private final EntityField primaryField;
+
+ public EntityObject(VariableElement variableElement, Map columns, EntityField parentField) {
+ super(variableElement, parentField);
+ typeElement = (TypeElement) ((DeclaredType) variableElement.asType()).asElement();
+ isPrimary = variableElement.getAnnotation(PrimaryKey.class) != null;
+ EntityField primary = null;
+ for (Element element : typeElement.getEnclosedElements()) {
+ if (element.getKind() == ElementKind.FIELD/* || element.getKind() == ElementKind.ENUM_CONSTANT*/) {
+ final VariableElement variable = (VariableElement) element;
+ EntityField field = null;
+ if (variable.getAnnotation(Column.class) != null) {
+ field = new EntityColumn(variable, null);
+ columns.put(field.getName(), (EntityColumn) field);
+ } else if (variable.getAnnotation(PrimaryKey.class) != null || variable.getAnnotation(Columns.class) != null) {
+ field = new EntityObject(variable, columns, null);
+ }
+ if (field != null) {
+ fields.add(field);
+ if (field.hasPrimary()) primary = field.getPrimaryKey();
+ //if (_auto == null && field.isAuto()) _auto = field.getAuto();
+ }
+ }
+ }
+
+ primaryField = isPrimary ? this : primary;
+ }
+
+ @Override
+ public boolean hasPrimary() { return primaryField != null; }
+
+ @Override
+ public boolean isPrimaryKey() { return isPrimary; }
+
+ @Override
+ public EntityField getPrimaryKey() { return primaryField; }
+
+ @Override
+ public void writeFetch(Appendable builder, String item) throws IOException {
+ final String varName = item + '_' + getVariableElement().getSimpleName();
+ builder.append(" final ")
+ .append(typeElement.getQualifiedName()).append(' ').append(varName)
+ .append(" = new ").append(typeElement.getQualifiedName()).append("();\n");
+ for (EntityField field : fields) {
+ field.writeFetch(builder, varName);
+ }
+ builder.append(" ").append(item).append('.');
+ if (writeSetter(builder)) {
+ builder.append('(').append(varName).append(");\n");
+ } else {
+ builder.append(" = ").append(varName).append(";\n");
+ }
+ }
+
+ @Override
+ public void writeColums(Appendable builder, String postfix, String div) throws IOException {
+ boolean comma = false;
+ for(EntityField field : fields) {
+ if (comma) builder.append(div); else comma = true;
+ field.writeColums(builder, postfix, div);
+ }
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/orm/Processor.java b/src/main/java/ua/net/uid/utils/db/old/orm/Processor.java
new file mode 100644
index 0000000..02e02df
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/orm/Processor.java
@@ -0,0 +1,89 @@
+/*
+ * 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.old.orm;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic;
+import ua.net.uid.utils.db.old.annotation.Table;
+
+@SupportedSourceVersion(SourceVersion.RELEASE_14)
+@SupportedAnnotationTypes({
+ "ua.net.uid.utils.db.annotation.Table",
+ "ua.net.uid.utils.db.annotation.Column",
+ "ua.net.uid.utils.db.annotation.Columns",
+
+/*
+ "ua.net.uid.utils.orm.annotations.Table",
+ "ua.net.uid.utils.orm.annotations.Select",
+ "ua.net.uid.utils.orm.annotations.Update",
+ */
+})
+public class Processor extends AbstractProcessor {
+
+ private final Map entityMetas = new HashMap<>();
+
+ @Override
+ public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
+ if (annotations.isEmpty()) {
+ return false;
+ }
+
+ message(Diagnostic.Kind.ERROR, "!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+
+ roundEnvironment
+ .getElementsAnnotatedWith(Table.class)
+ .stream()
+ .filter(element -> element instanceof TypeElement)
+ .forEachOrdered(element -> getEntityMeta((TypeElement) element));
+ //createDAOMeta(Select.class, roundEnv);
+ //createDAOMeta(Update.class, roundEnv);
+ /*for (DAOMeta generator : generators.values()) {
+ try {
+ generator.generate(processingEnv.getFiler());
+ } catch (Exception ex) {
+ message(Diagnostic.Kind.ERROR, generator.getTypeElement().getQualifiedName()+" : "+ex.getMessage());
+ }
+ }*/
+ return true;
+ }
+
+ public EntityMeta getEntityMeta(TypeElement element) {
+ EntityMeta result = entityMetas.get(element);
+ message(Diagnostic.Kind.NOTE, element.getQualifiedName());
+ if (result == null) {
+ try {
+ result = new EntityMeta(this, (TypeElement) element);
+ result.generateFetcher(processingEnv.getFiler());
+ entityMetas.put((TypeElement) element, result);
+ } catch (Exception ex) {
+ message(Diagnostic.Kind.ERROR, ((TypeElement) element).getQualifiedName() + " : " + ex.getMessage());
+ }
+ }
+ return result;
+ }
+
+ public void message(Diagnostic.Kind kind, CharSequence message) {
+ processingEnv.getMessager().printMessage(kind, message);
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/db/old/query/Condition.java b/src/main/java/ua/net/uid/utils/db/old/query/Condition.java
new file mode 100644
index 0000000..e183359
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/db/old/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.old.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