| 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.mode; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.StringJoiner; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.command.Parser; |
| 13 |
import org.h2.engine.Constants; |
| 14 |
import org.h2.engine.RightOwner; |
| 15 |
import org.h2.engine.SessionLocal; |
| 16 |
import org.h2.engine.User; |
| 17 |
import org.h2.expression.Expression; |
| 18 |
import org.h2.expression.ValueExpression; |
| 19 |
import org.h2.expression.function.CurrentGeneralValueSpecification; |
| 20 |
import org.h2.expression.function.RandFunction; |
| 21 |
import org.h2.index.Index; |
| 22 |
import org.h2.message.DbException; |
| 23 |
import org.h2.schema.Schema; |
| 24 |
import org.h2.server.pg.PgServer; |
| 25 |
import org.h2.table.Column; |
| 26 |
import org.h2.table.Table; |
| 27 |
import org.h2.util.StringUtils; |
| 28 |
import org.h2.value.TypeInfo; |
| 29 |
import org.h2.value.Value; |
| 30 |
import org.h2.value.ValueArray; |
| 31 |
import org.h2.value.ValueBigint; |
| 32 |
import org.h2.value.ValueBoolean; |
| 33 |
import org.h2.value.ValueInteger; |
| 34 |
import org.h2.value.ValueNull; |
| 35 |
import org.h2.value.ValueVarchar; |
| 36 |
|
| 37 |
/** |
| 38 |
* Functions for {@link org.h2.engine.Mode.ModeEnum#PostgreSQL} compatibility |
| 39 |
* mode. |
| 40 |
*/ |
| 41 |
public final class FunctionsPostgreSQL extends ModeFunction { |
| 42 |
|
| 43 |
private static final int CURRENT_DATABASE = 3001; |
| 44 |
|
| 45 |
private static final int CURRTID2 = CURRENT_DATABASE + 1; |
| 46 |
|
| 47 |
private static final int FORMAT_TYPE = CURRTID2 + 1; |
| 48 |
|
| 49 |
private static final int HAS_DATABASE_PRIVILEGE = FORMAT_TYPE + 1; |
| 50 |
|
| 51 |
private static final int HAS_SCHEMA_PRIVILEGE = HAS_DATABASE_PRIVILEGE + 1; |
| 52 |
|
| 53 |
private static final int HAS_TABLE_PRIVILEGE = HAS_SCHEMA_PRIVILEGE + 1; |
| 54 |
|
| 55 |
private static final int LASTVAL = HAS_TABLE_PRIVILEGE + 1; |
| 56 |
|
| 57 |
private static final int VERSION = LASTVAL + 1; |
| 58 |
|
| 59 |
private static final int OBJ_DESCRIPTION = VERSION + 1; |
| 60 |
|
| 61 |
private static final int PG_ENCODING_TO_CHAR = OBJ_DESCRIPTION + 1; |
| 62 |
|
| 63 |
private static final int PG_GET_EXPR = PG_ENCODING_TO_CHAR + 1; |
| 64 |
|
| 65 |
private static final int PG_GET_INDEXDEF = PG_GET_EXPR + 1; |
| 66 |
|
| 67 |
private static final int PG_GET_USERBYID = PG_GET_INDEXDEF + 1; |
| 68 |
|
| 69 |
private static final int PG_POSTMASTER_START_TIME = PG_GET_USERBYID + 1; |
| 70 |
|
| 71 |
private static final int PG_RELATION_SIZE = PG_POSTMASTER_START_TIME + 1; |
| 72 |
|
| 73 |
private static final int PG_TABLE_IS_VISIBLE = PG_RELATION_SIZE + 1; |
| 74 |
|
| 75 |
private static final int SET_CONFIG = PG_TABLE_IS_VISIBLE + 1; |
| 76 |
|
| 77 |
private static final int ARRAY_TO_STRING = SET_CONFIG + 1; |
| 78 |
|
| 79 |
private static final int PG_STAT_GET_NUMSCANS = ARRAY_TO_STRING + 1; |
| 80 |
|
| 81 |
private static final int TO_DATE = PG_STAT_GET_NUMSCANS + 1; |
| 82 |
|
| 83 |
private static final int TO_TIMESTAMP = TO_DATE + 1; |
| 84 |
|
| 85 |
private static final int GEN_RANDOM_UUID = TO_TIMESTAMP + 1; |
| 86 |
|
| 87 |
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(32); |
| 88 |
|
| 89 |
static { |
| 90 |
FUNCTIONS.put("CURRENT_DATABASE", |
| 91 |
new FunctionInfo("CURRENT_DATABASE", CURRENT_DATABASE, 0, Value.VARCHAR, true, false)); |
| 92 |
FUNCTIONS.put("CURRTID2", new FunctionInfo("CURRTID2", CURRTID2, 2, Value.INTEGER, true, false)); |
| 93 |
FUNCTIONS.put("FORMAT_TYPE", new FunctionInfo("FORMAT_TYPE", FORMAT_TYPE, 2, Value.VARCHAR, false, true)); |
| 94 |
FUNCTIONS.put("HAS_DATABASE_PRIVILEGE", new FunctionInfo("HAS_DATABASE_PRIVILEGE", HAS_DATABASE_PRIVILEGE, |
| 95 |
VAR_ARGS, Value.BOOLEAN, true, false)); |
| 96 |
FUNCTIONS.put("HAS_SCHEMA_PRIVILEGE", |
| 97 |
new FunctionInfo("HAS_SCHEMA_PRIVILEGE", HAS_SCHEMA_PRIVILEGE, VAR_ARGS, Value.BOOLEAN, true, false)); |
| 98 |
FUNCTIONS.put("HAS_TABLE_PRIVILEGE", |
| 99 |
new FunctionInfo("HAS_TABLE_PRIVILEGE", HAS_TABLE_PRIVILEGE, VAR_ARGS, Value.BOOLEAN, true, false)); |
| 100 |
FUNCTIONS.put("LASTVAL", new FunctionInfo("LASTVAL", LASTVAL, 0, Value.BIGINT, true, false)); |
| 101 |
FUNCTIONS.put("VERSION", new FunctionInfo("VERSION", VERSION, 0, Value.VARCHAR, true, false)); |
| 102 |
FUNCTIONS.put("OBJ_DESCRIPTION", |
| 103 |
new FunctionInfo("OBJ_DESCRIPTION", OBJ_DESCRIPTION, VAR_ARGS, Value.VARCHAR, true, false)); |
| 104 |
FUNCTIONS.put("PG_ENCODING_TO_CHAR", |
| 105 |
new FunctionInfo("PG_ENCODING_TO_CHAR", PG_ENCODING_TO_CHAR, 1, Value.VARCHAR, true, true)); |
| 106 |
FUNCTIONS.put("PG_GET_EXPR", // |
| 107 |
new FunctionInfo("PG_GET_EXPR", PG_GET_EXPR, VAR_ARGS, Value.VARCHAR, true, true)); |
| 108 |
FUNCTIONS.put("PG_GET_INDEXDEF", |
| 109 |
new FunctionInfo("PG_GET_INDEXDEF", PG_GET_INDEXDEF, VAR_ARGS, Value.VARCHAR, true, false)); |
| 110 |
FUNCTIONS.put("PG_GET_USERBYID", |
| 111 |
new FunctionInfo("PG_GET_USERBYID", PG_GET_USERBYID, 1, Value.VARCHAR, true, false)); |
| 112 |
FUNCTIONS.put("PG_POSTMASTER_START_TIME", // |
| 113 |
new FunctionInfo("PG_POSTMASTER_START_TIME", PG_POSTMASTER_START_TIME, 0, Value.TIMESTAMP_TZ, true, |
| 114 |
false)); |
| 115 |
FUNCTIONS.put("PG_RELATION_SIZE", |
| 116 |
new FunctionInfo("PG_RELATION_SIZE", PG_RELATION_SIZE, VAR_ARGS, Value.BIGINT, true, false)); |
| 117 |
FUNCTIONS.put("PG_TABLE_IS_VISIBLE", |
| 118 |
new FunctionInfo("PG_TABLE_IS_VISIBLE", PG_TABLE_IS_VISIBLE, 1, Value.BOOLEAN, true, false)); |
| 119 |
FUNCTIONS.put("SET_CONFIG", new FunctionInfo("SET_CONFIG", SET_CONFIG, 3, Value.VARCHAR, true, false)); |
| 120 |
FUNCTIONS.put("ARRAY_TO_STRING", |
| 121 |
new FunctionInfo("ARRAY_TO_STRING", ARRAY_TO_STRING, VAR_ARGS, Value.VARCHAR, false, true)); |
| 122 |
FUNCTIONS.put("PG_STAT_GET_NUMSCANS", |
| 123 |
new FunctionInfo("PG_STAT_GET_NUMSCANS", PG_STAT_GET_NUMSCANS, 1, Value.INTEGER, true, true)); |
| 124 |
FUNCTIONS.put("TO_DATE", new FunctionInfo("TO_DATE", TO_DATE, 2, Value.DATE, true, true)); |
| 125 |
FUNCTIONS.put("TO_TIMESTAMP", |
| 126 |
new FunctionInfo("TO_TIMESTAMP", TO_TIMESTAMP, 2, Value.TIMESTAMP_TZ, true, true)); |
| 127 |
FUNCTIONS.put("GEN_RANDOM_UUID", |
| 128 |
new FunctionInfo("GEN_RANDOM_UUID", GEN_RANDOM_UUID, 0, Value.UUID, true, false)); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns mode-specific function for a given name, or {@code null}. |
| 133 |
* |
| 134 |
* @param upperName |
| 135 |
* the upper-case name of a function |
| 136 |
* @return the function with specified name or {@code null} |
| 137 |
*/ |
| 138 |
public static FunctionsPostgreSQL getFunction(String upperName) { |
| 139 |
FunctionInfo info = FUNCTIONS.get(upperName); |
| 140 |
if (info != null) { |
| 141 |
return new FunctionsPostgreSQL(info); |
| 142 |
} |
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
private FunctionsPostgreSQL(FunctionInfo info) { |
| 147 |
super(info); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
protected void checkParameterCount(int len) { |
| 152 |
int min, max; |
| 153 |
switch (info.type) { |
| 154 |
case HAS_DATABASE_PRIVILEGE: |
| 155 |
case HAS_SCHEMA_PRIVILEGE: |
| 156 |
case HAS_TABLE_PRIVILEGE: |
| 157 |
min = 2; |
| 158 |
max = 3; |
| 159 |
break; |
| 160 |
case OBJ_DESCRIPTION: |
| 161 |
case PG_RELATION_SIZE: |
| 162 |
min = 1; |
| 163 |
max = 2; |
| 164 |
break; |
| 165 |
case PG_GET_INDEXDEF: |
| 166 |
if (len != 1 && len != 3) { |
| 167 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, "1, 3"); |
| 168 |
} |
| 169 |
return; |
| 170 |
case PG_GET_EXPR: |
| 171 |
case ARRAY_TO_STRING: |
| 172 |
min = 2; |
| 173 |
max = 3; |
| 174 |
break; |
| 175 |
default: |
| 176 |
throw DbException.getInternalError("type=" + info.type); |
| 177 |
} |
| 178 |
if (len < min || len > max) { |
| 179 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, min + ".." + max); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public Expression optimize(SessionLocal session) { |
| 185 |
switch (info.type) { |
| 186 |
case CURRENT_DATABASE: |
| 187 |
return new CurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_CATALOG) |
| 188 |
.optimize(session); |
| 189 |
case GEN_RANDOM_UUID: |
| 190 |
return new RandFunction(null, RandFunction.RANDOM_UUID).optimize(session); |
| 191 |
default: |
| 192 |
boolean allConst = optimizeArguments(session); |
| 193 |
type = TypeInfo.getTypeInfo(info.returnDataType); |
| 194 |
if (allConst) { |
| 195 |
return ValueExpression.get(getValue(session)); |
| 196 |
} |
| 197 |
} |
| 198 |
return this; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public Value getValue(SessionLocal session) { |
| 203 |
Value[] values = getArgumentsValues(session, args); |
| 204 |
if (values == null) { |
| 205 |
return ValueNull.INSTANCE; |
| 206 |
} |
| 207 |
Value v0 = getNullOrValue(session, args, values, 0); |
| 208 |
Value v1 = getNullOrValue(session, args, values, 1); |
| 209 |
Value v2 = getNullOrValue(session, args, values, 2); |
| 210 |
Value result; |
| 211 |
switch (info.type) { |
| 212 |
case CURRTID2: |
| 213 |
// Not implemented |
| 214 |
result = ValueInteger.get(1); |
| 215 |
break; |
| 216 |
case FORMAT_TYPE: |
| 217 |
result = v0 != ValueNull.INSTANCE ? ValueVarchar.get(PgServer.formatType(v0.getInt())) // |
| 218 |
: ValueNull.INSTANCE; |
| 219 |
break; |
| 220 |
case HAS_DATABASE_PRIVILEGE: |
| 221 |
case HAS_SCHEMA_PRIVILEGE: |
| 222 |
case HAS_TABLE_PRIVILEGE: |
| 223 |
case PG_TABLE_IS_VISIBLE: |
| 224 |
// Not implemented |
| 225 |
result = ValueBoolean.TRUE; |
| 226 |
break; |
| 227 |
case LASTVAL: |
| 228 |
result = session.getLastIdentity(); |
| 229 |
if (result == ValueNull.INSTANCE) { |
| 230 |
throw DbException.get(ErrorCode.CURRENT_SEQUENCE_VALUE_IS_NOT_DEFINED_IN_SESSION_1, "lastval()"); |
| 231 |
} |
| 232 |
result = result.convertToBigint(null); |
| 233 |
break; |
| 234 |
case VERSION: |
| 235 |
result = ValueVarchar |
| 236 |
.get("PostgreSQL " + Constants.PG_VERSION + " server protocol using H2 " + Constants.FULL_VERSION); |
| 237 |
break; |
| 238 |
case OBJ_DESCRIPTION: |
| 239 |
// Not implemented |
| 240 |
result = ValueNull.INSTANCE; |
| 241 |
break; |
| 242 |
case PG_ENCODING_TO_CHAR: |
| 243 |
result = ValueVarchar.get(encodingToChar(v0.getInt())); |
| 244 |
break; |
| 245 |
case PG_GET_EXPR: |
| 246 |
// Not implemented |
| 247 |
result = ValueNull.INSTANCE; |
| 248 |
break; |
| 249 |
case PG_GET_INDEXDEF: |
| 250 |
result = getIndexdef(session, v0.getInt(), v1, v2); |
| 251 |
break; |
| 252 |
case PG_GET_USERBYID: |
| 253 |
result = ValueVarchar.get(getUserbyid(session, v0.getInt())); |
| 254 |
break; |
| 255 |
case PG_POSTMASTER_START_TIME: |
| 256 |
result = session.getDatabase().getSystemSession().getSessionStart(); |
| 257 |
break; |
| 258 |
case PG_RELATION_SIZE: |
| 259 |
// Optional second argument is ignored |
| 260 |
result = relationSize(session, v0); |
| 261 |
break; |
| 262 |
case SET_CONFIG: |
| 263 |
// Not implemented |
| 264 |
result = v1.convertTo(Value.VARCHAR); |
| 265 |
break; |
| 266 |
case ARRAY_TO_STRING: |
| 267 |
if (v0 == ValueNull.INSTANCE || v1 == ValueNull.INSTANCE) { |
| 268 |
result = ValueNull.INSTANCE; |
| 269 |
break; |
| 270 |
} |
| 271 |
StringJoiner joiner = new StringJoiner(v1.getString()); |
| 272 |
if (v0.getValueType() != Value.ARRAY) { |
| 273 |
throw DbException.getInvalidValueException("ARRAY_TO_STRING array", v0); |
| 274 |
} |
| 275 |
String nullString = null; |
| 276 |
if (v2 != null) { |
| 277 |
nullString = v2.getString(); |
| 278 |
} |
| 279 |
for (Value v : ((ValueArray) v0).getList()) { |
| 280 |
if (v != ValueNull.INSTANCE) { |
| 281 |
joiner.add(v.getString()); |
| 282 |
} else if (nullString != null) { |
| 283 |
joiner.add(nullString); |
| 284 |
} |
| 285 |
} |
| 286 |
result = ValueVarchar.get(joiner.toString()); |
| 287 |
break; |
| 288 |
case PG_STAT_GET_NUMSCANS: |
| 289 |
// Not implemented |
| 290 |
result = ValueInteger.get(0); |
| 291 |
break; |
| 292 |
case TO_DATE: |
| 293 |
result = ToDateParser.toDate(session, v0.getString(), v1.getString()).convertToDate(session); |
| 294 |
break; |
| 295 |
case TO_TIMESTAMP: |
| 296 |
result = ToDateParser.toTimestampTz(session, v0.getString(), v1.getString()); |
| 297 |
break; |
| 298 |
default: |
| 299 |
throw DbException.getInternalError("type=" + info.type); |
| 300 |
} |
| 301 |
return result; |
| 302 |
} |
| 303 |
|
| 304 |
private static String encodingToChar(int code) { |
| 305 |
switch (code) { |
| 306 |
case 0: |
| 307 |
return "SQL_ASCII"; |
| 308 |
case 6: |
| 309 |
return "UTF8"; |
| 310 |
case 8: |
| 311 |
return "LATIN1"; |
| 312 |
default: |
| 313 |
// This function returns empty string for unknown encodings |
| 314 |
return code < 40 ? "UTF8" : ""; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
private static Value getIndexdef(SessionLocal session, int indexId, Value ordinalPosition, Value pretty) { |
| 319 |
for (Schema schema : session.getDatabase().getAllSchemasNoMeta()) { |
| 320 |
for (Index index : schema.getAllIndexes()) { |
| 321 |
if (index.getId() == indexId) { |
| 322 |
if (!index.getTable().isHidden()) { |
| 323 |
int ordinal; |
| 324 |
if (ordinalPosition == null || (ordinal = ordinalPosition.getInt()) == 0) { |
| 325 |
return ValueVarchar.get(index.getCreateSQL()); |
| 326 |
} |
| 327 |
Column[] columns; |
| 328 |
if (ordinal >= 1 && ordinal <= (columns = index.getColumns()).length) { |
| 329 |
return ValueVarchar.get(columns[ordinal - 1].getName()); |
| 330 |
} |
| 331 |
} |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
return ValueNull.INSTANCE; |
| 337 |
} |
| 338 |
|
| 339 |
private static String getUserbyid(SessionLocal session, int uid) { |
| 340 |
User u = session.getUser(); |
| 341 |
String name; |
| 342 |
search: { |
| 343 |
if (u.getId() == uid) { |
| 344 |
name = u.getName(); |
| 345 |
break search; |
| 346 |
} else { |
| 347 |
if (u.isAdmin()) { |
| 348 |
for (RightOwner rightOwner : session.getDatabase().getAllUsersAndRoles()) { |
| 349 |
if (rightOwner.getId() == uid) { |
| 350 |
name = rightOwner.getName(); |
| 351 |
break search; |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
return "unknown (OID=" + uid + ')'; |
| 357 |
} |
| 358 |
if (session.getDatabase().getSettings().databaseToLower) { |
| 359 |
name = StringUtils.toLowerEnglish(name); |
| 360 |
} |
| 361 |
return name; |
| 362 |
} |
| 363 |
|
| 364 |
private static Value relationSize(SessionLocal session, Value tableOidOrName) { |
| 365 |
Table t; |
| 366 |
if (tableOidOrName.getValueType() == Value.INTEGER) { |
| 367 |
int tid = tableOidOrName.getInt(); |
| 368 |
for (Schema schema : session.getDatabase().getAllSchemasNoMeta()) { |
| 369 |
for (Table table : schema.getAllTablesAndViews(session)) { |
| 370 |
if (tid == table.getId()) { |
| 371 |
t = table; |
| 372 |
break; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
return ValueNull.INSTANCE; |
| 377 |
} else { |
| 378 |
t = new Parser(session).parseTableName(tableOidOrName.getString()); |
| 379 |
} |
| 380 |
return ValueBigint.get(t.getDiskSpaceUsed()); |
| 381 |
} |
| 382 |
|
| 383 |
} |