| 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 org.h2.engine.Database; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.ExpressionVisitor; |
| 11 |
import org.h2.expression.Operation0; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.util.HasSQL; |
| 14 |
import org.h2.util.ParserUtil; |
| 15 |
import org.h2.util.StringUtils; |
| 16 |
import org.h2.value.TypeInfo; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueNull; |
| 19 |
import org.h2.value.ValueVarchar; |
| 20 |
|
| 21 |
/** |
| 22 |
* Simple general value specifications. |
| 23 |
*/ |
| 24 |
public final class CurrentGeneralValueSpecification extends Operation0 implements NamedExpression { |
| 25 |
|
| 26 |
/** |
| 27 |
* The "CURRENT_CATALOG" general value specification. |
| 28 |
*/ |
| 29 |
public static final int CURRENT_CATALOG = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* The "CURRENT_PATH" general value specification. |
| 33 |
*/ |
| 34 |
public static final int CURRENT_PATH = CURRENT_CATALOG + 1; |
| 35 |
|
| 36 |
/** |
| 37 |
* The function "CURRENT_ROLE" general value specification. |
| 38 |
*/ |
| 39 |
public static final int CURRENT_ROLE = CURRENT_PATH + 1; |
| 40 |
|
| 41 |
/** |
| 42 |
* The function "CURRENT_SCHEMA" general value specification. |
| 43 |
*/ |
| 44 |
public static final int CURRENT_SCHEMA = CURRENT_ROLE + 1; |
| 45 |
|
| 46 |
/** |
| 47 |
* The function "CURRENT_USER" general value specification. |
| 48 |
*/ |
| 49 |
public static final int CURRENT_USER = CURRENT_SCHEMA + 1; |
| 50 |
|
| 51 |
/** |
| 52 |
* The function "SESSION_USER" general value specification. |
| 53 |
*/ |
| 54 |
public static final int SESSION_USER = CURRENT_USER + 1; |
| 55 |
|
| 56 |
/** |
| 57 |
* The function "SYSTEM_USER" general value specification. |
| 58 |
*/ |
| 59 |
public static final int SYSTEM_USER = SESSION_USER + 1; |
| 60 |
|
| 61 |
private static final String[] NAMES = { "CURRENT_CATALOG", "CURRENT_PATH", "CURRENT_ROLE", "CURRENT_SCHEMA", |
| 62 |
"CURRENT_USER", "SESSION_USER", "SYSTEM_USER" }; |
| 63 |
|
| 64 |
private final int specification; |
| 65 |
|
| 66 |
public CurrentGeneralValueSpecification(int specification) { |
| 67 |
this.specification = specification; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public Value getValue(SessionLocal session) { |
| 72 |
String s; |
| 73 |
switch (specification) { |
| 74 |
case CURRENT_CATALOG: |
| 75 |
s = session.getDatabase().getShortName(); |
| 76 |
break; |
| 77 |
case CURRENT_PATH: { |
| 78 |
String[] searchPath = session.getSchemaSearchPath(); |
| 79 |
if (searchPath != null) { |
| 80 |
StringBuilder builder = new StringBuilder(); |
| 81 |
for (int i = 0; i < searchPath.length; i++) { |
| 82 |
if (i > 0) { |
| 83 |
builder.append(','); |
| 84 |
} |
| 85 |
ParserUtil.quoteIdentifier(builder, searchPath[i], HasSQL.DEFAULT_SQL_FLAGS); |
| 86 |
} |
| 87 |
s = builder.toString(); |
| 88 |
} else { |
| 89 |
s = ""; |
| 90 |
} |
| 91 |
break; |
| 92 |
} |
| 93 |
case CURRENT_ROLE: { |
| 94 |
Database db = session.getDatabase(); |
| 95 |
s = db.getPublicRole().getName(); |
| 96 |
if (db.getSettings().databaseToLower) { |
| 97 |
s = StringUtils.toLowerEnglish(s); |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
case CURRENT_SCHEMA: |
| 102 |
s = session.getCurrentSchemaName(); |
| 103 |
break; |
| 104 |
case CURRENT_USER: |
| 105 |
case SESSION_USER: |
| 106 |
case SYSTEM_USER: |
| 107 |
s = session.getUser().getName(); |
| 108 |
if (session.getDatabase().getSettings().databaseToLower) { |
| 109 |
s = StringUtils.toLowerEnglish(s); |
| 110 |
} |
| 111 |
break; |
| 112 |
default: |
| 113 |
throw DbException.getInternalError("specification=" + specification); |
| 114 |
} |
| 115 |
return s != null ? ValueVarchar.get(s, session) : ValueNull.INSTANCE; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 120 |
return builder.append(getName()); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 125 |
switch (visitor.getType()) { |
| 126 |
case ExpressionVisitor.DETERMINISTIC: |
| 127 |
return false; |
| 128 |
} |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public TypeInfo getType() { |
| 134 |
return TypeInfo.TYPE_VARCHAR; |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public int getCost() { |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public String getName() { |
| 144 |
return NAMES[specification]; |
| 145 |
} |
| 146 |
|
| 147 |
} |