| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.function; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.InputStreamReader; |
| 11 |
import java.io.OutputStream; |
| 12 |
import java.io.Reader; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.Paths; |
| 15 |
|
| 16 |
import org.h2.engine.Database; |
| 17 |
import org.h2.engine.SessionLocal; |
| 18 |
import org.h2.expression.Expression; |
| 19 |
import org.h2.expression.ExpressionVisitor; |
| 20 |
import org.h2.message.DbException; |
| 21 |
import org.h2.store.fs.FileUtils; |
| 22 |
import org.h2.util.IOUtils; |
| 23 |
import org.h2.value.TypeInfo; |
| 24 |
import org.h2.value.Value; |
| 25 |
import org.h2.value.ValueBigint; |
| 26 |
import org.h2.value.ValueLob; |
| 27 |
import org.h2.value.ValueNull; |
| 28 |
|
| 29 |
/** |
| 30 |
* A FILE_READ or FILE_WRITE function. |
| 31 |
*/ |
| 32 |
public final class FileFunction extends Function1_2 { |
| 33 |
|
| 34 |
/** |
| 35 |
* FILE_READ() (non-standard). |
| 36 |
*/ |
| 37 |
public static final int FILE_READ = 0; |
| 38 |
|
| 39 |
/** |
| 40 |
* FILE_WRITE() (non-standard). |
| 41 |
*/ |
| 42 |
public static final int FILE_WRITE = FILE_READ + 1; |
| 43 |
|
| 44 |
private static final String[] NAMES = { // |
| 45 |
"FILE_READ", "FILE_WRITE" // |
| 46 |
}; |
| 47 |
|
| 48 |
private final int function; |
| 49 |
|
| 50 |
public FileFunction(Expression arg1, Expression arg2, int function) { |
| 51 |
super(arg1, arg2); |
| 52 |
this.function = function; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Value getValue(SessionLocal session) { |
| 57 |
session.getUser().checkAdmin(); |
| 58 |
Value v1 = left.getValue(session); |
| 59 |
if (v1 == ValueNull.INSTANCE) { |
| 60 |
return ValueNull.INSTANCE; |
| 61 |
} |
| 62 |
switch (function) { |
| 63 |
case FILE_READ: { |
| 64 |
String fileName = v1.getString(); |
| 65 |
Database database = session.getDatabase(); |
| 66 |
try { |
| 67 |
long fileLength = FileUtils.size(fileName); |
| 68 |
ValueLob lob; |
| 69 |
try (InputStream in = FileUtils.newInputStream(fileName)) { |
| 70 |
if (right == null) { |
| 71 |
lob = database.getLobStorage().createBlob(in, fileLength); |
| 72 |
} else { |
| 73 |
Value v2 = right.getValue(session); |
| 74 |
Reader reader = v2 == ValueNull.INSTANCE ? new InputStreamReader(in) |
| 75 |
: new InputStreamReader(in, v2.getString()); |
| 76 |
lob = database.getLobStorage().createClob(reader, fileLength); |
| 77 |
} |
| 78 |
} |
| 79 |
v1 = session.addTemporaryLob(lob); |
| 80 |
} catch (IOException e) { |
| 81 |
throw DbException.convertIOException(e, fileName); |
| 82 |
} |
| 83 |
break; |
| 84 |
} |
| 85 |
case FILE_WRITE: { |
| 86 |
Value v2 = right.getValue(session); |
| 87 |
if (v2 == ValueNull.INSTANCE) { |
| 88 |
v1 = ValueNull.INSTANCE; |
| 89 |
} else { |
| 90 |
String fileName = v2.getString(); |
| 91 |
try (OutputStream fileOutputStream = Files.newOutputStream(Paths.get(fileName)); |
| 92 |
InputStream in = v1.getInputStream()) { |
| 93 |
v1 = ValueBigint.get(IOUtils.copy(in, fileOutputStream)); |
| 94 |
} catch (IOException e) { |
| 95 |
throw DbException.convertIOException(e, fileName); |
| 96 |
} |
| 97 |
} |
| 98 |
break; |
| 99 |
} |
| 100 |
default: |
| 101 |
throw DbException.getInternalError("function=" + function); |
| 102 |
} |
| 103 |
return v1; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public Expression optimize(SessionLocal session) { |
| 108 |
left = left.optimize(session); |
| 109 |
if (right != null) { |
| 110 |
right = right.optimize(session); |
| 111 |
} |
| 112 |
switch (function) { |
| 113 |
case FILE_READ: |
| 114 |
type = right == null ? TypeInfo.getTypeInfo(Value.BLOB, Integer.MAX_VALUE, 0, null) |
| 115 |
: TypeInfo.getTypeInfo(Value.CLOB, Integer.MAX_VALUE, 0, null); |
| 116 |
break; |
| 117 |
case FILE_WRITE: |
| 118 |
type = TypeInfo.TYPE_BIGINT; |
| 119 |
break; |
| 120 |
default: |
| 121 |
throw DbException.getInternalError("function=" + function); |
| 122 |
} |
| 123 |
return this; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 128 |
switch (visitor.getType()) { |
| 129 |
case ExpressionVisitor.DETERMINISTIC: |
| 130 |
case ExpressionVisitor.QUERY_COMPARABLE: |
| 131 |
return false; |
| 132 |
case ExpressionVisitor.READONLY: |
| 133 |
if (function == FILE_WRITE) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
return super.isEverything(visitor); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public String getName() { |
| 142 |
return NAMES[function]; |
| 143 |
} |
| 144 |
|
| 145 |
} |