001    /*
002     @license.text@ 
003     */
004    package biz.hammurapi.sql;
005    
006    import java.sql.PreparedStatement;
007    import java.sql.SQLException;
008    import java.util.StringTokenizer;
009    
010    import biz.hammurapi.legacy.persistence.PersistenceException;
011    import biz.hammurapi.legacy.persistence.Storage;
012    
013    /**
014     * @author Pavel Vlasov
015     * @version $Revision: 1.1 $
016     */
017    public class JdbcStorage implements Storage {
018            
019            private SQLProcessor processor;
020    
021            public JdbcStorage(SQLProcessor processor) {
022                    this.processor=processor;
023            }
024            
025            public SQLProcessor getProcessor() {
026                    return processor;
027            }
028    
029            /**
030             * Key format should be:
031             * {table}:{key field}:{key value}:{projector}
032             */
033            public String put(Object o) throws PersistenceException {
034                    if (o instanceof JdbcPersistable) {
035                            return ((JdbcPersistable) o).store(this);
036                    }
037                    return null;
038            }
039    
040            public Object get(String key) throws PersistenceException {
041                    final StringTokenizer st=new StringTokenizer(key, ":");
042                    try {
043                            return processor.projectSingleObject(
044                                                    "DELETE FROM "+st.nextToken()+" WHERE "+st.nextToken()+"=?",
045                                                    new Parameterizer() {
046                                                            public void parameterize(PreparedStatement ps) throws SQLException {
047                                                                    ps.setString(1, st.nextToken());
048                                                            }
049                                                    }, 
050                                                    (Projector) Class.forName(st.nextToken()).newInstance());
051                    } catch (InstantiationException e) {
052                            throw new PersistenceException(e);
053                    } catch (IllegalAccessException e) {
054                            throw new PersistenceException(e);
055                    } catch (ClassNotFoundException e) {
056                            throw new PersistenceException(e);
057                    } catch (SQLException e) {
058                            throw new PersistenceException(e);
059                    }
060            }
061    
062            public void remove(String key) throws PersistenceException {
063                    final StringTokenizer st=new StringTokenizer(key, ":");
064                    try {
065                            processor.processUpdate(
066                                            "DELETE FROM "+st.nextToken()+" WHERE "+st.nextToken()+"=?",
067                                            new Parameterizer() {
068                                                    public void parameterize(PreparedStatement ps) throws SQLException {
069                                                            ps.setString(1, st.nextToken());
070                                                    }
071                                            });
072                    } catch (SQLException e) {
073                            throw new PersistenceException(e);
074                    }
075            }
076    
077    }