AccountVariables.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2004-2013 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.variables;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.Map.Entry;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.L2DatabaseFactory;
  28. /**
  29. * @author UnAfraid
  30. */
  31. public class AccountVariables extends AbstractVariables
  32. {
  33. private static final Logger _log = Logger.getLogger(AccountVariables.class.getName());
  34. // SQL Queries.
  35. private static final String SELECT_QUERY = "SELECT * FROM account_gsdata WHERE account_name = ?";
  36. private static final String DELETE_QUERY = "DELETE FROM account_gsdata WHERE account_name = ?";
  37. private static final String INSERT_QUERY = "INSERT INTO account_gsdata (account_name, var, value) VALUES (?, ?, ?)";
  38. private final String _accountName;
  39. public AccountVariables(String accountName)
  40. {
  41. _accountName = accountName;
  42. load();
  43. }
  44. @Override
  45. protected void load()
  46. {
  47. // Restore previous variables.
  48. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  49. PreparedStatement st = con.prepareStatement(SELECT_QUERY))
  50. {
  51. st.setString(1, _accountName);
  52. try (ResultSet rset = st.executeQuery())
  53. {
  54. while (rset.next())
  55. {
  56. set(rset.getString("var"), rset.getString("value"));
  57. }
  58. }
  59. }
  60. catch (SQLException e)
  61. {
  62. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore variables for: " + _accountName, e);
  63. }
  64. finally
  65. {
  66. compareAndSetChanges(true, false);
  67. }
  68. }
  69. @Override
  70. public void store()
  71. {
  72. // No changes, nothing to store.
  73. if (!hasChanges())
  74. {
  75. return;
  76. }
  77. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  78. {
  79. // Clear previous entries.
  80. try (PreparedStatement st = con.prepareStatement(DELETE_QUERY))
  81. {
  82. st.setString(1, _accountName);
  83. st.execute();
  84. }
  85. // Insert all variables.
  86. try (PreparedStatement st = con.prepareStatement(INSERT_QUERY))
  87. {
  88. st.setString(1, _accountName);
  89. for (Entry<String, Object> entry : getSet().entrySet())
  90. {
  91. st.setString(2, entry.getKey());
  92. st.setString(3, String.valueOf(entry.getValue()));
  93. st.addBatch();
  94. }
  95. st.executeBatch();
  96. }
  97. }
  98. catch (SQLException e)
  99. {
  100. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update variables for: " + _accountName, e);
  101. }
  102. finally
  103. {
  104. compareAndSetChanges(true, false);
  105. }
  106. }
  107. }