All Downloads are FREE. Search and download functionalities are using the official Maven repository.

space.lingu.light.sql.AsciiSQLGenerator Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/*
 * Copyright (C) 2022 Lingu Light Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package space.lingu.light.sql;

import space.lingu.light.OnConflictStrategy;
import space.lingu.light.util.StringUtil;

import java.util.StringJoiner;

/**
 * Standard ASCII SQL statement generator.
 * 

* Also, can use as a base class for other database extensions. * * @author RollW */ public class AsciiSQLGenerator implements SQLGenerator { public AsciiSQLGenerator() { } @Override public String insert(String tableName, String... valueArgs) { return insert(tableName, OnConflictStrategy.ABORT, valueArgs); } @Override public String insert(String tableName, OnConflictStrategy onConflict, String... valueArgs) { // INSERT INTO `tableName` (`valueArg1`, `valueArg2`) VALUES (?, ?) return buildInsertWithStart(tableName, "INSERT", valueArgs); } protected String buildInsertWithStart(String tableName, String start, String... valueArgs) { if (StringUtil.isEmpty(tableName)) { return null; } StringBuilder builder = new StringBuilder(start) .append(" INTO ") .append(escapeParam(tableName)) .append(" ("); StringJoiner valueArgJoiner = new StringJoiner(", "); for (String valueArg : valueArgs) { valueArgJoiner.add(escapeParam(valueArg)); } builder.append(valueArgJoiner).append(") VALUES "); if (valueArgs.length == 1) { builder.append("(?)"); return builder.toString(); } for (int i = 0; i < valueArgs.length; i++) { if (i == 0) { builder.append("(?"); } else if (i < valueArgs.length - 1) { builder.append(", ?"); } else { builder.append(", ?)"); } } return builder.toString(); } @Override public String delete(String tableName, String... conditions) { // DELETE FROM `tableName` WHERE `condition1` =? AND `condition2` =? if (StringUtil.isEmpty(tableName)) { return null; } StringBuilder builder = new StringBuilder("DELETE FROM ") .append(escapeParam(tableName)).append(" "); return builder.append(wheres(conditions)).toString(); } private String wheres(String[] wheres) { StringBuilder whereBuilder = new StringBuilder(); if (wheres.length == 0) { return whereBuilder.toString(); } whereBuilder.append(" WHERE "); StringJoiner whereJoiner = new StringJoiner(" AND "); for (String where : wheres) { whereJoiner.add(escapeParam(where) + "=? "); } whereBuilder.append(whereJoiner); return whereBuilder.toString(); } @Override public String update(String tableName, String[] whereConditions, String[] valueArgs) { return buildUpdateWithStart(tableName, "UPDATE", whereConditions, valueArgs); } @Override public String update(String tableName, OnConflictStrategy onConflict, String[] whereConditions, String[] valueArgs) { return update(tableName, whereConditions, valueArgs); } protected String buildUpdateWithStart(String tableName, String start, String[] whereConditions, String[] valueArgs) { if (StringUtil.isEmpty(tableName) || whereConditions == null || valueArgs == null) { return null; } StringBuilder builder = new StringBuilder(start) .append(" ") .append(escapeParam(tableName)) .append(" "); builder.append("SET "); StringJoiner valueJoiner = new StringJoiner(", "); for (String valueArg : valueArgs) { valueJoiner.add(escapeParam(valueArg) + "=?"); } builder.append(valueJoiner); return builder.append(wheres(whereConditions)).toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy