├── .github └── workflows │ └── release.yml ├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── smartorm-all └── pom.xml ├── smartorm-core ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── nineteen │ └── smartorm │ └── SmartDAO.java ├── smartorm-example ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── nineteen │ │ ├── PersonDAO.java │ │ └── PersonEntity.java │ └── resources │ └── smartorm.properties └── smartorm-generate ├── pom.xml └── src ├── main ├── kotlin │ └── com │ │ └── github │ │ └── nineteen │ │ ├── GptUtils.kt │ │ ├── PromptUtils.kt │ │ ├── SmartConfig.kt │ │ ├── SourceUtils.kt │ │ └── processor │ │ └── SmartORMProcessor.kt └── resources │ └── META-INF │ └── services │ └── javax.annotation.processing.Processor └── test ├── kotlin └── com │ └── github │ └── nineteen │ └── SmartConfigKtTest.kt └── resources └── smartorm.properties /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Publish package to the Maven Central Repository 2 | on: 3 | release: 4 | types: [created] 5 | jobs: 6 | publish: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v3 10 | - name: Set up Maven Central Repository 11 | uses: actions/setup-java@v3.5.0 12 | with: 13 | java-version: '8' 14 | distribution: 'temurin' 15 | server-id: ossrh 16 | server-username: MAVEN_USERNAME 17 | server-password: MAVEN_PASSWORD 18 | gpg-private-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} # Value of the GPG private key to import 19 | gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase 20 | 21 | - name: Publish package 22 | run: mvn clean deploy -Prelease 23 | env: 24 | MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} 25 | MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} 26 | MAVEN_GPG_PASSPHRASE: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} 27 | -------------------------------------------------------------------------------- /.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 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | .idea/ 26 | target/ 27 | .flattened-pom.xml 28 | *.iml 29 | **/.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 1994 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SmartORM 2 | 3 | `SmartORM` is a Java ORM (Object-Relational Mapping) framework that enables you to automatically generate CRUD (Create, 4 | Read, Update, Delete) code at compile time using GPT. With `SmartORM`, you can simply write DAO ( 5 | Data Access Object) interfaces and get complete CRUD operations implemented automatically, while remaining compatible 6 | with the JPA (Java Persistence API) specification. 7 | 8 | ## Features 9 | 10 | - Automatic generation of CRUD code at compile time 11 | - JPA-compliant interface and entity annotations 12 | - Supports multiple database types, such as MySQL, PostgreSQL, SQLite, etc. 13 | 14 | ## Requirements 15 | 16 | - Java 1.8 or above 17 | - An OpenAI API key 18 | 19 | ## Installation 20 | 21 | To use SmartORM in your Java project, you can add the following Maven dependency to your project's pom.xml file: 22 | 23 | ```xml 24 | 25 | 26 | io.github.1994 27 | smartorm-all 28 | 1.0.0.Alpha 29 | 30 | ``` 31 | 32 | > Please note that this project is currently under development and is still in its early preview version. 33 | 34 | ## Usage 35 | 36 | The `@SmartDAO` annotation is a core annotation in `SmartORM`, used to define a DAO (Data Access Object) interface and 37 | its 38 | associated entity class and database connection. 39 | 40 | for example: 41 | 42 | ```java 43 | 44 | @SmartDAO(datasource = "datasource", entity = PersonEntity.class) 45 | public interface PersonDAO { 46 | 47 | int insert(PersonEntity entity); 48 | 49 | PersonEntity getById(Long id); 50 | 51 | int deleteById(Long id); 52 | 53 | List findByGreaterThanBirth(Long birth); 54 | } 55 | 56 | @Entity 57 | @Table(name = "test_person") 58 | @Data 59 | public class PersonEntity { 60 | 61 | private Long id; 62 | 63 | private String name; 64 | 65 | private Long birth; 66 | 67 | private int status; 68 | 69 | } 70 | 71 | ``` 72 | 73 | The `@SmartDAO` annotation takes two parameters: 74 | 75 | - datasource: the name of the database connection to use 76 | - entity: the class of the entity associated with the DAO interface 77 | 78 | In this example, the PersonDAO interface is associated with the PersonEntity class and the datasource database 79 | connection. 80 | 81 | Once you've defined your DAO interface with the `@SmartDAO` annotation, `SmartORM` will automatically generate the 82 | implementation code for you at compile time, providing basic CRUD operations (insert, get by ID, delete by ID) and any 83 | additional methods you define in your DAO interface. 84 | 85 | ### Custom Methods 86 | 87 | In addition to the basic CRUD operations, you can define custom methods in your DAO interface and SmartORM will generate 88 | the implementation code for them as well. For instance, in the example above, we've defined a `findByGreaterThanBirth` 89 | method that retrieves all `PersonEntity` instances whose birth date is greater than a given value. 90 | 91 | ```java 92 | List findByGreaterThanBirth(Long birth); 93 | ``` 94 | 95 | This method will generate the SQL code to retrieve all matching rows from the person table and map them to PersonEntity 96 | instances. 97 | 98 | ## Configuration File 99 | 100 | `SmartORM` can be configured using a `smartorm.properties` file in the resources folder of your Maven project. Here's an 101 | example configuration file: 102 | 103 | ```properties 104 | smartorm.gpt.token= 105 | ## optional proxy 106 | # smartorm.gpt.proxy=localhost:7890 107 | ## optional proxy type 108 | # smartorm.gpt.proxy.type=http or socks 109 | ## optional gpt model default is gpt-3.5-turbo 110 | # smartorm.gpt.model=gpt-3.5-turbo 111 | ## optional default is mysql 112 | # smartorm.dialect=mysql 113 | ``` 114 | 115 | ### GPT Settings 116 | 117 | The following settings are related to GPT: 118 | 119 | - `smartorm.gpt.token`: Your OpenAI API token, which is required for SmartORM to access the GPT API. You can obtain an 120 | API 121 | token by signing up for OpenAI's GPT service. 122 | - `smartorm.gpt.proxy`: Optional proxy server for SmartORM to use when accessing the GPT API. If you need to use a proxy 123 | server, specify the proxy server hostname and port here. 124 | - `smartorm.gpt.proxy`.type: Optional proxy server type for SmartORM to use when accessing the GPT API. If you're using 125 | a 126 | proxy server, specify the proxy server type as either http or socks. 127 | - `smartorm.gpt.model`: Optional GPT model to use for generating code. The default is gpt-3.5-turbo, but you can specify 128 | any 129 | of the GPT models provided by OpenAI. 130 | 131 | ### Database Settings 132 | 133 | The following settings are related to database access: 134 | 135 | - `smartorm.dialect`: Optional database dialect for SmartORM to use. The default is mysql, but you can specify any of 136 | the 137 | supported database dialects, such as postgresql or sqlite. 138 | 139 | ## Example 140 | 141 | [full example](./smartorm-example/README.md) 142 | 143 | ## TODOS 144 | 145 | - [ ] Support for more JPA annotations 146 | Currently, `SmartORM` only supports a limited subset of JPA annotations. Adding 147 | support for more annotations such as `@ManyToOne`, `@OneToMany`, `@OneToOne`, and `@JoinColumn` would make it easier 148 | to work with more complex object models. 149 | 150 | - [ ] Integration with other frameworks: 151 | Integration with other frameworks: Integrating SmartORM with popular Java frameworks such as Mybatis or Hibernate 152 | would make it easier to use it in existing projects and leverage their features and capabilities. 153 | 154 | - [ ] Improved error handling: 155 | `SmartORM` currently lacks detailed error messages and logging, which can make it 156 | difficult to diagnose and fix issues. Implementing more informative error messages and better logging would make it 157 | easier for developers to identify and fix problems. 158 | 159 | - [ ] Improved performance: While SmartORM generates efficient code, there may be opportunities to optimize performance 160 | further. For example, by using batch operations for insert, update, and delete statements, or by implementing a cache 161 | for frequently accessed data. 162 | 163 | - [ ] Better documentation 164 | 165 | - [ ] IDE support 166 | 167 | - [ ] Support for Transactions: 168 | Currently, SmartORM does not have built-in support for transactions. 169 | 170 | ## Contribution 171 | 172 | As you can see from the above Todos, there are still many features to be implemented in `SmartORM`. 173 | 174 | Welcome to contribute to `SmartORM` by submitting pull requests! I appreciate all kinds of contributions, including but 175 | not limited to bug fixes, new features, and documentation improvements. -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | 9 | MIT License 10 | http://www.opensource.org/licenses/mit-license.php 11 | 12 | 13 | 14 | 15 | scm:git:git://github.com/1994/SmartORM.git 16 | scm:git:ssh://github.com/1994/SmartORM.git 17 | https://github.com/1994/SmartORM 18 | 19 | 20 | 21 | 22 | 1994 23 | 1994@users.noreply.github.com 24 | Sonatype 25 | http://www.sonatype.com 26 | 27 | 28 | 29 | 30 | 31 | ossrh 32 | https://s01.oss.sonatype.org/content/repositories/snapshots 33 | 34 | 35 | ossrh 36 | https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ 37 | 38 | 39 | 40 | 41 | 42 | jcenter 43 | JCenter 44 | https://jcenter.bintray.com/ 45 | 46 | 47 | 48 | 49 | 50 | release 51 | 52 | 53 | 54 | org.apache.maven.plugins 55 | maven-gpg-plugin 56 | 3.0.1 57 | 58 | 59 | sign-artifacts 60 | verify 61 | 62 | sign 63 | 64 | 65 | 66 | --pinentry-mode 67 | loopback 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | SmartORM 79 | a Java ORM (Object-Relational Mapping) framework driven by GPT 80 | https://github.com/1994/SmartORM 81 | 82 | io.github.1994 83 | SmartORM 84 | ${revision} 85 | pom 86 | 87 | smartorm-generate 88 | smartorm-core 89 | smartorm-all 90 | 91 | 92 | 93 | UTF-8 94 | 1.8 95 | 1.8 96 | 1.0.0.Alpha 97 | 98 | 99 | 100 | 101 | 102 | io.github.1994 103 | smartorm-core 104 | ${project.version} 105 | 106 | 107 | io.github.1994 108 | smartorm-generate 109 | ${project.version} 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | org.codehaus.mojo 118 | flatten-maven-plugin 119 | 1.3.0 120 | 121 | 122 | oss 123 | 124 | 125 | 126 | 127 | flatten 128 | process-resources 129 | 130 | flatten 131 | 132 | 133 | 134 | 135 | flatten.clean 136 | clean 137 | 138 | clean 139 | 140 | 141 | 142 | 143 | 144 | org.sonatype.plugins 145 | nexus-staging-maven-plugin 146 | 1.6.7 147 | true 148 | 149 | ossrh 150 | https://s01.oss.sonatype.org/ 151 | true 152 | 153 | 154 | 155 | org.apache.maven.plugins 156 | maven-source-plugin 157 | 3.2.1 158 | 159 | 160 | attach-sources 161 | verify 162 | 163 | jar-no-fork 164 | 165 | 166 | 167 | 168 | 169 | org.jetbrains.dokka 170 | dokka-maven-plugin 171 | 1.6.10 172 | 173 | 174 | package 175 | attach-javadocs-dokka 176 | 177 | javadocJar 178 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /smartorm-all/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.1994 8 | SmartORM 9 | ${revision} 10 | 11 | SmartORM 12 | smartorm-all 13 | 14 | 15 | 16 | io.github.1994 17 | smartorm-generate 18 | 19 | 20 | io.github.1994 21 | smartorm-core 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /smartorm-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.1994 8 | SmartORM 9 | ${revision} 10 | 11 | SmartORM 12 | smartorm-core 13 | 14 | 15 | jakarta.persistence 16 | jakarta.persistence-api 17 | 3.1.0 18 | 19 | 20 | -------------------------------------------------------------------------------- /smartorm-core/src/main/java/com/github/nineteen/smartorm/SmartDAO.java: -------------------------------------------------------------------------------- 1 | package com.github.nineteen.smartorm; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface SmartDAO { 11 | 12 | String datasource(); 13 | 14 | Class entity(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /smartorm-example/README.md: -------------------------------------------------------------------------------- 1 | ## SmartORM Example 2 | 3 | This is an example project for using SmartORM, a Java ORM framework that can automatically generate CRUD code at compile 4 | time using GPT. 5 | 6 | ## Prerequisites 7 | 8 | To run this example, you'll need the following: 9 | 10 | - Java 1.8 or above 11 | - An OpenAI API key 12 | - Maven 13 | 14 | ## Getting Started 15 | 16 | 1. Open the `smartorm.properties` file located in `src/main/resources`. Replace `` with your actual 17 | OpenAI API key. 18 | 2. Build the project with Maven: 19 | 20 | ```shell 21 | mvn compile 22 | ``` 23 | 24 | The generated CRUD code will be located in the` target/generated-sources/annotations` directory. You can use this code 25 | to interact with your database. -------------------------------------------------------------------------------- /smartorm-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | io.github.1994 7 | smartorm-example 8 | 1.0.0-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 1.8 13 | 1.8 14 | 15 | 16 | 17 | 18 | io.github.1994 19 | smartorm-all 20 | 1.0.0.Alpha 21 | 22 | 23 | org.springframework.boot 24 | spring-boot 25 | 2.7.3 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-jdbc 30 | 2.7.3 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | 1.18.20 36 | provided 37 | 38 | 39 | 40 | 41 | 42 | 43 | maven-resources-plugin 44 | 3.0.2 45 | 46 | 47 | 48 | src/main/resources 49 | 50 | **/*.properties 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /smartorm-example/src/main/java/com/github/nineteen/PersonDAO.java: -------------------------------------------------------------------------------- 1 | package com.github.nineteen; 2 | 3 | import com.github.nineteen.smartorm.SmartDAO; 4 | 5 | import java.util.List; 6 | 7 | @SmartDAO(datasource = "datasource", entity = PersonEntity.class) 8 | public interface PersonDAO { 9 | 10 | int insert(PersonEntity entity); 11 | 12 | PersonEntity getById(Long id); 13 | 14 | int deleteById(Long id); 15 | List findByGreaterThanBirth(Long birth); 16 | } 17 | -------------------------------------------------------------------------------- /smartorm-example/src/main/java/com/github/nineteen/PersonEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.nineteen; 2 | 3 | 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.Table; 6 | import lombok.Data; 7 | 8 | @Entity 9 | @Table(name = "test_person") 10 | @Data 11 | public class PersonEntity { 12 | 13 | private Long id; 14 | 15 | private String name; 16 | 17 | private Long birth; 18 | 19 | private int status; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /smartorm-example/src/main/resources/smartorm.properties: -------------------------------------------------------------------------------- 1 | smartorm.gpt.token= 2 | 3 | ## optional proxy 4 | # smartorm.gpt.proxy=localhost:7890 5 | 6 | ## optional proxy type 7 | # smartorm.gpt.proxy.type=http or socks 8 | 9 | ## optional gpt model default is gpt-3.5-turbo 10 | # smartorm.gpt.model=gpt-3.5-turbo 11 | 12 | ## optional default is mysql 13 | # smartorm.dialect=mysql -------------------------------------------------------------------------------- /smartorm-generate/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.1994 8 | SmartORM 9 | ${revision} 10 | 11 | SmartORM 12 | smartorm-generate 13 | 14 | 15 | 1.8.10 16 | official 17 | 18 | 19 | 20 | 21 | com.squareup.okhttp3 22 | okhttp 23 | 4.10.0 24 | 25 | 26 | com.google.auto.service 27 | auto-service 28 | 1.0.1 29 | 30 | 31 | com.google.code.gson 32 | gson 33 | 2.10.1 34 | 35 | 36 | org.jetbrains.kotlin 37 | kotlin-stdlib-jdk8 38 | ${kotlin.version} 39 | 40 | 41 | org.jetbrains.kotlin 42 | kotlin-test 43 | ${kotlin.version} 44 | test 45 | 46 | 47 | org.junit.jupiter 48 | junit-jupiter 49 | RELEASE 50 | test 51 | 52 | 53 | 54 | 55 | src/main/kotlin 56 | src/test/kotlin 57 | 58 | 59 | org.jetbrains.kotlin 60 | kotlin-maven-plugin 61 | ${kotlin.version} 62 | 63 | 64 | compile 65 | compile 66 | 67 | compile 68 | 69 | 70 | 71 | test-compile 72 | test-compile 73 | 74 | test-compile 75 | 76 | 77 | 78 | 79 | 1.8 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /smartorm-generate/src/main/kotlin/com/github/nineteen/GptUtils.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen 2 | 3 | import com.google.gson.FieldNamingPolicy 4 | import com.google.gson.GsonBuilder 5 | import okhttp3.HttpUrl 6 | import okhttp3.MediaType.Companion.toMediaTypeOrNull 7 | import okhttp3.OkHttpClient 8 | import okhttp3.Request 9 | import okhttp3.RequestBody.Companion.toRequestBody 10 | import java.util.concurrent.TimeUnit 11 | 12 | 13 | const val ROLE_USER = "user" 14 | const val ROLE_SYSTEM = "system" 15 | const val ROLE_ASSISTANT = "assistant" 16 | 17 | enum class MessageRole(val role: String) { 18 | USER(ROLE_USER), SYSTEM(ROLE_SYSTEM), ASSISTANT(ROLE_ASSISTANT) 19 | } 20 | 21 | 22 | data class CompletionMessage(val role: String, val content: String) 23 | 24 | class MessageBuilder { 25 | var messageRole: MessageRole = MessageRole.USER 26 | var content = "" 27 | fun build(): CompletionMessage = CompletionMessage(messageRole.role, content) 28 | } 29 | 30 | fun message(block: MessageBuilder.() -> Unit): CompletionMessage = MessageBuilder().apply(block).build() 31 | 32 | data class CompletionRequest( 33 | val model: String, val temperature: Double = 0.0, val messages: List 34 | ) 35 | 36 | data class CompletionResponse( 37 | val choices: List, val usage: CompletionUsage 38 | ) 39 | 40 | data class CompletionUsage(val promptTokens: Long, val totalTokens: Long) 41 | data class CompletionChoice(val message: CompletionMessage) 42 | 43 | object GptUtils { 44 | 45 | private val JSON = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create() 46 | 47 | private val CONFIG: GptConfig? = getConfig()?.gptConfig 48 | 49 | private val CLIENT = OkHttpClient.Builder().connectTimeout(5, TimeUnit.MINUTES).writeTimeout(5, TimeUnit.MINUTES) 50 | .readTimeout(5, TimeUnit.MINUTES).callTimeout(5, TimeUnit.MINUTES).proxy(CONFIG?.proxy).build() 51 | 52 | private val HTTP_URL = 53 | HttpUrl.Builder().scheme("https").host("api.openai.com").addPathSegment("v1").addPathSegment("chat") 54 | .addPathSegment("completions").build() 55 | 56 | private val REQUEST = Request.Builder().apply { 57 | this.addHeader("Content-Type", "application/json") 58 | this.addHeader("Authorization", "Bearer ${CONFIG?.token}") 59 | } 60 | 61 | fun completion(userContent: String, systemContent: String): String? { 62 | if ((CONFIG == null) || CONFIG.token.isBlank()) { 63 | return null 64 | } 65 | 66 | val completionRequest = CompletionRequest(model = CONFIG.model, messages = listOf(message { 67 | messageRole = MessageRole.USER 68 | content = userContent 69 | }, message { 70 | messageRole = MessageRole.SYSTEM 71 | content = systemContent 72 | })) 73 | 74 | val toRequestBody = JSON.toJson(completionRequest).toRequestBody("application/json".toMediaTypeOrNull()) 75 | val build = REQUEST.post(toRequestBody).url(HTTP_URL).build() 76 | 77 | val result: String = CLIENT.newCall(build).execute().use { 78 | it.body?.string() 79 | } ?: return null 80 | 81 | val r = JSON.fromJson(result, CompletionResponse::class.java) 82 | return r.choices[0].message.content 83 | } 84 | } -------------------------------------------------------------------------------- /smartorm-generate/src/main/kotlin/com/github/nineteen/PromptUtils.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen 2 | 3 | import javax.annotation.processing.ProcessingEnvironment 4 | import javax.lang.model.element.Element 5 | 6 | data class PromptContext( 7 | val smartConfig: SmartConfig, 8 | val dao: Element, 9 | val entity: Element, 10 | val processingEnvironment: ProcessingEnvironment, 11 | val datasource: String, 12 | ) 13 | 14 | object PromptUtils { 15 | 16 | 17 | fun getPrompt(context: PromptContext): String { 18 | return "Please generate a Spring JDBC implementation for the ${context.dao.simpleName} interface based on the JPA specification. The implementation should use a ${context.smartConfig.databaseConfig.dialect} database and the datasource bean named '${context.datasource}' for connecting to the database.\n" + 19 | "Here are the source codes for the ${context.dao.simpleName} interface and the corresponding entity class: The `${context.dao.simpleName}` source code: ${ 20 | SourceUtils.readSource( 21 | context.dao, 22 | processorEnv = context.processingEnvironment 23 | ) 24 | }\n" + 25 | "It's entity class `${context.entity.simpleName}` source code: ${ 26 | SourceUtils.readSource( 27 | context.entity, 28 | processorEnv = context.processingEnvironment 29 | ) 30 | }\n" + 31 | "Please return only the generated Java code for the ${context.dao.simpleName} implementation without any explanation or comments." 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /smartorm-generate/src/main/kotlin/com/github/nineteen/SmartConfig.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen 2 | 3 | import java.io.InputStream 4 | import java.net.InetSocketAddress 5 | import java.net.Proxy 6 | import java.net.Proxy.Type 7 | import java.util.* 8 | 9 | 10 | data class GptConfig(val token: String, val model: String = "gpt-3.5-turbo", val proxy: Proxy?) 11 | data class DatabaseConfig(val dialect: String) 12 | data class SmartConfig(val gptConfig: GptConfig, val databaseConfig: DatabaseConfig) 13 | 14 | const val CONFIG_FILE = "smartorm.properties" 15 | 16 | fun getConfig(): SmartConfig? { 17 | val p = Properties().apply { 18 | val resourceAsStream: InputStream = 19 | SmartConfig::javaClass.javaClass.classLoader.getResourceAsStream(CONFIG_FILE)!! 20 | this.load(resourceAsStream) 21 | } 22 | val token = p.getProperty("smartorm.gpt.token") ?: return null 23 | val model = p.getProperty("smartorm.gpt.model", "gpt-3.5-turbo") 24 | val proxyStr = p.getProperty("smartorm.gpt.proxy") 25 | var proxy: Proxy? = null 26 | if (proxyStr?.isNotBlank() == true) { 27 | var type: Type = Type.SOCKS 28 | val proxyType = p.getProperty("smartorm.gpt.proxy.type", "socks") 29 | when (proxyType) { 30 | "http" -> { 31 | type = Type.HTTP 32 | } 33 | 34 | "socks" -> { 35 | type = Type.SOCKS 36 | } 37 | } 38 | val split = proxyStr.split(":") 39 | var port = 80 40 | if (split.size > 1) { 41 | port = split[1].toInt() 42 | } 43 | proxy = Proxy(type, InetSocketAddress(split[0], port)) 44 | } 45 | val dialect = p.getProperty("smartorm.dialect", "mysql") 46 | return SmartConfig(GptConfig(token = token, model = model, proxy = proxy), databaseConfig = DatabaseConfig(dialect)) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /smartorm-generate/src/main/kotlin/com/github/nineteen/SourceUtils.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen 2 | 3 | import com.google.common.io.Files 4 | import java.io.File 5 | import java.nio.charset.Charset 6 | import javax.annotation.processing.ProcessingEnvironment 7 | import javax.lang.model.element.Element 8 | import javax.lang.model.type.TypeMirror 9 | import javax.tools.StandardLocation 10 | 11 | object SourceUtils { 12 | 13 | fun readSource(element: Element, processorEnv: ProcessingEnvironment): String { 14 | val path = processorEnv.filer.getResource( 15 | StandardLocation.SOURCE_PATH, 16 | processorEnv.elementUtils.getPackageOf(element).toString(), 17 | "${element.simpleName}.java" 18 | ).toUri() 19 | 20 | // todo read source from compiler 21 | return Files.readLines(File(path), Charset.defaultCharset()).joinToString("") 22 | } 23 | 24 | fun readSource(typeMirror: TypeMirror, processingEnvironment: ProcessingEnvironment): String { 25 | val typeElement = processingEnvironment.elementUtils.getTypeElement( 26 | processingEnvironment.typeUtils.erasure(typeMirror).toString() 27 | ) 28 | 29 | return readSource(typeElement, processingEnvironment) 30 | } 31 | } -------------------------------------------------------------------------------- /smartorm-generate/src/main/kotlin/com/github/nineteen/processor/SmartORMProcessor.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen.processor 2 | 3 | import com.github.nineteen.* 4 | import com.google.auto.common.AnnotationMirrors 5 | import com.google.auto.common.BasicAnnotationProcessor 6 | import com.google.common.collect.ImmutableSetMultimap 7 | import javax.annotation.processing.* 8 | import javax.lang.model.SourceVersion 9 | import javax.lang.model.element.Element 10 | import javax.lang.model.element.ElementKind 11 | 12 | const val SMART_DAO = "com.github.nineteen.smartorm.SmartDAO" 13 | 14 | @SupportedAnnotationTypes( 15 | SMART_DAO 16 | ) 17 | @SupportedSourceVersion(SourceVersion.RELEASE_8) 18 | open class SmartORMProcessor : BasicAnnotationProcessor() { 19 | 20 | override fun steps(): MutableIterable { 21 | return mutableListOf(SmartORMStep(processingEnv)) 22 | } 23 | 24 | class SmartORMStep(private val processorEnv: ProcessingEnvironment) : Step { 25 | 26 | override fun annotations(): MutableSet { 27 | return mutableSetOf(SMART_DAO) 28 | } 29 | 30 | override fun process(elementsByAnnotation: ImmutableSetMultimap?): MutableSet { 31 | val smartDaos = elementsByAnnotation?.get(SMART_DAO) 32 | smartDaos?.filter { it.kind == ElementKind.INTERFACE && processorEnv.elementUtils.getAllAnnotationMirrors(it).size > 0 } 33 | ?.forEach { 34 | val allAnnotationMirrors = processorEnv.elementUtils.getAllAnnotationMirrors(it) 35 | val datasourceValue = 36 | AnnotationMirrors.getAnnotationValue(allAnnotationMirrors.first(), "datasource") 37 | val entityValue = 38 | AnnotationMirrors.getAnnotationValue(allAnnotationMirrors.first(), "entity") 39 | 40 | val entity = processorEnv.elementUtils.getTypeElement(entityValue.value.toString()) 41 | 42 | val prompt = PromptUtils.getPrompt( 43 | PromptContext( 44 | getConfig()!!, 45 | it, 46 | entity, 47 | processorEnv, 48 | datasourceValue.toString() 49 | ) 50 | ) 51 | val completion = GptUtils.completion("", prompt) 52 | 53 | val javaFile = completion?.substringAfter("```java\n")?.substringBefore("```") 54 | val packageName = processorEnv.elementUtils.getPackageOf(it).toString() 55 | 56 | if (javaFile?.isNotBlank() == true) { 57 | processorEnv.filer.createSourceFile("${packageName}.${it.simpleName}Impl").openWriter() 58 | .use { 59 | it.write(javaFile) 60 | } 61 | } 62 | } 63 | return mutableSetOf() 64 | } 65 | 66 | } 67 | } -------------------------------------------------------------------------------- /smartorm-generate/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.github.nineteen.processor.SmartORMProcessor -------------------------------------------------------------------------------- /smartorm-generate/src/test/kotlin/com/github/nineteen/SmartConfigKtTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.nineteen 2 | 3 | import org.junit.jupiter.api.Test 4 | 5 | class SmartConfigKtTest { 6 | 7 | @Test 8 | fun get() { 9 | val c = getConfig() 10 | assert(c?.gptConfig?.token.equals("demo.token.asd")) 11 | } 12 | } -------------------------------------------------------------------------------- /smartorm-generate/src/test/resources/smartorm.properties: -------------------------------------------------------------------------------- 1 | smartorm.gtp.token=demo.token.asd --------------------------------------------------------------------------------