1 package com.atlassian.activeobjects.backup;
2
3 import com.atlassian.dbexporter.Column;
4 import com.atlassian.dbexporter.ImportExportErrorService;
5 import com.atlassian.dbexporter.Table;
6 import com.google.common.base.Function;
7 import com.google.common.base.Predicate;
8 import net.java.ao.util.StringUtils;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.sql.Connection;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16
17 import static com.atlassian.dbexporter.jdbc.JdbcUtils.closeQuietly;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static com.google.common.collect.Iterables.concat;
20 import static com.google.common.collect.Iterables.filter;
21 import static com.google.common.collect.Iterables.transform;
22
23 final class SqlUtils {
24 private static final Logger LOGGER = LoggerFactory.getLogger("net.java.ao.sql");
25
26 static Iterable<TableColumnPair> tableColumnPairs(Iterable<Table> tables) {
27 return concat(transform(tables, new AutoIncrementColumnIterableFunction()));
28 }
29
30 static void executeUpdate(ImportExportErrorService errorService, String tableName, Statement s, String sql) {
31 try {
32 if (!StringUtils.isBlank(sql)) {
33 LOGGER.debug(sql);
34 s.executeUpdate(sql);
35 }
36 } catch (SQLException e) {
37 onSqlException(errorService, tableName, sql, e);
38 }
39 }
40
41 private static void onSqlException(ImportExportErrorService errorService, String table, String sql, SQLException e) {
42 if (sql.startsWith("DROP") && e.getMessage().contains("does not exist")) {
43 LOGGER.debug("Ignoring exception for SQL <" + sql + ">", e);
44 return;
45 }
46 throw errorService.newImportExportSqlException(table, "Error executing update for SQL statement '" + sql + "'", e);
47 }
48
49 static void executeUpdate(ImportExportErrorService errorService, String tableName, Connection connection, String sql) {
50 Statement stmt = null;
51 try {
52 stmt = connection.createStatement();
53 executeUpdate(errorService, tableName, stmt, sql);
54 } catch (SQLException e) {
55 throw errorService.newImportExportSqlException(tableName, "", e);
56 } finally {
57 closeQuietly(stmt);
58 }
59 }
60
61 static int getIntFromResultSet(ImportExportErrorService errorService, String tableName, ResultSet res) {
62 try {
63 return res.next() ? res.getInt(1) : 1;
64 } catch (SQLException e) {
65 throw errorService.newImportExportSqlException(tableName, "Error getting int value from result set.", e);
66 }
67 }
68
69 static ResultSet executeQuery(ImportExportErrorService errorService, String tableName, Statement s, String sql) {
70 try {
71 return s.executeQuery(sql);
72 } catch (SQLException e) {
73 throw errorService.newImportExportSqlException(tableName, "Error executing query for SQL statement '" + sql + "'", e);
74 }
75 }
76
77 static class TableColumnPair {
78 final Table table;
79 final Column column;
80
81 public TableColumnPair(Table table, Column column) {
82 this.table = checkNotNull(table);
83 this.column = checkNotNull(column);
84 }
85 }
86
87 private static class AutoIncrementColumnIterableFunction implements Function<Table, Iterable<TableColumnPair>> {
88 @Override
89 public Iterable<TableColumnPair> apply(final Table table) {
90 return transform(filter(table.getColumns(), new IsAutoIncrementColumn()), new Function<Column, TableColumnPair>() {
91 @Override
92 public TableColumnPair apply(Column column) {
93 return new TableColumnPair(table, column);
94 }
95 });
96 }
97 }
98
99 private static class IsAutoIncrementColumn implements Predicate<Column> {
100 @Override
101 public boolean apply(Column column) {
102 return column.isAutoIncrement();
103 }
104 }
105 }