001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.database; 020 021import com.plotsquared.core.util.StringMan; 022 023import java.sql.PreparedStatement; 024import java.sql.SQLException; 025import java.util.stream.Collectors; 026import java.util.stream.IntStream; 027 028public abstract class StmtMod<T> { 029 030 public abstract String getCreateMySQL(int size); 031 032 public String getCreateMySQL(int size, String query, int params) { 033 StringBuilder statement = new StringBuilder(query); 034 for (int i = 0; i < size - 1; i++) { 035 statement.append('(').append(StringMan.repeat(",?", params).substring(1)).append("),"); 036 } 037 statement.append('(').append(StringMan.repeat(",?", params).substring(1)).append(')'); 038 return statement.toString(); 039 } 040 041 public String getCreateSQLite(int size, String query, int params) { 042 String modParams = StringMan.repeat(",?", params).substring(1); 043 return IntStream.range(0, size - 1).mapToObj(i -> "UNION SELECT " + modParams + ' ') 044 .collect(Collectors.joining("", query, "")); 045 } 046 047 public abstract String getCreateSQLite(int size); 048 049 public abstract String getCreateSQL(); 050 051 public abstract void setMySQL(PreparedStatement stmt, int i, T obj) throws SQLException; 052 053 public abstract void setSQLite(PreparedStatement stmt, int i, T obj) throws SQLException; 054 055 public abstract void setSQL(PreparedStatement stmt, T obj) throws SQLException; 056 057}