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.Blob;
023import java.sql.Connection;
024
025import lucee.commons.lang.ClassUtil;
026import lucee.commons.lang.ExceptionUtil;
027import lucee.runtime.op.Caster;
028
029public class OracleBlob {
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 setBytes;
036
037        public static Blob createBlob(Connection conn,byte[] barr,Blob defaultValue) {
038                try{
039                        Class clazz = ClassUtil.loadClass("oracle.sql.BLOB");
040                        
041                        // BLOB.DURATION_SESSION
042                        if(duration==null)      duration = Caster.toInteger(clazz.getField("DURATION_SESSION").getInt(null));
043                        // BLOB.MODE_READWRITE
044                        if(mode==null)          mode = Caster.toInteger(clazz.getField("MODE_READWRITE").getInt(null));
045 
046                        //BLOB blob = BLOB.createTemporary(conn, false, BLOB.DURATION_SESSION);
047                        if(createTemporary==null || createTemporary.getDeclaringClass()!=clazz)
048                                createTemporary = clazz.getMethod("createTemporary", new Class[]{Connection.class,boolean.class,int.class});
049                        Object blob = createTemporary.invoke(null, new Object[]{conn,Boolean.FALSE,duration});
050                        
051                        //blob.open(BLOB.MODE_READWRITE);
052                        if(open==null || open.getDeclaringClass()!=clazz)
053                                open = clazz.getMethod("open", new Class[]{int.class});
054                        open.invoke(blob, new Object[]{mode});
055
056                        //blob.setBytes(1,barr);
057                        if(setBytes==null || setBytes.getDeclaringClass()!=clazz)
058                                setBytes = clazz.getMethod("setBytes", new Class[]{long.class,byte[].class});
059                        setBytes.invoke(blob, new Object[]{Long.valueOf(1),barr});
060
061                        return (Blob) blob;
062                }
063                catch(Throwable t){
064                ExceptionUtil.rethrowIfNecessary(t);
065                        //print.printST(t);
066                }
067                return defaultValue;
068        }
069
070}