001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.db.driver.state; 020 021import java.sql.PreparedStatement; 022import java.sql.ResultSet; 023import java.sql.SQLException; 024import java.sql.Statement; 025 026import lucee.runtime.PageContext; 027import lucee.runtime.PageContextImpl; 028import lucee.runtime.debug.ActiveQuery; 029 030public class StateUtil { 031 032 public static boolean execute(PageContext pc, Statement stat, String sql) throws SQLException { 033 if(pc==null) return stat.execute(sql); 034 PageContextImpl pci = (PageContextImpl)pc; 035 try { 036 setActiveStatement(pci,stat,sql); 037 return stat.execute(sql); 038 } 039 finally { 040 pci.releaseActiveQuery(); 041 } 042 } 043 044 public static boolean execute(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException { 045 if(pc==null) return stat.execute(sql,autoGeneratedKeys); 046 PageContextImpl pci = (PageContextImpl) pc; 047 try { 048 setActiveStatement(pci,stat,sql); 049 return stat.execute(sql,autoGeneratedKeys); 050 } 051 finally { 052 pci.releaseActiveQuery(); 053 } 054 } 055 056 public static boolean execute(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException { 057 if(pc==null) return stat.execute(sql,columnIndexes); 058 PageContextImpl pci = (PageContextImpl) pc; 059 try { 060 setActiveStatement(pci,stat,sql); 061 return stat.execute(sql,columnIndexes); 062 } 063 finally { 064 pci.releaseActiveQuery(); 065 } 066 } 067 068 public static boolean execute(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException { 069 if(pc==null) return stat.execute(sql,columnNames); 070 PageContextImpl pci = (PageContextImpl) pc; 071 try { 072 setActiveStatement(pci,stat,sql); 073 return stat.execute(sql,columnNames); 074 } 075 finally { 076 pci.releaseActiveQuery(); 077 } 078 } 079 080 public static ResultSet executeQuery(PageContext pc, Statement stat, String sql) throws SQLException { 081 if(pc==null) return stat.executeQuery(sql); 082 PageContextImpl pci = (PageContextImpl) pc; 083 try { 084 setActiveStatement(pci,stat,sql); 085 return stat.executeQuery(sql); 086 } 087 finally { 088 pci.releaseActiveQuery(); 089 } 090 } 091 092 public static int executeUpdate(PageContext pc, Statement stat, String sql) throws SQLException { 093 if(pc==null) return stat.executeUpdate(sql); 094 PageContextImpl pci = (PageContextImpl) pc; 095 try { 096 setActiveStatement(pci,stat,sql); 097 return stat.executeUpdate(sql); 098 } 099 finally { 100 pci.releaseActiveQuery(); 101 } 102 } 103 104 public static int executeUpdate(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException { 105 if(pc==null) return stat.executeUpdate(sql,autoGeneratedKeys); 106 PageContextImpl pci = (PageContextImpl) pc; 107 try { 108 setActiveStatement(pci,stat,sql); 109 return stat.executeUpdate(sql,autoGeneratedKeys); 110 } 111 finally { 112 pci.releaseActiveQuery(); 113 } 114 } 115 116 public static int executeUpdate(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException { 117 if(pc==null) return stat.executeUpdate(sql,columnIndexes); 118 PageContextImpl pci = (PageContextImpl) pc; 119 try { 120 setActiveStatement(pci,stat,sql); 121 return stat.executeUpdate(sql,columnIndexes); 122 } 123 finally { 124 pci.releaseActiveQuery(); 125 } 126 } 127 128 public static int executeUpdate(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException { 129 if(pc==null) return stat.executeUpdate(sql,columnNames); 130 PageContextImpl pci = (PageContextImpl) pc; 131 try { 132 setActiveStatement(pci,stat,sql); 133 return stat.executeUpdate(sql,columnNames); 134 } 135 finally { 136 pci.releaseActiveQuery(); 137 } 138 } 139 140 public static boolean execute(PageContext pc, PreparedStatement stat, String sql) throws SQLException { 141 if(pc==null) return stat.execute(); 142 PageContextImpl pci = (PageContextImpl) pc; 143 try { 144 setActiveStatement(pci,stat,sql); 145 return stat.execute(); 146 } 147 finally { 148 pci.releaseActiveQuery(); 149 } 150 } 151 152 public static ResultSet executeQuery(PageContext pc, PreparedStatement stat, String sql) throws SQLException { 153 if(pc==null) return stat.executeQuery(); 154 PageContextImpl pci = (PageContextImpl) pc; 155 try { 156 setActiveStatement(pci,stat,sql); 157 return stat.executeQuery(); 158 } 159 finally { 160 pci.releaseActiveQuery(); 161 } 162 } 163 164 public static int executeUpdate(PageContext pc, PreparedStatement stat, String sql) throws SQLException { 165 if(pc==null) return stat.executeUpdate(); 166 PageContextImpl pci = (PageContextImpl) pc; 167 try { 168 setActiveStatement(pci,stat,sql); 169 return stat.executeUpdate(); 170 } 171 finally { 172 pci.releaseActiveQuery(); 173 } 174 } 175 176 177 178 private static void setActiveStatement(PageContextImpl pc,Statement stat, String sql) { 179 pc.setActiveQuery(new ActiveQuery(sql,System.currentTimeMillis())); 180 } 181}