Pārlūkot izejas kodu

BETA: DB Factory: Unused code & ProviderType.

Patch by: FBIagent
Reviewed by: jurchiks, Zoey76
Zoey76 11 gadi atpakaļ
vecāks
revīzija
ddc4e3990f

+ 0 - 121
L2J_Server_BETA/java/com/l2jserver/L2DatabaseFactory.java

@@ -19,7 +19,6 @@
 package com.l2jserver;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -37,18 +36,8 @@ public class L2DatabaseFactory
 {
 	private static final Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
 	
-	/**
-	 * The Enum ProviderType.
-	 */
-	private static enum ProviderType
-	{
-		MySql,
-		MsSql
-	}
-	
 	private static L2DatabaseFactory _instance;
 	private static volatile ScheduledExecutorService _executor;
-	private ProviderType _providerType;
 	private ComboPooledDataSource _source;
 	
 	/**
@@ -114,15 +103,6 @@ public class L2DatabaseFactory
 			{
 				_log.fine("Database Connection Working");
 			}
-			
-			if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
-			{
-				_providerType = ProviderType.MsSql;
-			}
-			else
-			{
-				_providerType = ProviderType.MySql;
-			}
 		}
 		catch (SQLException x)
 		{
@@ -143,33 +123,6 @@ public class L2DatabaseFactory
 		}
 	}
 	
-	/**
-	 * Prepared query select.
-	 * @param fields the fields
-	 * @param tableName the table name
-	 * @param whereClause the where clause
-	 * @param returnOnlyTopRecord the return only top record
-	 * @return the string
-	 */
-	public final String prepQuerySelect(String[] fields, String tableName, String whereClause, boolean returnOnlyTopRecord)
-	{
-		String msSqlTop1 = "";
-		String mySqlTop1 = "";
-		if (returnOnlyTopRecord)
-		{
-			if (getProviderType() == ProviderType.MsSql)
-			{
-				msSqlTop1 = " Top 1 ";
-			}
-			if (getProviderType() == ProviderType.MySql)
-			{
-				mySqlTop1 = " Limit 1 ";
-			}
-		}
-		String query = "SELECT " + msSqlTop1 + safetyString(fields) + " FROM " + tableName + " WHERE " + whereClause + mySqlTop1;
-		return query;
-	}
-	
 	/**
 	 * Shutdown.
 	 */
@@ -193,52 +146,6 @@ public class L2DatabaseFactory
 		}
 	}
 	
-	/**
-	 * Safety string.
-	 * @param whatToCheck the what to check
-	 * @return the string
-	 */
-	public final String safetyString(String... whatToCheck)
-	{
-		// NOTE: Use brace as a safety precaution just in case name is a reserved word
-		final char braceLeft;
-		final char braceRight;
-		
-		if (getProviderType() == ProviderType.MsSql)
-		{
-			braceLeft = '[';
-			braceRight = ']';
-		}
-		else
-		{
-			braceLeft = '`';
-			braceRight = '`';
-		}
-		
-		int length = 0;
-		
-		for (String word : whatToCheck)
-		{
-			length += word.length() + 4;
-		}
-		
-		final StringBuilder sbResult = new StringBuilder(length);
-		
-		for (String word : whatToCheck)
-		{
-			if (sbResult.length() > 0)
-			{
-				sbResult.append(", ");
-			}
-			
-			sbResult.append(braceLeft);
-			sbResult.append(word);
-			sbResult.append(braceRight);
-		}
-		
-		return sbResult.toString();
-	}
-	
 	/**
 	 * Gets the single instance of L2DatabaseFactory.
 	 * @return single instance of L2DatabaseFactory
@@ -364,32 +271,4 @@ public class L2DatabaseFactory
 	{
 		return _source.getNumIdleConnectionsDefaultUser();
 	}
-	
-	/**
-	 * Gets the provider type.
-	 * @return the provider type
-	 */
-	public final ProviderType getProviderType()
-	{
-		return _providerType;
-	}
-	
-	/**
-	 * Designed to execute simple db operations like INSERT, UPDATE, DELETE.
-	 * @param query
-	 * @param params
-	 * @throws SQLException
-	 */
-	public void executeQuery(String query, Object... params) throws SQLException
-	{
-		try (Connection con = getConnection();
-			PreparedStatement st = con.prepareStatement(query))
-		{
-			for (int i = 0; i < params.length; i++)
-			{
-				st.setObject(i + 1, params[i]);
-			}
-			st.execute();
-		}
-	}
 }

+ 3 - 11
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/HerbDropTable.java

@@ -19,8 +19,8 @@
 package com.l2jserver.gameserver.datatables;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -50,16 +50,8 @@ public class HerbDropTable
 	private void restoreData()
 	{
 		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
-			PreparedStatement statement = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
-			{
-				"groupId",
-				"itemId",
-				"min",
-				"max",
-				"category",
-				"chance"
-			}) + " FROM herb_droplist_groups ORDER BY groupId, chance DESC");
-			ResultSet dropData = statement.executeQuery())
+			Statement statement = con.createStatement();
+			ResultSet dropData = statement.executeQuery("SELECT groupId,itemId,min,max,category,chance FROM herb_droplist_groups ORDER BY groupId,chance DESC"))
 		{
 			L2DropData dropDat = null;
 			while (dropData.next())

+ 23 - 22
L2J_Server_BETA/java/com/l2jserver/gameserver/datatables/TerritoryTable.java

@@ -18,12 +18,17 @@
  */
 package com.l2jserver.gameserver.datatables;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import com.l2jserver.L2DatabaseFactory;
 import com.l2jserver.gameserver.model.L2Territory;
-import com.l2jserver.util.lib.SqlUtils;
 
 /**
  * @author Balancer, Mr
@@ -68,30 +73,26 @@ public class TerritoryTable
 	public void load()
 	{
 		_territory.clear();
-		Integer[][] point = SqlUtils.get2DIntArray(new String[]
+		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
+			Statement stmt = con.createStatement();
+			ResultSet rset = stmt.executeQuery("SELECT * FROM locations WHERE loc_id>0"))
 		{
-			"loc_id",
-			"loc_x",
-			"loc_y",
-			"loc_zmin",
-			"loc_zmax",
-			"proc"
-		}, "locations", "loc_id > 0");
-		for (Integer[] row : point)
-		{
-			Integer terr = row[0];
-			if (terr == null)
-			{
-				_log.warning(getClass().getSimpleName() + ": Null territory!");
-				continue;
-			}
-			
-			if (_territory.get(terr) == null)
+			while (rset.next())
 			{
-				L2Territory t = new L2Territory(terr);
-				_territory.put(terr, t);
+				int terrId = rset.getInt("loc_id");
+				L2Territory terr = _territory.get(terrId);
+				if (terr == null)
+				{
+					terr = new L2Territory(terrId);
+					_territory.put(terrId, terr);
+				}
+				terr.add(rset.getInt("loc_x"), rset.getInt("loc_y"), rset.getInt("loc_zmin"), rset.getInt("loc_zmax"), rset.getInt("proc"));
 			}
-			_territory.get(terr).add(row[1], row[2], row[3], row[4], row[5]);
+			_log.info("TerritoryTable: Loaded " + _territory.size() + " territories from database.");
+		}
+		catch (SQLException e)
+		{
+			_log.log(Level.SEVERE, "TerritoryTable: Failed to load territories from database!", e);
 		}
 	}
 	

+ 1 - 1
L2J_Server_BETA/java/com/l2jserver/gameserver/scripting/scriptengine/events/TransformEvent.java

@@ -76,6 +76,6 @@ public class TransformEvent implements L2Event
 	 */
 	public void setTransforming(boolean transforming)
 	{
-		this._transforming = transforming;
+		_transforming = transforming;
 	}
 }

+ 0 - 154
L2J_Server_BETA/java/com/l2jserver/util/lib/SqlUtils.java

@@ -1,154 +0,0 @@
-/*
- * Copyright (C) 2004-2013 L2J Server
- * 
- * This file is part of L2J Server.
- * 
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- * 
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package com.l2jserver.util.lib;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import com.l2jserver.L2DatabaseFactory;
-
-public class SqlUtils
-{
-	private static Logger _log = Logger.getLogger(SqlUtils.class.getName());
-	
-	public static SqlUtils getInstance()
-	{
-		return SingletonHolder._instance;
-	}
-	
-	public static Integer getIntValue(String resultField, String tableName, String whereClause)
-	{
-		String query = "";
-		Integer res = null;
-		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
-		{
-			query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
-			{
-				resultField
-			}, tableName, whereClause, true);
-			
-			try (PreparedStatement ps = con.prepareStatement(query);
-				ResultSet rs = ps.executeQuery())
-			{
-				if (rs.next())
-				{
-					res = rs.getInt(1);
-				}
-			}
-		}
-		catch (Exception e)
-		{
-			_log.log(Level.WARNING, "Error in query '" + query + "':", e);
-		}
-		return res;
-	}
-	
-	public static Integer[] getIntArray(String resultField, String tableName, String whereClause)
-	{
-		String query = "";
-		Integer[] res = null;
-		try
-		{
-			query = L2DatabaseFactory.getInstance().prepQuerySelect(new String[]
-			{
-				resultField
-			}, tableName, whereClause, false);
-			
-			try (Connection con = L2DatabaseFactory.getInstance().getConnection();
-				PreparedStatement ps = con.prepareStatement(query);
-				ResultSet rs = ps.executeQuery())
-			{
-				int rows = 0;
-				while (rs.next())
-				{
-					rows++;
-				}
-				
-				if (rows == 0)
-				{
-					return new Integer[0];
-				}
-				
-				res = new Integer[rows - 1];
-				
-				rs.first();
-				
-				int row = 0;
-				while (rs.next())
-				{
-					res[row] = rs.getInt(1);
-				}
-			}
-		}
-		catch (Exception e)
-		{
-			_log.log(Level.WARNING, "mSGI: Error in query '" + query + "':", e);
-		}
-		return res;
-	}
-	
-	public static Integer[][] get2DIntArray(String[] resultFields, String usedTables, String whereClause)
-	{
-		long start = System.currentTimeMillis();
-		String query = "";
-		Integer res[][] = null;
-		try
-		{
-			query = L2DatabaseFactory.getInstance().prepQuerySelect(resultFields, usedTables, whereClause, false);
-			try (Connection con = L2DatabaseFactory.getInstance().getConnection();
-				PreparedStatement ps = con.prepareStatement(query);
-				ResultSet rs = ps.executeQuery())
-			{
-				int rows = 0;
-				while (rs.next())
-				{
-					rows++;
-				}
-				
-				res = new Integer[rows - 1][resultFields.length];
-				
-				rs.first();
-				
-				int row = 0;
-				while (rs.next())
-				{
-					for (int i = 0; i < resultFields.length; i++)
-					{
-						res[row][i] = rs.getInt(i + 1);
-					}
-					row++;
-				}
-			}
-		}
-		catch (Exception e)
-		{
-			_log.log(Level.WARNING, "Error in query '" + query + "':", e);
-		}
-		_log.fine("Get all rows in query '" + query + "' in " + (System.currentTimeMillis() - start) + "ms");
-		return res;
-	}
-	
-	private static class SingletonHolder
-	{
-		protected static final SqlUtils _instance = new SqlUtils();
-	}
-}