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.commons.sql;
020
021import java.lang.reflect.Method;
022import java.sql.Clob;
023import java.sql.Connection;
024
025import lucee.commons.lang.ClassUtil;
026import lucee.commons.lang.ExceptionUtil;
027import lucee.runtime.op.Caster;
028
029public class OracleClob {
030
031        private static Integer duration;
032        private static Integer mode;
033        private static Method createTemporary;
034        private static Method open;
035        private static Method setString;
036
037        public static Clob createClob(Connection conn,String value,Clob defaultValue) {
038                try{
039                        Class clazz = ClassUtil.loadClass("oracle.sql.CLOB");
040                        
041                        // CLOB.DURATION_SESSION;
042                        if(duration==null)      duration = Caster.toInteger(clazz.getField("DURATION_SESSION").getInt(null));
043                        // CLOB.MODE_READWRITE
044                        if(mode==null)          mode = Caster.toInteger(clazz.getField("MODE_READWRITE").getInt(null));
045 
046                        //CLOB c = CLOB.createTemporary(conn, false, CLOB.DURATION_SESSION);
047                        if(createTemporary==null || createTemporary.getDeclaringClass()!=clazz)
048                                createTemporary = clazz.getMethod("createTemporary", new Class[]{Connection.class,boolean.class,int.class});
049                        Object clob = createTemporary.invoke(null, new Object[]{conn,Boolean.FALSE,duration});
050                        
051                        // c.open(CLOB.MODE_READWRITE);
052                        if(open==null || open.getDeclaringClass()!=clazz)
053                                open = clazz.getMethod("open", new Class[]{int.class});
054                        open.invoke(clob, new Object[]{mode});
055
056                        //c.setString(1,value);
057                        if(setString==null || setString.getDeclaringClass()!=clazz)
058                                setString = clazz.getMethod("setString", new Class[]{long.class,String.class});
059                        setString.invoke(clob, new Object[]{Long.valueOf(1),value});
060
061                        return (Clob) clob;
062                }
063                catch(Throwable t){
064                ExceptionUtil.rethrowIfNecessary(t);
065                        //print.printST(t);
066                }
067                return defaultValue;
068        }
069
070}