├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── dev │ └── krynn │ └── sql │ ├── KrynnSQL.java │ ├── annotations │ ├── Column.java │ ├── MegaAnnotationName.java │ ├── PrimaryKey.java │ └── Table.java │ ├── compiler │ ├── CompiledTemplate.java │ ├── Compiler.java │ ├── data │ │ └── DataCompiler.java │ └── field │ │ └── CompiledField.java │ ├── connection │ └── DatabaseConnection.java │ ├── database │ ├── Database.java │ └── DatabaseFactory.java │ ├── impl │ ├── compiler │ │ ├── CompiledTemplateImpl.java │ │ ├── CompilerImpl.java │ │ ├── data │ │ │ ├── DataCompilers.java │ │ │ └── DataType.java │ │ ├── exception │ │ │ └── CompilerException.java │ │ └── field │ │ │ └── CompiledFieldImpl.java │ ├── connection │ │ └── DatabaseConnectionImpl.java │ ├── database │ │ ├── DatabaseFactoryImpl.java │ │ └── DatabaseImpl.java │ └── table │ │ ├── TableFactoryImpl.java │ │ ├── TableImpl.java │ │ └── cache │ │ └── CacheableTableImpl.java │ ├── query │ ├── Query.java │ └── QueryBuilder.java │ ├── table │ ├── Table.java │ ├── TableFactory.java │ └── cache │ │ └── CacheableTable.java │ └── util │ └── QueryUtil.java └── test └── java └── dev └── krynn └── sql ├── AbstractResultSet.java ├── KrynnTest.java ├── SecondUser.java ├── ThirdUser.java └── User.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | .idea/ 13 | 14 | # Package Files # 15 | *.jar 16 | *.war 17 | *.nar 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: true 3 | 4 | jdk: 5 | - oraclejdk8 6 | - openjdk11 7 | 8 | install: 9 | - mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V 10 | 11 | script: 12 | - mvn test 13 | 14 | notifications: 15 | email: false -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Krynn-SQL [![][travis img]][travis] [![][license img]][license] [![][discord img]][discord] [![CodeFactor](https://www.codefactor.io/repository/github/oskarr1239/krynn-sql/badge)](https://www.codefactor.io/repository/github/oskarr1239/krynn-sql) 2 | 3 | ## About 4 | Simple java framework like ORM to build sql requests, based on annotations. 5 | 6 | ## TODO 7 | - Create documentation friendly for new developers. 8 | - Implement cache system. 9 | 10 | For the full list check [Projects](https://github.com/Oskarr1239/krynn-sql/projects/3). 11 | 12 | ## Example 13 | ```java 14 | @Table("users") 15 | public class User { 16 | 17 | @Column 18 | @PrimaryKey 19 | private UUID uuid; 20 | 21 | @Column 22 | private String name; 23 | 24 | public User(UUID uuid, String name) { 25 | this.uuid = uuid; 26 | this.name = name; 27 | } 28 | } 29 | ``` 30 | 31 | ```java 32 | KrynnSQL krynnSQL = new KrynnSQL(yourHikariConfig); 33 | 34 | Database database = krynnSQL.getDatabase("krynn"); 35 | Table table = database.table(User.class); 36 | 37 | User user = new User(UUID.randomUUID(), "testuser"); 38 | 39 | //Insert / Update user 40 | table.update(user); 41 | 42 | //Query users 43 | List users = table.query("SELECT * FROM {table}"); 44 | 45 | 46 | ``` 47 | 48 | ## License 49 | The Krynn-SQL is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0). 50 | 51 | ## Ideas and bugs 52 | If you have any issues or suggestions, please submit them [here](https://github.com/Oskarr1239/krynn-sql/issues). 53 | 54 | [travis]: https://travis-ci.org/Oskarr1239/krynn-sql 55 | [travis img]: https://travis-ci.org/Oskarr1239/krynn-sql.svg?branch=master 56 | 57 | [license]:https://opensource.org/licenses/Apache-2.0 58 | [license img]:https://img.shields.io/badge/License-Apache%202.0-blue.svg 59 | 60 | [discord]: https://discord.gg/RkyqMdF 61 | [discord img]: https://img.shields.io/discord/563074773110882304.svg -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | Krynn SQL 8 | dev.krynn 9 | sql 10 | 0.1.0 11 | Simple framework like ORM to build sql requests, based on annotations 12 | 13 | 14 | 8 15 | UTF-8 16 | 17 | 3.8.0 18 | 19 | 0.9.11 20 | 5.5.0-M1 21 | 3.3.1 22 | 8.0.16 23 | 2.4.0 24 | 25 | 26 | 27 | 28 | 29 | org.apache.maven.plugins 30 | maven-surefire-plugin 31 | 2.22.1 32 | 33 | false 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | ${maven.compiler.version} 40 | 41 | ${krynn.java.version} 42 | ${krynn.java.version} 43 | ${krynn.encoding} 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.reflections 52 | reflections 53 | ${reflections.version} 54 | 55 | 56 | org.junit.jupiter 57 | junit-jupiter-engine 58 | ${junit.version} 59 | test 60 | 61 | 62 | com.zaxxer 63 | HikariCP 64 | ${hikaricp.version} 65 | 66 | 67 | mysql 68 | mysql-connector-java 69 | ${mysql.version} 70 | 71 | 72 | ch.vorburger.mariaDB4j 73 | mariaDB4j 74 | ${mariadb4j.version} 75 | test 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/KrynnSQL.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import com.zaxxer.hikari.HikariConfig; 20 | import dev.krynn.sql.compiler.Compiler; 21 | import dev.krynn.sql.compiler.data.DataCompiler; 22 | import dev.krynn.sql.connection.DatabaseConnection; 23 | import dev.krynn.sql.database.Database; 24 | import dev.krynn.sql.database.DatabaseFactory; 25 | import dev.krynn.sql.impl.compiler.CompilerImpl; 26 | import dev.krynn.sql.impl.connection.DatabaseConnectionImpl; 27 | import dev.krynn.sql.impl.database.DatabaseFactoryImpl; 28 | import dev.krynn.sql.impl.table.TableFactoryImpl; 29 | import dev.krynn.sql.table.TableFactory; 30 | 31 | import java.lang.reflect.Type; 32 | import java.sql.Connection; 33 | import java.sql.SQLException; 34 | 35 | public class KrynnSQL { 36 | 37 | private static final Compiler COMPILER = new CompilerImpl(); 38 | private DatabaseConnection connection = new DatabaseConnectionImpl(); 39 | private DatabaseFactory databaseFactory; 40 | private TableFactory tableFactory; 41 | 42 | public KrynnSQL(HikariConfig config) { 43 | connection.config(config); 44 | databaseFactory = new DatabaseFactoryImpl(this, connection); 45 | tableFactory = new TableFactoryImpl(this, connection); 46 | } 47 | 48 | public Database getDatabase(String name) { 49 | return databaseFactory.getOrCreate(name); 50 | } 51 | 52 | public void registerDataCompiler(DataCompiler dataCompiler, Type... primitives) { 53 | COMPILER.registerDataCompiler(dataCompiler, primitives); 54 | } 55 | 56 | public Connection getConnection() throws SQLException { 57 | return connection.connection(); 58 | } 59 | 60 | public static Compiler getCompiler() { 61 | return COMPILER; 62 | } 63 | 64 | public TableFactory getTableFactory() { 65 | return tableFactory; 66 | } 67 | 68 | public DatabaseFactory getDatabaseFactory() { 69 | return databaseFactory; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/annotations/Column.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.annotations; 18 | 19 | import dev.krynn.sql.compiler.data.DataCompiler; 20 | import dev.krynn.sql.impl.compiler.data.DataCompilers; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | public @interface Column { 30 | 31 | String sqlType() default ""; 32 | 33 | /** 34 | * Numeric compilerType of #sqlType 35 | * @see java.sql.Types 36 | */ 37 | int numericType() default Integer.MAX_VALUE; 38 | 39 | Class dataCompiler() default DataCompilers.ObjectCompiler.class; 40 | 41 | String name() default ""; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/annotations/MegaAnnotationName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.annotations; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.FIELD) 26 | public @interface MegaAnnotationName { 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/annotations/PrimaryKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.annotations; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.FIELD) 26 | public @interface PrimaryKey { 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/annotations/Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.annotations; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.TYPE) 26 | public @interface Table { 27 | 28 | String value(); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/compiler/CompiledTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.compiler; 18 | 19 | import dev.krynn.sql.compiler.field.CompiledField; 20 | 21 | import java.util.Map; 22 | 23 | public interface CompiledTemplate { 24 | 25 | Map compiledFields(); 26 | 27 | CompiledField primaryKey(); 28 | 29 | String table(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/compiler/Compiler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.compiler; 18 | 19 | import dev.krynn.sql.compiler.data.DataCompiler; 20 | 21 | import java.lang.reflect.Type; 22 | import java.sql.ResultSet; 23 | 24 | public interface Compiler { 25 | 26 | void compile(Class clazz); 27 | 28 | C build(Class clazz, ResultSet resultSet); 29 | 30 | void registerDataCompiler(DataCompiler dataCompiler, Type... primitives); 31 | 32 | CompiledTemplate findOrCreate(Type type); 33 | 34 | CompiledTemplate findTemplate(Type type); 35 | 36 | DataCompiler findDataCompiler(Type type); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/compiler/data/DataCompiler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.compiler.data; 18 | 19 | import java.lang.reflect.ParameterizedType; 20 | import java.lang.reflect.Type; 21 | 22 | public interface DataCompiler { 23 | 24 | T compile(I original); 25 | 26 | I decompile(T toDecompile); 27 | 28 | default Type compilerType() { 29 | return ((ParameterizedType) getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1]; 30 | } 31 | 32 | default Type decompilerType() { 33 | return ((ParameterizedType) getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/compiler/field/CompiledField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.compiler.field; 18 | 19 | import dev.krynn.sql.compiler.data.DataCompiler; 20 | 21 | import java.lang.reflect.Field; 22 | 23 | public interface CompiledField { 24 | 25 | Field field(); 26 | 27 | String sqlType(); 28 | 29 | int numericType(); 30 | 31 | String name(); 32 | 33 | DataCompiler dataCompiler(); 34 | 35 | boolean isPrimary(); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/connection/DatabaseConnection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.connection; 18 | 19 | import com.zaxxer.hikari.HikariConfig; 20 | 21 | import java.sql.Connection; 22 | import java.sql.SQLException; 23 | 24 | public interface DatabaseConnection { 25 | 26 | Connection connection() throws SQLException; 27 | 28 | void close(); 29 | 30 | void config(HikariConfig config); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/database/Database.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.database; 18 | 19 | import dev.krynn.sql.table.Table; 20 | 21 | public interface Database { 22 | 23 | Table table(Class type); 24 | 25 | String name(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/database/DatabaseFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.database; 18 | 19 | public interface DatabaseFactory { 20 | 21 | Database get(String name); 22 | 23 | Database getOrCreate(String name); 24 | 25 | void create(String name); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/CompiledTemplateImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler; 18 | 19 | import dev.krynn.sql.compiler.CompiledTemplate; 20 | import dev.krynn.sql.compiler.field.CompiledField; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | 25 | public class CompiledTemplateImpl implements CompiledTemplate { 26 | 27 | private Map fieldMap; 28 | private CompiledField primaryKey; 29 | private String table; 30 | 31 | public CompiledTemplateImpl(Map fieldMap, CompiledField primaryKey, String table) { 32 | this.fieldMap = fieldMap; 33 | this.primaryKey = primaryKey; 34 | this.table = table; 35 | } 36 | 37 | private CompiledTemplateImpl(Builder builder) { 38 | fieldMap = builder.fieldMap; 39 | primaryKey = builder.primaryKey; 40 | table = builder.table; 41 | } 42 | 43 | public static Builder newBuilder() { 44 | return new Builder(); 45 | } 46 | 47 | @Override 48 | public Map compiledFields() { 49 | return this.fieldMap; 50 | } 51 | 52 | @Override 53 | public CompiledField primaryKey() { 54 | return this.primaryKey; 55 | } 56 | 57 | @Override 58 | public String table() { 59 | return this.table; 60 | } 61 | 62 | public static final class Builder { 63 | private Map fieldMap; 64 | private CompiledField primaryKey; 65 | private String table; 66 | 67 | private Builder() { 68 | } 69 | 70 | public Builder fieldMap(Map val) { 71 | fieldMap = val; 72 | return this; 73 | } 74 | 75 | public Builder primaryKey(CompiledField val) { 76 | primaryKey = val; 77 | return this; 78 | } 79 | 80 | public Builder table(String val) { 81 | table = val; 82 | return this; 83 | } 84 | 85 | public CompiledTemplateImpl build() { 86 | return new CompiledTemplateImpl(this); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/CompilerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler; 18 | 19 | import com.google.common.reflect.TypeToken; 20 | import dev.krynn.sql.annotations.Column; 21 | import dev.krynn.sql.annotations.PrimaryKey; 22 | import dev.krynn.sql.annotations.Table; 23 | import dev.krynn.sql.compiler.CompiledTemplate; 24 | import dev.krynn.sql.compiler.Compiler; 25 | import dev.krynn.sql.compiler.data.DataCompiler; 26 | import dev.krynn.sql.compiler.field.CompiledField; 27 | import dev.krynn.sql.impl.compiler.data.DataType; 28 | import dev.krynn.sql.impl.compiler.exception.CompilerException; 29 | import dev.krynn.sql.impl.compiler.field.CompiledFieldImpl; 30 | import org.reflections.ReflectionUtils; 31 | 32 | import java.lang.annotation.Annotation; 33 | import java.lang.reflect.Field; 34 | import java.lang.reflect.Type; 35 | import java.sql.ResultSet; 36 | import java.sql.SQLException; 37 | import java.util.*; 38 | 39 | import static dev.krynn.sql.impl.compiler.data.DataCompilers.*; 40 | 41 | public class CompilerImpl implements Compiler { 42 | 43 | private Map templateMap = new HashMap<>(); 44 | 45 | private Map> dataCompilerMap = new HashMap<>(); 46 | 47 | { 48 | registerDataCompiler(OBJECT_COMPILER); 49 | registerDataCompiler(INT_COMPILER, int.class); 50 | registerDataCompiler(STRING_COMPILER); 51 | registerDataCompiler(LONG_COMPILER, long.class); 52 | registerDataCompiler(BOOLEAN_COMPILER, boolean.class); 53 | registerDataCompiler(UUID_COMPILER); 54 | registerDataCompiler(SHORT_COMPILER, short.class); 55 | registerDataCompiler(FLOAT_COMPILER, float.class); 56 | registerDataCompiler(DOUBLE_COMPILER, double.class); 57 | } 58 | 59 | @Override 60 | @SuppressWarnings("unchecked") 61 | public void compile(Class clazz) { 62 | if(!clazz.isAnnotationPresent(Table.class)) { 63 | throw new CompilerException(); 64 | } 65 | Map compiledFieldMap = new HashMap<>(); 66 | 67 | ReflectionUtils.getFields(clazz, input -> Objects.requireNonNull(input).isAnnotationPresent(Column.class)).forEach(field -> { 68 | Column annotation = field.getAnnotation(Column.class); 69 | String name = annotation.name(); 70 | DataType dataType = DataType.getType(field.getType()); 71 | String sqlType = dataType.getSqlType(); 72 | int numericType = dataType.getNumericType(); 73 | DataCompiler dataCompiler = findDataCompiler(field.getGenericType()); 74 | boolean primaryKey = false; 75 | 76 | if(annotation.name().isEmpty()) { 77 | name = field.getName(); 78 | } 79 | if(!annotation.sqlType().isEmpty()) { 80 | sqlType = annotation.sqlType(); 81 | } 82 | if(annotation.numericType() != Integer.MAX_VALUE) { 83 | numericType = annotation.numericType(); 84 | } 85 | if(!annotation.dataCompiler().equals(OBJECT_COMPILER.getClass())) { 86 | try { 87 | dataCompiler = annotation.dataCompiler().getDeclaredConstructor().newInstance(); 88 | } catch (Exception e) { 89 | throw new CompilerException(e); 90 | } 91 | } 92 | 93 | if(field.isAnnotationPresent(PrimaryKey.class)) { 94 | primaryKey = true; 95 | } 96 | compiledFieldMap.put(name, CompiledFieldImpl.newBuilder() 97 | .name(name) 98 | .sqlType(sqlType) 99 | .numericType(numericType) 100 | .dataCompiler(dataCompiler) 101 | .field(field) 102 | .primaryKey(primaryKey) 103 | .build()); 104 | }); 105 | 106 | CompiledField primaryKey = compiledFieldMap.entrySet().stream().filter(ftr -> ftr.getValue().isPrimary()).findFirst().get().getValue(); 107 | 108 | CompiledTemplate template = CompiledTemplateImpl.newBuilder() 109 | .fieldMap(compiledFieldMap) 110 | .table(clazz.getAnnotation(Table.class).value()) 111 | .primaryKey(primaryKey).build(); 112 | this.templateMap.put(clazz, template); 113 | 114 | } 115 | 116 | @Override 117 | public C build(Class clazz, ResultSet resultSet) { 118 | if(findTemplate(TypeToken.of(clazz).getType()) == null) { 119 | compile(clazz); 120 | } 121 | 122 | CompiledTemplate template = findTemplate(TypeToken.of(clazz).getType()); 123 | 124 | try { 125 | C t = clazz.getDeclaredConstructor().newInstance(); 126 | 127 | template.compiledFields().forEach((s, compiledField) -> { 128 | try { 129 | Field field = compiledField.field(); 130 | field.setAccessible(true); 131 | field.set(t, tryDecompile(compiledField.dataCompiler(), resultSet.getObject(compiledField.name(), (Class) compiledField.dataCompiler().decompilerType()))); 132 | } catch (IllegalAccessException | SQLException e) { 133 | e.printStackTrace(); 134 | } 135 | }); 136 | return t; 137 | } catch (Exception e) { 138 | throw new CompilerException(e); 139 | } 140 | } 141 | 142 | @Override 143 | public void registerDataCompiler(DataCompiler dataCompiler, Type... primitives) { 144 | this.dataCompilerMap.put(dataCompiler.compilerType(), dataCompiler); 145 | for(Type type : primitives) { 146 | this.dataCompilerMap.put(type, dataCompiler); 147 | } 148 | } 149 | 150 | @Override 151 | public CompiledTemplate findOrCreate(Type type) { 152 | if(findTemplate(type) == null) { 153 | compile(type.getClass()); 154 | } 155 | return findTemplate(type); 156 | } 157 | 158 | @Override 159 | public CompiledTemplate findTemplate(Type type) { 160 | return this.templateMap.get(type); 161 | } 162 | 163 | @Override 164 | public DataCompiler findDataCompiler(Type type) { 165 | return dataCompilerMap.get(type); 166 | } 167 | 168 | @SuppressWarnings("unchecked") 169 | private I tryDecompile(DataCompiler dataCompiler, Object o) { 170 | //idc 171 | return dataCompiler.decompile((T) o); 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/data/DataCompilers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler.data; 18 | 19 | import dev.krynn.sql.compiler.data.DataCompiler; 20 | import dev.krynn.sql.impl.compiler.exception.CompilerException; 21 | 22 | import java.util.UUID; 23 | 24 | public class DataCompilers { 25 | 26 | public static final DataCompiler OBJECT_COMPILER = new ObjectCompiler(); 27 | public static final DataCompiler STRING_COMPILER = new StringCompiler(); 28 | public static final DataCompiler INT_COMPILER = new IntCompiler(); 29 | public static final DataCompiler LONG_COMPILER = new LongCompiler(); 30 | public static final DataCompiler BOOLEAN_COMPILER = new BooleanCompiler(); 31 | public static final DataCompiler UUID_COMPILER = new UUIDCompiler(); 32 | public static final DataCompiler SHORT_COMPILER = new ShortCompiler(); 33 | public static final DataCompiler DOUBLE_COMPILER = new DoubleCompiler(); 34 | public static final DataCompiler FLOAT_COMPILER = new FloatCompiler(); 35 | 36 | public static class ObjectCompiler implements DataCompiler { 37 | @Override 38 | public String compile(Object original) { 39 | throw new CompilerException("type not supported"); 40 | } 41 | 42 | @Override 43 | public Object decompile(String toDecompile) { 44 | throw new CompilerException("type not supported"); 45 | } 46 | } 47 | 48 | public static class StringCompiler implements DataCompiler { 49 | 50 | @Override 51 | public String compile(String original) { 52 | return original; 53 | } 54 | 55 | @Override 56 | public String decompile(String toDecompile) { 57 | return toDecompile; 58 | } 59 | } 60 | 61 | public static class IntCompiler implements DataCompiler { 62 | 63 | @Override 64 | public Integer compile(Integer original) { 65 | return original; 66 | } 67 | 68 | @Override 69 | public Integer decompile(Integer toDecompile) { 70 | return toDecompile; 71 | } 72 | } 73 | 74 | public static class LongCompiler implements DataCompiler { 75 | 76 | @Override 77 | public Long compile(Long original) { 78 | return original; 79 | } 80 | 81 | @Override 82 | public Long decompile(Long toDecompile) { 83 | return toDecompile; 84 | } 85 | } 86 | 87 | public static class BooleanCompiler implements DataCompiler { 88 | //Dont ask 89 | 90 | @Override 91 | public Boolean compile(Boolean original) { 92 | return original; 93 | } 94 | 95 | @Override 96 | public Boolean decompile(Boolean toDecompile) { 97 | return toDecompile; 98 | } 99 | } 100 | 101 | public static class UUIDCompiler implements DataCompiler { 102 | 103 | @Override 104 | public String compile(UUID original) { 105 | return original.toString(); 106 | } 107 | 108 | @Override 109 | public UUID decompile(String toDecompile) { 110 | return UUID.fromString(toDecompile); 111 | } 112 | } 113 | 114 | public static class ShortCompiler implements DataCompiler { 115 | 116 | @Override 117 | public Short compile(Short original) { 118 | return original; 119 | } 120 | 121 | @Override 122 | public Short decompile(Short toDecompile) { 123 | return toDecompile; 124 | } 125 | } 126 | 127 | public static class DoubleCompiler implements DataCompiler { 128 | 129 | @Override 130 | public Double compile(Double original) { 131 | return original; 132 | } 133 | 134 | @Override 135 | public Double decompile(Double toDecompile) { 136 | return toDecompile; 137 | } 138 | } 139 | 140 | public static class FloatCompiler implements DataCompiler { 141 | 142 | @Override 143 | public Float compile(Float original) { 144 | return original; 145 | } 146 | 147 | @Override 148 | public Float decompile(Float toDecompile) { 149 | return toDecompile; 150 | } 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/data/DataType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler.data; 18 | 19 | import java.sql.Types; 20 | 21 | public enum DataType { 22 | 23 | STRING("LONGTEXT", Types.LONGVARCHAR, String.class), 24 | INT("INT", Types.INTEGER, Integer.class, int.class), 25 | LONG("BIGINT", Types.BIGINT, Long.class, long.class), 26 | BOOLEAN("TYNYINT(1)", Types.TINYINT, Boolean.class, boolean.class), 27 | UUID("VARCHAR(36)", Types.VARCHAR, java.util.UUID.class), 28 | SHORT("SMALLINT", Types.SMALLINT, Short.class, short.class), 29 | DOUBLE("DOUBLE", Types.DOUBLE, Double.class, double.class), 30 | FLOAT("FLOAT", Types.FLOAT, Float.class, float.class), 31 | //Always last :XD: 32 | OBJECT("LONGTEXT", Types.LONGVARCHAR, Object.class); 33 | 34 | private Class[] clazz; 35 | private String sqlType; 36 | private int numericType; 37 | 38 | DataType(String sqlType, int numericType, Class... clazz) { 39 | this.clazz = clazz; 40 | this.sqlType = sqlType; 41 | this.numericType = numericType; 42 | } 43 | 44 | public static DataType getType(Class clazz) { 45 | for (DataType value : values()) { 46 | for(Class valueClass : value.getClazz()) { 47 | if(clazz.equals(valueClass)) return value; 48 | } 49 | } 50 | return OBJECT; 51 | } 52 | 53 | public Class[] getClazz() { 54 | return clazz; 55 | } 56 | 57 | public String getSqlType() { 58 | return sqlType; 59 | } 60 | 61 | public int getNumericType() { 62 | return numericType; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/exception/CompilerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler.exception; 18 | 19 | public class CompilerException extends RuntimeException { 20 | 21 | public CompilerException(Throwable cause) { 22 | super(cause); 23 | } 24 | 25 | public CompilerException() { 26 | super(); 27 | } 28 | 29 | public CompilerException(String message) { 30 | super(message); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/compiler/field/CompiledFieldImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.compiler.field; 18 | 19 | import dev.krynn.sql.compiler.data.DataCompiler; 20 | import dev.krynn.sql.compiler.field.CompiledField; 21 | 22 | import java.lang.reflect.Field; 23 | 24 | public class CompiledFieldImpl implements CompiledField { 25 | 26 | private Field field; 27 | private String sqlType; 28 | private int numericType; 29 | private String name; 30 | private DataCompiler dataCompiler; 31 | private boolean primaryKey; 32 | 33 | public CompiledFieldImpl(Field field, String sqlType, String name, DataCompiler dataCompiler) { 34 | this.field = field; 35 | this.sqlType = sqlType; 36 | this.name = name; 37 | this.dataCompiler = dataCompiler; 38 | } 39 | 40 | public CompiledFieldImpl(Field field, String sqlType, String name, DataCompiler dataCompiler, boolean primaryKey) { 41 | this.field = field; 42 | this.sqlType = sqlType; 43 | this.name = name; 44 | this.dataCompiler = dataCompiler; 45 | this.primaryKey = primaryKey; 46 | } 47 | 48 | public CompiledFieldImpl(Field field, String sqlType, int numericType, String name, DataCompiler dataCompiler, boolean primaryKey) { 49 | this.field = field; 50 | this.sqlType = sqlType; 51 | this.numericType = numericType; 52 | this.name = name; 53 | this.dataCompiler = dataCompiler; 54 | this.primaryKey = primaryKey; 55 | } 56 | 57 | private CompiledFieldImpl(Builder builder) { 58 | field = builder.field; 59 | sqlType = builder.sqlType; 60 | numericType = builder.numericType; 61 | name = builder.name; 62 | dataCompiler = builder.dataCompiler; 63 | primaryKey = builder.primaryKey; 64 | } 65 | 66 | public static Builder newBuilder() { 67 | return new Builder(); 68 | } 69 | 70 | @Override 71 | public Field field() { 72 | return this.field; 73 | } 74 | 75 | @Override 76 | public String sqlType() { 77 | return this.sqlType; 78 | } 79 | 80 | @Override 81 | public int numericType() { 82 | return this.numericType; 83 | } 84 | 85 | @Override 86 | public String name() { 87 | return this.name; 88 | } 89 | 90 | @Override 91 | public DataCompiler dataCompiler() { 92 | return this.dataCompiler; 93 | } 94 | 95 | @Override 96 | public boolean isPrimary() { 97 | return this.primaryKey; 98 | } 99 | 100 | 101 | public static final class Builder { 102 | private Field field; 103 | private String sqlType; 104 | private int numericType; 105 | private String name; 106 | private DataCompiler dataCompiler; 107 | private boolean primaryKey; 108 | 109 | private Builder() { 110 | } 111 | 112 | public Builder field(Field val) { 113 | field = val; 114 | return this; 115 | } 116 | 117 | public Builder sqlType(String val) { 118 | sqlType = val; 119 | return this; 120 | } 121 | 122 | public Builder numericType(int val) { 123 | numericType = val; 124 | return this; 125 | } 126 | 127 | public Builder name(String val) { 128 | name = val; 129 | return this; 130 | } 131 | 132 | public Builder dataCompiler(DataCompiler val) { 133 | dataCompiler = val; 134 | return this; 135 | } 136 | 137 | public Builder primaryKey(boolean val) { 138 | primaryKey = val; 139 | return this; 140 | } 141 | 142 | public CompiledFieldImpl build() { 143 | return new CompiledFieldImpl(this); 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/connection/DatabaseConnectionImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.connection; 18 | 19 | import com.zaxxer.hikari.HikariConfig; 20 | import com.zaxxer.hikari.HikariDataSource; 21 | import dev.krynn.sql.connection.DatabaseConnection; 22 | 23 | import java.sql.Connection; 24 | import java.sql.SQLException; 25 | 26 | public class DatabaseConnectionImpl implements DatabaseConnection { 27 | 28 | private HikariDataSource dataSource; 29 | 30 | @Override 31 | public Connection connection() throws SQLException{ 32 | return this.dataSource.getConnection(); 33 | } 34 | 35 | @Override 36 | public void close() { 37 | this.dataSource.close(); 38 | } 39 | 40 | @Override 41 | public void config(HikariConfig config) { 42 | this.dataSource = new HikariDataSource(config); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/database/DatabaseFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.database; 18 | 19 | import com.google.common.cache.Cache; 20 | import dev.krynn.sql.KrynnSQL; 21 | import dev.krynn.sql.connection.DatabaseConnection; 22 | import dev.krynn.sql.database.Database; 23 | import dev.krynn.sql.database.DatabaseFactory; 24 | import dev.krynn.sql.query.Query; 25 | 26 | import java.sql.Connection; 27 | import java.sql.PreparedStatement; 28 | import java.sql.SQLException; 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | public class DatabaseFactoryImpl implements DatabaseFactory { 33 | 34 | private DatabaseConnection connection; 35 | 36 | private KrynnSQL krynnSQL; 37 | 38 | private List databases = new ArrayList<>(); 39 | 40 | public DatabaseFactoryImpl(KrynnSQL krynnSQL, DatabaseConnection connection) { 41 | this.connection = connection; 42 | this.krynnSQL = krynnSQL; 43 | } 44 | 45 | @Override 46 | public Database get(String name) { 47 | return new DatabaseImpl(krynnSQL, name); 48 | } 49 | 50 | @Override 51 | public Database getOrCreate(String name) { 52 | if(!databases.contains(name)) { 53 | create(name); 54 | } 55 | return get(name); 56 | } 57 | 58 | @Override 59 | public void create(String name) { 60 | try(Connection con = connection.connection()) { 61 | PreparedStatement statement = con.prepareStatement(String.format(Query.CREATE_DATABASE, name)); 62 | statement.executeUpdate(); 63 | databases.add(name); 64 | } catch (SQLException e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/database/DatabaseImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.database; 18 | 19 | import dev.krynn.sql.KrynnSQL; 20 | import dev.krynn.sql.database.Database; 21 | import dev.krynn.sql.impl.table.TableImpl; 22 | import dev.krynn.sql.table.Table; 23 | 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | public class DatabaseImpl implements Database { 28 | 29 | private String name; 30 | private KrynnSQL krynnSQL; 31 | 32 | public DatabaseImpl(KrynnSQL krynnSQL, String name) { 33 | this.name = name; 34 | this.krynnSQL = krynnSQL; 35 | } 36 | 37 | @Override 38 | public Table table(Class type) { 39 | return krynnSQL.getTableFactory().getOrCreate(this, type); 40 | } 41 | 42 | @Override 43 | public String name() { 44 | return this.name; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/table/TableFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.table; 18 | 19 | import dev.krynn.sql.KrynnSQL; 20 | import dev.krynn.sql.compiler.CompiledTemplate; 21 | import dev.krynn.sql.connection.DatabaseConnection; 22 | import dev.krynn.sql.database.Database; 23 | import dev.krynn.sql.table.Table; 24 | import dev.krynn.sql.table.TableFactory; 25 | import dev.krynn.sql.util.QueryUtil; 26 | 27 | import java.sql.Connection; 28 | import java.sql.PreparedStatement; 29 | import java.sql.SQLException; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | public class TableFactoryImpl implements TableFactory { 34 | 35 | private DatabaseConnection databaseConnection; 36 | 37 | private KrynnSQL krynnSQL; 38 | 39 | private List tables = new ArrayList<>(); 40 | 41 | public TableFactoryImpl(KrynnSQL krynnSQL, DatabaseConnection connection) { 42 | this.databaseConnection = connection; 43 | this.krynnSQL = krynnSQL; 44 | } 45 | 46 | @Override 47 | public Table get(Database database, Class type) { 48 | return new TableImpl<>(krynnSQL, database, type); 49 | } 50 | 51 | @Override 52 | public Table getOrCreate(Database database, Class type) { 53 | CompiledTemplate template = KrynnSQL.getCompiler().findOrCreate(type); 54 | if(!tables.contains(database.name() + "." + template.table())) { 55 | create(database, type); 56 | } 57 | return get(database, type); 58 | } 59 | 60 | @Override 61 | public void create(Database database, Class type) { 62 | try(Connection con = databaseConnection.connection()) { 63 | PreparedStatement table = QueryUtil.table(database.name(), con, type); 64 | table.executeUpdate(); 65 | tables.add(database + "." + KrynnSQL.getCompiler().findTemplate(type).table()); 66 | } catch (SQLException e) { 67 | e.printStackTrace(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/table/TableImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.table; 18 | 19 | import com.google.common.reflect.TypeToken; 20 | import dev.krynn.sql.KrynnSQL; 21 | import dev.krynn.sql.compiler.CompiledTemplate; 22 | import dev.krynn.sql.database.Database; 23 | import dev.krynn.sql.table.Table; 24 | import dev.krynn.sql.util.QueryUtil; 25 | 26 | import java.lang.reflect.Type; 27 | import java.sql.Connection; 28 | import java.sql.PreparedStatement; 29 | import java.sql.ResultSet; 30 | import java.sql.SQLException; 31 | import java.util.ArrayList; 32 | import java.util.Collections; 33 | import java.util.List; 34 | 35 | public class TableImpl implements Table { 36 | 37 | private Class clazz; 38 | 39 | private Database database; 40 | 41 | private KrynnSQL krynnSQL; 42 | 43 | public TableImpl(KrynnSQL krynnSQL, Database database, Class clazz) { 44 | this.database = database; 45 | this.clazz = clazz; 46 | this.krynnSQL = krynnSQL; 47 | } 48 | 49 | public List query(String statement) { 50 | try(Connection connection = krynnSQL.getConnection()) { 51 | if(KrynnSQL.getCompiler().findTemplate(clazz) == null) { 52 | KrynnSQL.getCompiler().compile(clazz); 53 | } 54 | ResultSet resultSet = connection.prepareStatement(statement.replace("{table}", database.name() + "." + KrynnSQL.getCompiler().findTemplate(clazz).table())).executeQuery(); 55 | List list = new ArrayList<>(); 56 | while(resultSet.next()) { 57 | list.add(KrynnSQL.getCompiler().build(clazz, resultSet)); 58 | } 59 | return list; 60 | } catch (SQLException e) { 61 | e.printStackTrace(); 62 | return Collections.emptyList(); 63 | } 64 | } 65 | 66 | public List query(T type) { 67 | throw new UnsupportedOperationException(); 68 | } 69 | 70 | public void update(String query) { 71 | try(Connection connection = krynnSQL.getConnection()) { 72 | connection.prepareStatement(query).executeUpdate(); 73 | } catch (SQLException e) { 74 | e.printStackTrace(); 75 | } 76 | } 77 | 78 | public void update(T type) { 79 | Type objectType = TypeToken.of(clazz).getType(); 80 | try(Connection connection = krynnSQL.getConnection()) { 81 | if(KrynnSQL.getCompiler().findTemplate(objectType) == null) { 82 | KrynnSQL.getCompiler().compile(type.getClass()); 83 | } 84 | QueryUtil.update(this.database.name(), connection, type, objectType).executeUpdate(); 85 | } catch (SQLException | IllegalAccessException e) { 86 | e.printStackTrace(); 87 | } 88 | } 89 | 90 | @Override 91 | public void delete(T type) { 92 | try(Connection connection = krynnSQL.getConnection()) { 93 | QueryUtil.delete(this.database.name(), connection, type, clazz).executeUpdate(); 94 | } catch (SQLException | IllegalAccessException e) { 95 | e.printStackTrace(); 96 | } 97 | } 98 | 99 | @Override 100 | public String name() { 101 | return KrynnSQL.getCompiler().findOrCreate(this.clazz).table(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/impl/table/cache/CacheableTableImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.impl.table.cache; 18 | 19 | import dev.krynn.sql.KrynnSQL; 20 | import dev.krynn.sql.compiler.field.CompiledField; 21 | import dev.krynn.sql.database.Database; 22 | import dev.krynn.sql.impl.table.TableImpl; 23 | import dev.krynn.sql.query.Query; 24 | import dev.krynn.sql.table.Table; 25 | import dev.krynn.sql.table.cache.CacheableTable; 26 | 27 | import java.lang.reflect.Field; 28 | import java.sql.Connection; 29 | import java.sql.PreparedStatement; 30 | import java.sql.ResultSet; 31 | import java.sql.SQLException; 32 | import java.util.HashMap; 33 | import java.util.List; 34 | import java.util.Map; 35 | 36 | public class CacheableTableImpl implements CacheableTable { 37 | 38 | private Table table; 39 | private Map cacheMap = new HashMap<>(); 40 | private CompiledField primaryKey; 41 | private Class clazz; 42 | private KrynnSQL krynnSQL; 43 | 44 | public CacheableTableImpl(KrynnSQL krynnSQL, Database database, Class clazz) { 45 | this.table = new TableImpl<>(krynnSQL, database, clazz); 46 | this.krynnSQL = krynnSQL; 47 | this.clazz = clazz; 48 | this.primaryKey = KrynnSQL.getCompiler().findOrCreate(clazz).primaryKey(); 49 | } 50 | 51 | @Override 52 | public void cache(T object) { 53 | throw new UnsupportedOperationException(); 54 | } 55 | 56 | @Override 57 | public void clear() { 58 | this.cacheMap.clear(); 59 | } 60 | 61 | @Override 62 | public void invalidate(T object) { 63 | try { 64 | this.cacheMap.remove(getKey(object)); 65 | } catch (IllegalAccessException e) { 66 | e.printStackTrace(); 67 | } 68 | } 69 | 70 | @Override 71 | public T cachedQuery(Object key) { 72 | if(this.cacheMap.containsKey(key)) return this.cacheMap.get(key); 73 | 74 | try(Connection connection = krynnSQL.getConnection()) { 75 | PreparedStatement statement = connection.prepareStatement(String.format(Query.SELECT_OBJECT, name(), primaryKey.name())); 76 | statement.setObject(1, key); 77 | //Maybe stupid... 78 | ResultSet resultSet = statement.executeQuery(); 79 | resultSet.next(); 80 | 81 | T build = KrynnSQL.getCompiler().build(clazz, resultSet); 82 | this.cacheMap.put(key, build); 83 | 84 | return build; 85 | } catch (SQLException e) { 86 | throw new RuntimeException(e); 87 | } 88 | } 89 | 90 | @Override 91 | public List query(T type) { 92 | return this.table.query(type); 93 | } 94 | 95 | @Override 96 | public List query(String query) { 97 | return this.table.query(query); 98 | } 99 | 100 | @Override 101 | public void update(String query) { 102 | this.table.update(query); 103 | } 104 | 105 | @Override 106 | public void update(T type) { 107 | this.table.update(type); 108 | } 109 | 110 | @Override 111 | public void delete(T type) { 112 | this.table.delete(type); 113 | } 114 | 115 | @Override 116 | public String name() { 117 | return this.table.name(); 118 | } 119 | 120 | private Object getKey(T object) throws IllegalAccessException { 121 | Field field = primaryKey.field(); 122 | field.setAccessible(true); 123 | return field.get(object); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/query/Query.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.query; 18 | 19 | public class Query { 20 | 21 | public static final String CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS %s"; 22 | 23 | public static final String CREATE_OR_UPDATE = "INSERT INTO %s (%s) VALUES(%s) ON DUPLICATE KEY UPDATE %s;"; 24 | 25 | public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS %s (%s)"; 26 | 27 | public static final String SELECT_OBJECT = "SELECT * FROM %s WHERE %s = ?"; 28 | 29 | public static final String DELETE_OBJECT = "DELETE FROM %s WHERE %s = ?"; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/query/QueryBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.query; 18 | 19 | public class QueryBuilder { 20 | //TODO 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/table/Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.table; 18 | 19 | import java.sql.PreparedStatement; 20 | import java.util.List; 21 | 22 | public interface Table { 23 | 24 | List query(T type); 25 | 26 | List query(String query); 27 | 28 | void update(String query); 29 | 30 | void update(T type); 31 | 32 | void delete(T type); 33 | 34 | String name(); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/table/TableFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.table; 18 | 19 | import dev.krynn.sql.database.Database; 20 | 21 | public interface TableFactory { 22 | 23 | Table get(Database database, Class type); 24 | 25 | Table getOrCreate(Database database, Class type); 26 | 27 | void create(Database database, Class type); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/table/cache/CacheableTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.table.cache; 18 | 19 | import dev.krynn.sql.table.Table; 20 | 21 | public interface CacheableTable extends Table { 22 | 23 | void cache(T object); 24 | 25 | void clear(); 26 | 27 | void invalidate(T object); 28 | 29 | T cachedQuery(Object key); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/dev/krynn/sql/util/QueryUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql.util; 18 | 19 | import dev.krynn.sql.KrynnSQL; 20 | import dev.krynn.sql.compiler.CompiledTemplate; 21 | import dev.krynn.sql.compiler.data.DataCompiler; 22 | import dev.krynn.sql.compiler.field.CompiledField; 23 | import dev.krynn.sql.query.Query; 24 | 25 | import java.lang.reflect.Field; 26 | import java.lang.reflect.Type; 27 | import java.sql.Connection; 28 | import java.sql.PreparedStatement; 29 | import java.sql.SQLException; 30 | import java.util.Collections; 31 | import java.util.Iterator; 32 | import java.util.Map; 33 | import java.util.Set; 34 | import java.util.stream.Collectors; 35 | 36 | public class QueryUtil { 37 | 38 | public static PreparedStatement update(String database, Connection connection, T type, Type objectType) throws IllegalAccessException, SQLException { 39 | CompiledTemplate template = KrynnSQL.getCompiler().findTemplate(objectType); 40 | 41 | Set> entries = template.compiledFields().entrySet(); 42 | Iterator> iterator = entries.iterator(); 43 | 44 | String values = String.join(", ", Collections.nCopies(entries.size(), "?")); 45 | String table = database + "." + template.table(); 46 | String fields = entries.stream().map(Map.Entry::getKey).collect(Collectors.joining(", ")); 47 | String onDuplicate = entries.stream().map(map -> map.getKey() + " = ?").collect(Collectors.joining(", ")); 48 | 49 | PreparedStatement preparedStatement = connection.prepareStatement(String.format(Query.CREATE_OR_UPDATE, table, fields, values, onDuplicate)); 50 | 51 | int index = 1; 52 | while(iterator.hasNext()) { 53 | Map.Entry next = iterator.next(); 54 | CompiledField value = next.getValue(); 55 | 56 | Field field = value.field(); 57 | field.setAccessible(true); 58 | Object val = tryCompile(value.dataCompiler(), field.get(type)); 59 | 60 | preparedStatement.setObject(index, val, value.numericType()); 61 | preparedStatement.setObject(index + entries.size(), val, value.numericType()); 62 | 63 | index++; 64 | } 65 | 66 | return preparedStatement; 67 | } 68 | 69 | public static PreparedStatement table(String database, Connection connection, Class clazz) throws SQLException { 70 | if(KrynnSQL.getCompiler().findTemplate(clazz) == null) { 71 | KrynnSQL.getCompiler().compile(clazz); 72 | } 73 | CompiledTemplate template = KrynnSQL.getCompiler().findTemplate(clazz); 74 | 75 | String name = database + "." + template.table(); 76 | String types = template.compiledFields() 77 | .entrySet() 78 | .stream() 79 | .map(compiledField -> compiledField.getKey() + " " + compiledField.getValue().sqlType()) 80 | .collect(Collectors.joining(", ")); 81 | 82 | types += String.format(", PRIMARY KEY(%s)", template.primaryKey().name()); 83 | 84 | return connection.prepareStatement(String.format(Query.CREATE_TABLE, name, types)); 85 | } 86 | 87 | public static PreparedStatement delete(String database, Connection connection, T object, Type type) throws IllegalAccessException, SQLException { 88 | CompiledTemplate template = KrynnSQL.getCompiler().findOrCreate(type); 89 | 90 | CompiledField primaryKey = template.primaryKey(); 91 | Object keyValue = primaryKey.field().get(object); 92 | 93 | PreparedStatement statement = connection.prepareStatement(String.format( 94 | Query.DELETE_OBJECT, 95 | database + "." + template.table(), 96 | primaryKey.name())); 97 | statement.setObject(1, keyValue, primaryKey.numericType()); 98 | return statement; 99 | } 100 | 101 | @SuppressWarnings("unchecked") 102 | private static T tryCompile(DataCompiler dataCompiler, Object o) { 103 | return dataCompiler.compile((I) o); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/test/java/dev/krynn/sql/AbstractResultSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import java.io.InputStream; 20 | import java.io.Reader; 21 | import java.math.BigDecimal; 22 | import java.net.URL; 23 | import java.sql.Array; 24 | import java.sql.Blob; 25 | import java.sql.Clob; 26 | import java.sql.Date; 27 | import java.sql.NClob; 28 | import java.sql.Ref; 29 | import java.sql.ResultSet; 30 | import java.sql.ResultSetMetaData; 31 | import java.sql.RowId; 32 | import java.sql.SQLException; 33 | import java.sql.SQLWarning; 34 | import java.sql.SQLXML; 35 | import java.sql.Statement; 36 | import java.sql.Time; 37 | import java.sql.Timestamp; 38 | import java.util.Calendar; 39 | import java.util.Map; 40 | 41 | public class AbstractResultSet implements ResultSet { 42 | 43 | @Override 44 | public boolean isWrapperFor(Class iface) throws SQLException { 45 | throw new UnsupportedOperationException(); 46 | } 47 | 48 | @Override 49 | public T unwrap(Class iface) throws SQLException { 50 | throw new UnsupportedOperationException(); 51 | } 52 | 53 | @Override 54 | public boolean absolute(int row) throws SQLException { 55 | throw new UnsupportedOperationException(); 56 | } 57 | 58 | @Override 59 | public void afterLast() throws SQLException { 60 | throw new UnsupportedOperationException(); 61 | } 62 | 63 | @Override 64 | public void beforeFirst() throws SQLException { 65 | throw new UnsupportedOperationException(); 66 | } 67 | 68 | @Override 69 | public void cancelRowUpdates() throws SQLException { 70 | throw new UnsupportedOperationException(); 71 | } 72 | 73 | @Override 74 | public void clearWarnings() throws SQLException { 75 | throw new UnsupportedOperationException(); 76 | } 77 | 78 | @Override 79 | public void close() throws SQLException { 80 | throw new UnsupportedOperationException(); 81 | } 82 | 83 | @Override 84 | public void deleteRow() throws SQLException { 85 | throw new UnsupportedOperationException(); 86 | } 87 | 88 | @Override 89 | public int findColumn(String columnLabel) throws SQLException { 90 | throw new UnsupportedOperationException(); 91 | } 92 | 93 | @Override 94 | public boolean first() throws SQLException { 95 | throw new UnsupportedOperationException(); 96 | } 97 | 98 | @Override 99 | public Array getArray(int columnIndex) throws SQLException { 100 | throw new UnsupportedOperationException(); 101 | } 102 | 103 | @Override 104 | public Array getArray(String columnLabel) throws SQLException { 105 | throw new UnsupportedOperationException(); 106 | } 107 | 108 | @Override 109 | public InputStream getAsciiStream(int columnIndex) throws SQLException { 110 | throw new UnsupportedOperationException(); 111 | } 112 | 113 | @Override 114 | public InputStream getAsciiStream(String columnLabel) throws SQLException { 115 | throw new UnsupportedOperationException(); 116 | } 117 | 118 | @Override 119 | public BigDecimal getBigDecimal(int columnIndex) throws SQLException { 120 | throw new UnsupportedOperationException(); 121 | } 122 | 123 | @Override 124 | public BigDecimal getBigDecimal(String columnLabel) throws SQLException { 125 | throw new UnsupportedOperationException(); 126 | } 127 | 128 | @Override 129 | public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { 130 | throw new UnsupportedOperationException(); 131 | } 132 | 133 | @Override 134 | public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { 135 | throw new UnsupportedOperationException(); 136 | } 137 | 138 | @Override 139 | public InputStream getBinaryStream(int columnIndex) throws SQLException { 140 | throw new UnsupportedOperationException(); 141 | } 142 | 143 | @Override 144 | public InputStream getBinaryStream(String columnLabel) throws SQLException { 145 | throw new UnsupportedOperationException(); 146 | } 147 | 148 | @Override 149 | public Blob getBlob(int columnIndex) throws SQLException { 150 | throw new UnsupportedOperationException(); 151 | } 152 | 153 | @Override 154 | public Blob getBlob(String columnLabel) throws SQLException { 155 | throw new UnsupportedOperationException(); 156 | } 157 | 158 | @Override 159 | public boolean getBoolean(int columnIndex) throws SQLException { 160 | throw new UnsupportedOperationException(); 161 | } 162 | 163 | @Override 164 | public boolean getBoolean(String columnLabel) throws SQLException { 165 | throw new UnsupportedOperationException(); 166 | } 167 | 168 | @Override 169 | public byte getByte(int columnIndex) throws SQLException { 170 | throw new UnsupportedOperationException(); 171 | } 172 | 173 | @Override 174 | public byte getByte(String columnLabel) throws SQLException { 175 | throw new UnsupportedOperationException(); 176 | } 177 | 178 | @Override 179 | public byte[] getBytes(int columnIndex) throws SQLException { 180 | throw new UnsupportedOperationException(); 181 | } 182 | 183 | @Override 184 | public byte[] getBytes(String columnLabel) throws SQLException { 185 | throw new UnsupportedOperationException(); 186 | } 187 | 188 | @Override 189 | public Reader getCharacterStream(int columnIndex) throws SQLException { 190 | throw new UnsupportedOperationException(); 191 | } 192 | 193 | @Override 194 | public Reader getCharacterStream(String columnLabel) throws SQLException { 195 | throw new UnsupportedOperationException(); 196 | } 197 | 198 | @Override 199 | public Clob getClob(int columnIndex) throws SQLException { 200 | throw new UnsupportedOperationException(); 201 | } 202 | 203 | @Override 204 | public Clob getClob(String columnLabel) throws SQLException { 205 | throw new UnsupportedOperationException(); 206 | } 207 | 208 | @Override 209 | public int getConcurrency() throws SQLException { 210 | throw new UnsupportedOperationException(); 211 | } 212 | 213 | @Override 214 | public String getCursorName() throws SQLException { 215 | throw new UnsupportedOperationException(); 216 | } 217 | 218 | @Override 219 | public Date getDate(int columnIndex) throws SQLException { 220 | throw new UnsupportedOperationException(); 221 | } 222 | 223 | @Override 224 | public Date getDate(String columnLabel) throws SQLException { 225 | throw new UnsupportedOperationException(); 226 | } 227 | 228 | @Override 229 | public Date getDate(int columnIndex, Calendar cal) throws SQLException { 230 | throw new UnsupportedOperationException(); 231 | } 232 | 233 | @Override 234 | public Date getDate(String columnLabel, Calendar cal) throws SQLException { 235 | throw new UnsupportedOperationException(); 236 | } 237 | 238 | @Override 239 | public double getDouble(int columnIndex) throws SQLException { 240 | throw new UnsupportedOperationException(); 241 | } 242 | 243 | @Override 244 | public double getDouble(String columnLabel) throws SQLException { 245 | throw new UnsupportedOperationException(); 246 | } 247 | 248 | @Override 249 | public int getFetchDirection() throws SQLException { 250 | throw new UnsupportedOperationException(); 251 | } 252 | 253 | @Override 254 | public int getFetchSize() throws SQLException { 255 | throw new UnsupportedOperationException(); 256 | } 257 | 258 | @Override 259 | public float getFloat(int columnIndex) throws SQLException { 260 | throw new UnsupportedOperationException(); 261 | } 262 | 263 | @Override 264 | public float getFloat(String columnLabel) throws SQLException { 265 | throw new UnsupportedOperationException(); 266 | } 267 | 268 | @Override 269 | public int getHoldability() throws SQLException { 270 | throw new UnsupportedOperationException(); 271 | } 272 | 273 | @Override 274 | public int getInt(int columnIndex) throws SQLException { 275 | throw new UnsupportedOperationException(); 276 | } 277 | 278 | @Override 279 | public int getInt(String columnLabel) throws SQLException { 280 | throw new UnsupportedOperationException(); 281 | } 282 | 283 | @Override 284 | public long getLong(int columnIndex) throws SQLException { 285 | throw new UnsupportedOperationException(); 286 | } 287 | 288 | @Override 289 | public long getLong(String columnLabel) throws SQLException { 290 | throw new UnsupportedOperationException(); 291 | } 292 | 293 | @Override 294 | public ResultSetMetaData getMetaData() throws SQLException { 295 | throw new UnsupportedOperationException(); 296 | } 297 | 298 | @Override 299 | public Reader getNCharacterStream(int columnIndex) throws SQLException { 300 | throw new UnsupportedOperationException(); 301 | } 302 | 303 | @Override 304 | public Reader getNCharacterStream(String columnLabel) throws SQLException { 305 | throw new UnsupportedOperationException(); 306 | } 307 | 308 | @Override 309 | public NClob getNClob(int columnIndex) throws SQLException { 310 | throw new UnsupportedOperationException(); 311 | } 312 | 313 | @Override 314 | public NClob getNClob(String columnLabel) throws SQLException { 315 | throw new UnsupportedOperationException(); 316 | } 317 | 318 | @Override 319 | public String getNString(int columnIndex) throws SQLException { 320 | throw new UnsupportedOperationException(); 321 | } 322 | 323 | @Override 324 | public String getNString(String columnLabel) throws SQLException { 325 | throw new UnsupportedOperationException(); 326 | } 327 | 328 | @Override 329 | public Object getObject(int columnIndex) throws SQLException { 330 | throw new UnsupportedOperationException(); 331 | } 332 | 333 | @Override 334 | public Object getObject(String columnLabel) throws SQLException { 335 | throw new UnsupportedOperationException(); 336 | } 337 | 338 | @Override 339 | public Object getObject(int columnIndex, Map> map) throws SQLException { 340 | throw new UnsupportedOperationException(); 341 | } 342 | 343 | @Override 344 | public Object getObject(String columnLabel, Map> map) throws SQLException { 345 | throw new UnsupportedOperationException(); 346 | } 347 | 348 | @Override 349 | public T getObject(int columnIndex, Class type) throws SQLException { 350 | throw new UnsupportedOperationException(); 351 | } 352 | 353 | @Override 354 | @SuppressWarnings("unchecked") 355 | public T getObject(String columnLabel, Class type) throws SQLException { 356 | if (type.equals(String.class)) { 357 | return (T) "test"; 358 | } else if (type.equals(Integer.class) || type.equals(int.class)) { 359 | return (T) ((Integer) 2115); 360 | } else if(type.equals(Boolean.class) || type.equals(boolean.class)) { 361 | return (T) ((Boolean) true); 362 | } 363 | return null; 364 | } 365 | 366 | @Override 367 | public Ref getRef(int columnIndex) throws SQLException { 368 | throw new UnsupportedOperationException(); 369 | } 370 | 371 | @Override 372 | public Ref getRef(String columnLabel) throws SQLException { 373 | throw new UnsupportedOperationException(); 374 | } 375 | 376 | @Override 377 | public int getRow() throws SQLException { 378 | throw new UnsupportedOperationException(); 379 | } 380 | 381 | @Override 382 | public RowId getRowId(int columnIndex) throws SQLException { 383 | throw new UnsupportedOperationException(); 384 | } 385 | 386 | @Override 387 | public RowId getRowId(String columnLabel) throws SQLException { 388 | throw new UnsupportedOperationException(); 389 | } 390 | 391 | @Override 392 | public SQLXML getSQLXML(int columnIndex) throws SQLException { 393 | throw new UnsupportedOperationException(); 394 | } 395 | 396 | @Override 397 | public SQLXML getSQLXML(String columnLabel) throws SQLException { 398 | throw new UnsupportedOperationException(); 399 | } 400 | 401 | @Override 402 | public short getShort(int columnIndex) throws SQLException { 403 | throw new UnsupportedOperationException(); 404 | } 405 | 406 | @Override 407 | public short getShort(String columnLabel) throws SQLException { 408 | throw new UnsupportedOperationException(); 409 | } 410 | 411 | @Override 412 | public Statement getStatement() throws SQLException { 413 | throw new UnsupportedOperationException(); 414 | } 415 | 416 | @Override 417 | public String getString(int columnIndex) throws SQLException { 418 | throw new UnsupportedOperationException(); 419 | } 420 | 421 | @Override 422 | public String getString(String columnLabel) throws SQLException { 423 | throw new UnsupportedOperationException(); 424 | } 425 | 426 | @Override 427 | public Time getTime(int columnIndex) throws SQLException { 428 | throw new UnsupportedOperationException(); 429 | } 430 | 431 | @Override 432 | public Time getTime(String columnLabel) throws SQLException { 433 | throw new UnsupportedOperationException(); 434 | } 435 | 436 | @Override 437 | public Time getTime(int columnIndex, Calendar cal) throws SQLException { 438 | throw new UnsupportedOperationException(); 439 | } 440 | 441 | @Override 442 | public Time getTime(String columnLabel, Calendar cal) throws SQLException { 443 | throw new UnsupportedOperationException(); 444 | } 445 | 446 | @Override 447 | public Timestamp getTimestamp(int columnIndex) throws SQLException { 448 | throw new UnsupportedOperationException(); 449 | } 450 | 451 | @Override 452 | public Timestamp getTimestamp(String columnLabel) throws SQLException { 453 | throw new UnsupportedOperationException(); 454 | } 455 | 456 | @Override 457 | public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { 458 | throw new UnsupportedOperationException(); 459 | } 460 | 461 | @Override 462 | public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { 463 | throw new UnsupportedOperationException(); 464 | } 465 | 466 | @Override 467 | public int getType() throws SQLException { 468 | throw new UnsupportedOperationException(); 469 | } 470 | 471 | @Override 472 | public URL getURL(int columnIndex) throws SQLException { 473 | throw new UnsupportedOperationException(); 474 | } 475 | 476 | @Override 477 | public URL getURL(String columnLabel) throws SQLException { 478 | throw new UnsupportedOperationException(); 479 | } 480 | 481 | @Override 482 | public InputStream getUnicodeStream(int columnIndex) throws SQLException { 483 | throw new UnsupportedOperationException(); 484 | } 485 | 486 | @Override 487 | public InputStream getUnicodeStream(String columnLabel) throws SQLException { 488 | throw new UnsupportedOperationException(); 489 | } 490 | 491 | @Override 492 | public SQLWarning getWarnings() throws SQLException { 493 | throw new UnsupportedOperationException(); 494 | } 495 | 496 | @Override 497 | public void insertRow() throws SQLException { 498 | throw new UnsupportedOperationException(); 499 | } 500 | 501 | @Override 502 | public boolean isAfterLast() throws SQLException { 503 | throw new UnsupportedOperationException(); 504 | } 505 | 506 | @Override 507 | public boolean isBeforeFirst() throws SQLException { 508 | throw new UnsupportedOperationException(); 509 | } 510 | 511 | @Override 512 | public boolean isClosed() throws SQLException { 513 | throw new UnsupportedOperationException(); 514 | } 515 | 516 | @Override 517 | public boolean isFirst() throws SQLException { 518 | throw new UnsupportedOperationException(); 519 | } 520 | 521 | @Override 522 | public boolean isLast() throws SQLException { 523 | throw new UnsupportedOperationException(); 524 | } 525 | 526 | @Override 527 | public boolean last() throws SQLException { 528 | throw new UnsupportedOperationException(); 529 | } 530 | 531 | @Override 532 | public void moveToCurrentRow() throws SQLException { 533 | throw new UnsupportedOperationException(); 534 | } 535 | 536 | @Override 537 | public void moveToInsertRow() throws SQLException { 538 | throw new UnsupportedOperationException(); 539 | } 540 | 541 | @Override 542 | public boolean next() throws SQLException { 543 | throw new UnsupportedOperationException(); 544 | } 545 | 546 | @Override 547 | public boolean previous() throws SQLException { 548 | throw new UnsupportedOperationException(); 549 | } 550 | 551 | @Override 552 | public void refreshRow() throws SQLException { 553 | throw new UnsupportedOperationException(); 554 | } 555 | 556 | @Override 557 | public boolean relative(int rows) throws SQLException { 558 | throw new UnsupportedOperationException(); 559 | } 560 | 561 | @Override 562 | public boolean rowDeleted() throws SQLException { 563 | throw new UnsupportedOperationException(); 564 | } 565 | 566 | @Override 567 | public boolean rowInserted() throws SQLException { 568 | throw new UnsupportedOperationException(); 569 | } 570 | 571 | @Override 572 | public boolean rowUpdated() throws SQLException { 573 | throw new UnsupportedOperationException(); 574 | } 575 | 576 | @Override 577 | public void setFetchDirection(int direction) throws SQLException { 578 | throw new UnsupportedOperationException(); 579 | } 580 | 581 | @Override 582 | public void setFetchSize(int rows) throws SQLException { 583 | throw new UnsupportedOperationException(); 584 | } 585 | 586 | @Override 587 | public void updateArray(int columnIndex, Array x) throws SQLException { 588 | throw new UnsupportedOperationException(); 589 | } 590 | 591 | @Override 592 | public void updateArray(String columnLabel, Array x) throws SQLException { 593 | throw new UnsupportedOperationException(); 594 | } 595 | 596 | @Override 597 | public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { 598 | throw new UnsupportedOperationException(); 599 | } 600 | 601 | @Override 602 | public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { 603 | throw new UnsupportedOperationException(); 604 | } 605 | 606 | @Override 607 | public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { 608 | throw new UnsupportedOperationException(); 609 | } 610 | 611 | @Override 612 | public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { 613 | throw new UnsupportedOperationException(); 614 | } 615 | 616 | @Override 617 | public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { 618 | throw new UnsupportedOperationException(); 619 | } 620 | 621 | @Override 622 | public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { 623 | throw new UnsupportedOperationException(); 624 | } 625 | 626 | @Override 627 | public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { 628 | throw new UnsupportedOperationException(); 629 | } 630 | 631 | @Override 632 | public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException { 633 | throw new UnsupportedOperationException(); 634 | } 635 | 636 | @Override 637 | public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { 638 | throw new UnsupportedOperationException(); 639 | } 640 | 641 | @Override 642 | public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { 643 | throw new UnsupportedOperationException(); 644 | } 645 | 646 | @Override 647 | public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException { 648 | throw new UnsupportedOperationException(); 649 | } 650 | 651 | @Override 652 | public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { 653 | throw new UnsupportedOperationException(); 654 | } 655 | 656 | @Override 657 | public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { 658 | throw new UnsupportedOperationException(); 659 | } 660 | 661 | @Override 662 | public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { 663 | throw new UnsupportedOperationException(); 664 | } 665 | 666 | @Override 667 | public void updateBlob(int columnIndex, Blob x) throws SQLException { 668 | throw new UnsupportedOperationException(); 669 | } 670 | 671 | @Override 672 | public void updateBlob(String columnLabel, Blob x) throws SQLException { 673 | throw new UnsupportedOperationException(); 674 | } 675 | 676 | @Override 677 | public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { 678 | throw new UnsupportedOperationException(); 679 | } 680 | 681 | @Override 682 | public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { 683 | throw new UnsupportedOperationException(); 684 | } 685 | 686 | @Override 687 | public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { 688 | throw new UnsupportedOperationException(); 689 | } 690 | 691 | @Override 692 | public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { 693 | throw new UnsupportedOperationException(); 694 | } 695 | 696 | @Override 697 | public void updateBoolean(int columnIndex, boolean x) throws SQLException { 698 | throw new UnsupportedOperationException(); 699 | } 700 | 701 | @Override 702 | public void updateBoolean(String columnLabel, boolean x) throws SQLException { 703 | throw new UnsupportedOperationException(); 704 | } 705 | 706 | @Override 707 | public void updateByte(int columnIndex, byte x) throws SQLException { 708 | throw new UnsupportedOperationException(); 709 | } 710 | 711 | @Override 712 | public void updateByte(String columnLabel, byte x) throws SQLException { 713 | throw new UnsupportedOperationException(); 714 | } 715 | 716 | @Override 717 | public void updateBytes(int columnIndex, byte[] x) throws SQLException { 718 | throw new UnsupportedOperationException(); 719 | } 720 | 721 | @Override 722 | public void updateBytes(String columnLabel, byte[] x) throws SQLException { 723 | throw new UnsupportedOperationException(); 724 | } 725 | 726 | @Override 727 | public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { 728 | throw new UnsupportedOperationException(); 729 | } 730 | 731 | @Override 732 | public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { 733 | throw new UnsupportedOperationException(); 734 | } 735 | 736 | @Override 737 | public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException { 738 | throw new UnsupportedOperationException(); 739 | } 740 | 741 | @Override 742 | public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { 743 | throw new UnsupportedOperationException(); 744 | } 745 | 746 | @Override 747 | public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { 748 | throw new UnsupportedOperationException(); 749 | } 750 | 751 | @Override 752 | public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { 753 | throw new UnsupportedOperationException(); 754 | } 755 | 756 | @Override 757 | public void updateClob(int columnIndex, Clob x) throws SQLException { 758 | throw new UnsupportedOperationException(); 759 | } 760 | 761 | @Override 762 | public void updateClob(String columnLabel, Clob x) throws SQLException { 763 | throw new UnsupportedOperationException(); 764 | } 765 | 766 | @Override 767 | public void updateClob(int columnIndex, Reader reader) throws SQLException { 768 | throw new UnsupportedOperationException(); 769 | } 770 | 771 | @Override 772 | public void updateClob(String columnLabel, Reader reader) throws SQLException { 773 | throw new UnsupportedOperationException(); 774 | } 775 | 776 | @Override 777 | public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { 778 | throw new UnsupportedOperationException(); 779 | } 780 | 781 | @Override 782 | public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { 783 | throw new UnsupportedOperationException(); 784 | } 785 | 786 | @Override 787 | public void updateDate(int columnIndex, Date x) throws SQLException { 788 | throw new UnsupportedOperationException(); 789 | } 790 | 791 | @Override 792 | public void updateDate(String columnLabel, Date x) throws SQLException { 793 | throw new UnsupportedOperationException(); 794 | } 795 | 796 | @Override 797 | public void updateDouble(int columnIndex, double x) throws SQLException { 798 | throw new UnsupportedOperationException(); 799 | } 800 | 801 | @Override 802 | public void updateDouble(String columnLabel, double x) throws SQLException { 803 | throw new UnsupportedOperationException(); 804 | } 805 | 806 | @Override 807 | public void updateFloat(int columnIndex, float x) throws SQLException { 808 | throw new UnsupportedOperationException(); 809 | } 810 | 811 | @Override 812 | public void updateFloat(String columnLabel, float x) throws SQLException { 813 | throw new UnsupportedOperationException(); 814 | } 815 | 816 | @Override 817 | public void updateInt(int columnIndex, int x) throws SQLException { 818 | throw new UnsupportedOperationException(); 819 | } 820 | 821 | @Override 822 | public void updateInt(String columnLabel, int x) throws SQLException { 823 | throw new UnsupportedOperationException(); 824 | } 825 | 826 | @Override 827 | public void updateLong(int columnIndex, long x) throws SQLException { 828 | throw new UnsupportedOperationException(); 829 | } 830 | 831 | @Override 832 | public void updateLong(String columnLabel, long x) throws SQLException { 833 | throw new UnsupportedOperationException(); 834 | } 835 | 836 | @Override 837 | public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { 838 | throw new UnsupportedOperationException(); 839 | } 840 | 841 | @Override 842 | public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { 843 | throw new UnsupportedOperationException(); 844 | } 845 | 846 | @Override 847 | public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { 848 | throw new UnsupportedOperationException(); 849 | } 850 | 851 | @Override 852 | public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { 853 | throw new UnsupportedOperationException(); 854 | } 855 | 856 | @Override 857 | public void updateNClob(int columnIndex, NClob nClob) throws SQLException { 858 | throw new UnsupportedOperationException(); 859 | } 860 | 861 | @Override 862 | public void updateNClob(String columnLabel, NClob nClob) throws SQLException { 863 | throw new UnsupportedOperationException(); 864 | } 865 | 866 | @Override 867 | public void updateNClob(int columnIndex, Reader reader) throws SQLException { 868 | throw new UnsupportedOperationException(); 869 | } 870 | 871 | @Override 872 | public void updateNClob(String columnLabel, Reader reader) throws SQLException { 873 | throw new UnsupportedOperationException(); 874 | } 875 | 876 | @Override 877 | public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { 878 | throw new UnsupportedOperationException(); 879 | } 880 | 881 | @Override 882 | public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { 883 | throw new UnsupportedOperationException(); 884 | } 885 | 886 | @Override 887 | public void updateNString(int columnIndex, String nString) throws SQLException { 888 | throw new UnsupportedOperationException(); 889 | } 890 | 891 | @Override 892 | public void updateNString(String columnLabel, String nString) throws SQLException { 893 | throw new UnsupportedOperationException(); 894 | } 895 | 896 | @Override 897 | public void updateNull(int columnIndex) throws SQLException { 898 | throw new UnsupportedOperationException(); 899 | } 900 | 901 | @Override 902 | public void updateNull(String columnLabel) throws SQLException { 903 | throw new UnsupportedOperationException(); 904 | } 905 | 906 | @Override 907 | public void updateObject(int columnIndex, Object x) throws SQLException { 908 | throw new UnsupportedOperationException(); 909 | } 910 | 911 | @Override 912 | public void updateObject(String columnLabel, Object x) throws SQLException { 913 | throw new UnsupportedOperationException(); 914 | } 915 | 916 | @Override 917 | public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException { 918 | throw new UnsupportedOperationException(); 919 | } 920 | 921 | @Override 922 | public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException { 923 | throw new UnsupportedOperationException(); 924 | } 925 | 926 | @Override 927 | public void updateRef(int columnIndex, Ref x) throws SQLException { 928 | throw new UnsupportedOperationException(); 929 | } 930 | 931 | @Override 932 | public void updateRef(String columnLabel, Ref x) throws SQLException { 933 | throw new UnsupportedOperationException(); 934 | } 935 | 936 | @Override 937 | public void updateRow() throws SQLException { 938 | throw new UnsupportedOperationException(); 939 | } 940 | 941 | @Override 942 | public void updateRowId(int columnIndex, RowId x) throws SQLException { 943 | throw new UnsupportedOperationException(); 944 | } 945 | 946 | @Override 947 | public void updateRowId(String columnLabel, RowId x) throws SQLException { 948 | throw new UnsupportedOperationException(); 949 | } 950 | 951 | @Override 952 | public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { 953 | throw new UnsupportedOperationException(); 954 | } 955 | 956 | @Override 957 | public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { 958 | throw new UnsupportedOperationException(); 959 | } 960 | 961 | @Override 962 | public void updateShort(int columnIndex, short x) throws SQLException { 963 | throw new UnsupportedOperationException(); 964 | } 965 | 966 | @Override 967 | public void updateShort(String columnLabel, short x) throws SQLException { 968 | throw new UnsupportedOperationException(); 969 | } 970 | 971 | @Override 972 | public void updateString(int columnIndex, String x) throws SQLException { 973 | throw new UnsupportedOperationException(); 974 | } 975 | 976 | @Override 977 | public void updateString(String columnLabel, String x) throws SQLException { 978 | throw new UnsupportedOperationException(); 979 | } 980 | 981 | @Override 982 | public void updateTime(int columnIndex, Time x) throws SQLException { 983 | throw new UnsupportedOperationException(); 984 | } 985 | 986 | @Override 987 | public void updateTime(String columnLabel, Time x) throws SQLException { 988 | throw new UnsupportedOperationException(); 989 | } 990 | 991 | @Override 992 | public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { 993 | throw new UnsupportedOperationException(); 994 | } 995 | 996 | @Override 997 | public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException { 998 | throw new UnsupportedOperationException(); 999 | } 1000 | 1001 | @Override 1002 | public boolean wasNull() throws SQLException { 1003 | throw new UnsupportedOperationException(); 1004 | } 1005 | } -------------------------------------------------------------------------------- /src/test/java/dev/krynn/sql/KrynnTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import dev.krynn.sql.compiler.Compiler; 20 | import dev.krynn.sql.compiler.data.DataCompiler; 21 | import dev.krynn.sql.impl.compiler.CompilerImpl; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; 25 | import static org.junit.jupiter.api.Assertions.assertTrue; 26 | 27 | public class KrynnTest { 28 | 29 | @Test 30 | void compilerTest() { 31 | Compiler compiler = new CompilerImpl(); 32 | compiler.compile(User.class); 33 | 34 | User build = compiler.build(User.class, new AbstractResultSet()); 35 | 36 | assertEquals("test", build.getTest()); 37 | assertEquals(2115, build.getTest2()); 38 | } 39 | 40 | @Test 41 | void customDataCompilerTest() { 42 | Compiler compiler = new CompilerImpl(); 43 | 44 | SecondUser build = compiler.build(SecondUser.class, new AbstractResultSet()); 45 | 46 | assertEquals("testxd", build.getTest2()); 47 | } 48 | 49 | @Test 50 | void primitivesTest() { 51 | Compiler compiler = new CompilerImpl(); 52 | 53 | ThirdUser user = compiler.build(ThirdUser.class, new AbstractResultSet()); 54 | 55 | assertEquals("test", user.getString()); 56 | assertEquals(2115, user.getPrimitive()); 57 | assertTrue(user.isToo()); 58 | } 59 | 60 | public static class CustomCompiler implements DataCompiler { 61 | 62 | @Override 63 | public String compile(String original) { 64 | return original + "xd"; 65 | } 66 | 67 | @Override 68 | public String decompile(String toDecompile) { 69 | return toDecompile + "xd"; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/dev/krynn/sql/SecondUser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import dev.krynn.sql.annotations.Column; 20 | import dev.krynn.sql.annotations.PrimaryKey; 21 | import dev.krynn.sql.annotations.Table; 22 | 23 | @Table("idk") 24 | public class SecondUser { 25 | 26 | @PrimaryKey 27 | @Column 28 | private Integer test; 29 | 30 | @Column(dataCompiler = KrynnTest.CustomCompiler.class) 31 | private String test2; 32 | 33 | public Integer getTest() { 34 | return test; 35 | } 36 | 37 | public String getTest2() { 38 | return test2; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/dev/krynn/sql/ThirdUser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import dev.krynn.sql.annotations.Column; 20 | import dev.krynn.sql.annotations.PrimaryKey; 21 | import dev.krynn.sql.annotations.Table; 22 | 23 | @Table("testo3") 24 | public class ThirdUser { 25 | 26 | @PrimaryKey 27 | @Column 28 | private int primitive; 29 | 30 | @Column 31 | private boolean too; 32 | 33 | @Column 34 | private String string; 35 | 36 | public ThirdUser(int primitive, boolean too, String string) { 37 | this.primitive = primitive; 38 | this.too = too; 39 | this.string = string; 40 | } 41 | 42 | public ThirdUser() { 43 | } 44 | 45 | public int getPrimitive() { 46 | return primitive; 47 | } 48 | 49 | public boolean isToo() { 50 | return too; 51 | } 52 | 53 | public String getString() { 54 | return string; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/dev/krynn/sql/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Oskarr1239 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dev.krynn.sql; 18 | 19 | import dev.krynn.sql.annotations.Column; 20 | import dev.krynn.sql.annotations.PrimaryKey; 21 | import dev.krynn.sql.annotations.Table; 22 | 23 | @Table("test") 24 | public class User { 25 | 26 | @PrimaryKey 27 | @Column 28 | private String test; 29 | 30 | @Column 31 | private Integer test2; 32 | 33 | public String getTest() { 34 | return test; 35 | } 36 | 37 | public Integer getTest2() { 38 | return test2; 39 | } 40 | } 41 | --------------------------------------------------------------------------------