| 30 | import org.parosproxy.paros.db.TableTag; |
| 31 | |
| 32 | public class SqlTableTag extends SqlAbstractTable implements TableTag { |
| 33 | |
| 34 | private static final String TABLE_NAME = DbSQL.getSQL("tag.table_name"); |
| 35 | |
| 36 | private static final String TAGID = DbSQL.getSQL("tag.field.tagid"); |
| 37 | private static final String HISTORYID = DbSQL.getSQL("tag.field.historyid"); |
| 38 | private static final String TAG = DbSQL.getSQL("tag.field.tag"); |
| 39 | |
| 40 | public SqlTableTag() {} |
| 41 | |
| 42 | @Override |
| 43 | protected void reconnect(Connection conn) throws DatabaseException { |
| 44 | try { |
| 45 | if (!DbUtils.hasTable(conn, TABLE_NAME)) { |
| 46 | // Need to create the table |
| 47 | DbUtils.execute(conn, DbSQL.getSQL("tag.ps.createtable")); |
| 48 | } |
| 49 | } catch (SQLException e) { |
| 50 | throw new DatabaseException(e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public synchronized RecordTag read(long tagId) throws DatabaseException { |
| 56 | SqlPreparedStatementWrapper psRead = null; |
| 57 | try { |
| 58 | psRead = DbSQL.getSingleton().getPreparedStatement("tag.ps.read"); |
| 59 | psRead.getPs().setLong(1, tagId); |
| 60 | |
| 61 | try (ResultSet rs = psRead.getPs().executeQuery()) { |
| 62 | return build(rs); |
| 63 | } |
| 64 | } catch (SQLException e) { |
| 65 | throw new DatabaseException(e); |
| 66 | } finally { |
| 67 | DbSQL.getSingleton().releasePreparedStatement(psRead); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public synchronized RecordTag insert(long historyId, String tag) throws DatabaseException { |
| 73 | SqlPreparedStatementWrapper psInsertTag = null; |
| 74 | try { |
| 75 | psInsertTag = DbSQL.getSingleton().getPreparedStatement("tag.ps.insert"); |
| 76 | psInsertTag.getPs().setLong(1, historyId); |
| 77 | psInsertTag.getPs().setString(2, tag); |
| 78 | psInsertTag.getPs().executeUpdate(); |
| 79 | |
| 80 | try (ResultSet rs = psInsertTag.getLastInsertedId()) { |
| 81 | rs.next(); |
| 82 | long id = rs.getLong(1); |
| 83 | return read(id); |
| 84 | } |
| 85 | } catch (SQLException e) { |
| 86 | throw new DatabaseException(e); |
| 87 | } finally { |
| 88 | DbSQL.getSingleton().releasePreparedStatement(psInsertTag); |
| 89 | } |