001    package railo.runtime.db.driver.state;
002    
003    import java.sql.PreparedStatement;
004    import java.sql.ResultSet;
005    import java.sql.SQLException;
006    import java.sql.Statement;
007    
008    import railo.runtime.PageContext;
009    import railo.runtime.PageContextImpl;
010    import railo.runtime.debug.ActiveQuery;
011    
012    public class StateUtil {
013            
014            public static boolean execute(PageContext pc, Statement stat, String sql) throws SQLException {
015                    if(pc==null) return stat.execute(sql);
016                    PageContextImpl pci = (PageContextImpl)pc;
017                    try {
018                            setActiveStatement(pci,stat,sql);
019                             return stat.execute(sql);
020                    }
021                    finally {
022                            pci.releaseActiveQuery();
023                    }
024            }
025    
026            public static boolean execute(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException {
027                    if(pc==null) return stat.execute(sql,autoGeneratedKeys);
028                    PageContextImpl pci = (PageContextImpl) pc;
029                    try {
030                            setActiveStatement(pci,stat,sql);
031                             return stat.execute(sql,autoGeneratedKeys);
032                    }
033                    finally {
034                            pci.releaseActiveQuery();
035                    }
036            }
037    
038            public static boolean execute(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException {
039                    if(pc==null) return stat.execute(sql,columnIndexes);
040                    PageContextImpl pci = (PageContextImpl) pc;
041                    try {
042                            setActiveStatement(pci,stat,sql);
043                            return stat.execute(sql,columnIndexes);
044                    }
045                    finally {
046                            pci.releaseActiveQuery();
047                    }
048            }
049    
050            public static boolean execute(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException {
051                    if(pc==null) return stat.execute(sql,columnNames);
052                    PageContextImpl pci = (PageContextImpl) pc;
053                    try {
054                            setActiveStatement(pci,stat,sql);
055                            return stat.execute(sql,columnNames);
056                    }
057                    finally {
058                            pci.releaseActiveQuery();
059                    }
060            }
061    
062            public static ResultSet executeQuery(PageContext pc, Statement stat, String sql) throws SQLException {
063                    if(pc==null) return stat.executeQuery(sql);
064                    PageContextImpl pci = (PageContextImpl) pc;
065                    try {
066                            setActiveStatement(pci,stat,sql);
067                            return stat.executeQuery(sql);
068                    }
069                    finally {
070                            pci.releaseActiveQuery();
071                    }
072            }
073    
074            public static int executeUpdate(PageContext pc, Statement stat, String sql) throws SQLException {
075                    if(pc==null) return stat.executeUpdate(sql);
076                    PageContextImpl pci = (PageContextImpl) pc;
077                    try {
078                            setActiveStatement(pci,stat,sql);
079                            return stat.executeUpdate(sql);
080                    }
081                    finally {
082                            pci.releaseActiveQuery();
083                    }
084            }
085    
086            public static int executeUpdate(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException {
087                    if(pc==null) return stat.executeUpdate(sql,autoGeneratedKeys);
088                    PageContextImpl pci = (PageContextImpl) pc;
089                    try {
090                            setActiveStatement(pci,stat,sql);
091                            return stat.executeUpdate(sql,autoGeneratedKeys);
092                    }
093                    finally {
094                            pci.releaseActiveQuery();
095                    }
096            }
097    
098            public static int executeUpdate(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException {
099                    if(pc==null) return stat.executeUpdate(sql,columnIndexes);
100                    PageContextImpl pci = (PageContextImpl) pc;
101                    try {
102                            setActiveStatement(pci,stat,sql);
103                            return stat.executeUpdate(sql,columnIndexes);
104                    }
105                    finally {
106                            pci.releaseActiveQuery();
107                    }
108            }
109    
110            public static int executeUpdate(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException {
111                    if(pc==null) return stat.executeUpdate(sql,columnNames);
112                    PageContextImpl pci = (PageContextImpl) pc;
113                    try {
114                            setActiveStatement(pci,stat,sql);
115                            return stat.executeUpdate(sql,columnNames);
116                    }
117                    finally {
118                            pci.releaseActiveQuery();
119                    }
120            }
121    
122            public static boolean execute(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
123                    if(pc==null) return stat.execute();
124                    PageContextImpl pci = (PageContextImpl) pc;
125                    try {
126                            setActiveStatement(pci,stat,sql);
127                            return stat.execute();
128                    }
129                    finally {
130                            pci.releaseActiveQuery();
131                    }
132            }
133    
134            public static ResultSet executeQuery(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
135                    if(pc==null) return stat.executeQuery();
136                    PageContextImpl pci = (PageContextImpl) pc;
137                    try {
138                            setActiveStatement(pci,stat,sql);
139                            return stat.executeQuery();
140                    }
141                    finally {
142                            pci.releaseActiveQuery();
143                    }
144            }
145    
146            public static int executeUpdate(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
147                    if(pc==null) return stat.executeUpdate();
148                    PageContextImpl pci = (PageContextImpl) pc;
149                    try {
150                            setActiveStatement(pci,stat,sql);
151                             return stat.executeUpdate();
152                    }
153                    finally {
154                            pci.releaseActiveQuery();
155                    }
156            }
157            
158            
159    
160            private static void setActiveStatement(PageContextImpl pc,Statement stat, String sql) {
161                    pc.setActiveQuery(new ActiveQuery(sql,System.currentTimeMillis()));
162            }
163    }