|
| 1 | +/*Copyright ©2025 APIJSON(https://github.com/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson; |
| 16 | + |
| 17 | +public class SQLUtil { |
| 18 | + public static boolean isRead(String sql) { |
| 19 | + return isRead(sql, false); |
| 20 | + } |
| 21 | + public static boolean isRead(String sql, boolean isRedis) { |
| 22 | + return ! isWrite(sql, isRedis); |
| 23 | + } |
| 24 | + |
| 25 | + public static boolean isWrite(String sql) { |
| 26 | + return isWrite(sql, false); |
| 27 | + } |
| 28 | + public static boolean isWrite(String sql, boolean isRedis) { |
| 29 | + return isInsert(sql, isRedis) || isUpdate(sql, isRedis) || isDelete(sql, isRedis); |
| 30 | + } |
| 31 | + |
| 32 | + public static boolean isSelect(String sql) { |
| 33 | + return isSelect(sql, false); |
| 34 | + } |
| 35 | + public static boolean isSelect(String sql, boolean isRedis) { |
| 36 | + return sql != null && ! (isInsert(sql, isRedis) || isUpdate(sql, isRedis) || isDelete(sql, isRedis)); |
| 37 | + } |
| 38 | + |
| 39 | + public static boolean isInsert(String sql) { |
| 40 | + return isInsert(sql, false); |
| 41 | + } |
| 42 | + public static boolean isInsert(String sql, boolean isRedis) { |
| 43 | + return sql != null && ((sql.startsWith("INSERT ") && ! isRedis) || (isRedis && ( |
| 44 | + sql.startsWith("SADD ") || sql.startsWith("ZADD ") |
| 45 | + || sql.startsWith("LPUSH ") || sql.startsWith("RPUSH ") |
| 46 | + ))); |
| 47 | + } |
| 48 | + |
| 49 | + public static boolean isUpdate(String sql) { |
| 50 | + return isUpdate(sql, false); |
| 51 | + } |
| 52 | + public static boolean isUpdate(String sql, boolean isRedis) { |
| 53 | + return sql != null && ((sql.startsWith("UPDATE ") && ! isRedis) || (isRedis && (sql.startsWith("SET ") |
| 54 | + || sql.startsWith("HSET ") || sql.startsWith("MSET ") |
| 55 | + ))); |
| 56 | + } |
| 57 | + |
| 58 | + public static boolean isDelete(String sql) { |
| 59 | + return isDelete(sql, false); |
| 60 | + } |
| 61 | + public static boolean isDelete(String sql, boolean isRedis) { |
| 62 | + return sql != null && ((sql.startsWith("DELETE ") && ! isRedis) |
| 63 | + || (isRedis && (sql.startsWith("DEL ") || sql.startsWith("HDEL ") || sql.startsWith("MDEL ") |
| 64 | + || sql.startsWith("SREM ") || sql.startsWith("ZREM ") |
| 65 | + || sql.startsWith("LPOP ") || sql.startsWith("RPOP ") |
| 66 | + || sql.startsWith("FLUSHDB ") || sql.startsWith("FLUSHALL ") |
| 67 | + ))); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments