├── .gitignore ├── COPYING ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle └── src │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── org │ │ └── mariotaku │ │ └── sqliteqb │ │ └── library │ │ ├── AllColumns.java │ │ ├── ArgsArray.java │ │ ├── Columns.java │ │ ├── Constraint.java │ │ ├── DataType.java │ │ ├── Expression.java │ │ ├── Join.java │ │ ├── NewColumn.java │ │ ├── OnConflict.java │ │ ├── OrderBy.java │ │ ├── RawItemArray.java │ │ ├── RawSQLLang.java │ │ ├── SQLConstants.java │ │ ├── SQLFunction.java │ │ ├── SQLFunctions.java │ │ ├── SQLLang.java │ │ ├── SQLQuery.java │ │ ├── SQLQueryBuilder.java │ │ ├── SQLQueryException.java │ │ ├── Selectable.java │ │ ├── SetValue.java │ │ ├── Table.java │ │ ├── Tables.java │ │ ├── Utils.java │ │ └── query │ │ ├── IBuilder.java │ │ ├── SQLAlterTableQuery.java │ │ ├── SQLCreateIndexQuery.java │ │ ├── SQLCreateTableQuery.java │ │ ├── SQLCreateTriggerQuery.java │ │ ├── SQLCreateViewQuery.java │ │ ├── SQLDeleteQuery.java │ │ ├── SQLDropIndexQuery.java │ │ ├── SQLDropQuery.java │ │ ├── SQLDropTableQuery.java │ │ ├── SQLDropTriggerQuery.java │ │ ├── SQLDropViewQuery.java │ │ ├── SQLInsertQuery.java │ │ ├── SQLSelectQuery.java │ │ ├── SQLUpdateQuery.java │ │ └── SQLWithSelectQuery.java │ └── test │ └── java │ └── org │ └── mariotaku │ └── sqliteqb │ └── library │ └── ExpressionTest.java ├── sample ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── org │ └── mariotaku │ └── sqliteqb │ └── sample │ └── Main.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | /build 3 | 4 | # Local configuration file (sdk path, etc) 5 | local.properties 6 | 7 | # Gradle generated files 8 | .gradle/ 9 | 10 | # Signing files 11 | .signing/ 12 | 13 | # User-specific configurations 14 | /.idea 15 | *.iml 16 | 17 | # OS-specific files 18 | .DS_Store 19 | .DS_Store? 20 | ._* 21 | .Spotlight-V100 22 | .Trashes 23 | ehthumbs.db 24 | Thumbs.db 25 | 26 | # Private files 27 | /signing.properties -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQLiteQB 2 | 3 | Make a contract with me and become a magical coder! 4 | 5 | Simplify & modelize your SQLite query, with fluent interface. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariotaku/SQLiteQB/115d510d2b32a1e900977ac6c47e929aa5d6b7fd/build.gradle -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 mariotaku 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 | # Project-wide Gradle settings. 18 | 19 | # IDE (e.g. Android Studio) users: 20 | # Gradle settings configured through the IDE *will override* 21 | # any settings specified in this file. 22 | 23 | # For more details on how to configure your build environment visit 24 | # http://www.gradle.org/docs/current/userguide/build_environment.html 25 | 26 | # Specifies the JVM arguments used for the daemon process. 27 | # The setting is particularly useful for tweaking memory settings. 28 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 29 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 30 | 31 | # When configured, Gradle will run in incubating parallel mode. 32 | # This option should only be used with decoupled projects. More details, visit 33 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 34 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariotaku/SQLiteQB/115d510d2b32a1e900977ac6c47e929aa5d6b7fd/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Nov 08 17:17:51 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 | apply plugin: 'java' 18 | apply plugin: 'maven' 19 | 20 | version = '0.9.18' 21 | 22 | repositories { 23 | mavenLocal() 24 | jcenter() 25 | } 26 | 27 | dependencies { 28 | testCompile 'junit:junit:4.12' 29 | testCompile 'org.tmatesoft.sqljet:sqljet:1.1.10' 30 | } 31 | 32 | sourceCompatibility = JavaVersion.VERSION_1_7 33 | targetCompatibility = JavaVersion.VERSION_1_7 34 | 35 | task sourcesJar(type: Jar, dependsOn: classes) { 36 | classifier = 'sources' 37 | from sourceSets.main.allSource 38 | } 39 | 40 | task javadocJar(type: Jar, dependsOn: javadoc) { 41 | classifier = 'javadoc' 42 | } 43 | 44 | artifacts { 45 | archives sourcesJar 46 | archives javadocJar 47 | } 48 | 49 | uploadArchives { 50 | repositories { 51 | mavenInstaller { 52 | pom.version = version 53 | pom.groupId = 'com.github.mariotaku' 54 | pom.artifactId = rootProject.name 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/AllColumns.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class AllColumns implements Selectable { 20 | 21 | private final String table; 22 | 23 | public AllColumns() { 24 | this(null); 25 | } 26 | 27 | public AllColumns(final String table) { 28 | this.table = table; 29 | } 30 | 31 | @Override 32 | public String getSQL() { 33 | return table != null ? table + ".*" : "*"; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/ArgsArray.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library; 2 | 3 | /** 4 | * Created by mariotaku on 15/8/12. 5 | */ 6 | public class ArgsArray implements Selectable { 7 | private final String sql; 8 | 9 | public ArgsArray(int size) { 10 | sql = Utils.toStringForSQL(size); 11 | } 12 | 13 | @Override 14 | public String getSQL() { 15 | return sql; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Columns.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class Columns implements Selectable { 20 | 21 | private final AbsColumn[] columns; 22 | 23 | public Columns(String... columns) { 24 | this(Columns.fromStrings(columns)); 25 | } 26 | 27 | public Columns(final AbsColumn... columns) { 28 | this.columns = columns; 29 | } 30 | 31 | private static Column[] fromStrings(String... columnsString) { 32 | final Column[] columns = new Column[columnsString.length]; 33 | for (int i = 0, j = columnsString.length; i < j; i++) { 34 | columns[i] = new Column(columnsString[i]); 35 | } 36 | return columns; 37 | } 38 | 39 | @Override 40 | public String getSQL() { 41 | return Utils.toString(columns, ',', true); 42 | } 43 | 44 | public abstract static class AbsColumn implements Selectable { 45 | 46 | } 47 | 48 | public static class AllColumn extends AbsColumn { 49 | 50 | private final Table table; 51 | 52 | public AllColumn() { 53 | this(null); 54 | } 55 | 56 | public AllColumn(final Table table) { 57 | this.table = table; 58 | } 59 | 60 | @Override 61 | public String getSQL() { 62 | return table != null ? table.getSQL() + ".*" : "*"; 63 | } 64 | 65 | } 66 | 67 | public static class Column extends AbsColumn { 68 | 69 | private final String sql, alias; 70 | 71 | public Column(final String sql) { 72 | this(sql, null); 73 | } 74 | 75 | public Column(final String sql, final String alias) { 76 | this.sql = sql; 77 | this.alias = alias; 78 | } 79 | 80 | public Column(final SQLLang sql, final String alias) { 81 | this.sql = sql.getSQL(); 82 | this.alias = alias; 83 | } 84 | 85 | public Column(final Table table, final String columnName) { 86 | this(table, columnName, null); 87 | } 88 | 89 | public Column(final Table table, final String columnName, final String alias) { 90 | this(table.getSQL() + "." + columnName, alias); 91 | } 92 | 93 | @Override 94 | public String getSQL() { 95 | if (alias == null) return sql; 96 | return sql + " AS " + alias; 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Constraint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 15/3/30. 21 | */ 22 | public class Constraint implements SQLLang { 23 | private final String name; 24 | private final String type; 25 | private final SQLQuery constraint; 26 | 27 | public Constraint(String name, String type, SQLQuery constraint) { 28 | this.name = name; 29 | this.type = type; 30 | this.constraint = constraint; 31 | } 32 | 33 | @Override 34 | public String getSQL() { 35 | final StringBuilder sb = new StringBuilder(); 36 | if (name != null) { 37 | sb.append("CONSTRAINT "); 38 | sb.append(name); 39 | sb.append(" "); 40 | } 41 | sb.append(type); 42 | sb.append(" "); 43 | sb.append(constraint.getSQL()); 44 | return sb.toString(); 45 | } 46 | 47 | public static Constraint unique(String name, Columns columns, OnConflict onConflict) { 48 | return new Constraint(name, "UNIQUE", new ColumnConflictConstaint(columns, onConflict)); 49 | } 50 | 51 | public static Constraint unique(Columns columns, OnConflict onConflict) { 52 | return unique(null, columns, onConflict); 53 | } 54 | 55 | private static final class ColumnConflictConstaint implements SQLQuery { 56 | 57 | private final Columns columns; 58 | private final OnConflict onConflict; 59 | 60 | public ColumnConflictConstaint(Columns columns, OnConflict onConflict) { 61 | this.columns = columns; 62 | this.onConflict = onConflict; 63 | } 64 | 65 | @Override 66 | public String getSQL() { 67 | final StringBuilder sb = new StringBuilder(); 68 | sb.append("("); 69 | sb.append(columns.getSQL()); 70 | sb.append(") "); 71 | sb.append("ON CONFLICT "); 72 | sb.append(onConflict.getAction()); 73 | return sb.toString(); 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/DataType.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library; 2 | 3 | /** 4 | * Created by mariotaku on 15/7/16. 5 | */ 6 | public abstract class DataType implements SQLLang { 7 | 8 | public static final String NULL = NULL().getSQL(); 9 | public static final String REAL = REAL().getSQL(); 10 | public static final String INTEGER = INTEGER().getSQL(); 11 | public static final String INTEGER_PRIMARY_KEY = INTEGER_PRIMARY_KEY().getSQL(); 12 | public static final String TEXT = TEXT().getSQL(); 13 | public static final String BLOB = BLOB().getSQL(); 14 | 15 | public static DataType NULL() { 16 | return new DataType() { 17 | @Override 18 | public String getSQL() { 19 | return "NULL"; 20 | } 21 | }; 22 | } 23 | 24 | public static DataType REAL() { 25 | return new DataType() { 26 | @Override 27 | public String getSQL() { 28 | return "REAL"; 29 | } 30 | }; 31 | } 32 | 33 | public static DataType INTEGER() { 34 | return new DataType() { 35 | @Override 36 | public String getSQL() { 37 | return "INTEGER"; 38 | } 39 | }; 40 | } 41 | 42 | public static DataType INTEGER_PRIMARY_KEY() { 43 | return new DataType() { 44 | @Override 45 | public String getSQL() { 46 | return "INTEGER PRIMARY KEY"; 47 | } 48 | }; 49 | } 50 | 51 | public static DataType TEXT() { 52 | return new DataType() { 53 | @Override 54 | public String getSQL() { 55 | return "TEXT"; 56 | } 57 | }; 58 | } 59 | 60 | public static DataType BLOB() { 61 | return new DataType() { 62 | @Override 63 | public String getSQL() { 64 | return "BLOB"; 65 | } 66 | }; 67 | } 68 | 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Expression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | import org.mariotaku.sqliteqb.library.Columns.Column; 20 | 21 | public class Expression implements SQLLang { 22 | private final String expr; 23 | 24 | public Expression(final String expr) { 25 | this.expr = expr; 26 | } 27 | 28 | public Expression(SQLLang lang) { 29 | this(lang.getSQL()); 30 | } 31 | 32 | public static Expression and(final Expression... expressions) { 33 | return new Expression(toExpr(expressions, "AND")); 34 | } 35 | 36 | public static Expression equals(final Column l, final Column r) { 37 | return new Expression(l.getSQL() + " = " + r.getSQL()); 38 | } 39 | 40 | public static Expression equals(final Column l, final Selectable r) { 41 | return new Expression(l.getSQL() + " = (" + r.getSQL() + ")"); 42 | } 43 | 44 | public static Expression equals(final String l, final Selectable r) { 45 | return new Expression(l + " = (" + r.getSQL() + ")"); 46 | } 47 | 48 | public static Expression equals(final Column l, final Number r) { 49 | return new Expression(l.getSQL() + " = " + r); 50 | } 51 | 52 | public static Expression equals(final Column l, final String r) { 53 | return new Expression(l.getSQL() + " = " + r); 54 | } 55 | 56 | public static Expression equals(final String l, final Number r) { 57 | return new Expression(l + " = " + r); 58 | } 59 | 60 | public static Expression greaterThan(final String l, final Number r) { 61 | return new Expression(l + " > " + r); 62 | } 63 | 64 | public static Expression greaterThan(final SQLLang l, final Number r) { 65 | return new Expression(l.getSQL() + " > " + r); 66 | } 67 | 68 | public static Expression greaterThan(final SQLLang l, final SQLLang r) { 69 | return new Expression(l.getSQL() + " > (" + r.getSQL() + ")"); 70 | } 71 | 72 | public static Expression greaterThanArgs(final String l) { 73 | return new Expression(l + " > ?"); 74 | } 75 | 76 | public static Expression greaterThanArgs(Column column) { 77 | return new Expression(column.getSQL() + " > ?"); 78 | } 79 | 80 | public static Expression greaterEquals(final String l, final Number r) { 81 | return new Expression(l + " >= " + r); 82 | } 83 | 84 | public static Expression greaterEqualsArgs(final String l) { 85 | return new Expression(l + " >= ?"); 86 | } 87 | 88 | public static Expression greaterEqualsArgs(final Column column) { 89 | return new Expression(column.getSQL() + " >= ?"); 90 | } 91 | 92 | public static Expression lesserEquals(final String l, final Number r) { 93 | return new Expression(l + " <= " + r); 94 | } 95 | 96 | public static Expression lesserEqualsArgs(final String l) { 97 | return new Expression(l + " <= ?"); 98 | } 99 | 100 | public static Expression lesserEqualsArgs(final Column column) { 101 | return new Expression(column.getSQL() + " <= ?"); 102 | } 103 | 104 | public static Expression lesserThan(final String l, final Number r) { 105 | return new Expression(l + " < " + r); 106 | } 107 | 108 | public static Expression lesserThan(final SQLLang l, final Number r) { 109 | return new Expression(l.getSQL() + " < " + r); 110 | } 111 | 112 | public static Expression lesserThanArgs(final String l) { 113 | return new Expression(l + " < ?"); 114 | } 115 | 116 | public static Expression lesserThanArgs(final Column column) { 117 | return new Expression(column.getSQL() + " < ?"); 118 | } 119 | 120 | public static Expression lesserThan(final SQLLang l, final SQLLang r) { 121 | return new Expression(l.getSQL() + " < (" + r.getSQL() + ")"); 122 | } 123 | 124 | public static Expression in(final Column column, final Selectable in) { 125 | return new Expression(column.getSQL() + " IN(" + in.getSQL() + ")"); 126 | } 127 | 128 | public static Expression notEquals(final String l, final Number r) { 129 | return new Expression(l + " != " + r); 130 | } 131 | 132 | public static Expression notEquals(final String l, final String r) { 133 | return new Expression(l + " != " + r); 134 | } 135 | 136 | public static Expression isNot(final String l, final String r) { 137 | return new Expression(l + " IS NOT " + r); 138 | } 139 | 140 | public static Expression isNot(final String l, final Number r) { 141 | return new Expression(l + " IS NOT " + r); 142 | } 143 | 144 | public static Expression isNotArgs(final String l) { 145 | return new Expression(l + " IS NOT ?"); 146 | } 147 | 148 | public static Expression notIn(final Column column, final Selectable in) { 149 | return new Expression(column.getSQL() + " NOT IN(" + in.getSQL() + ")"); 150 | } 151 | 152 | public static Expression notInArgs(String l, int argsSize) { 153 | return notInArgs(new Column(l), argsSize); 154 | } 155 | 156 | public static Expression notInArgs(Column c, int argsSize) { 157 | return notIn(c, new ArgsArray(argsSize)); 158 | } 159 | 160 | public static Expression notNull(final Column column) { 161 | return new Expression(column.getSQL() + " NOT NULL"); 162 | } 163 | 164 | public static Expression or(final Expression... expressions) { 165 | return new Expression(toExpr(expressions, "OR")); 166 | } 167 | 168 | private static String toExpr(final Expression[] array, final String token) { 169 | final StringBuilder builder = new StringBuilder(); 170 | builder.append('('); 171 | final int length = array.length; 172 | for (int i = 0; i < length; i++) { 173 | if (i > 0) { 174 | builder.append(" "); 175 | builder.append(token); 176 | builder.append(" "); 177 | } 178 | builder.append(array[i].getSQL()); 179 | } 180 | builder.append(')'); 181 | return builder.toString(); 182 | } 183 | 184 | public static Expression equalsArgs(String l) { 185 | return new Expression(l + " = ?"); 186 | } 187 | 188 | public static Expression equalsArgs(Column l) { 189 | return new Expression(l.getSQL() + " = ?"); 190 | } 191 | 192 | public static Expression notEqualsArgs(String l) { 193 | return new Expression(l + " != ?"); 194 | } 195 | 196 | public static Expression notEqualsArgs(Column l) { 197 | return new Expression(l.getSQL() + " != ?"); 198 | } 199 | 200 | public static Expression inArgs(String l, int argsSize) { 201 | return inArgs(new Column(l), argsSize); 202 | } 203 | 204 | public static Expression inArgs(Column c, int argsSize) { 205 | return in(c, new ArgsArray(argsSize)); 206 | } 207 | 208 | public static Expression isNull(Column column) { 209 | return new Expression(column.getSQL() + " IS NULL"); 210 | } 211 | 212 | public static Expression greaterThan(Column column1, Column column2) { 213 | return new Expression(column1.getSQL() + " > " + column2.getSQL()); 214 | } 215 | 216 | public static Expression like(final Column column, final SQLLang expression) { 217 | return new Expression(column.getSQL() + " LIKE " + expression.getSQL()); 218 | } 219 | 220 | public static Expression likeRaw(final Column column, final String pattern) { 221 | return new Expression(column.getSQL() + " LIKE " + pattern); 222 | } 223 | 224 | public static Expression likeRaw(final Column column, final String pattern, final String escape) { 225 | return new Expression(column.getSQL() + " LIKE " + pattern + " ESCAPE '" + escape + "'"); 226 | } 227 | 228 | public static Expression likeArgs(final Column column) { 229 | return new Expression(column.getSQL() + " LIKE ?"); 230 | } 231 | 232 | public static Expression likeEscapeArgs(final Column column) { 233 | return new Expression(column.getSQL() + " LIKE ? ESCAPE ?"); 234 | } 235 | 236 | public static Expression notLike(final Column column, final SQLLang expression) { 237 | return new Expression(column.getSQL() + " NOT LIKE " + expression.getSQL()); 238 | } 239 | 240 | public static Expression notLikeRaw(final Column column, final String pattern) { 241 | return new Expression(column.getSQL() + " NOT LIKE " + pattern); 242 | } 243 | 244 | public static Expression notLikeRaw(final Column column, final String pattern, final String escape) { 245 | return new Expression(column.getSQL() + " NOT LIKE " + pattern + " ESCAPE '" + escape + "'"); 246 | } 247 | 248 | public static Expression notLikeArgs(final Column column) { 249 | return new Expression(column.getSQL() + " NOT LIKE ?"); 250 | } 251 | 252 | public static Expression notLikeEscapeArgs(final Column column) { 253 | return new Expression(column.getSQL() + " NOT LIKE ? ESCAPE ?"); 254 | } 255 | 256 | @Override 257 | public String getSQL() { 258 | return expr; 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Join.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 15/1/12. 21 | */ 22 | public class Join implements SQLLang { 23 | 24 | private final boolean natural; 25 | private final Operation operation; 26 | private final Selectable source; 27 | private final Expression on; 28 | 29 | public Join(boolean natural, Operation operation, Selectable source, Expression on) { 30 | this.natural = natural; 31 | this.operation = operation; 32 | this.source = source; 33 | this.on = on; 34 | } 35 | 36 | @Override 37 | public String getSQL() { 38 | if (operation == null) throw new IllegalArgumentException("operation can't be null!"); 39 | if (source == null) throw new IllegalArgumentException("source can't be null!"); 40 | final StringBuilder builder = new StringBuilder(); 41 | if (natural) { 42 | builder.append("NATURAL "); 43 | } 44 | builder.append(operation.getSQL()); 45 | builder.append(" JOIN "); 46 | builder.append(source.getSQL()); 47 | if (on != null) { 48 | builder.append(" ON "); 49 | builder.append(on.getSQL()); 50 | } 51 | return builder.toString(); 52 | } 53 | 54 | public enum Operation implements SQLLang { 55 | LEFT("LEFT"), LEFT_OUTER("LEFT OUTER"), INNER("INNER"), CROSS("CROSS"); 56 | private final String op; 57 | 58 | Operation(String op) { 59 | this.op = op; 60 | } 61 | 62 | @Override 63 | public String getSQL() { 64 | return op; 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/NewColumn.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class NewColumn implements SQLLang { 20 | 21 | private final String name; 22 | private final String type; 23 | 24 | public NewColumn(final String name, final String type) { 25 | this.name = name; 26 | this.type = type; 27 | } 28 | 29 | public static NewColumn[] createNewColumns(final String[] colNames, final String[] colTypes) { 30 | if (colNames == null || colTypes == null || colNames.length != colTypes.length) 31 | throw new IllegalArgumentException("length of columns and types not match."); 32 | final NewColumn[] newColumns = new NewColumn[colNames.length]; 33 | for (int i = 0, j = colNames.length; i < j; i++) { 34 | newColumns[i] = new NewColumn(colNames[i], colTypes[i]); 35 | } 36 | return newColumns; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | @Override 44 | public String getSQL() { 45 | if (name == null || type == null) 46 | throw new NullPointerException("name and type must not be null!"); 47 | return name + " " + type; 48 | } 49 | 50 | public String getType() { 51 | return type; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/OnConflict.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 14-8-7. 21 | */ 22 | public enum OnConflict { 23 | ROLLBACK("ROLLBACK"), ABORT("ABORT"), REPLACE("REPLACE"), FAIL("FAIL"), IGNORE("IGNORE"); 24 | private final String action; 25 | 26 | OnConflict(final String action) { 27 | this.action = action; 28 | } 29 | 30 | public String getAction() { 31 | return action; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/OrderBy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | 20 | public class OrderBy implements SQLLang { 21 | 22 | private final String[] orderBy; 23 | private final boolean[] ascending; 24 | 25 | public OrderBy(final String[] orderBy, final boolean[] ascending) { 26 | this.orderBy = orderBy; 27 | this.ascending = ascending; 28 | } 29 | 30 | public OrderBy(final Columns.Column[] orderBy, final boolean[] ascending) { 31 | this.orderBy = Utils.toStringArray(orderBy); 32 | this.ascending = ascending; 33 | } 34 | 35 | public OrderBy(final String... orderBy) { 36 | this(orderBy, null); 37 | } 38 | 39 | public OrderBy(final String orderBy, final boolean ascending) { 40 | this.orderBy = new String[]{orderBy}; 41 | this.ascending = new boolean[]{ascending}; 42 | } 43 | 44 | @Override 45 | public String getSQL() { 46 | final StringBuilder sb = new StringBuilder(); 47 | for (int i = 0, j = orderBy.length; i < j; i++) { 48 | if (i > 0) { 49 | sb.append(", "); 50 | } 51 | sb.append(orderBy[i]); 52 | if (ascending != null) { 53 | sb.append(ascending[i] ? " ASC" : " DESC"); 54 | } 55 | } 56 | return sb.toString(); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/RawItemArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class RawItemArray implements Selectable { 20 | 21 | private final Object[] array; 22 | 23 | public RawItemArray(final long[] array) { 24 | final Long[] converted = new Long[array.length]; 25 | for (int i = 0, j = array.length; i < j; i++) { 26 | converted[i] = array[i]; 27 | } 28 | this.array = converted; 29 | } 30 | 31 | public RawItemArray(final Object[] array) { 32 | this.array = array; 33 | } 34 | 35 | public RawItemArray(final int[] array) { 36 | final Integer[] converted = new Integer[array.length]; 37 | for (int i = 0, j = array.length; i < j; i++) { 38 | converted[i] = array[i]; 39 | } 40 | this.array = converted; 41 | } 42 | 43 | @Override 44 | public String getSQL() { 45 | return Utils.toString(array, ',', true); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/RawSQLLang.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 15/6/24. 21 | */ 22 | public final class RawSQLLang implements SQLLang, Selectable { 23 | 24 | private final String statement; 25 | 26 | public RawSQLLang(String statement) { 27 | this.statement = statement; 28 | } 29 | 30 | @Override 31 | public String getSQL() { 32 | return statement; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLConstants.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library; 2 | 3 | /** 4 | * Created by mariotaku on 15/11/8. 5 | */ 6 | public class SQLConstants { 7 | 8 | public static SQLLang NULL = new RawSQLLang("NULL"); 9 | public static SQLLang EMPTY = new RawSQLLang("''"); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLFunction.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library; 2 | 3 | /** 4 | * Created by mariotaku on 16/2/25. 5 | */ 6 | public class SQLFunction implements SQLLang, Selectable { 7 | private final String sql; 8 | 9 | public SQLFunction(String sql) { 10 | this.sql = sql; 11 | } 12 | 13 | @Override 14 | public String getSQL() { 15 | return sql; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLFunctions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class SQLFunctions { 20 | 21 | public static String SUM(final String val) { 22 | return "SUM (" + val + ")"; 23 | } 24 | 25 | public static SQLFunction SUM(final Selectable val) { 26 | return new SQLFunction("SUM (" + val.getSQL() + ")"); 27 | } 28 | 29 | public static String MAX(final String val) { 30 | return "MAX (" + val + ")"; 31 | } 32 | 33 | public static SQLFunction MAX(final Selectable val) { 34 | return new SQLFunction("MAX (" + val.getSQL() + ")"); 35 | } 36 | 37 | public static String MIN(final String val) { 38 | return "MIN (" + val + ")"; 39 | } 40 | 41 | public static SQLFunction MIN(final Selectable val) { 42 | return new SQLFunction("MIN (" + val.getSQL() + ")"); 43 | } 44 | 45 | public static String COUNT() { 46 | return COUNT("*"); 47 | } 48 | 49 | public static String COUNT(final String val) { 50 | return "COUNT (" + val + ")"; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLLang.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public interface SQLLang extends Cloneable { 20 | 21 | /** 22 | * Build SQL query string 23 | * 24 | * @return SQL query 25 | */ 26 | String getSQL(); 27 | } 28 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 14-8-6. 21 | */ 22 | public interface SQLQuery extends SQLLang { 23 | } 24 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLQueryBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | import org.mariotaku.sqliteqb.library.query.*; 20 | 21 | public class SQLQueryBuilder { 22 | 23 | private SQLQueryBuilder() { 24 | throw new AssertionError("You can't create instance for this class"); 25 | } 26 | 27 | public static SQLAlterTableQuery.Builder alterTable(final String table) { 28 | return new SQLAlterTableQuery.Builder().alterTable(table); 29 | } 30 | 31 | public static SQLCreateTableQuery.Builder createTable(final boolean temporary, final boolean createIfNotExists, 32 | final String name) { 33 | return new SQLCreateTableQuery.Builder().createTable(temporary, createIfNotExists, name); 34 | } 35 | 36 | public static SQLCreateTableQuery.Builder createTable(final boolean createIfNotExists, final String name) { 37 | return createTable(false, createIfNotExists, name); 38 | } 39 | 40 | public static SQLCreateTableQuery.Builder createTable(final String name) { 41 | return createTable(false, false, name); 42 | } 43 | 44 | public static SQLCreateViewQuery.Builder createView(final boolean temporary, final boolean createIfNotExists, 45 | final String name) { 46 | return new SQLCreateViewQuery.Builder().createView(temporary, createIfNotExists, name); 47 | } 48 | 49 | public static SQLCreateIndexQuery.Builder createIndex(final boolean unique, final boolean createIfNotExists) { 50 | return new SQLCreateIndexQuery.Builder().createIndex(unique, createIfNotExists); 51 | } 52 | 53 | 54 | public static SQLCreateTriggerQuery.Builder createTrigger(final boolean temporary, final boolean createIfNotExists, 55 | final String name) { 56 | return new SQLCreateTriggerQuery.Builder().createTrigger(temporary, createIfNotExists, name); 57 | } 58 | 59 | public static SQLCreateViewQuery.Builder createView(final boolean createIfNotExists, final String name) { 60 | return createView(false, createIfNotExists, name); 61 | } 62 | 63 | public static SQLCreateViewQuery.Builder createView(final String name) { 64 | return createView(false, false, name); 65 | } 66 | 67 | public static SQLDeleteQuery.Builder deleteFrom(Table table) { 68 | return new SQLDeleteQuery.Builder().from(table); 69 | } 70 | 71 | public static SQLDropTableQuery dropTable(final boolean dropIfExists, final String table) { 72 | return new SQLDropTableQuery(dropIfExists, table); 73 | } 74 | 75 | public static SQLDropIndexQuery dropIndex(final boolean dropIfExists, final String table) { 76 | return new SQLDropIndexQuery(dropIfExists, table); 77 | } 78 | 79 | public static SQLDropViewQuery dropView(final boolean dropIfExists, final String table) { 80 | return new SQLDropViewQuery(dropIfExists, table); 81 | } 82 | 83 | public static SQLDropTriggerQuery dropTrigger(final boolean dropIfExists, final String table) { 84 | return new SQLDropTriggerQuery(dropIfExists, table); 85 | } 86 | 87 | public static SQLInsertQuery.Builder insertInto(final OnConflict onConflict, final String table) { 88 | return new SQLInsertQuery.Builder().insertInto(onConflict, table); 89 | } 90 | 91 | public static SQLUpdateQuery.Builder update(final OnConflict onConflict, final Table table) { 92 | return new SQLUpdateQuery.Builder().update(onConflict, table); 93 | } 94 | 95 | public static SQLUpdateQuery.Builder update(final OnConflict onConflict, final String table) { 96 | return update(onConflict, new Table(table)); 97 | } 98 | 99 | public static SQLInsertQuery.Builder insertInto(final String table) { 100 | return insertInto(null, table); 101 | } 102 | 103 | public static SQLSelectQuery.Builder select(final boolean distinct, final Selectable select) { 104 | return new SQLSelectQuery.Builder().select(distinct, select); 105 | } 106 | 107 | public static SQLSelectQuery.Builder select(final Selectable select) { 108 | return select(false, select); 109 | } 110 | 111 | public static SQLWithSelectQuery.Builder with(String name, Selectable as) { 112 | return new SQLWithSelectQuery.Builder().with(name, as); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SQLQueryException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class SQLQueryException extends RuntimeException { 20 | 21 | private static final long serialVersionUID = 910158450604676104L; 22 | 23 | public SQLQueryException() { 24 | } 25 | 26 | public SQLQueryException(final String detailMessage) { 27 | super(detailMessage); 28 | } 29 | 30 | public SQLQueryException(final String detailMessage, final Throwable throwable) { 31 | super(detailMessage, throwable); 32 | } 33 | 34 | public SQLQueryException(final Throwable throwable) { 35 | super(throwable); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Selectable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public interface Selectable extends SQLLang { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/SetValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | /** 20 | * Created by mariotaku on 14-8-7. 21 | */ 22 | public class SetValue implements SQLLang { 23 | 24 | private final Columns.Column column; 25 | private final SQLLang expression; 26 | 27 | public SetValue(Columns.Column column, SQLLang expression) { 28 | this.column = column; 29 | this.expression = expression; 30 | } 31 | 32 | public SetValue(String column, SQLLang expression) { 33 | this(new Columns.Column(column), expression); 34 | } 35 | 36 | 37 | @Override 38 | public String getSQL() { 39 | return column.getSQL() + " = " + expression.getSQL(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class Table implements Selectable { 20 | 21 | public static final Table NEW = new Table("NEW"); 22 | 23 | private final String table; 24 | 25 | public Table(final String table) { 26 | this.table = table; 27 | } 28 | 29 | @Override 30 | public String getSQL() { 31 | return table; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Tables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class Tables extends Table { 20 | 21 | private final String[] tables; 22 | 23 | public Tables(final String... tables) { 24 | super(null); 25 | this.tables = tables; 26 | } 27 | 28 | @Override 29 | public String getSQL() { 30 | return Utils.toString(tables, ',', true); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library; 18 | 19 | public class Utils { 20 | 21 | public static String toString(final Object[] array, final char token, final boolean includeSpace) { 22 | final StringBuilder builder = new StringBuilder(); 23 | final int length = array.length; 24 | for (int i = 0; i < length; i++) { 25 | final String string = objectToString(array[i]); 26 | if (string != null) { 27 | if (i > 0) { 28 | builder.append(includeSpace ? token + " " : token); 29 | } 30 | builder.append(string); 31 | } 32 | } 33 | return builder.toString(); 34 | } 35 | 36 | public static String toStringForSQL(final int size) { 37 | final StringBuilder builder = new StringBuilder(); 38 | for (int i = 0; i < size; i++) { 39 | if (i > 0) { 40 | builder.append(','); 41 | } 42 | builder.append('?'); 43 | } 44 | return builder.toString(); 45 | } 46 | 47 | private static String objectToString(Object o) { 48 | if (o instanceof SQLLang) 49 | return ((SQLLang) o).getSQL(); 50 | return o != null ? o.toString() : null; 51 | } 52 | 53 | public static String[] toStringArray(Object[] array) { 54 | final String[] strings = new String[array.length]; 55 | final int length = array.length; 56 | for (int i = 0; i < length; i++) { 57 | strings[i] = objectToString(array[i]); 58 | } 59 | return strings; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/IBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.SQLLang; 20 | 21 | public interface IBuilder { 22 | 23 | public T build(); 24 | 25 | /** 26 | * Equivalent to {@link #build()}.{@link SQLLang#getSQL()} 27 | * 28 | * @return 29 | */ 30 | public String buildSQL(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLAlterTableQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.NewColumn; 20 | import org.mariotaku.sqliteqb.library.SQLQuery; 21 | 22 | public class SQLAlterTableQuery implements SQLQuery { 23 | 24 | private String table; 25 | private String renameTo; 26 | private NewColumn addColumn; 27 | 28 | @Override 29 | public String getSQL() { 30 | if (table == null) throw new NullPointerException("table must not be null!"); 31 | if (renameTo == null && addColumn == null) throw new NullPointerException(); 32 | final StringBuilder sb = new StringBuilder(); 33 | sb.append("ALTER TABLE "); 34 | sb.append(table); 35 | if (renameTo != null) { 36 | sb.append(" RENAME TO "); 37 | sb.append(renameTo); 38 | } else if (addColumn != null) { 39 | sb.append(" ADD COLUMN "); 40 | sb.append(addColumn.getSQL()); 41 | } 42 | return sb.toString(); 43 | } 44 | 45 | void setAddColumn(final NewColumn addColumn) { 46 | this.addColumn = addColumn; 47 | } 48 | 49 | void setRenameTo(final String renameTo) { 50 | this.renameTo = renameTo; 51 | } 52 | 53 | void setTable(final String table) { 54 | this.table = table; 55 | } 56 | 57 | public static final class Builder implements IBuilder { 58 | 59 | private final SQLAlterTableQuery query = new SQLAlterTableQuery(); 60 | private boolean buildCalled; 61 | 62 | public Builder addColumn(final NewColumn addColumn) { 63 | checkNotBuilt(); 64 | query.setAddColumn(addColumn); 65 | return this; 66 | } 67 | 68 | public Builder alterTable(final String table) { 69 | checkNotBuilt(); 70 | query.setTable(table); 71 | return this; 72 | } 73 | 74 | @Override 75 | public SQLAlterTableQuery build() { 76 | return query; 77 | } 78 | 79 | @Override 80 | public String buildSQL() { 81 | return build().getSQL(); 82 | } 83 | 84 | public Builder renameTo(final String renameTo) { 85 | checkNotBuilt(); 86 | query.setRenameTo(renameTo); 87 | return this; 88 | } 89 | 90 | private void checkNotBuilt() { 91 | if (buildCalled) throw new IllegalStateException(); 92 | } 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLCreateIndexQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.Columns; 20 | import org.mariotaku.sqliteqb.library.Expression; 21 | import org.mariotaku.sqliteqb.library.SQLQuery; 22 | import org.mariotaku.sqliteqb.library.Table; 23 | 24 | public class SQLCreateIndexQuery implements SQLQuery { 25 | 26 | private boolean unique; 27 | private boolean createIfNotExists; 28 | private Table table; 29 | private String indexName; 30 | private Columns indexedColumns; 31 | private Expression where; 32 | 33 | SQLCreateIndexQuery() { 34 | 35 | } 36 | 37 | @Override 38 | public String getSQL() { 39 | if (table == null) throw new NullPointerException("Table must not be null!"); 40 | if (indexName == null) 41 | throw new NullPointerException("SELECT statement must not be null!"); 42 | final StringBuilder sb = new StringBuilder("CREATE"); 43 | if (unique) { 44 | sb.append(" UNIQUE"); 45 | } 46 | sb.append(" INDEX"); 47 | if (createIfNotExists) { 48 | sb.append(" IF NOT EXISTS"); 49 | } 50 | if (indexedColumns == null) 51 | throw new NullPointerException("Indexed columns must not be null !"); 52 | sb.append(" "); 53 | sb.append(indexName); 54 | sb.append(" ON "); 55 | sb.append(table.getSQL()); 56 | sb.append(" ("); 57 | sb.append(indexedColumns.getSQL()); 58 | sb.append(")"); 59 | if (where != null) { 60 | sb.append(" WHERE"); 61 | sb.append(where.getSQL()); 62 | } 63 | return sb.toString(); 64 | } 65 | 66 | public void setIndexedColumns(Columns indexedColumns) { 67 | this.indexedColumns = indexedColumns; 68 | } 69 | 70 | public void setWhere(Expression where) { 71 | this.where = where; 72 | } 73 | 74 | void setIndexName(final String indexName) { 75 | this.indexName = indexName; 76 | } 77 | 78 | void setCreateIfNotExists(final boolean createIfNotExists) { 79 | this.createIfNotExists = createIfNotExists; 80 | } 81 | 82 | void setTable(final Table table) { 83 | this.table = table; 84 | } 85 | 86 | void setUnique(final boolean unique) { 87 | this.unique = unique; 88 | } 89 | 90 | public static final class Builder implements IBuilder { 91 | 92 | private final SQLCreateIndexQuery query = new SQLCreateIndexQuery(); 93 | private boolean buildCalled; 94 | 95 | public Builder on(final Table table, Columns indexedColumns) { 96 | checkNotBuilt(); 97 | query.setTable(table); 98 | query.setIndexedColumns(indexedColumns); 99 | return this; 100 | } 101 | 102 | public Builder name(final String name) { 103 | checkNotBuilt(); 104 | query.setIndexName(name); 105 | return this; 106 | } 107 | 108 | public Builder where(final Expression expression) { 109 | checkNotBuilt(); 110 | query.setWhere(expression); 111 | return this; 112 | } 113 | 114 | 115 | @Override 116 | public SQLCreateIndexQuery build() { 117 | buildCalled = true; 118 | return query; 119 | } 120 | 121 | @Override 122 | public String buildSQL() { 123 | return build().getSQL(); 124 | } 125 | 126 | 127 | public Builder createIndex(final boolean unique, final boolean createIfNotExists) { 128 | checkNotBuilt(); 129 | query.setUnique(unique); 130 | query.setCreateIfNotExists(createIfNotExists); 131 | return this; 132 | } 133 | 134 | private void checkNotBuilt() { 135 | if (buildCalled) throw new IllegalStateException(); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLCreateTableQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.Constraint; 20 | import org.mariotaku.sqliteqb.library.NewColumn; 21 | import org.mariotaku.sqliteqb.library.SQLQuery; 22 | import org.mariotaku.sqliteqb.library.Utils; 23 | 24 | public class SQLCreateTableQuery implements SQLQuery { 25 | 26 | private boolean temporary; 27 | private boolean createIfNotExists; 28 | private String table; 29 | private NewColumn[] newColumns; 30 | private SQLSelectQuery selectStmt; 31 | private Constraint[] constraints; 32 | 33 | SQLCreateTableQuery() { 34 | } 35 | 36 | @Override 37 | public String getSQL() { 38 | if (table == null) throw new NullPointerException("NAME must not be null!"); 39 | if ((newColumns == null || newColumns.length == 0) && selectStmt == null) 40 | throw new NullPointerException("Columns or AS must not be null!"); 41 | final StringBuilder sb = new StringBuilder("CREATE "); 42 | if (temporary) { 43 | sb.append("TEMPORARY "); 44 | } 45 | sb.append("TABLE "); 46 | if (createIfNotExists) { 47 | sb.append("IF NOT EXISTS "); 48 | } 49 | sb.append(table); 50 | sb.append(' '); 51 | if (newColumns != null && newColumns.length > 0) { 52 | sb.append('('); 53 | sb.append(Utils.toString(newColumns, ',', true)); 54 | if (constraints != null && constraints.length > 0) { 55 | sb.append(", "); 56 | sb.append(Utils.toString(constraints, ',', true)); 57 | sb.append(' '); 58 | } 59 | sb.append(')'); 60 | } else { 61 | sb.append("AS "); 62 | sb.append(selectStmt.getSQL()); 63 | } 64 | return sb.toString(); 65 | } 66 | 67 | void setAs(final SQLSelectQuery selectStmt) { 68 | this.selectStmt = selectStmt; 69 | } 70 | 71 | void setCreateIfNotExists(final boolean createIfNotExists) { 72 | this.createIfNotExists = createIfNotExists; 73 | } 74 | 75 | void setNewColumns(final NewColumn[] newColumns) { 76 | this.newColumns = newColumns; 77 | } 78 | 79 | void setTable(final String table) { 80 | this.table = table; 81 | } 82 | 83 | void setTemporary(final boolean temporary) { 84 | this.temporary = temporary; 85 | } 86 | 87 | public static final class Builder implements IBuilder { 88 | 89 | private final SQLCreateTableQuery query = new SQLCreateTableQuery(); 90 | 91 | private boolean buildCalled; 92 | 93 | public Builder as(final SQLSelectQuery selectStmt) { 94 | checkNotBuilt(); 95 | query.setAs(selectStmt); 96 | return this; 97 | } 98 | 99 | @Override 100 | public SQLCreateTableQuery build() { 101 | buildCalled = true; 102 | return query; 103 | } 104 | 105 | @Override 106 | public String buildSQL() { 107 | return build().getSQL(); 108 | } 109 | 110 | public Builder columns(final NewColumn... newColumns) { 111 | checkNotBuilt(); 112 | query.setNewColumns(newColumns); 113 | return this; 114 | } 115 | 116 | public Builder constraint(final Constraint... constraints) { 117 | checkNotBuilt(); 118 | query.setConstraints(constraints); 119 | return this; 120 | } 121 | 122 | public Builder createTable(final boolean temporary, final boolean createIfNotExists, final String table) { 123 | checkNotBuilt(); 124 | query.setTemporary(temporary); 125 | query.setCreateIfNotExists(createIfNotExists); 126 | query.setTable(table); 127 | return this; 128 | } 129 | 130 | public Builder createTable(final boolean createIfNotExists, final String table) { 131 | return createTable(false, createIfNotExists, table); 132 | } 133 | 134 | public Builder createTemporaryTable(final boolean createIfNotExists, final String table) { 135 | return createTable(true, createIfNotExists, table); 136 | } 137 | 138 | private void checkNotBuilt() { 139 | if (buildCalled) throw new IllegalStateException(); 140 | } 141 | 142 | } 143 | 144 | private void setConstraints(Constraint[] constraints) { 145 | this.constraints = constraints; 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLCreateTriggerQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.*; 20 | 21 | /** 22 | * Created by mariotaku on 14-8-6. 23 | */ 24 | public class SQLCreateTriggerQuery implements SQLQuery { 25 | 26 | private boolean temporary; 27 | private boolean createIfNotExists; 28 | private boolean forEachRow; 29 | private String name; 30 | private Table on; 31 | private Type type; 32 | private Event event; 33 | private Columns updateOf; 34 | private SQLQuery[] actions; 35 | private Expression when; 36 | 37 | void setActions(SQLQuery[] actions) { 38 | this.actions = actions; 39 | } 40 | 41 | void setForEachRow(boolean forEachRow) { 42 | this.forEachRow = forEachRow; 43 | } 44 | 45 | void setOn(Table on) { 46 | this.on = on; 47 | } 48 | 49 | void setUpdateOf(Columns updateOf) { 50 | this.updateOf = updateOf; 51 | } 52 | 53 | void setType(Type type) { 54 | this.type = type; 55 | } 56 | 57 | void setEvent(Event event) { 58 | this.event = event; 59 | } 60 | 61 | void setWhen(Expression when) { 62 | this.when = when; 63 | } 64 | 65 | @Override 66 | public String getSQL() { 67 | if (name == null) throw new NullPointerException("NAME must not be null!"); 68 | if (event == null) throw new NullPointerException("EVENT must not be null!"); 69 | if (on == null) throw new NullPointerException("ON must not be null!"); 70 | if (actions == null) throw new NullPointerException("ACTIONS must not be null!"); 71 | final StringBuilder sb = new StringBuilder("CREATE "); 72 | if (temporary) { 73 | sb.append("TEMPORARY "); 74 | } 75 | sb.append("TRIGGER "); 76 | if (createIfNotExists) { 77 | sb.append("IF NOT EXISTS "); 78 | } 79 | sb.append(name); 80 | sb.append(' '); 81 | if (type != null) { 82 | sb.append(type.getSQL()); 83 | sb.append(' '); 84 | } 85 | sb.append(event.getSQL()); 86 | sb.append(' '); 87 | if (event == Event.UPDATE) { 88 | sb.append(updateOf.getSQL()); 89 | sb.append(' '); 90 | } 91 | sb.append("ON "); 92 | sb.append(on.getSQL()); 93 | sb.append(' '); 94 | if (forEachRow) { 95 | sb.append("FOR EACH ROW"); 96 | } 97 | if (when != null) { 98 | sb.append(" WHEN "); 99 | sb.append(when.getSQL()); 100 | sb.append(' '); 101 | } 102 | sb.append(" BEGIN "); 103 | sb.append(Utils.toString(actions, ';', true)); 104 | sb.append("; END"); 105 | return sb.toString(); 106 | } 107 | 108 | void setCreateIfNotExists(final boolean createIfNotExists) { 109 | this.createIfNotExists = createIfNotExists; 110 | } 111 | 112 | void setName(final String name) { 113 | this.name = name; 114 | } 115 | 116 | void setTemporary(final boolean temporary) { 117 | this.temporary = temporary; 118 | } 119 | 120 | public static enum Type implements SQLLang { 121 | BEFORE("BEFORE"), AFTER("AFTER"), INSTEAD_OF("INSTEAD OF"); 122 | private final String lang; 123 | 124 | Type(String lang) { 125 | this.lang = lang; 126 | } 127 | 128 | @Override 129 | public String getSQL() { 130 | return lang; 131 | } 132 | } 133 | 134 | public static enum Event implements SQLLang { 135 | INSERT("INSERT"), DELETE("DELETE"), UPDATE("UPDATE"); 136 | private final String lang; 137 | 138 | Event(String lang) { 139 | this.lang = lang; 140 | } 141 | 142 | @Override 143 | public String getSQL() { 144 | return lang; 145 | } 146 | } 147 | 148 | public static class Builder implements IBuilder { 149 | 150 | private final SQLCreateTriggerQuery query = new SQLCreateTriggerQuery(); 151 | private boolean buildCalled; 152 | 153 | public Builder forEachRow(final boolean forEachRow) { 154 | checkNotBuilt(); 155 | query.setForEachRow(forEachRow); 156 | return this; 157 | } 158 | 159 | public Builder on(final Table on) { 160 | checkNotBuilt(); 161 | query.setOn(on); 162 | return this; 163 | } 164 | 165 | public Builder event(Event event) { 166 | checkNotBuilt(); 167 | query.setEvent(event); 168 | return this; 169 | } 170 | 171 | public Builder type(Type type) { 172 | checkNotBuilt(); 173 | query.setType(type); 174 | return this; 175 | } 176 | 177 | public Builder updateOf(Columns updateOf) { 178 | checkNotBuilt(); 179 | query.setUpdateOf(updateOf); 180 | return this; 181 | } 182 | 183 | public Builder actions(SQLQuery... actions) { 184 | checkNotBuilt(); 185 | query.setActions(actions); 186 | return this; 187 | } 188 | 189 | public Builder when(Expression when) { 190 | checkNotBuilt(); 191 | query.setWhen(when); 192 | return this; 193 | } 194 | 195 | @Override 196 | public SQLCreateTriggerQuery build() { 197 | buildCalled = true; 198 | return query; 199 | } 200 | 201 | @Override 202 | public String buildSQL() { 203 | return build().getSQL(); 204 | } 205 | 206 | private void checkNotBuilt() { 207 | if (buildCalled) throw new IllegalStateException(); 208 | } 209 | 210 | public Builder createTrigger(boolean temporary, boolean createIfNotExists, String name) { 211 | checkNotBuilt(); 212 | query.setTemporary(temporary); 213 | query.setCreateIfNotExists(createIfNotExists); 214 | query.setName(name); 215 | return this; 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLCreateViewQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.SQLQuery; 20 | 21 | public class SQLCreateViewQuery implements SQLQuery { 22 | 23 | private boolean temporary; 24 | private boolean createIfNotExists; 25 | private String name; 26 | private SQLSelectQuery selectStmt; 27 | 28 | SQLCreateViewQuery() { 29 | 30 | } 31 | 32 | @Override 33 | public String getSQL() { 34 | if (name == null) throw new NullPointerException("NAME must not be null!"); 35 | if (selectStmt == null) 36 | throw new NullPointerException("SELECT statement must not be null!"); 37 | final StringBuilder sb = new StringBuilder("CREATE "); 38 | if (temporary) { 39 | sb.append("TEMPORARY "); 40 | } 41 | sb.append("VIEW "); 42 | if (createIfNotExists) { 43 | sb.append("IF NOT EXISTS "); 44 | } 45 | sb.append(name); 46 | sb.append(" AS "); 47 | sb.append(selectStmt.getSQL()); 48 | return sb.toString(); 49 | } 50 | 51 | void setAs(final SQLSelectQuery selectStmt) { 52 | this.selectStmt = selectStmt; 53 | } 54 | 55 | void setCreateIfNotExists(final boolean createIfNotExists) { 56 | this.createIfNotExists = createIfNotExists; 57 | } 58 | 59 | void setName(final String name) { 60 | this.name = name; 61 | } 62 | 63 | void setTemporary(final boolean temporary) { 64 | this.temporary = temporary; 65 | } 66 | 67 | public static final class Builder implements IBuilder { 68 | 69 | private final SQLCreateViewQuery query = new SQLCreateViewQuery(); 70 | private boolean buildCalled; 71 | 72 | public Builder as(final SQLSelectQuery selectStmt) { 73 | checkNotBuilt(); 74 | query.setAs(selectStmt); 75 | return this; 76 | } 77 | 78 | @Override 79 | public SQLCreateViewQuery build() { 80 | buildCalled = true; 81 | return query; 82 | } 83 | 84 | @Override 85 | public String buildSQL() { 86 | return build().getSQL(); 87 | } 88 | 89 | public Builder createTemporaryView(final boolean createIfNotExists, final String name) { 90 | return createView(true, createIfNotExists, name); 91 | } 92 | 93 | public Builder createView(final boolean temporary, final boolean createIfNotExists, final String name) { 94 | checkNotBuilt(); 95 | query.setTemporary(temporary); 96 | query.setCreateIfNotExists(createIfNotExists); 97 | query.setName(name); 98 | return this; 99 | } 100 | 101 | public Builder createView(final boolean createIfNotExists, final String name) { 102 | return createView(false, createIfNotExists, name); 103 | } 104 | 105 | private void checkNotBuilt() { 106 | if (buildCalled) throw new IllegalStateException(); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDeleteQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.Expression; 20 | import org.mariotaku.sqliteqb.library.SQLQuery; 21 | import org.mariotaku.sqliteqb.library.Table; 22 | 23 | public class SQLDeleteQuery implements SQLQuery { 24 | 25 | private Table table; 26 | private Expression where; 27 | 28 | @Override 29 | public String getSQL() { 30 | final StringBuilder sb = new StringBuilder(); 31 | sb.append("DELETE FROM "); 32 | sb.append(table.getSQL()); 33 | if (where != null) { 34 | sb.append(" WHERE "); 35 | sb.append(where.getSQL()); 36 | } 37 | return sb.toString(); 38 | } 39 | 40 | void setFrom(final Table table) { 41 | this.table = table; 42 | } 43 | 44 | void setWhere(final Expression where) { 45 | this.where = where; 46 | } 47 | 48 | public static final class Builder implements IBuilder { 49 | private final SQLDeleteQuery query = new SQLDeleteQuery(); 50 | private boolean buildCalled; 51 | 52 | @Override 53 | public SQLDeleteQuery build() { 54 | buildCalled = true; 55 | return query; 56 | } 57 | 58 | @Override 59 | public String buildSQL() { 60 | return build().getSQL(); 61 | } 62 | 63 | public Builder from(final Table table) { 64 | checkNotBuilt(); 65 | query.setFrom(table); 66 | return this; 67 | } 68 | 69 | public Builder where(final Expression where) { 70 | checkNotBuilt(); 71 | query.setWhere(where); 72 | return this; 73 | } 74 | 75 | private void checkNotBuilt() { 76 | if (buildCalled) throw new IllegalStateException(); 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDropIndexQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | public class SQLDropIndexQuery extends SQLDropQuery { 20 | 21 | public SQLDropIndexQuery(final boolean dropIfExists, final String table) { 22 | super(dropIfExists, "INDEX", table); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDropQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.SQLQuery; 20 | 21 | public class SQLDropQuery implements SQLQuery { 22 | 23 | private final boolean dropIfExists; 24 | private final String type; 25 | private final String target; 26 | 27 | public SQLDropQuery(final boolean dropIfExists, final String type, final String target) { 28 | if (target == null) throw new NullPointerException(); 29 | this.dropIfExists = dropIfExists; 30 | this.type = type; 31 | this.target = target; 32 | } 33 | 34 | @Override 35 | public final String getSQL() { 36 | final StringBuilder sb = new StringBuilder(); 37 | sb.append("DROP "); 38 | sb.append(type); 39 | if (dropIfExists) { 40 | sb.append(" IF EXISTS"); 41 | } 42 | sb.append(" "); 43 | sb.append(target); 44 | return sb.toString(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDropTableQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | public class SQLDropTableQuery extends SQLDropQuery { 20 | 21 | public SQLDropTableQuery(final boolean dropIfExists, final String table) { 22 | super(dropIfExists, "TABLE", table); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDropTriggerQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | public class SQLDropTriggerQuery extends SQLDropQuery { 20 | 21 | public SQLDropTriggerQuery(final boolean dropIfExists, final String table) { 22 | super(dropIfExists, "TRIGGER", table); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLDropViewQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | public class SQLDropViewQuery extends SQLDropQuery { 20 | 21 | public SQLDropViewQuery(final boolean dropIfExists, final String table) { 22 | super(dropIfExists, "VIEW", table); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLInsertQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.OnConflict; 20 | import org.mariotaku.sqliteqb.library.SQLQuery; 21 | import org.mariotaku.sqliteqb.library.Utils; 22 | 23 | public class SQLInsertQuery implements SQLQuery { 24 | 25 | private OnConflict onConflict; 26 | private String table; 27 | private String[] columns; 28 | private String values; 29 | 30 | SQLInsertQuery() { 31 | 32 | } 33 | 34 | @Override 35 | public String getSQL() { 36 | if (table == null) throw new NullPointerException("table must not be null!"); 37 | final StringBuilder sb = new StringBuilder(); 38 | sb.append("INSERT "); 39 | if (onConflict != null) { 40 | sb.append("OR "); 41 | sb.append(onConflict.getAction()); 42 | sb.append(" "); 43 | } 44 | sb.append("INTO "); 45 | sb.append(table); 46 | sb.append(" ("); 47 | sb.append(Utils.toString(columns, ',', true)); 48 | sb.append(") "); 49 | sb.append(values); 50 | return sb.toString(); 51 | } 52 | 53 | void setColumns(final String[] columns) { 54 | this.columns = columns; 55 | } 56 | 57 | void setOnConflict(final OnConflict onConflict) { 58 | this.onConflict = onConflict; 59 | } 60 | 61 | void setSelect(final SQLSelectQuery select) { 62 | this.values = select.getSQL(); 63 | } 64 | 65 | void setValues(final String... values) { 66 | this.values = "VALUES (" + Utils.toString(values, ',', true) + ")"; 67 | } 68 | 69 | void setTable(final String table) { 70 | this.table = table; 71 | } 72 | 73 | public static final class Builder implements IBuilder { 74 | 75 | private final SQLInsertQuery query = new SQLInsertQuery(); 76 | 77 | private boolean buildCalled; 78 | 79 | @Override 80 | public SQLInsertQuery build() { 81 | buildCalled = true; 82 | return query; 83 | } 84 | 85 | @Override 86 | public String buildSQL() { 87 | return build().getSQL(); 88 | } 89 | 90 | public Builder columns(final String[] columns) { 91 | checkNotBuilt(); 92 | query.setColumns(columns); 93 | return this; 94 | } 95 | 96 | public Builder values(final String[] values) { 97 | checkNotBuilt(); 98 | query.setValues(values); 99 | return this; 100 | } 101 | 102 | public Builder values(final String values) { 103 | checkNotBuilt(); 104 | query.setValues(values); 105 | return this; 106 | } 107 | 108 | public Builder insertInto(final OnConflict onConflict, final String table) { 109 | checkNotBuilt(); 110 | query.setOnConflict(onConflict); 111 | query.setTable(table); 112 | return this; 113 | } 114 | 115 | public Builder insertInto(final String table) { 116 | return insertInto(null, table); 117 | } 118 | 119 | public Builder select(final SQLSelectQuery select) { 120 | checkNotBuilt(); 121 | query.setSelect(select); 122 | return this; 123 | } 124 | 125 | private void checkNotBuilt() { 126 | if (buildCalled) throw new IllegalStateException(); 127 | } 128 | 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLSelectQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.*; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | public class SQLSelectQuery implements SQLQuery, Selectable { 25 | 26 | private final List internalQueries = new ArrayList<>(); 27 | 28 | private InternalQuery currentInternalQuery; 29 | private SQLLang orderBy; 30 | private SQLLang limit = null, offset = null; 31 | 32 | SQLSelectQuery() { 33 | initCurrentQuery(); 34 | } 35 | 36 | @Override 37 | public String getSQL() { 38 | final StringBuilder sb = new StringBuilder(); 39 | final int size = internalQueries.size(); 40 | for (int i = 0; i < size; i++) { 41 | if (i != 0) { 42 | sb.append(" UNION "); 43 | } 44 | final InternalQuery query = internalQueries.get(i); 45 | sb.append(query.getSQL()); 46 | } 47 | if (orderBy != null) { 48 | sb.append(" ORDER BY "); 49 | sb.append(orderBy.getSQL()); 50 | } 51 | if (limit != null) { 52 | sb.append(" LIMIT "); 53 | sb.append(limit.getSQL()); 54 | if (offset != null) { 55 | sb.append(" OFFSET "); 56 | sb.append(offset.getSQL()); 57 | } 58 | } 59 | return sb.toString(); 60 | } 61 | 62 | private void initCurrentQuery() { 63 | currentInternalQuery = new InternalQuery(); 64 | internalQueries.add(currentInternalQuery); 65 | } 66 | 67 | void setDistinct(final boolean distinct) { 68 | currentInternalQuery.setDistinct(distinct); 69 | } 70 | 71 | void setFrom(final Selectable from) { 72 | currentInternalQuery.setFrom(from); 73 | } 74 | 75 | void setGroupBy(final Selectable groupBy) { 76 | currentInternalQuery.setGroupBy(groupBy); 77 | } 78 | 79 | void setHaving(final Expression having) { 80 | currentInternalQuery.setHaving(having); 81 | } 82 | 83 | void setJoin(final Join join) { 84 | currentInternalQuery.setJoin(join); 85 | } 86 | 87 | void setLimit(final SQLLang limit) { 88 | this.limit = limit; 89 | } 90 | 91 | void setOffset(final SQLLang offset) { 92 | this.offset = offset; 93 | } 94 | 95 | void setOrderBy(final SQLLang orderBy) { 96 | this.orderBy = orderBy; 97 | } 98 | 99 | void setSelect(final Selectable select) { 100 | currentInternalQuery.setSelect(select); 101 | } 102 | 103 | void setWhere(final Expression where) { 104 | currentInternalQuery.setWhere(where); 105 | } 106 | 107 | void union() { 108 | initCurrentQuery(); 109 | } 110 | 111 | public static class Builder implements IBuilder { 112 | private final SQLSelectQuery query = new SQLSelectQuery(); 113 | boolean buildCalled; 114 | 115 | @Override 116 | public SQLSelectQuery build() { 117 | buildCalled = true; 118 | return query; 119 | } 120 | 121 | @Override 122 | public String buildSQL() { 123 | return build().getSQL(); 124 | } 125 | 126 | public Builder from(final Selectable from) { 127 | checkNotBuilt(); 128 | query.setFrom(from); 129 | return this; 130 | } 131 | 132 | public Builder groupBy(final Selectable groupBy) { 133 | checkNotBuilt(); 134 | query.setGroupBy(groupBy); 135 | return this; 136 | } 137 | 138 | public Builder having(final Expression having) { 139 | checkNotBuilt(); 140 | query.setHaving(having); 141 | return this; 142 | } 143 | 144 | public Builder limit(final SQLLang limit) { 145 | checkNotBuilt(); 146 | query.setLimit(limit); 147 | return this; 148 | } 149 | 150 | public Builder join(final Join join) { 151 | checkNotBuilt(); 152 | query.setJoin(join); 153 | return this; 154 | } 155 | 156 | public Builder offset(final SQLLang offset) { 157 | query.setOffset(offset); 158 | return this; 159 | } 160 | 161 | public Builder orderBy(final SQLLang orderBy) { 162 | checkNotBuilt(); 163 | query.setOrderBy(orderBy); 164 | return this; 165 | } 166 | 167 | public Builder select(final boolean distinct, final Selectable select) { 168 | checkNotBuilt(); 169 | query.setSelect(select); 170 | query.setDistinct(distinct); 171 | return this; 172 | } 173 | 174 | public Builder select(final Selectable select) { 175 | checkNotBuilt(); 176 | select(false, select); 177 | return this; 178 | } 179 | 180 | public Builder union() { 181 | checkNotBuilt(); 182 | query.union(); 183 | return this; 184 | } 185 | 186 | public Builder where(final Expression where) { 187 | checkNotBuilt(); 188 | query.setWhere(where); 189 | return this; 190 | } 191 | 192 | void checkNotBuilt() { 193 | if (buildCalled) throw new IllegalStateException(); 194 | } 195 | 196 | } 197 | 198 | private static class InternalQuery implements SQLLang { 199 | 200 | private boolean distinct; 201 | private Selectable select, from, groupBy; 202 | private Expression where, having; 203 | private Join join; 204 | 205 | @Override 206 | public String getSQL() { 207 | if (select == null) throw new SQLQueryException("selectable is null"); 208 | final StringBuilder sb = new StringBuilder("SELECT"); 209 | if (distinct) { 210 | sb.append(" DISTINCT"); 211 | } 212 | sb.append(" "); 213 | sb.append(select.getSQL()); 214 | if (!(select instanceof SQLSelectQuery) && from == null) 215 | throw new SQLQueryException("FROM not specified"); 216 | else if (from != null) { 217 | if (from instanceof SQLSelectQuery) { 218 | sb.append(" FROM ("); 219 | sb.append(from.getSQL()); 220 | sb.append(")"); 221 | } else { 222 | sb.append(" FROM "); 223 | sb.append(from.getSQL()); 224 | } 225 | } 226 | if (join != null) { 227 | sb.append(" "); 228 | sb.append(join.getSQL()); 229 | } 230 | if (where != null) { 231 | sb.append(" WHERE "); 232 | sb.append(where.getSQL()); 233 | } 234 | if (groupBy != null) { 235 | sb.append(" GROUP BY "); 236 | sb.append(groupBy.getSQL()); 237 | if (having != null) { 238 | sb.append(" HAVING "); 239 | sb.append(having.getSQL()); 240 | } 241 | } 242 | return sb.toString(); 243 | } 244 | 245 | void setJoin(final Join join) { 246 | this.join = join; 247 | } 248 | 249 | void setDistinct(final boolean distinct) { 250 | this.distinct = distinct; 251 | } 252 | 253 | void setFrom(final Selectable from) { 254 | this.from = from; 255 | } 256 | 257 | void setGroupBy(final Selectable groupBy) { 258 | this.groupBy = groupBy; 259 | } 260 | 261 | void setHaving(final Expression having) { 262 | this.having = having; 263 | } 264 | 265 | void setSelect(final Selectable select) { 266 | this.select = select; 267 | } 268 | 269 | void setWhere(final Expression where) { 270 | this.where = where; 271 | } 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLUpdateQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 org.mariotaku.sqliteqb.library.query; 18 | 19 | import org.mariotaku.sqliteqb.library.Expression; 20 | import org.mariotaku.sqliteqb.library.OnConflict; 21 | import org.mariotaku.sqliteqb.library.SQLQuery; 22 | import org.mariotaku.sqliteqb.library.SetValue; 23 | import org.mariotaku.sqliteqb.library.Table; 24 | import org.mariotaku.sqliteqb.library.Utils; 25 | 26 | public class SQLUpdateQuery implements SQLQuery { 27 | 28 | private OnConflict onConflict; 29 | private Table table; 30 | private SetValue[] values; 31 | private Expression where; 32 | 33 | SQLUpdateQuery() { 34 | 35 | } 36 | 37 | @Override 38 | public String getSQL() { 39 | if (table == null) throw new NullPointerException("table must not be null!"); 40 | final StringBuilder sb = new StringBuilder(); 41 | sb.append("UPDATE "); 42 | if (onConflict != null) { 43 | sb.append("OR "); 44 | sb.append(onConflict.getAction()); 45 | sb.append(" "); 46 | } 47 | sb.append(table.getSQL()); 48 | sb.append(" SET "); 49 | sb.append(Utils.toString(values, ',', true)); 50 | if (where != null) { 51 | sb.append(" WHERE "); 52 | sb.append(where.getSQL()); 53 | } 54 | return sb.toString(); 55 | } 56 | 57 | void setWhere(final Expression where) { 58 | this.where = where; 59 | } 60 | 61 | void setValues(final SetValue[] columns) { 62 | this.values = columns; 63 | } 64 | 65 | void setOnConflict(final OnConflict onConflict) { 66 | this.onConflict = onConflict; 67 | } 68 | 69 | void setTable(final Table table) { 70 | this.table = table; 71 | } 72 | 73 | public static final class Builder implements IBuilder { 74 | 75 | private final SQLUpdateQuery query = new SQLUpdateQuery(); 76 | 77 | private boolean buildCalled; 78 | 79 | @Override 80 | public SQLUpdateQuery build() { 81 | buildCalled = true; 82 | return query; 83 | } 84 | 85 | @Override 86 | public String buildSQL() { 87 | return build().getSQL(); 88 | } 89 | 90 | public Builder set(final SetValue... values) { 91 | checkNotBuilt(); 92 | query.setValues(values); 93 | return this; 94 | } 95 | 96 | public Builder where(final Expression where) { 97 | checkNotBuilt(); 98 | query.setWhere(where); 99 | return this; 100 | } 101 | 102 | public Builder update(final OnConflict onConflict, final Table table) { 103 | checkNotBuilt(); 104 | query.setOnConflict(onConflict); 105 | query.setTable(table); 106 | return this; 107 | } 108 | 109 | public Builder update(final Table table) { 110 | return update(null, table); 111 | } 112 | 113 | private void checkNotBuilt() { 114 | if (buildCalled) throw new IllegalStateException(); 115 | } 116 | 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /library/src/main/java/org/mariotaku/sqliteqb/library/query/SQLWithSelectQuery.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library.query; 2 | 3 | import org.mariotaku.sqliteqb.library.SQLLang; 4 | import org.mariotaku.sqliteqb.library.Selectable; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * Created by mariotaku on 15/12/14. 11 | */ 12 | public class SQLWithSelectQuery extends SQLSelectQuery { 13 | 14 | private List ctes = new ArrayList<>(); 15 | 16 | @Override 17 | public String getSQL() { 18 | StringBuilder sb = new StringBuilder(); 19 | sb.append("WITH "); 20 | boolean first = true; 21 | for (CTE cte : ctes) { 22 | if (!first) { 23 | sb.append(" ,"); 24 | } 25 | sb.append(cte.getSQL()); 26 | first = false; 27 | } 28 | sb.append(" "); 29 | sb.append(select.getSQL()); 30 | return sb.toString(); 31 | } 32 | 33 | private SQLSelectQuery select; 34 | 35 | void setSelectQuery(SQLSelectQuery select) { 36 | this.select = select; 37 | } 38 | 39 | public static class Builder extends SQLSelectQuery.Builder { 40 | 41 | private final SQLWithSelectQuery internalQuery = new SQLWithSelectQuery(); 42 | 43 | @Override 44 | public SQLWithSelectQuery build() { 45 | internalQuery.setSelectQuery(super.build()); 46 | return internalQuery; 47 | } 48 | 49 | @Override 50 | public String buildSQL() { 51 | return build().getSQL(); 52 | } 53 | 54 | public Builder with(String name, Selectable as) { 55 | internalQuery.with(name, as); 56 | return this; 57 | } 58 | } 59 | 60 | private void with(String name, Selectable as) { 61 | ctes.add(new CTE(name, as)); 62 | } 63 | 64 | static class CTE implements SQLLang { 65 | private final String name; 66 | private final Selectable as; 67 | 68 | CTE(String name, Selectable as) { 69 | this.name = name; 70 | this.as = as; 71 | } 72 | 73 | @Override 74 | public String getSQL() { 75 | return name + " AS (" + as.getSQL() + ")"; 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /library/src/test/java/org/mariotaku/sqliteqb/library/ExpressionTest.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.library; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.mariotaku.sqliteqb.library.Columns.Column; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | 9 | /** 10 | * Created by mariotaku on 16/2/3. 11 | */ 12 | public class ExpressionTest { 13 | 14 | @Before 15 | public void setUp() throws Exception { 16 | } 17 | 18 | @Test 19 | public void testAnd() throws Exception { 20 | 21 | } 22 | 23 | @Test 24 | public void testEquals() throws Exception { 25 | assertEquals(Expression.equals(new Column("a"), new Column("b")).getSQL(), "a = b"); 26 | } 27 | 28 | @Test 29 | public void testEquals1() throws Exception { 30 | assertEquals(Expression.equals(new Column("a"), new Table("b")).getSQL(), "a = (b)"); 31 | } 32 | 33 | @Test 34 | public void testEquals2() throws Exception { 35 | assertEquals(Expression.equals("a", new Table("b")).getSQL(), "a = (b)"); 36 | } 37 | 38 | @Test 39 | public void testEquals3() throws Exception { 40 | 41 | } 42 | 43 | @Test 44 | public void testEquals4() throws Exception { 45 | 46 | } 47 | 48 | @Test 49 | public void testEquals5() throws Exception { 50 | 51 | } 52 | 53 | @Test 54 | public void testGreaterThan() throws Exception { 55 | 56 | } 57 | 58 | @Test 59 | public void testGreaterEquals() throws Exception { 60 | 61 | } 62 | 63 | @Test 64 | public void testLesserEquals() throws Exception { 65 | 66 | } 67 | 68 | @Test 69 | public void testLesserThan() throws Exception { 70 | 71 | } 72 | 73 | @Test 74 | public void testIn() throws Exception { 75 | 76 | } 77 | 78 | @Test 79 | public void testNotEquals() throws Exception { 80 | 81 | } 82 | 83 | @Test 84 | public void testNotEquals1() throws Exception { 85 | 86 | } 87 | 88 | @Test 89 | public void testIsNot() throws Exception { 90 | 91 | } 92 | 93 | @Test 94 | public void testIsNot1() throws Exception { 95 | 96 | } 97 | 98 | @Test 99 | public void testIsNotArgs() throws Exception { 100 | 101 | } 102 | 103 | @Test 104 | public void testNotIn() throws Exception { 105 | 106 | } 107 | 108 | @Test 109 | public void testNotNull() throws Exception { 110 | 111 | } 112 | 113 | @Test 114 | public void testOr() throws Exception { 115 | 116 | } 117 | 118 | @Test 119 | public void testEqualsArgs() throws Exception { 120 | 121 | } 122 | 123 | @Test 124 | public void testInArgs() throws Exception { 125 | 126 | } 127 | 128 | @Test 129 | public void testInArgs1() throws Exception { 130 | 131 | } 132 | 133 | @Test 134 | public void testIsNull() throws Exception { 135 | 136 | } 137 | 138 | @Test 139 | public void testGreaterThan1() throws Exception { 140 | 141 | } 142 | 143 | @Test 144 | public void testLikeRaw() throws Exception { 145 | 146 | } 147 | 148 | @Test 149 | public void testLike() throws Exception { 150 | 151 | } 152 | 153 | @Test 154 | public void testLikeRaw1() throws Exception { 155 | 156 | } 157 | } -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'application' 3 | 4 | mainClassName = 'org.mariotaku.sqliteqb.sample.Main' 5 | 6 | repositories { 7 | jcenter() 8 | } 9 | 10 | dependencies { 11 | compile project(':library') 12 | } 13 | -------------------------------------------------------------------------------- /sample/src/main/java/org/mariotaku/sqliteqb/sample/Main.java: -------------------------------------------------------------------------------- 1 | package org.mariotaku.sqliteqb.sample; 2 | 3 | import org.mariotaku.sqliteqb.library.Columns; 4 | import org.mariotaku.sqliteqb.library.SQLQueryBuilder; 5 | import org.mariotaku.sqliteqb.library.Table; 6 | import org.mariotaku.sqliteqb.library.query.SQLWithSelectQuery; 7 | 8 | /** 9 | * Created by mariotaku on 15/12/14. 10 | */ 11 | public class Main { 12 | public static void main(String[] args) { 13 | SQLWithSelectQuery.Builder builder = SQLQueryBuilder.with("filtered_ids", 14 | SQLQueryBuilder.select(new Columns.Column("user_id")) 15 | .from(new Table("filtered_users")) 16 | .build()); 17 | builder.select(new Columns.AllColumn()); 18 | builder.from(new Table("test1")); 19 | final String sql = builder.buildSQL(); 20 | System.out.println(sql); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 mariotaku 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 | rootProject.name = 'SQLiteQB' 17 | include ':library' 18 | include ':sample' 19 | 20 | --------------------------------------------------------------------------------