SQL query parsing
This commit is contained in:
		| @@ -1,18 +1,16 @@ | |||||||
| #modules: |  | ||||||
| #  - cz.marwland.mc.features.Borders |  | ||||||
| dataSource: | dataSource: | ||||||
|   method: 'mariadb'  |   method: mariadb | ||||||
|   address: 'localhost' |   address: localhost | ||||||
|   database: 'factions' |   database: factions | ||||||
|   username: 'root' |   username: root | ||||||
|   password: '' |   password: '' | ||||||
|   table-prefix: 'f_' |   table-prefix: f_ | ||||||
|    |  | ||||||
|   pool-settings: |   pool-settings: | ||||||
|     maximum-pool-size: 10 |     maximum-pool-size: 10 | ||||||
|     minimum-idle: 10 |     minimum-idle: 10 | ||||||
|     maximum-lifetime: 1800000 # 30 minutes |     maximum-lifetime: 1800000 | ||||||
|     connection-timeout: 5000 # 5 seconds |     connection-timeout: 5000 | ||||||
|     properties: |     properties: | ||||||
|       useUnicode: true |       useUnicode: true | ||||||
|       characterEncoding: utf8 |       characterEncoding: utf8 | ||||||
|  | modules: | ||||||
|   | |||||||
| @@ -1,5 +1,13 @@ | |||||||
| package cz.marwland.mc.core.storage; | package cz.marwland.mc.core.storage; | ||||||
|  |  | ||||||
|  | import java.io.BufferedReader; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.io.InputStreamReader; | ||||||
|  | import java.nio.charset.StandardCharsets; | ||||||
|  | import java.sql.Connection; | ||||||
|  | import java.sql.SQLException; | ||||||
|  | import java.sql.Statement; | ||||||
| import java.util.function.Function; | import java.util.function.Function; | ||||||
|  |  | ||||||
| public class SQLStorage { | public class SQLStorage { | ||||||
| @@ -23,8 +31,8 @@ public class SQLStorage { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public Function<String, String> getStatementProcessor() { | 	public Function<String, String> getStatementProcessor() { | ||||||
|         return this.statementProcessor; | 		return this.statementProcessor; | ||||||
|     } | 	} | ||||||
|  |  | ||||||
| 	public void init() { | 	public void init() { | ||||||
| 		this.connectionFactory.init(); | 		this.connectionFactory.init(); | ||||||
| @@ -37,4 +45,67 @@ public class SQLStorage { | |||||||
| 			ex.printStackTrace(); | 			ex.printStackTrace(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	public void executeRaw(InputStream is) throws SQLException, IOException { | ||||||
|  | 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { | ||||||
|  | 			try (Connection connection = this.connectionFactory.getConnection()) { | ||||||
|  | 				try (Statement s = connection.createStatement()) { | ||||||
|  | 					StringBuilder sb = new StringBuilder(); | ||||||
|  | 					String line; | ||||||
|  | 					while ((line = reader.readLine()) != null) { | ||||||
|  | 						if (line.startsWith("--") || line.startsWith("#")) | ||||||
|  | 							continue; | ||||||
|  |  | ||||||
|  | 						sb.append(line); | ||||||
|  |  | ||||||
|  | 						// check for end of declaration | ||||||
|  | 						if (line.endsWith(";")) { | ||||||
|  | 							sb.deleteCharAt(sb.length() - 1); | ||||||
|  |  | ||||||
|  | 							String result = this.statementProcessor.apply(sb.toString().trim()); | ||||||
|  | 							if (!result.isEmpty()) | ||||||
|  | 								s.addBatch(result); | ||||||
|  |  | ||||||
|  | 							// reset | ||||||
|  | 							sb = new StringBuilder(); | ||||||
|  | 						} | ||||||
|  | 					} | ||||||
|  | 					s.executeBatch(); | ||||||
|  | 				} finally { | ||||||
|  | 					connection.close(); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} finally { | ||||||
|  | 			is.close(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public void executeRaw(String sql) throws SQLException, IOException { | ||||||
|  | 		try (Connection connection = this.connectionFactory.getConnection()) { | ||||||
|  | 			try (Statement s = connection.createStatement()) { | ||||||
|  | 				StringBuilder sb = new StringBuilder(); | ||||||
|  | 				for (String line : sql.split("\n")) { | ||||||
|  | 					if (line.startsWith("--") || line.startsWith("#")) | ||||||
|  | 						continue; | ||||||
|  |  | ||||||
|  | 					sb.append(line); | ||||||
|  |  | ||||||
|  | 					// check for end of declaration | ||||||
|  | 					if (line.endsWith(";")) { | ||||||
|  | 						sb.deleteCharAt(sb.length() - 1); | ||||||
|  |  | ||||||
|  | 						String result = this.statementProcessor.apply(sb.toString().trim()); | ||||||
|  | 						if (!result.isEmpty()) | ||||||
|  | 							s.addBatch(result); | ||||||
|  |  | ||||||
|  | 						// reset | ||||||
|  | 						sb = new StringBuilder(); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				s.executeBatch(); | ||||||
|  | 			} finally { | ||||||
|  | 				connection.close(); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user