| 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 |
* Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888 |
| 7 |
* Support for the operator "&&" as an alias for SPATIAL_INTERSECTS |
| 8 |
*/ |
| 9 |
package org.h2.command; |
| 10 |
|
| 11 |
import static org.h2.command.Token.ASTERISK; |
| 12 |
import static org.h2.command.Token.AT; |
| 13 |
import static org.h2.command.Token.BIGGER; |
| 14 |
import static org.h2.command.Token.BIGGER_EQUAL; |
| 15 |
import static org.h2.command.Token.CLOSE_BRACE; |
| 16 |
import static org.h2.command.Token.CLOSE_BRACKET; |
| 17 |
import static org.h2.command.Token.CLOSE_PAREN; |
| 18 |
import static org.h2.command.Token.COLON; |
| 19 |
import static org.h2.command.Token.COLON_COLON; |
| 20 |
import static org.h2.command.Token.COLON_EQ; |
| 21 |
import static org.h2.command.Token.COMMA; |
| 22 |
import static org.h2.command.Token.CONCATENATION; |
| 23 |
import static org.h2.command.Token.DOT; |
| 24 |
import static org.h2.command.Token.END_OF_INPUT; |
| 25 |
import static org.h2.command.Token.EQUAL; |
| 26 |
import static org.h2.command.Token.LITERAL; |
| 27 |
import static org.h2.command.Token.MINUS_SIGN; |
| 28 |
import static org.h2.command.Token.NOT_EQUAL; |
| 29 |
import static org.h2.command.Token.NOT_TILDE; |
| 30 |
import static org.h2.command.Token.OPEN_BRACE; |
| 31 |
import static org.h2.command.Token.OPEN_BRACKET; |
| 32 |
import static org.h2.command.Token.OPEN_PAREN; |
| 33 |
import static org.h2.command.Token.PARAMETER; |
| 34 |
import static org.h2.command.Token.PERCENT; |
| 35 |
import static org.h2.command.Token.PLUS_SIGN; |
| 36 |
import static org.h2.command.Token.SEMICOLON; |
| 37 |
import static org.h2.command.Token.SLASH; |
| 38 |
import static org.h2.command.Token.SMALLER; |
| 39 |
import static org.h2.command.Token.SMALLER_EQUAL; |
| 40 |
import static org.h2.command.Token.SPATIAL_INTERSECTS; |
| 41 |
import static org.h2.command.Token.TILDE; |
| 42 |
import static org.h2.command.Token.TOKENS; |
| 43 |
import static org.h2.util.ParserUtil.ALL; |
| 44 |
import static org.h2.util.ParserUtil.AND; |
| 45 |
import static org.h2.util.ParserUtil.ANY; |
| 46 |
import static org.h2.util.ParserUtil.ARRAY; |
| 47 |
import static org.h2.util.ParserUtil.AS; |
| 48 |
import static org.h2.util.ParserUtil.ASYMMETRIC; |
| 49 |
import static org.h2.util.ParserUtil.AUTHORIZATION; |
| 50 |
import static org.h2.util.ParserUtil.BETWEEN; |
| 51 |
import static org.h2.util.ParserUtil.CASE; |
| 52 |
import static org.h2.util.ParserUtil.CAST; |
| 53 |
import static org.h2.util.ParserUtil.CHECK; |
| 54 |
import static org.h2.util.ParserUtil.CONSTRAINT; |
| 55 |
import static org.h2.util.ParserUtil.CROSS; |
| 56 |
import static org.h2.util.ParserUtil.CURRENT_CATALOG; |
| 57 |
import static org.h2.util.ParserUtil.CURRENT_DATE; |
| 58 |
import static org.h2.util.ParserUtil.CURRENT_PATH; |
| 59 |
import static org.h2.util.ParserUtil.CURRENT_ROLE; |
| 60 |
import static org.h2.util.ParserUtil.CURRENT_SCHEMA; |
| 61 |
import static org.h2.util.ParserUtil.CURRENT_TIME; |
| 62 |
import static org.h2.util.ParserUtil.CURRENT_TIMESTAMP; |
| 63 |
import static org.h2.util.ParserUtil.CURRENT_USER; |
| 64 |
import static org.h2.util.ParserUtil.DAY; |
| 65 |
import static org.h2.util.ParserUtil.DEFAULT; |
| 66 |
import static org.h2.util.ParserUtil.DISTINCT; |
| 67 |
import static org.h2.util.ParserUtil.ELSE; |
| 68 |
import static org.h2.util.ParserUtil.END; |
| 69 |
import static org.h2.util.ParserUtil.EXCEPT; |
| 70 |
import static org.h2.util.ParserUtil.EXISTS; |
| 71 |
import static org.h2.util.ParserUtil.FALSE; |
| 72 |
import static org.h2.util.ParserUtil.FETCH; |
| 73 |
import static org.h2.util.ParserUtil.FIRST_KEYWORD; |
| 74 |
import static org.h2.util.ParserUtil.FOR; |
| 75 |
import static org.h2.util.ParserUtil.FOREIGN; |
| 76 |
import static org.h2.util.ParserUtil.FROM; |
| 77 |
import static org.h2.util.ParserUtil.FULL; |
| 78 |
import static org.h2.util.ParserUtil.GROUP; |
| 79 |
import static org.h2.util.ParserUtil.HAVING; |
| 80 |
import static org.h2.util.ParserUtil.HOUR; |
| 81 |
import static org.h2.util.ParserUtil.IDENTIFIER; |
| 82 |
import static org.h2.util.ParserUtil.IF; |
| 83 |
import static org.h2.util.ParserUtil.IN; |
| 84 |
import static org.h2.util.ParserUtil.INNER; |
| 85 |
import static org.h2.util.ParserUtil.INTERSECT; |
| 86 |
import static org.h2.util.ParserUtil.INTERVAL; |
| 87 |
import static org.h2.util.ParserUtil.IS; |
| 88 |
import static org.h2.util.ParserUtil.JOIN; |
| 89 |
import static org.h2.util.ParserUtil.KEY; |
| 90 |
import static org.h2.util.ParserUtil.LAST_KEYWORD; |
| 91 |
import static org.h2.util.ParserUtil.LEFT; |
| 92 |
import static org.h2.util.ParserUtil.LIKE; |
| 93 |
import static org.h2.util.ParserUtil.LIMIT; |
| 94 |
import static org.h2.util.ParserUtil.LOCALTIME; |
| 95 |
import static org.h2.util.ParserUtil.LOCALTIMESTAMP; |
| 96 |
import static org.h2.util.ParserUtil.MINUS; |
| 97 |
import static org.h2.util.ParserUtil.MINUTE; |
| 98 |
import static org.h2.util.ParserUtil.MONTH; |
| 99 |
import static org.h2.util.ParserUtil.NATURAL; |
| 100 |
import static org.h2.util.ParserUtil.NOT; |
| 101 |
import static org.h2.util.ParserUtil.NULL; |
| 102 |
import static org.h2.util.ParserUtil.OFFSET; |
| 103 |
import static org.h2.util.ParserUtil.ON; |
| 104 |
import static org.h2.util.ParserUtil.OR; |
| 105 |
import static org.h2.util.ParserUtil.ORDER; |
| 106 |
import static org.h2.util.ParserUtil.PRIMARY; |
| 107 |
import static org.h2.util.ParserUtil.QUALIFY; |
| 108 |
import static org.h2.util.ParserUtil.RIGHT; |
| 109 |
import static org.h2.util.ParserUtil.ROW; |
| 110 |
import static org.h2.util.ParserUtil.ROWNUM; |
| 111 |
import static org.h2.util.ParserUtil.SECOND; |
| 112 |
import static org.h2.util.ParserUtil.SELECT; |
| 113 |
import static org.h2.util.ParserUtil.SESSION_USER; |
| 114 |
import static org.h2.util.ParserUtil.SET; |
| 115 |
import static org.h2.util.ParserUtil.SOME; |
| 116 |
import static org.h2.util.ParserUtil.SYMMETRIC; |
| 117 |
import static org.h2.util.ParserUtil.SYSTEM_USER; |
| 118 |
import static org.h2.util.ParserUtil.TABLE; |
| 119 |
import static org.h2.util.ParserUtil.TO; |
| 120 |
import static org.h2.util.ParserUtil.TRUE; |
| 121 |
import static org.h2.util.ParserUtil.UNION; |
| 122 |
import static org.h2.util.ParserUtil.UNIQUE; |
| 123 |
import static org.h2.util.ParserUtil.UNKNOWN; |
| 124 |
import static org.h2.util.ParserUtil.USER; |
| 125 |
import static org.h2.util.ParserUtil.USING; |
| 126 |
import static org.h2.util.ParserUtil.VALUE; |
| 127 |
import static org.h2.util.ParserUtil.VALUES; |
| 128 |
import static org.h2.util.ParserUtil.WHEN; |
| 129 |
import static org.h2.util.ParserUtil.WHERE; |
| 130 |
import static org.h2.util.ParserUtil.WINDOW; |
| 131 |
import static org.h2.util.ParserUtil.WITH; |
| 132 |
import static org.h2.util.ParserUtil.YEAR; |
| 133 |
import static org.h2.util.ParserUtil._ROWID_; |
| 134 |
|
| 135 |
import java.math.BigDecimal; |
| 136 |
import java.nio.charset.Charset; |
| 137 |
import java.text.Collator; |
| 138 |
import java.util.ArrayList; |
| 139 |
import java.util.Arrays; |
| 140 |
import java.util.BitSet; |
| 141 |
import java.util.Collection; |
| 142 |
import java.util.Collections; |
| 143 |
import java.util.HashSet; |
| 144 |
import java.util.LinkedHashMap; |
| 145 |
import java.util.LinkedHashSet; |
| 146 |
import java.util.List; |
| 147 |
import java.util.StringJoiner; |
| 148 |
import java.util.TreeSet; |
| 149 |
|
| 150 |
import org.h2.api.ErrorCode; |
| 151 |
import org.h2.api.IntervalQualifier; |
| 152 |
import org.h2.api.Trigger; |
| 153 |
import org.h2.command.ddl.AlterDomainAddConstraint; |
| 154 |
import org.h2.command.ddl.AlterDomainDropConstraint; |
| 155 |
import org.h2.command.ddl.AlterDomainExpressions; |
| 156 |
import org.h2.command.ddl.AlterDomainRename; |
| 157 |
import org.h2.command.ddl.AlterDomainRenameConstraint; |
| 158 |
import org.h2.command.ddl.AlterIndexRename; |
| 159 |
import org.h2.command.ddl.AlterSchemaRename; |
| 160 |
import org.h2.command.ddl.AlterSequence; |
| 161 |
import org.h2.command.ddl.AlterTableAddConstraint; |
| 162 |
import org.h2.command.ddl.AlterTableAlterColumn; |
| 163 |
import org.h2.command.ddl.AlterTableDropConstraint; |
| 164 |
import org.h2.command.ddl.AlterTableRename; |
| 165 |
import org.h2.command.ddl.AlterTableRenameColumn; |
| 166 |
import org.h2.command.ddl.AlterTableRenameConstraint; |
| 167 |
import org.h2.command.ddl.AlterUser; |
| 168 |
import org.h2.command.ddl.AlterView; |
| 169 |
import org.h2.command.ddl.Analyze; |
| 170 |
import org.h2.command.ddl.CommandWithColumns; |
| 171 |
import org.h2.command.ddl.CreateAggregate; |
| 172 |
import org.h2.command.ddl.CreateConstant; |
| 173 |
import org.h2.command.ddl.CreateDomain; |
| 174 |
import org.h2.command.ddl.CreateFunctionAlias; |
| 175 |
import org.h2.command.ddl.CreateIndex; |
| 176 |
import org.h2.command.ddl.CreateLinkedTable; |
| 177 |
import org.h2.command.ddl.CreateMaterializedView; |
| 178 |
import org.h2.command.ddl.CreateRole; |
| 179 |
import org.h2.command.ddl.CreateSchema; |
| 180 |
import org.h2.command.ddl.CreateSequence; |
| 181 |
import org.h2.command.ddl.CreateSynonym; |
| 182 |
import org.h2.command.ddl.CreateTable; |
| 183 |
import org.h2.command.ddl.CreateTrigger; |
| 184 |
import org.h2.command.ddl.CreateUser; |
| 185 |
import org.h2.command.ddl.CreateView; |
| 186 |
import org.h2.command.ddl.DeallocateProcedure; |
| 187 |
import org.h2.command.ddl.DefineCommand; |
| 188 |
import org.h2.command.ddl.DropAggregate; |
| 189 |
import org.h2.command.ddl.DropConstant; |
| 190 |
import org.h2.command.ddl.DropDatabase; |
| 191 |
import org.h2.command.ddl.DropDomain; |
| 192 |
import org.h2.command.ddl.DropFunctionAlias; |
| 193 |
import org.h2.command.ddl.DropIndex; |
| 194 |
import org.h2.command.ddl.DropMaterializedView; |
| 195 |
import org.h2.command.ddl.DropRole; |
| 196 |
import org.h2.command.ddl.DropSchema; |
| 197 |
import org.h2.command.ddl.DropSequence; |
| 198 |
import org.h2.command.ddl.DropSynonym; |
| 199 |
import org.h2.command.ddl.DropTable; |
| 200 |
import org.h2.command.ddl.DropTrigger; |
| 201 |
import org.h2.command.ddl.DropUser; |
| 202 |
import org.h2.command.ddl.DropView; |
| 203 |
import org.h2.command.ddl.GrantRevoke; |
| 204 |
import org.h2.command.ddl.PrepareProcedure; |
| 205 |
import org.h2.command.ddl.RefreshMaterializedView; |
| 206 |
import org.h2.command.ddl.SequenceOptions; |
| 207 |
import org.h2.command.ddl.SetComment; |
| 208 |
import org.h2.command.ddl.TruncateTable; |
| 209 |
import org.h2.command.dml.AlterTableSet; |
| 210 |
import org.h2.command.dml.BackupCommand; |
| 211 |
import org.h2.command.dml.Call; |
| 212 |
import org.h2.command.dml.CommandWithValues; |
| 213 |
import org.h2.command.dml.DataChangeStatement; |
| 214 |
import org.h2.command.dml.Delete; |
| 215 |
import org.h2.command.dml.ExecuteImmediate; |
| 216 |
import org.h2.command.dml.ExecuteProcedure; |
| 217 |
import org.h2.command.dml.Explain; |
| 218 |
import org.h2.command.dml.Help; |
| 219 |
import org.h2.command.dml.Insert; |
| 220 |
import org.h2.command.dml.Merge; |
| 221 |
import org.h2.command.dml.MergeUsing; |
| 222 |
import org.h2.command.dml.NoOperation; |
| 223 |
import org.h2.command.dml.RunScriptCommand; |
| 224 |
import org.h2.command.dml.ScriptCommand; |
| 225 |
import org.h2.command.dml.Set; |
| 226 |
import org.h2.command.dml.SetClauseList; |
| 227 |
import org.h2.command.dml.SetSessionCharacteristics; |
| 228 |
import org.h2.command.dml.SetTypes; |
| 229 |
import org.h2.command.dml.TransactionCommand; |
| 230 |
import org.h2.command.dml.Update; |
| 231 |
import org.h2.command.query.ForUpdate; |
| 232 |
import org.h2.command.query.Query; |
| 233 |
import org.h2.command.query.QueryOrderBy; |
| 234 |
import org.h2.command.query.Select; |
| 235 |
import org.h2.command.query.SelectUnion; |
| 236 |
import org.h2.command.query.TableValueConstructor; |
| 237 |
import org.h2.constraint.ConstraintActionType; |
| 238 |
import org.h2.engine.ConnectionInfo; |
| 239 |
import org.h2.engine.Constants; |
| 240 |
import org.h2.engine.Database; |
| 241 |
import org.h2.engine.DbObject; |
| 242 |
import org.h2.engine.DbSettings; |
| 243 |
import org.h2.engine.IsolationLevel; |
| 244 |
import org.h2.engine.Mode; |
| 245 |
import org.h2.engine.Mode.ModeEnum; |
| 246 |
import org.h2.engine.NullsDistinct; |
| 247 |
import org.h2.engine.Procedure; |
| 248 |
import org.h2.engine.Right; |
| 249 |
import org.h2.engine.SessionLocal; |
| 250 |
import org.h2.engine.User; |
| 251 |
import org.h2.expression.Alias; |
| 252 |
import org.h2.expression.ArrayConstructorByQuery; |
| 253 |
import org.h2.expression.ArrayElementReference; |
| 254 |
import org.h2.expression.BinaryOperation; |
| 255 |
import org.h2.expression.BinaryOperation.OpType; |
| 256 |
import org.h2.expression.ConcatenationOperation; |
| 257 |
import org.h2.expression.DomainValueExpression; |
| 258 |
import org.h2.expression.Expression; |
| 259 |
import org.h2.expression.ExpressionColumn; |
| 260 |
import org.h2.expression.ExpressionList; |
| 261 |
import org.h2.expression.ExpressionWithFlags; |
| 262 |
import org.h2.expression.ExpressionWithVariableParameters; |
| 263 |
import org.h2.expression.FieldReference; |
| 264 |
import org.h2.expression.Format; |
| 265 |
import org.h2.expression.Format.FormatEnum; |
| 266 |
import org.h2.expression.Parameter; |
| 267 |
import org.h2.expression.Rownum; |
| 268 |
import org.h2.expression.SearchedCase; |
| 269 |
import org.h2.expression.SequenceValue; |
| 270 |
import org.h2.expression.SimpleCase; |
| 271 |
import org.h2.expression.Subquery; |
| 272 |
import org.h2.expression.TimeZoneOperation; |
| 273 |
import org.h2.expression.TypedValueExpression; |
| 274 |
import org.h2.expression.UnaryOperation; |
| 275 |
import org.h2.expression.ValueExpression; |
| 276 |
import org.h2.expression.Variable; |
| 277 |
import org.h2.expression.Wildcard; |
| 278 |
import org.h2.expression.aggregate.AbstractAggregate; |
| 279 |
import org.h2.expression.aggregate.Aggregate; |
| 280 |
import org.h2.expression.aggregate.AggregateType; |
| 281 |
import org.h2.expression.aggregate.JavaAggregate; |
| 282 |
import org.h2.expression.aggregate.ListaggArguments; |
| 283 |
import org.h2.expression.analysis.DataAnalysisOperation; |
| 284 |
import org.h2.expression.analysis.Window; |
| 285 |
import org.h2.expression.analysis.WindowFrame; |
| 286 |
import org.h2.expression.analysis.WindowFrameBound; |
| 287 |
import org.h2.expression.analysis.WindowFrameBoundType; |
| 288 |
import org.h2.expression.analysis.WindowFrameExclusion; |
| 289 |
import org.h2.expression.analysis.WindowFrameUnits; |
| 290 |
import org.h2.expression.analysis.WindowFunction; |
| 291 |
import org.h2.expression.analysis.WindowFunctionType; |
| 292 |
import org.h2.expression.condition.BetweenPredicate; |
| 293 |
import org.h2.expression.condition.BooleanTest; |
| 294 |
import org.h2.expression.condition.CompareLike; |
| 295 |
import org.h2.expression.condition.CompareLike.LikeType; |
| 296 |
import org.h2.expression.condition.Comparison; |
| 297 |
import org.h2.expression.condition.ConditionAndOr; |
| 298 |
import org.h2.expression.condition.ConditionAndOrN; |
| 299 |
import org.h2.expression.condition.ConditionIn; |
| 300 |
import org.h2.expression.condition.ConditionInArray; |
| 301 |
import org.h2.expression.condition.ConditionInQuery; |
| 302 |
import org.h2.expression.condition.ConditionLocalAndGlobal; |
| 303 |
import org.h2.expression.condition.ConditionNot; |
| 304 |
import org.h2.expression.condition.ExistsPredicate; |
| 305 |
import org.h2.expression.condition.IsJsonPredicate; |
| 306 |
import org.h2.expression.condition.NullPredicate; |
| 307 |
import org.h2.expression.condition.TypePredicate; |
| 308 |
import org.h2.expression.condition.UniquePredicate; |
| 309 |
import org.h2.expression.function.ArrayFunction; |
| 310 |
import org.h2.expression.function.BitFunction; |
| 311 |
import org.h2.expression.function.BuiltinFunctions; |
| 312 |
import org.h2.expression.function.CSVWriteFunction; |
| 313 |
import org.h2.expression.function.CardinalityExpression; |
| 314 |
import org.h2.expression.function.CastSpecification; |
| 315 |
import org.h2.expression.function.CoalesceFunction; |
| 316 |
import org.h2.expression.function.CompatibilitySequenceValueFunction; |
| 317 |
import org.h2.expression.function.CompressFunction; |
| 318 |
import org.h2.expression.function.ConcatFunction; |
| 319 |
import org.h2.expression.function.CryptFunction; |
| 320 |
import org.h2.expression.function.CurrentDateTimeValueFunction; |
| 321 |
import org.h2.expression.function.CurrentGeneralValueSpecification; |
| 322 |
import org.h2.expression.function.DBObjectFunction; |
| 323 |
import org.h2.expression.function.DataTypeSQLFunction; |
| 324 |
import org.h2.expression.function.DateTimeFormatFunction; |
| 325 |
import org.h2.expression.function.DateTimeFunction; |
| 326 |
import org.h2.expression.function.DayMonthNameFunction; |
| 327 |
import org.h2.expression.function.FileFunction; |
| 328 |
import org.h2.expression.function.HashFunction; |
| 329 |
import org.h2.expression.function.JavaFunction; |
| 330 |
import org.h2.expression.function.JsonConstructorFunction; |
| 331 |
import org.h2.expression.function.LengthFunction; |
| 332 |
import org.h2.expression.function.MathFunction; |
| 333 |
import org.h2.expression.function.MathFunction1; |
| 334 |
import org.h2.expression.function.MathFunction2; |
| 335 |
import org.h2.expression.function.NullIfFunction; |
| 336 |
import org.h2.expression.function.RandFunction; |
| 337 |
import org.h2.expression.function.RegexpFunction; |
| 338 |
import org.h2.expression.function.SessionControlFunction; |
| 339 |
import org.h2.expression.function.SetFunction; |
| 340 |
import org.h2.expression.function.SignalFunction; |
| 341 |
import org.h2.expression.function.SoundexFunction; |
| 342 |
import org.h2.expression.function.StringFunction; |
| 343 |
import org.h2.expression.function.StringFunction1; |
| 344 |
import org.h2.expression.function.StringFunction2; |
| 345 |
import org.h2.expression.function.SubstringFunction; |
| 346 |
import org.h2.expression.function.SysInfoFunction; |
| 347 |
import org.h2.expression.function.TableInfoFunction; |
| 348 |
import org.h2.expression.function.ToCharFunction; |
| 349 |
import org.h2.expression.function.TrimFunction; |
| 350 |
import org.h2.expression.function.TruncateValueFunction; |
| 351 |
import org.h2.expression.function.XMLFunction; |
| 352 |
import org.h2.expression.function.table.ArrayTableFunction; |
| 353 |
import org.h2.expression.function.table.CSVReadFunction; |
| 354 |
import org.h2.expression.function.table.JavaTableFunction; |
| 355 |
import org.h2.expression.function.table.LinkSchemaFunction; |
| 356 |
import org.h2.expression.function.table.TableFunction; |
| 357 |
import org.h2.index.Index; |
| 358 |
import org.h2.message.DbException; |
| 359 |
import org.h2.mode.FunctionsPostgreSQL; |
| 360 |
import org.h2.mode.ModeFunction; |
| 361 |
import org.h2.mode.OnDuplicateKeyValues; |
| 362 |
import org.h2.mode.Regclass; |
| 363 |
import org.h2.result.SortOrder; |
| 364 |
import org.h2.schema.Domain; |
| 365 |
import org.h2.schema.FunctionAlias; |
| 366 |
import org.h2.schema.Schema; |
| 367 |
import org.h2.schema.Sequence; |
| 368 |
import org.h2.schema.UserAggregate; |
| 369 |
import org.h2.schema.UserDefinedFunction; |
| 370 |
import org.h2.table.Column; |
| 371 |
import org.h2.table.DataChangeDeltaTable; |
| 372 |
import org.h2.table.DataChangeDeltaTable.ResultOption; |
| 373 |
import org.h2.table.DualTable; |
| 374 |
import org.h2.table.FunctionTable; |
| 375 |
import org.h2.table.IndexColumn; |
| 376 |
import org.h2.table.IndexHints; |
| 377 |
import org.h2.table.MaterializedView; |
| 378 |
import org.h2.table.QueryExpressionTable; |
| 379 |
import org.h2.table.RangeTable; |
| 380 |
import org.h2.table.Table; |
| 381 |
import org.h2.table.TableFilter; |
| 382 |
import org.h2.table.TableView; |
| 383 |
import org.h2.util.HasSQL; |
| 384 |
import org.h2.util.IntervalUtils; |
| 385 |
import org.h2.util.ParserUtil; |
| 386 |
import org.h2.util.StringUtils; |
| 387 |
import org.h2.util.Utils; |
| 388 |
import org.h2.util.geometry.EWKTUtils; |
| 389 |
import org.h2.util.json.JSONItemType; |
| 390 |
import org.h2.util.json.JsonConstructorUtils; |
| 391 |
import org.h2.value.CompareMode; |
| 392 |
import org.h2.value.DataType; |
| 393 |
import org.h2.value.ExtTypeInfoEnum; |
| 394 |
import org.h2.value.ExtTypeInfoGeometry; |
| 395 |
import org.h2.value.ExtTypeInfoNumeric; |
| 396 |
import org.h2.value.ExtTypeInfoRow; |
| 397 |
import org.h2.value.TypeInfo; |
| 398 |
import org.h2.value.Value; |
| 399 |
import org.h2.value.ValueArray; |
| 400 |
import org.h2.value.ValueBigint; |
| 401 |
import org.h2.value.ValueDate; |
| 402 |
import org.h2.value.ValueDouble; |
| 403 |
import org.h2.value.ValueGeometry; |
| 404 |
import org.h2.value.ValueInteger; |
| 405 |
import org.h2.value.ValueInterval; |
| 406 |
import org.h2.value.ValueJson; |
| 407 |
import org.h2.value.ValueNull; |
| 408 |
import org.h2.value.ValueNumeric; |
| 409 |
import org.h2.value.ValueRow; |
| 410 |
import org.h2.value.ValueTime; |
| 411 |
import org.h2.value.ValueTimeTimeZone; |
| 412 |
import org.h2.value.ValueTimestamp; |
| 413 |
import org.h2.value.ValueTimestampTimeZone; |
| 414 |
import org.h2.value.ValueUuid; |
| 415 |
import org.h2.value.ValueVarchar; |
| 416 |
|
| 417 |
/** |
| 418 |
* The parser is used to convert a SQL statement string to an command object. |
| 419 |
* |
| 420 |
* @author Thomas Mueller |
| 421 |
* @author Noel Grandin |
| 422 |
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888 |
| 423 |
*/ |
| 424 |
public class Parser { |
| 425 |
|
| 426 |
private static final String WITH_STATEMENT_SUPPORTS_LIMITED_SUB_STATEMENTS = |
| 427 |
"WITH statement supports only SELECT, TABLE, VALUES, " + |
| 428 |
"CREATE TABLE, INSERT, UPDATE, MERGE or DELETE statements"; |
| 429 |
|
| 430 |
private final Database database; |
| 431 |
private final SessionLocal session; |
| 432 |
|
| 433 |
/** |
| 434 |
* @see org.h2.engine.DbSettings#databaseToLower |
| 435 |
*/ |
| 436 |
private final boolean identifiersToLower; |
| 437 |
/** |
| 438 |
* @see org.h2.engine.DbSettings#databaseToUpper |
| 439 |
*/ |
| 440 |
private final boolean identifiersToUpper; |
| 441 |
|
| 442 |
/** |
| 443 |
* @see org.h2.engine.SessionLocal#isVariableBinary() |
| 444 |
*/ |
| 445 |
private final boolean variableBinary; |
| 446 |
|
| 447 |
private final BitSet nonKeywords; |
| 448 |
|
| 449 |
ArrayList<Token> tokens; |
| 450 |
int tokenIndex; |
| 451 |
Token token; |
| 452 |
private int currentTokenType; |
| 453 |
private String currentToken; |
| 454 |
private String sqlCommand; |
| 455 |
private CreateView createView; |
| 456 |
private Prepared currentPrepared; |
| 457 |
private Select currentSelect; |
| 458 |
private List<TableView> cteCleanups; |
| 459 |
private ArrayList<Parameter> parameters; |
| 460 |
private BitSet usedParameters = new BitSet(); |
| 461 |
private String schemaName; |
| 462 |
private ArrayList<String> expectedList; |
| 463 |
private boolean rightsChecked; |
| 464 |
private boolean recompileAlways; |
| 465 |
private boolean literalsChecked; |
| 466 |
private int orderInFrom; |
| 467 |
private boolean parseDomainConstraint; |
| 468 |
|
| 469 |
/** |
| 470 |
* Parses the specified collection of non-keywords. |
| 471 |
* |
| 472 |
* @param nonKeywords array of non-keywords in upper case |
| 473 |
* @return bit set of non-keywords, or {@code null} |
| 474 |
*/ |
| 475 |
public static BitSet parseNonKeywords(String[] nonKeywords) { |
| 476 |
if (nonKeywords.length == 0) { |
| 477 |
return null; |
| 478 |
} |
| 479 |
BitSet set = new BitSet(); |
| 480 |
for (String nonKeyword : nonKeywords) { |
| 481 |
int index = Arrays.binarySearch(TOKENS, FIRST_KEYWORD, LAST_KEYWORD + 1, nonKeyword); |
| 482 |
if (index >= 0) { |
| 483 |
set.set(index); |
| 484 |
} |
| 485 |
} |
| 486 |
return set.isEmpty() ? null : set; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Formats a comma-separated list of keywords. |
| 491 |
* |
| 492 |
* @param nonKeywords bit set of non-keywords, or {@code null} |
| 493 |
* @return comma-separated list of non-keywords |
| 494 |
*/ |
| 495 |
public static String formatNonKeywords(BitSet nonKeywords) { |
| 496 |
if (nonKeywords == null || nonKeywords.isEmpty()) { |
| 497 |
return ""; |
| 498 |
} |
| 499 |
StringBuilder builder = new StringBuilder(); |
| 500 |
for (int i = -1; (i = nonKeywords.nextSetBit(i + 1)) >= 0;) { |
| 501 |
if (i >= FIRST_KEYWORD && i <= LAST_KEYWORD) { |
| 502 |
if (builder.length() > 0) { |
| 503 |
builder.append(','); |
| 504 |
} |
| 505 |
builder.append(TOKENS[i]); |
| 506 |
} |
| 507 |
} |
| 508 |
return builder.toString(); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Creates a new instance of parser. |
| 513 |
* |
| 514 |
* @param session the session |
| 515 |
*/ |
| 516 |
public Parser(SessionLocal session) { |
| 517 |
this.database = session.getDatabase(); |
| 518 |
DbSettings settings = database.getSettings(); |
| 519 |
this.identifiersToLower = settings.databaseToLower; |
| 520 |
this.identifiersToUpper = settings.databaseToUpper; |
| 521 |
this.variableBinary = session.isVariableBinary(); |
| 522 |
this.nonKeywords = session.getNonKeywords(); |
| 523 |
this.session = session; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Creates a new instance of parser for special use cases. |
| 528 |
*/ |
| 529 |
public Parser() { |
| 530 |
database = null; |
| 531 |
identifiersToLower = false; |
| 532 |
identifiersToUpper = false; |
| 533 |
variableBinary = false; |
| 534 |
nonKeywords = null; |
| 535 |
session = null; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Parse the statement and prepare it for execution. |
| 540 |
* |
| 541 |
* @param sql the SQL statement to parse |
| 542 |
* @return the prepared object |
| 543 |
*/ |
| 544 |
public Prepared prepare(String sql) { |
| 545 |
Prepared p = parse(sql, null); |
| 546 |
p.prepare(); |
| 547 |
if (currentTokenType != END_OF_INPUT) { |
| 548 |
throw getSyntaxError(); |
| 549 |
} |
| 550 |
return p; |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Parse a query and prepare its expressions. Rights and literals must be |
| 555 |
* already checked. |
| 556 |
* |
| 557 |
* @param sql the SQL statement to parse |
| 558 |
* @return the prepared object |
| 559 |
*/ |
| 560 |
public Query prepareQueryExpression(String sql) { |
| 561 |
Query q = (Query) parse(sql, null); |
| 562 |
q.prepareExpressions(); |
| 563 |
if (currentTokenType != END_OF_INPUT) { |
| 564 |
throw getSyntaxError(); |
| 565 |
} |
| 566 |
return q; |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Parse a statement or a list of statements, and prepare it for execution. |
| 571 |
* |
| 572 |
* @param sql the SQL statement to parse |
| 573 |
* @return the command object |
| 574 |
*/ |
| 575 |
public Command prepareCommand(String sql) { |
| 576 |
try { |
| 577 |
Prepared p = parse(sql, null); |
| 578 |
if (currentTokenType != SEMICOLON && currentTokenType != END_OF_INPUT) { |
| 579 |
addExpected(SEMICOLON); |
| 580 |
throw getSyntaxError(); |
| 581 |
} |
| 582 |
try { |
| 583 |
p.prepare(); |
| 584 |
} catch (Throwable t) { |
| 585 |
CommandContainer.clearCTE(session, p); |
| 586 |
throw t; |
| 587 |
} |
| 588 |
int sqlIndex = token.start(); |
| 589 |
if (sqlIndex < sql.length()) { |
| 590 |
sql = sql.substring(0, sqlIndex); |
| 591 |
} |
| 592 |
CommandContainer c = new CommandContainer(session, sql, p); |
| 593 |
while (currentTokenType == SEMICOLON) { |
| 594 |
read(); |
| 595 |
} |
| 596 |
if (currentTokenType != END_OF_INPUT) { |
| 597 |
int offset = token.start(); |
| 598 |
return prepareCommandList(c, p, sql, sqlCommand.substring(offset), getRemainingTokens(offset)); |
| 599 |
} |
| 600 |
return c; |
| 601 |
} catch (DbException e) { |
| 602 |
throw e.addSQL(sqlCommand); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
private CommandList prepareCommandList(CommandContainer command, Prepared p, String sql, String remainingSql, |
| 607 |
ArrayList<Token> remainingTokens) { |
| 608 |
try { |
| 609 |
ArrayList<Prepared> list = Utils.newSmallArrayList(); |
| 610 |
for (;;) { |
| 611 |
if (p instanceof DefineCommand) { |
| 612 |
// Next commands may depend on results of this command. |
| 613 |
return new CommandList(session, sql, command, list, parameters, remainingSql); |
| 614 |
} |
| 615 |
try { |
| 616 |
p = parse(remainingSql, remainingTokens); |
| 617 |
} catch (DbException ex) { |
| 618 |
// This command may depend on results of previous commands. |
| 619 |
if (ex.getErrorCode() == ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS) { |
| 620 |
throw ex; |
| 621 |
} |
| 622 |
return new CommandList(session, sql, command, list, parameters, remainingSql); |
| 623 |
} |
| 624 |
list.add(p); |
| 625 |
if (currentTokenType != SEMICOLON && currentTokenType != END_OF_INPUT) { |
| 626 |
addExpected(SEMICOLON); |
| 627 |
throw getSyntaxError(); |
| 628 |
} |
| 629 |
while (currentTokenType == SEMICOLON) { |
| 630 |
read(); |
| 631 |
} |
| 632 |
if (currentTokenType == END_OF_INPUT) { |
| 633 |
break; |
| 634 |
} |
| 635 |
int offset = token.start(); |
| 636 |
remainingSql = sqlCommand.substring(offset); |
| 637 |
remainingTokens = getRemainingTokens(offset); |
| 638 |
} |
| 639 |
return new CommandList(session, sql, command, list, parameters, null); |
| 640 |
} catch (Throwable t) { |
| 641 |
command.clearCTE(); |
| 642 |
throw t; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
private ArrayList<Token> getRemainingTokens(int offset) { |
| 647 |
List<Token> subList = tokens.subList(tokenIndex, tokens.size()); |
| 648 |
ArrayList<Token> remainingTokens = new ArrayList<>(subList); |
| 649 |
subList.clear(); |
| 650 |
tokens.add(new Token.EndOfInputToken(offset)); |
| 651 |
for (Token token : remainingTokens) { |
| 652 |
token.subtractFromStart(offset); |
| 653 |
} |
| 654 |
return remainingTokens; |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Parse the statement, but don't prepare it for execution. |
| 659 |
* |
| 660 |
* @param sql the SQL statement to parse |
| 661 |
* @param tokens tokens, or null |
| 662 |
* @return the prepared object |
| 663 |
*/ |
| 664 |
Prepared parse(String sql, ArrayList<Token> tokens) { |
| 665 |
initialize(sql, tokens, false); |
| 666 |
Prepared p; |
| 667 |
try { |
| 668 |
// first, try the fast variant |
| 669 |
p = parse(false); |
| 670 |
} catch (DbException e) { |
| 671 |
if (e.getErrorCode() == ErrorCode.SYNTAX_ERROR_1) { |
| 672 |
// now, get the detailed exception |
| 673 |
resetTokenIndex(); |
| 674 |
p = parse(true); |
| 675 |
} else { |
| 676 |
throw e.addSQL(sql); |
| 677 |
} |
| 678 |
} |
| 679 |
return p; |
| 680 |
} |
| 681 |
|
| 682 |
private Prepared parse(boolean withExpectedList) { |
| 683 |
if (withExpectedList) { |
| 684 |
expectedList = new ArrayList<>(); |
| 685 |
} else { |
| 686 |
expectedList = null; |
| 687 |
} |
| 688 |
currentSelect = null; |
| 689 |
currentPrepared = null; |
| 690 |
createView = null; |
| 691 |
cteCleanups = null; |
| 692 |
recompileAlways = false; |
| 693 |
usedParameters.clear(); |
| 694 |
read(); |
| 695 |
Prepared p; |
| 696 |
try { |
| 697 |
p = parsePrepared(); |
| 698 |
p.setCteCleanups(cteCleanups); |
| 699 |
} catch (Throwable t) { |
| 700 |
if (cteCleanups != null) { |
| 701 |
CommandContainer.clearCTE(session, cteCleanups); |
| 702 |
} |
| 703 |
throw t; |
| 704 |
} |
| 705 |
p.setPrepareAlways(recompileAlways); |
| 706 |
p.setParameterList(parameters); |
| 707 |
return p; |
| 708 |
} |
| 709 |
|
| 710 |
private Prepared parsePrepared() { |
| 711 |
int start = tokenIndex; |
| 712 |
Prepared c = null; |
| 713 |
switch (currentTokenType) { |
| 714 |
case END_OF_INPUT: |
| 715 |
case SEMICOLON: |
| 716 |
c = new NoOperation(session); |
| 717 |
setSQL(c, start); |
| 718 |
return c; |
| 719 |
case PARAMETER: |
| 720 |
// read the ? as a parameter |
| 721 |
// this is an 'out' parameter - set a dummy value |
| 722 |
readParameter().setValue(ValueNull.INSTANCE); |
| 723 |
read(EQUAL); |
| 724 |
start = tokenIndex; |
| 725 |
read("CALL"); |
| 726 |
c = parseCall(); |
| 727 |
break; |
| 728 |
case OPEN_PAREN: |
| 729 |
case SELECT: |
| 730 |
case TABLE: |
| 731 |
case VALUES: |
| 732 |
c = parseQuery(); |
| 733 |
break; |
| 734 |
case WITH: |
| 735 |
read(); |
| 736 |
c = parseWithStatementOrQuery(start); |
| 737 |
break; |
| 738 |
case SET: |
| 739 |
read(); |
| 740 |
c = parseSet(); |
| 741 |
break; |
| 742 |
case IDENTIFIER: |
| 743 |
if (token.isQuoted()) { |
| 744 |
break; |
| 745 |
} |
| 746 |
/* |
| 747 |
* Convert a-z to A-Z. This method is safe, because only A-Z |
| 748 |
* characters are considered below. |
| 749 |
* |
| 750 |
* Unquoted identifier is never empty. |
| 751 |
*/ |
| 752 |
switch (currentToken.charAt(0) & 0xffdf) { |
| 753 |
case 'A': |
| 754 |
if (readIf("ALTER")) { |
| 755 |
c = parseAlter(); |
| 756 |
} else if (readIf("ANALYZE")) { |
| 757 |
c = parseAnalyze(); |
| 758 |
} |
| 759 |
break; |
| 760 |
case 'B': |
| 761 |
if (readIf("BACKUP")) { |
| 762 |
c = parseBackup(); |
| 763 |
} else if (readIf("BEGIN")) { |
| 764 |
c = parseBegin(); |
| 765 |
} |
| 766 |
break; |
| 767 |
case 'C': |
| 768 |
if (readIf("COMMIT")) { |
| 769 |
c = parseCommit(); |
| 770 |
} else if (readIf("CREATE")) { |
| 771 |
c = parseCreate(); |
| 772 |
} else if (readIf("CALL")) { |
| 773 |
c = parseCall(); |
| 774 |
} else if (readIf("CHECKPOINT")) { |
| 775 |
c = parseCheckpoint(); |
| 776 |
} else if (readIf("COMMENT")) { |
| 777 |
c = parseComment(); |
| 778 |
} |
| 779 |
break; |
| 780 |
case 'D': |
| 781 |
if (readIf("DELETE")) { |
| 782 |
c = parseDelete(start); |
| 783 |
} else if (readIf("DROP")) { |
| 784 |
c = parseDrop(); |
| 785 |
} else if (readIf("DECLARE")) { |
| 786 |
// support for DECLARE GLOBAL TEMPORARY TABLE... |
| 787 |
c = parseCreate(); |
| 788 |
} else if (database.getMode().getEnum() != ModeEnum.MSSQLServer && readIf("DEALLOCATE")) { |
| 789 |
/* |
| 790 |
* PostgreSQL-style DEALLOCATE is disabled in MSSQLServer |
| 791 |
* mode because PostgreSQL-style EXECUTE is redefined in |
| 792 |
* this mode. |
| 793 |
*/ |
| 794 |
c = parseDeallocate(); |
| 795 |
} |
| 796 |
break; |
| 797 |
case 'E': |
| 798 |
if (readIf("EXPLAIN")) { |
| 799 |
c = parseExplain(); |
| 800 |
} else if (database.getMode().getEnum() != ModeEnum.MSSQLServer) { |
| 801 |
if (readIf("EXECUTE")) { |
| 802 |
c = parseExecutePostgre(); |
| 803 |
} |
| 804 |
} else { |
| 805 |
if (readIf("EXEC") || readIf("EXECUTE")) { |
| 806 |
c = parseExecuteSQLServer(); |
| 807 |
} |
| 808 |
} |
| 809 |
break; |
| 810 |
case 'G': |
| 811 |
if (readIf("GRANT")) { |
| 812 |
c = parseGrantRevoke(CommandInterface.GRANT); |
| 813 |
} |
| 814 |
break; |
| 815 |
case 'H': |
| 816 |
if (readIf("HELP")) { |
| 817 |
c = parseHelp(); |
| 818 |
} |
| 819 |
break; |
| 820 |
case 'I': |
| 821 |
if (readIf("INSERT")) { |
| 822 |
c = parseInsert(start); |
| 823 |
} |
| 824 |
break; |
| 825 |
case 'M': |
| 826 |
if (readIf("MERGE")) { |
| 827 |
c = parseMerge(start); |
| 828 |
} |
| 829 |
break; |
| 830 |
case 'P': |
| 831 |
if (readIf("PREPARE")) { |
| 832 |
c = parsePrepare(); |
| 833 |
} |
| 834 |
break; |
| 835 |
case 'R': |
| 836 |
if (readIf("ROLLBACK")) { |
| 837 |
c = parseRollback(); |
| 838 |
} else if (readIf("REVOKE")) { |
| 839 |
c = parseGrantRevoke(CommandInterface.REVOKE); |
| 840 |
} else if (readIf("RUNSCRIPT")) { |
| 841 |
c = parseRunScript(); |
| 842 |
} else if (readIf("RELEASE")) { |
| 843 |
c = parseReleaseSavepoint(); |
| 844 |
} else if (database.getMode().replaceInto && readIf("REPLACE")) { |
| 845 |
c = parseReplace(start); |
| 846 |
} else if (readIf("REFRESH")) { |
| 847 |
c = parseRefresh(start); |
| 848 |
} |
| 849 |
break; |
| 850 |
case 'S': |
| 851 |
if (readIf("SAVEPOINT")) { |
| 852 |
c = parseSavepoint(); |
| 853 |
} else if (readIf("SCRIPT")) { |
| 854 |
c = parseScript(); |
| 855 |
} else if (readIf("SHUTDOWN")) { |
| 856 |
c = parseShutdown(); |
| 857 |
} else if (readIf("SHOW")) { |
| 858 |
c = parseShow(); |
| 859 |
} |
| 860 |
break; |
| 861 |
case 'T': |
| 862 |
if (readIf("TRUNCATE")) { |
| 863 |
c = parseTruncate(); |
| 864 |
} |
| 865 |
break; |
| 866 |
case 'U': |
| 867 |
if (readIf("UPDATE")) { |
| 868 |
c = parseUpdate(start); |
| 869 |
} else if (readIf("USE")) { |
| 870 |
c = parseUse(); |
| 871 |
} |
| 872 |
break; |
| 873 |
} |
| 874 |
} |
| 875 |
if (c == null) { |
| 876 |
throw getSyntaxError(); |
| 877 |
} |
| 878 |
boolean withParamValues = readIf(OPEN_BRACE); |
| 879 |
if (withParamValues) { |
| 880 |
do { |
| 881 |
int index = (int) readLong() - 1; |
| 882 |
if (index < 0 || index >= parameters.size()) { |
| 883 |
throw getSyntaxError(); |
| 884 |
} |
| 885 |
Parameter p = parameters.get(index); |
| 886 |
if (p == null) { |
| 887 |
throw getSyntaxError(); |
| 888 |
} |
| 889 |
read(COLON); |
| 890 |
Expression expr = readExpression(); |
| 891 |
expr = expr.optimize(session); |
| 892 |
p.setValue(expr.getValue(session)); |
| 893 |
} while (readIf(COMMA)); |
| 894 |
read(CLOSE_BRACE); |
| 895 |
for (Parameter p : parameters) { |
| 896 |
p.checkSet(); |
| 897 |
} |
| 898 |
c.setWithParamValues(true); |
| 899 |
} |
| 900 |
if (withParamValues || c.getSQL() == null) { |
| 901 |
setSQL(c, start); |
| 902 |
} |
| 903 |
return c; |
| 904 |
} |
| 905 |
|
| 906 |
private DbException getSyntaxError() { |
| 907 |
if (expectedList == null || expectedList.isEmpty()) { |
| 908 |
return DbException.getSyntaxError(sqlCommand, token.start()); |
| 909 |
} |
| 910 |
return DbException.getSyntaxError(sqlCommand, token.start(), String.join(", ", expectedList)); |
| 911 |
} |
| 912 |
|
| 913 |
private Prepared parseBackup() { |
| 914 |
BackupCommand command = new BackupCommand(session); |
| 915 |
read(TO); |
| 916 |
command.setFileName(readExpression()); |
| 917 |
return command; |
| 918 |
} |
| 919 |
|
| 920 |
private Prepared parseAnalyze() { |
| 921 |
Analyze command = new Analyze(session); |
| 922 |
if (readIf(TABLE)) { |
| 923 |
Table table = readTableOrView(); |
| 924 |
command.setTable(table); |
| 925 |
} |
| 926 |
if (readIf("SAMPLE_SIZE")) { |
| 927 |
command.setTop(readNonNegativeInt()); |
| 928 |
} |
| 929 |
return command; |
| 930 |
} |
| 931 |
|
| 932 |
private TransactionCommand parseBegin() { |
| 933 |
TransactionCommand command; |
| 934 |
if (!readIf("WORK")) { |
| 935 |
readIf("TRANSACTION"); |
| 936 |
} |
| 937 |
command = new TransactionCommand(session, CommandInterface.BEGIN); |
| 938 |
return command; |
| 939 |
} |
| 940 |
|
| 941 |
private TransactionCommand parseCommit() { |
| 942 |
TransactionCommand command; |
| 943 |
if (readIf("TRANSACTION")) { |
| 944 |
command = new TransactionCommand(session, CommandInterface.COMMIT_TRANSACTION); |
| 945 |
command.setTransactionName(readIdentifier()); |
| 946 |
return command; |
| 947 |
} |
| 948 |
command = new TransactionCommand(session, CommandInterface.COMMIT); |
| 949 |
readIf("WORK"); |
| 950 |
return command; |
| 951 |
} |
| 952 |
|
| 953 |
private TransactionCommand parseShutdown() { |
| 954 |
int type = CommandInterface.SHUTDOWN; |
| 955 |
if (readIf("IMMEDIATELY")) { |
| 956 |
type = CommandInterface.SHUTDOWN_IMMEDIATELY; |
| 957 |
} else if (readIf("COMPACT")) { |
| 958 |
type = CommandInterface.SHUTDOWN_COMPACT; |
| 959 |
} else if (readIf("DEFRAG")) { |
| 960 |
type = CommandInterface.SHUTDOWN_DEFRAG; |
| 961 |
} else { |
| 962 |
readIf("SCRIPT"); |
| 963 |
} |
| 964 |
return new TransactionCommand(session, type); |
| 965 |
} |
| 966 |
|
| 967 |
private TransactionCommand parseRollback() { |
| 968 |
TransactionCommand command; |
| 969 |
if (readIf("TRANSACTION")) { |
| 970 |
command = new TransactionCommand(session, CommandInterface.ROLLBACK_TRANSACTION); |
| 971 |
command.setTransactionName(readIdentifier()); |
| 972 |
return command; |
| 973 |
} |
| 974 |
readIf("WORK"); |
| 975 |
if (readIf(TO, "SAVEPOINT")) { |
| 976 |
command = new TransactionCommand(session, CommandInterface.ROLLBACK_TO_SAVEPOINT); |
| 977 |
command.setSavepointName(readIdentifier()); |
| 978 |
} else { |
| 979 |
command = new TransactionCommand(session, CommandInterface.ROLLBACK); |
| 980 |
} |
| 981 |
return command; |
| 982 |
} |
| 983 |
|
| 984 |
private Prepared parsePrepare() { |
| 985 |
if (readIf("COMMIT")) { |
| 986 |
TransactionCommand command = new TransactionCommand(session, CommandInterface.PREPARE_COMMIT); |
| 987 |
command.setTransactionName(readIdentifier()); |
| 988 |
return command; |
| 989 |
} |
| 990 |
return parsePrepareProcedure(); |
| 991 |
} |
| 992 |
|
| 993 |
private Prepared parsePrepareProcedure() { |
| 994 |
if (database.getMode().getEnum() == ModeEnum.MSSQLServer) { |
| 995 |
throw getSyntaxError(); |
| 996 |
/* |
| 997 |
* PostgreSQL-style PREPARE is disabled in MSSQLServer mode |
| 998 |
* because PostgreSQL-style EXECUTE is redefined in this |
| 999 |
* mode. |
| 1000 |
*/ |
| 1001 |
} |
| 1002 |
String procedureName = readIdentifier(); |
| 1003 |
if (readIf(OPEN_PAREN)) { |
| 1004 |
ArrayList<Column> list = Utils.newSmallArrayList(); |
| 1005 |
for (int i = 0;; i++) { |
| 1006 |
Column column = parseColumnForTable("C" + i, true); |
| 1007 |
list.add(column); |
| 1008 |
if (!readIfMore()) { |
| 1009 |
break; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
} |
| 1013 |
read(AS); |
| 1014 |
Prepared prep = parsePrepared(); |
| 1015 |
PrepareProcedure command = new PrepareProcedure(session); |
| 1016 |
command.setProcedureName(procedureName); |
| 1017 |
command.setPrepared(prep); |
| 1018 |
return command; |
| 1019 |
} |
| 1020 |
|
| 1021 |
private TransactionCommand parseSavepoint() { |
| 1022 |
TransactionCommand command = new TransactionCommand(session, CommandInterface.SAVEPOINT); |
| 1023 |
command.setSavepointName(readIdentifier()); |
| 1024 |
return command; |
| 1025 |
} |
| 1026 |
|
| 1027 |
private Prepared parseReleaseSavepoint() { |
| 1028 |
Prepared command = new NoOperation(session); |
| 1029 |
readIf("SAVEPOINT"); |
| 1030 |
readIdentifier(); |
| 1031 |
return command; |
| 1032 |
} |
| 1033 |
|
| 1034 |
private Schema findSchema(String schemaName) { |
| 1035 |
if (schemaName == null) { |
| 1036 |
return null; |
| 1037 |
} |
| 1038 |
Schema schema = database.findSchema(schemaName); |
| 1039 |
if (schema == null) { |
| 1040 |
if (equalsToken("SESSION", schemaName)) { |
| 1041 |
// for local temporary tables |
| 1042 |
schema = database.getSchema(session.getCurrentSchemaName()); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return schema; |
| 1046 |
} |
| 1047 |
|
| 1048 |
private Schema getSchema(String schemaName) { |
| 1049 |
if (schemaName == null) { |
| 1050 |
return null; |
| 1051 |
} |
| 1052 |
Schema schema = findSchema(schemaName); |
| 1053 |
if (schema == null) { |
| 1054 |
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName); |
| 1055 |
} |
| 1056 |
return schema; |
| 1057 |
} |
| 1058 |
|
| 1059 |
private Schema getSchema() { |
| 1060 |
return getSchema(schemaName); |
| 1061 |
} |
| 1062 |
/* |
| 1063 |
* Gets the current schema for scenarios that need a guaranteed, non-null schema object. |
| 1064 |
* |
| 1065 |
* This routine is solely here |
| 1066 |
* because of the function readIdentifierWithSchema(String defaultSchemaName) - which |
| 1067 |
* is often called with a null parameter (defaultSchemaName) - then 6 lines into the function |
| 1068 |
* that routine nullifies the state field schemaName - which I believe is a bug. |
| 1069 |
* |
| 1070 |
* There are about 7 places where "readIdentifierWithSchema(null)" is called in this file. |
| 1071 |
* |
| 1072 |
* In other words when is it legal to not have an active schema defined by schemaName ? |
| 1073 |
* I don't think it's ever a valid case. I don't understand when that would be allowed. |
| 1074 |
* I spent a long time trying to figure this out. |
| 1075 |
* As another proof of this point, the command "SET SCHEMA=NULL" is not a valid command. |
| 1076 |
* |
| 1077 |
* I did try to fix this in readIdentifierWithSchema(String defaultSchemaName) |
| 1078 |
* - but every fix I tried cascaded so many unit test errors - so |
| 1079 |
* I gave up. I think this needs a bigger effort to fix his, as part of bigger, dedicated story. |
| 1080 |
* |
| 1081 |
*/ |
| 1082 |
private Schema getSchemaWithDefault() { |
| 1083 |
if (schemaName == null) { |
| 1084 |
schemaName = session.getCurrentSchemaName(); |
| 1085 |
} |
| 1086 |
return getSchema(schemaName); |
| 1087 |
} |
| 1088 |
|
| 1089 |
private Column readTableColumn(TableFilter filter) { |
| 1090 |
String columnName = readIdentifier(); |
| 1091 |
if (readIf(DOT)) { |
| 1092 |
columnName = readTableColumn(filter, columnName); |
| 1093 |
} |
| 1094 |
return filter.getTable().getColumn(columnName); |
| 1095 |
} |
| 1096 |
|
| 1097 |
private String readTableColumn(TableFilter filter, String tableAlias) { |
| 1098 |
String columnName = readIdentifier(); |
| 1099 |
if (readIf(DOT)) { |
| 1100 |
String schema = tableAlias; |
| 1101 |
tableAlias = columnName; |
| 1102 |
columnName = readIdentifier(); |
| 1103 |
if (readIf(DOT)) { |
| 1104 |
checkDatabaseName(schema); |
| 1105 |
schema = tableAlias; |
| 1106 |
tableAlias = columnName; |
| 1107 |
columnName = readIdentifier(); |
| 1108 |
} |
| 1109 |
if (!equalsToken(schema, filter.getTable().getSchema().getName())) { |
| 1110 |
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schema); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
if (!equalsToken(tableAlias, filter.getTableAlias())) { |
| 1114 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableAlias); |
| 1115 |
} |
| 1116 |
return columnName; |
| 1117 |
} |
| 1118 |
|
| 1119 |
private DataChangeStatement parseUpdate(int start) { |
| 1120 |
Update command = new Update(session); |
| 1121 |
currentPrepared = command; |
| 1122 |
Expression fetch = null; |
| 1123 |
if (database.getMode().topInDML && readIf("TOP")) { |
| 1124 |
read(OPEN_PAREN); |
| 1125 |
fetch = readTerm().optimize(session); |
| 1126 |
read(CLOSE_PAREN); |
| 1127 |
} |
| 1128 |
TableFilter targetTableFilter = readSimpleTableFilter(); |
| 1129 |
command.setTableFilter(targetTableFilter); |
| 1130 |
int backupIndex = tokenIndex; |
| 1131 |
if (database.getMode().discardWithTableHints) { |
| 1132 |
discardWithTableHints(); |
| 1133 |
} |
| 1134 |
command.setSetClauseList(readUpdateSetClause(targetTableFilter)); |
| 1135 |
if (database.getMode().allowUsingFromClauseInUpdateStatement && readIf(FROM)) { |
| 1136 |
setTokenIndex(backupIndex); |
| 1137 |
return parseUpdateFrom(targetTableFilter, start); |
| 1138 |
} |
| 1139 |
if (readIf(WHERE)) { |
| 1140 |
command.setCondition(readExpression()); |
| 1141 |
} |
| 1142 |
if (fetch == null) { |
| 1143 |
// for MySQL compatibility |
| 1144 |
// (this syntax is supported, but ignored) |
| 1145 |
readIfOrderBy(); |
| 1146 |
fetch = readFetchOrLimit(); |
| 1147 |
} |
| 1148 |
command.setFetch(fetch); |
| 1149 |
setSQL(command, start); |
| 1150 |
return command; |
| 1151 |
} |
| 1152 |
|
| 1153 |
private MergeUsing parseUpdateFrom(TableFilter targetTableFilter, int start) { |
| 1154 |
MergeUsing command = new MergeUsing(session, targetTableFilter); |
| 1155 |
currentPrepared = command; |
| 1156 |
SetClauseList updateSetClause = readUpdateSetClause(targetTableFilter); |
| 1157 |
read(FROM); |
| 1158 |
command.setSourceTableFilter(readTableReference()); |
| 1159 |
command.setOnCondition(readIf(WHERE) ? readExpression() : ValueExpression.TRUE); |
| 1160 |
MergeUsing.WhenMatchedThenUpdate update = command.new WhenMatchedThenUpdate(); |
| 1161 |
update.setSetClauseList(updateSetClause); |
| 1162 |
command.addWhen(update); |
| 1163 |
setSQL(command, start); |
| 1164 |
return command; |
| 1165 |
} |
| 1166 |
|
| 1167 |
private SetClauseList readUpdateSetClause(TableFilter filter) { |
| 1168 |
read(SET); |
| 1169 |
SetClauseList list = new SetClauseList(filter.getTable()); |
| 1170 |
do { |
| 1171 |
if (readIf(OPEN_PAREN)) { |
| 1172 |
ArrayList<Column> columns = Utils.newSmallArrayList(); |
| 1173 |
ArrayList<Expression[]> allIndexes = Utils.newSmallArrayList(); |
| 1174 |
do { |
| 1175 |
columns.add(readTableColumn(filter)); |
| 1176 |
allIndexes.add(readUpdateSetClauseArrayIndexes()); |
| 1177 |
} while (readIfMore()); |
| 1178 |
read(EQUAL); |
| 1179 |
list.addMultiple(columns, allIndexes, readExpression()); |
| 1180 |
} else { |
| 1181 |
Column column = readTableColumn(filter); |
| 1182 |
Expression[] arrayIndexes = readUpdateSetClauseArrayIndexes(); |
| 1183 |
read(EQUAL); |
| 1184 |
list.addSingle(column, arrayIndexes, |
| 1185 |
arrayIndexes == null ? readExpressionOrDefault() : readExpression()); |
| 1186 |
} |
| 1187 |
} while (readIf(COMMA)); |
| 1188 |
return list; |
| 1189 |
} |
| 1190 |
|
| 1191 |
private Expression[] readUpdateSetClauseArrayIndexes() { |
| 1192 |
if (readIf(OPEN_BRACKET)) { |
| 1193 |
ArrayList<Expression> list = Utils.newSmallArrayList(); |
| 1194 |
do { |
| 1195 |
list.add(readExpression()); |
| 1196 |
read(CLOSE_BRACKET); |
| 1197 |
} while (readIf(OPEN_BRACKET)); |
| 1198 |
return list.toArray(new Expression[0]); |
| 1199 |
} |
| 1200 |
return null; |
| 1201 |
} |
| 1202 |
|
| 1203 |
private TableFilter readSimpleTableFilter() { |
| 1204 |
return new TableFilter(session, readTableOrView(), readFromAlias(null), rightsChecked, currentSelect, 0, null); |
| 1205 |
} |
| 1206 |
|
| 1207 |
private Delete parseDelete(int start) { |
| 1208 |
Delete command = new Delete(session); |
| 1209 |
Expression fetch = null; |
| 1210 |
if (database.getMode().topInDML && readIf("TOP")) { |
| 1211 |
fetch = readTerm().optimize(session); |
| 1212 |
} |
| 1213 |
currentPrepared = command; |
| 1214 |
if (!readIf(FROM) && database.getMode().deleteIdentifierFrom) { |
| 1215 |
readIdentifierWithSchema(); |
| 1216 |
read(FROM); |
| 1217 |
} |
| 1218 |
command.setTableFilter(readSimpleTableFilter()); |
| 1219 |
if (readIf(WHERE)) { |
| 1220 |
command.setCondition(readExpression()); |
| 1221 |
} |
| 1222 |
if (fetch == null) { |
| 1223 |
fetch = readFetchOrLimit(); |
| 1224 |
} |
| 1225 |
command.setFetch(fetch); |
| 1226 |
setSQL(command, start); |
| 1227 |
return command; |
| 1228 |
} |
| 1229 |
|
| 1230 |
private Expression readFetchOrLimit() { |
| 1231 |
Expression fetch = null; |
| 1232 |
if (readIf(FETCH)) { |
| 1233 |
if (!readIf("FIRST")) { |
| 1234 |
read("NEXT"); |
| 1235 |
} |
| 1236 |
if (readIf(ROW) || readIf("ROWS")) { |
| 1237 |
fetch = ValueExpression.get(ValueInteger.get(1)); |
| 1238 |
} else { |
| 1239 |
fetch = readExpression().optimize(session); |
| 1240 |
if (!readIf(ROW)) { |
| 1241 |
read("ROWS"); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
read("ONLY"); |
| 1245 |
} else if (database.getMode().limit && readIf(LIMIT)) { |
| 1246 |
fetch = readTerm().optimize(session); |
| 1247 |
} |
| 1248 |
return fetch; |
| 1249 |
} |
| 1250 |
|
| 1251 |
private IndexColumn[] parseIndexColumnList() { |
| 1252 |
ArrayList<IndexColumn> columns = Utils.newSmallArrayList(); |
| 1253 |
do { |
| 1254 |
columns.add(new IndexColumn(readIdentifier(), parseSortType())); |
| 1255 |
} while (readIfMore()); |
| 1256 |
return columns.toArray(new IndexColumn[0]); |
| 1257 |
} |
| 1258 |
|
| 1259 |
private int parseSortType() { |
| 1260 |
int sortType = !readIf("ASC") && readIf("DESC") ? SortOrder.DESCENDING : SortOrder.ASCENDING; |
| 1261 |
if (readIf("NULLS")) { |
| 1262 |
if (readIf("FIRST")) { |
| 1263 |
sortType |= SortOrder.NULLS_FIRST; |
| 1264 |
} else { |
| 1265 |
read("LAST"); |
| 1266 |
sortType |= SortOrder.NULLS_LAST; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
return sortType; |
| 1270 |
} |
| 1271 |
|
| 1272 |
private String[] parseColumnList() { |
| 1273 |
ArrayList<String> columns = Utils.newSmallArrayList(); |
| 1274 |
do { |
| 1275 |
columns.add(readIdentifier()); |
| 1276 |
} while (readIfMore()); |
| 1277 |
return columns.toArray(new String[0]); |
| 1278 |
} |
| 1279 |
|
| 1280 |
private Column[] parseColumnList(Table table) { |
| 1281 |
ArrayList<Column> columns = Utils.newSmallArrayList(); |
| 1282 |
HashSet<Column> set = new HashSet<>(); |
| 1283 |
if (!readIf(CLOSE_PAREN)) { |
| 1284 |
do { |
| 1285 |
Column column = parseColumn(table); |
| 1286 |
if (!set.add(column)) { |
| 1287 |
throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column.getTraceSQL()); |
| 1288 |
} |
| 1289 |
columns.add(column); |
| 1290 |
} while (readIfMore()); |
| 1291 |
} |
| 1292 |
return columns.toArray(new Column[0]); |
| 1293 |
} |
| 1294 |
|
| 1295 |
private Column parseColumn(Table table) { |
| 1296 |
if (currentTokenType == _ROWID_) { |
| 1297 |
read(); |
| 1298 |
return table.getRowIdColumn(); |
| 1299 |
} |
| 1300 |
return table.getColumn(readIdentifier()); |
| 1301 |
} |
| 1302 |
|
| 1303 |
/** |
| 1304 |
* Read comma or closing brace. |
| 1305 |
* |
| 1306 |
* @return {@code true} if comma is read, {@code false} if brace is read |
| 1307 |
*/ |
| 1308 |
private boolean readIfMore() { |
| 1309 |
if (readIf(COMMA)) { |
| 1310 |
return true; |
| 1311 |
} |
| 1312 |
read(CLOSE_PAREN); |
| 1313 |
return false; |
| 1314 |
} |
| 1315 |
|
| 1316 |
private Prepared parseHelp() { |
| 1317 |
HashSet<String> conditions = new HashSet<>(); |
| 1318 |
while (currentTokenType != END_OF_INPUT) { |
| 1319 |
conditions.add(StringUtils.toUpperEnglish(readIdentifierOrKeyword())); |
| 1320 |
} |
| 1321 |
return new Help(session, conditions.toArray(new String[0])); |
| 1322 |
} |
| 1323 |
|
| 1324 |
private Prepared parseShow() { |
| 1325 |
StringBuilder buff = new StringBuilder("SELECT "); |
| 1326 |
if (readIf("CLIENT_ENCODING")) { |
| 1327 |
// for PostgreSQL compatibility |
| 1328 |
buff.append("'UNICODE' CLIENT_ENCODING"); |
| 1329 |
} else if (readIf("DEFAULT_TRANSACTION_ISOLATION")) { |
| 1330 |
// for PostgreSQL compatibility |
| 1331 |
buff.append("'read committed' DEFAULT_TRANSACTION_ISOLATION"); |
| 1332 |
} else if (readIf("TRANSACTION")) { |
| 1333 |
// for PostgreSQL compatibility |
| 1334 |
read("ISOLATION"); |
| 1335 |
read("LEVEL"); |
| 1336 |
buff.append("LOWER(ISOLATION_LEVEL) TRANSACTION_ISOLATION FROM INFORMATION_SCHEMA.SESSIONS " |
| 1337 |
+ "WHERE SESSION_ID = SESSION_ID()"); |
| 1338 |
} else if (readIf("DATESTYLE")) { |
| 1339 |
// for PostgreSQL compatibility |
| 1340 |
buff.append("'ISO' DATESTYLE"); |
| 1341 |
} else if (readIf("SEARCH_PATH")) { |
| 1342 |
// for PostgreSQL compatibility |
| 1343 |
String[] searchPath = session.getSchemaSearchPath(); |
| 1344 |
StringBuilder searchPathBuff = new StringBuilder(); |
| 1345 |
if (searchPath != null) { |
| 1346 |
for (int i = 0; i < searchPath.length; i++) { |
| 1347 |
if (i > 0) { |
| 1348 |
searchPathBuff.append(", "); |
| 1349 |
} |
| 1350 |
ParserUtil.quoteIdentifier(searchPathBuff, searchPath[i], HasSQL.QUOTE_ONLY_WHEN_REQUIRED); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
StringUtils.quoteStringSQL(buff, searchPathBuff.toString()); |
| 1354 |
buff.append(" SEARCH_PATH"); |
| 1355 |
} else if (readIf("SERVER_VERSION")) { |
| 1356 |
// for PostgreSQL compatibility |
| 1357 |
buff.append("'" + Constants.PG_VERSION + "' SERVER_VERSION"); |
| 1358 |
} else if (readIf("SERVER_ENCODING")) { |
| 1359 |
// for PostgreSQL compatibility |
| 1360 |
buff.append("'UTF8' SERVER_ENCODING"); |
| 1361 |
} else if (readIf("SSL")) { |
| 1362 |
// for PostgreSQL compatibility |
| 1363 |
buff.append("'off' SSL"); |
| 1364 |
} else if (readIf("TABLES")) { |
| 1365 |
// for MySQL compatibility |
| 1366 |
String schema = database.getMainSchema().getName(); |
| 1367 |
if (readIf(FROM)) { |
| 1368 |
schema = readIdentifier(); |
| 1369 |
} |
| 1370 |
buff.append("TABLE_NAME, TABLE_SCHEMA FROM " |
| 1371 |
+ "INFORMATION_SCHEMA.TABLES " |
| 1372 |
+ "WHERE TABLE_SCHEMA="); |
| 1373 |
StringUtils.quoteStringSQL(buff, schema).append(" ORDER BY TABLE_NAME"); |
| 1374 |
} else if (readIf("COLUMNS")) { |
| 1375 |
// for MySQL compatibility |
| 1376 |
read(FROM); |
| 1377 |
String tableName = readIdentifierWithSchema(); |
| 1378 |
String schemaName = getSchema().getName(); |
| 1379 |
if (readIf(FROM)) { |
| 1380 |
schemaName = readIdentifier(); |
| 1381 |
} |
| 1382 |
buff.append("C.COLUMN_NAME FIELD, "); |
| 1383 |
boolean oldInformationSchema = session.isOldInformationSchema(); |
| 1384 |
if (oldInformationSchema) { |
| 1385 |
buff.append("C.COLUMN_TYPE"); |
| 1386 |
} else { |
| 1387 |
buff.append("DATA_TYPE_SQL("); |
| 1388 |
StringUtils.quoteStringSQL(buff, schemaName).append(", "); |
| 1389 |
StringUtils.quoteStringSQL(buff, tableName).append(", 'TABLE', C.DTD_IDENTIFIER)"); |
| 1390 |
} |
| 1391 |
buff.append(" TYPE, " |
| 1392 |
+ "C.IS_NULLABLE \"NULL\", " |
| 1393 |
+ "CASE (SELECT MAX(I.INDEX_TYPE_NAME) FROM " |
| 1394 |
+ "INFORMATION_SCHEMA.INDEXES I "); |
| 1395 |
if (!oldInformationSchema) { |
| 1396 |
buff.append("JOIN INFORMATION_SCHEMA.INDEX_COLUMNS IC "); |
| 1397 |
} |
| 1398 |
buff.append("WHERE I.TABLE_SCHEMA=C.TABLE_SCHEMA " |
| 1399 |
+ "AND I.TABLE_NAME=C.TABLE_NAME "); |
| 1400 |
if (oldInformationSchema) { |
| 1401 |
buff.append("AND I.COLUMN_NAME=C.COLUMN_NAME"); |
| 1402 |
} else { |
| 1403 |
buff.append("AND IC.TABLE_SCHEMA=C.TABLE_SCHEMA " |
| 1404 |
+ "AND IC.TABLE_NAME=C.TABLE_NAME " |
| 1405 |
+ "AND IC.INDEX_SCHEMA=I.INDEX_SCHEMA " |
| 1406 |
+ "AND IC.INDEX_NAME=I.INDEX_NAME " |
| 1407 |
+ "AND IC.COLUMN_NAME=C.COLUMN_NAME"); |
| 1408 |
} |
| 1409 |
buff.append(')' |
| 1410 |
+ "WHEN 'PRIMARY KEY' THEN 'PRI' " |
| 1411 |
+ "WHEN 'UNIQUE INDEX' THEN 'UNI' ELSE '' END `KEY`, " |
| 1412 |
+ "COALESCE(COLUMN_DEFAULT, 'NULL') `DEFAULT` " |
| 1413 |
+ "FROM INFORMATION_SCHEMA.COLUMNS C " |
| 1414 |
+ "WHERE C.TABLE_SCHEMA="); |
| 1415 |
StringUtils.quoteStringSQL(buff, schemaName).append(" AND C.TABLE_NAME="); |
| 1416 |
StringUtils.quoteStringSQL(buff, tableName).append(" ORDER BY C.ORDINAL_POSITION"); |
| 1417 |
} else if (readIf("DATABASES") || readIf("SCHEMAS")) { |
| 1418 |
// for MySQL compatibility |
| 1419 |
buff.append("SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA"); |
| 1420 |
} else if (database.getMode().getEnum() == ModeEnum.PostgreSQL && readIf(ALL)) { |
| 1421 |
// for PostgreSQL compatibility |
| 1422 |
buff.append("NAME, SETTING FROM PG_CATALOG.PG_SETTINGS"); |
| 1423 |
} |
| 1424 |
boolean b = session.getAllowLiterals(); |
| 1425 |
try { |
| 1426 |
// need to temporarily enable it |
| 1427 |
session.setAllowLiterals(true); |
| 1428 |
return session.prepare(buff.toString()); |
| 1429 |
} finally { |
| 1430 |
session.setAllowLiterals(b); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
private boolean isDerivedTable() { |
| 1435 |
int offset = tokenIndex; |
| 1436 |
int level = 0; |
| 1437 |
while (tokens.get(offset).tokenType() == OPEN_PAREN) { |
| 1438 |
level++; |
| 1439 |
offset++; |
| 1440 |
} |
| 1441 |
boolean query = isDirectQuery(offset); |
| 1442 |
s: if (query && level > 0) { |
| 1443 |
offset = scanToCloseParen(offset + 1); |
| 1444 |
if (offset < 0) { |
| 1445 |
query = false; |
| 1446 |
break s; |
| 1447 |
} |
| 1448 |
for (;;) { |
| 1449 |
switch (tokens.get(offset).tokenType()) { |
| 1450 |
case SEMICOLON: |
| 1451 |
case END_OF_INPUT: |
| 1452 |
query = false; |
| 1453 |
break s; |
| 1454 |
case OPEN_PAREN: |
| 1455 |
offset = scanToCloseParen(offset + 1); |
| 1456 |
if (offset < 0) { |
| 1457 |
query = false; |
| 1458 |
break s; |
| 1459 |
} |
| 1460 |
break; |
| 1461 |
case CLOSE_PAREN: |
| 1462 |
if (--level == 0) { |
| 1463 |
break s; |
| 1464 |
} |
| 1465 |
offset++; |
| 1466 |
break; |
| 1467 |
case JOIN: |
| 1468 |
query = false; |
| 1469 |
break s; |
| 1470 |
default: |
| 1471 |
offset++; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
} |
| 1475 |
return query; |
| 1476 |
} |
| 1477 |
|
| 1478 |
private boolean isQuery() { |
| 1479 |
int offset = tokenIndex; |
| 1480 |
int level = 0; |
| 1481 |
while (tokens.get(offset).tokenType() == OPEN_PAREN) { |
| 1482 |
level++; |
| 1483 |
offset++; |
| 1484 |
} |
| 1485 |
boolean query = isDirectQuery(offset); |
| 1486 |
s: if (query && level > 0) { |
| 1487 |
offset++; |
| 1488 |
do { |
| 1489 |
offset = scanToCloseParen(offset); |
| 1490 |
if (offset < 0) { |
| 1491 |
query = false; |
| 1492 |
break s; |
| 1493 |
} |
| 1494 |
switch (tokens.get(offset).tokenType()) { |
| 1495 |
default: |
| 1496 |
query = false; |
| 1497 |
break s; |
| 1498 |
case END_OF_INPUT: |
| 1499 |
case SEMICOLON: |
| 1500 |
case CLOSE_PAREN: |
| 1501 |
case ORDER: |
| 1502 |
case OFFSET: |
| 1503 |
case FETCH: |
| 1504 |
case LIMIT: |
| 1505 |
case UNION: |
| 1506 |
case EXCEPT: |
| 1507 |
case MINUS: |
| 1508 |
case INTERSECT: |
| 1509 |
} |
| 1510 |
} while (--level > 0); |
| 1511 |
} |
| 1512 |
return query; |
| 1513 |
} |
| 1514 |
|
| 1515 |
private int scanToCloseParen(int offset) { |
| 1516 |
for (int level = 0;;) { |
| 1517 |
switch (tokens.get(offset).tokenType()) { |
| 1518 |
case SEMICOLON: |
| 1519 |
case END_OF_INPUT: |
| 1520 |
return -1; |
| 1521 |
case OPEN_PAREN: |
| 1522 |
level++; |
| 1523 |
break; |
| 1524 |
case CLOSE_PAREN: |
| 1525 |
if (--level < 0) { |
| 1526 |
return offset + 1; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
offset++; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
private boolean isQueryQuick() { |
| 1534 |
int offset = tokenIndex; |
| 1535 |
while (tokens.get(offset).tokenType() == OPEN_PAREN) { |
| 1536 |
offset++; |
| 1537 |
} |
| 1538 |
return isDirectQuery(offset); |
| 1539 |
} |
| 1540 |
|
| 1541 |
private boolean isDirectQuery(int offset) { |
| 1542 |
boolean query; |
| 1543 |
switch (tokens.get(offset).tokenType()) { |
| 1544 |
case SELECT: |
| 1545 |
case VALUES: |
| 1546 |
case WITH: |
| 1547 |
query = true; |
| 1548 |
break; |
| 1549 |
case TABLE: |
| 1550 |
query = tokens.get(offset + 1).tokenType() != OPEN_PAREN; |
| 1551 |
break; |
| 1552 |
default: |
| 1553 |
query = false; |
| 1554 |
} |
| 1555 |
return query; |
| 1556 |
} |
| 1557 |
|
| 1558 |
private Prepared parseMerge(int start) { |
| 1559 |
read("INTO"); |
| 1560 |
TableFilter targetTableFilter = readSimpleTableFilter(); |
| 1561 |
if (readIf(USING)) { |
| 1562 |
return parseMergeUsing(targetTableFilter, start); |
| 1563 |
} |
| 1564 |
return parseMergeInto(targetTableFilter, start); |
| 1565 |
} |
| 1566 |
|
| 1567 |
private Prepared parseMergeInto(TableFilter targetTableFilter, int start) { |
| 1568 |
Merge command = new Merge(session, false); |
| 1569 |
currentPrepared = command; |
| 1570 |
command.setTable(targetTableFilter.getTable()); |
| 1571 |
Table table = command.getTable(); |
| 1572 |
if (readIf(OPEN_PAREN)) { |
| 1573 |
if (isQueryQuick()) { |
| 1574 |
command.setQuery(parseQuery()); |
| 1575 |
read(CLOSE_PAREN); |
| 1576 |
return command; |
| 1577 |
} |
| 1578 |
command.setColumns(parseColumnList(table)); |
| 1579 |
} |
| 1580 |
if (readIf(KEY, OPEN_PAREN)) { |
| 1581 |
command.setKeys(parseColumnList(table)); |
| 1582 |
} |
| 1583 |
if (readIf(VALUES)) { |
| 1584 |
parseValuesForCommand(command); |
| 1585 |
} else { |
| 1586 |
command.setQuery(parseQuery()); |
| 1587 |
} |
| 1588 |
setSQL(command, start); |
| 1589 |
return command; |
| 1590 |
} |
| 1591 |
|
| 1592 |
private MergeUsing parseMergeUsing(TableFilter targetTableFilter, int start) { |
| 1593 |
MergeUsing command = new MergeUsing(session, targetTableFilter); |
| 1594 |
currentPrepared = command; |
| 1595 |
command.setSourceTableFilter(readTableReference()); |
| 1596 |
read(ON); |
| 1597 |
Expression condition = readExpression(); |
| 1598 |
command.setOnCondition(condition); |
| 1599 |
|
| 1600 |
read(WHEN); |
| 1601 |
do { |
| 1602 |
boolean matched = readIf("MATCHED"); |
| 1603 |
if (matched) { |
| 1604 |
parseWhenMatched(command); |
| 1605 |
} else { |
| 1606 |
parseWhenNotMatched(command); |
| 1607 |
} |
| 1608 |
} while (readIf(WHEN)); |
| 1609 |
|
| 1610 |
setSQL(command, start); |
| 1611 |
return command; |
| 1612 |
} |
| 1613 |
|
| 1614 |
private void parseWhenMatched(MergeUsing command) { |
| 1615 |
Expression and = readIf(AND) ? readExpression() : null; |
| 1616 |
read("THEN"); |
| 1617 |
MergeUsing.When when; |
| 1618 |
if (readIf("UPDATE")) { |
| 1619 |
MergeUsing.WhenMatchedThenUpdate update = command.new WhenMatchedThenUpdate(); |
| 1620 |
update.setSetClauseList(readUpdateSetClause(command.getTargetTableFilter())); |
| 1621 |
when = update; |
| 1622 |
} else { |
| 1623 |
read("DELETE"); |
| 1624 |
when = command.new WhenMatchedThenDelete(); |
| 1625 |
} |
| 1626 |
if (and == null && database.getMode().mergeWhere && readIf(WHERE)) { |
| 1627 |
and = readExpression(); |
| 1628 |
} |
| 1629 |
when.setAndCondition(and); |
| 1630 |
command.addWhen(when); |
| 1631 |
} |
| 1632 |
|
| 1633 |
private void parseWhenNotMatched(MergeUsing command) { |
| 1634 |
read(NOT); |
| 1635 |
read("MATCHED"); |
| 1636 |
Expression and = readIf(AND) ? readExpression() : null; |
| 1637 |
read("THEN"); |
| 1638 |
read("INSERT"); |
| 1639 |
Column[] columns = readIf(OPEN_PAREN) ? parseColumnList(command.getTargetTableFilter().getTable()) : null; |
| 1640 |
Boolean overridingSystem = readIfOverriding(); |
| 1641 |
read(VALUES); |
| 1642 |
read(OPEN_PAREN); |
| 1643 |
ArrayList<Expression> values = Utils.newSmallArrayList(); |
| 1644 |
if (!readIf(CLOSE_PAREN)) { |
| 1645 |
do { |
| 1646 |
values.add(readExpressionOrDefault()); |
| 1647 |
} while (readIfMore()); |
| 1648 |
} |
| 1649 |
MergeUsing.WhenNotMatched when = command.new WhenNotMatched(columns, overridingSystem, |
| 1650 |
values.toArray(new Expression[0])); |
| 1651 |
when.setAndCondition(and); |
| 1652 |
command.addWhen(when); |
| 1653 |
} |
| 1654 |
|
| 1655 |
private Insert parseInsert(int start) { |
| 1656 |
Insert command = new Insert(session); |
| 1657 |
currentPrepared = command; |
| 1658 |
Mode mode = database.getMode(); |
| 1659 |
if (mode.onDuplicateKeyUpdate && readIf("IGNORE")) { |
| 1660 |
command.setIgnore(true); |
| 1661 |
} |
| 1662 |
read("INTO"); |
| 1663 |
Table table = readTableOrView(); |
| 1664 |
command.setTable(table); |
| 1665 |
Column[] columns = null; |
| 1666 |
if (readIf(OPEN_PAREN)) { |
| 1667 |
if (isQueryQuick()) { |
| 1668 |
command.setQuery(parseQuery()); |
| 1669 |
read(CLOSE_PAREN); |
| 1670 |
return command; |
| 1671 |
} |
| 1672 |
columns = parseColumnList(table); |
| 1673 |
command.setColumns(columns); |
| 1674 |
} |
| 1675 |
Boolean overridingSystem = readIfOverriding(); |
| 1676 |
command.setOverridingSystem(overridingSystem); |
| 1677 |
boolean requireQuery = false; |
| 1678 |
if (readIf("DIRECT")) { |
| 1679 |
requireQuery = true; |
| 1680 |
command.setInsertFromSelect(true); |
| 1681 |
} |
| 1682 |
if (readIf("SORTED")) { |
| 1683 |
requireQuery = true; |
| 1684 |
} |
| 1685 |
readValues: { |
| 1686 |
if (!requireQuery) { |
| 1687 |
if (overridingSystem == null && readIf(DEFAULT, VALUES)) { |
| 1688 |
command.addRow(new Expression[0]); |
| 1689 |
break readValues; |
| 1690 |
} |
| 1691 |
if (readIf(VALUES)) { |
| 1692 |
parseValuesForCommand(command); |
| 1693 |
break readValues; |
| 1694 |
} |
| 1695 |
if (readIf(SET)) { |
| 1696 |
parseInsertSet(command, table, columns); |
| 1697 |
break readValues; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
command.setQuery(parseQuery()); |
| 1701 |
} |
| 1702 |
if (mode.onDuplicateKeyUpdate || mode.insertOnConflict || mode.isolationLevelInSelectOrInsertStatement) { |
| 1703 |
parseInsertCompatibility(command, table, mode); |
| 1704 |
} |
| 1705 |
setSQL(command, start); |
| 1706 |
return command; |
| 1707 |
} |
| 1708 |
|
| 1709 |
private Boolean readIfOverriding() { |
| 1710 |
Boolean overridingSystem = null; |
| 1711 |
if (readIf("OVERRIDING", USER, VALUE)) { |
| 1712 |
overridingSystem = Boolean.FALSE; |
| 1713 |
} else if (readIf("OVERRIDING", "SYSTEM", VALUE)) { |
| 1714 |
overridingSystem = Boolean.TRUE; |
| 1715 |
} |
| 1716 |
return overridingSystem; |
| 1717 |
} |
| 1718 |
|
| 1719 |
private void parseInsertSet(Insert command, Table table, Column[] columns) { |
| 1720 |
if (columns != null) { |
| 1721 |
throw getSyntaxError(); |
| 1722 |
} |
| 1723 |
ArrayList<Column> columnList = Utils.newSmallArrayList(); |
| 1724 |
ArrayList<Expression> values = Utils.newSmallArrayList(); |
| 1725 |
do { |
| 1726 |
columnList.add(parseColumn(table)); |
| 1727 |
read(EQUAL); |
| 1728 |
values.add(readExpressionOrDefault()); |
| 1729 |
} while (readIf(COMMA)); |
| 1730 |
command.setColumns(columnList.toArray(new Column[0])); |
| 1731 |
command.addRow(values.toArray(new Expression[0])); |
| 1732 |
} |
| 1733 |
|
| 1734 |
private void parseInsertCompatibility(Insert command, Table table, Mode mode) { |
| 1735 |
if (mode.onDuplicateKeyUpdate) { |
| 1736 |
if (readIf(ON, "DUPLICATE", KEY, "UPDATE")) { |
| 1737 |
do { |
| 1738 |
String columnName = readIdentifier(); |
| 1739 |
if (readIf(DOT)) { |
| 1740 |
String schemaOrTableName = columnName; |
| 1741 |
String tableOrColumnName = readIdentifier(); |
| 1742 |
if (readIf(DOT)) { |
| 1743 |
if (!table.getSchema().getName().equals(schemaOrTableName)) { |
| 1744 |
throw DbException.get(ErrorCode.SCHEMA_NAME_MUST_MATCH); |
| 1745 |
} |
| 1746 |
columnName = readIdentifier(); |
| 1747 |
} else { |
| 1748 |
columnName = tableOrColumnName; |
| 1749 |
tableOrColumnName = schemaOrTableName; |
| 1750 |
} |
| 1751 |
if (!table.getName().equals(tableOrColumnName)) { |
| 1752 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableOrColumnName); |
| 1753 |
} |
| 1754 |
} |
| 1755 |
Column column = table.getColumn(columnName); |
| 1756 |
read(EQUAL); |
| 1757 |
command.addAssignmentForDuplicate(column, readExpressionOrDefault()); |
| 1758 |
} while (readIf(COMMA)); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
if (mode.insertOnConflict) { |
| 1762 |
if (readIf(ON, "CONFLICT", "DO", "NOTHING")) { |
| 1763 |
command.setIgnore(true); |
| 1764 |
} |
| 1765 |
} |
| 1766 |
if (mode.isolationLevelInSelectOrInsertStatement) { |
| 1767 |
parseIsolationClause(); |
| 1768 |
} |
| 1769 |
} |
| 1770 |
|
| 1771 |
/** |
| 1772 |
* MySQL compatibility. REPLACE is similar to MERGE. |
| 1773 |
*/ |
| 1774 |
private Merge parseReplace(int start) { |
| 1775 |
Merge command = new Merge(session, true); |
| 1776 |
currentPrepared = command; |
| 1777 |
read("INTO"); |
| 1778 |
Table table = readTableOrView(); |
| 1779 |
command.setTable(table); |
| 1780 |
if (readIf(OPEN_PAREN)) { |
| 1781 |
if (isQueryQuick()) { |
| 1782 |
command.setQuery(parseQuery()); |
| 1783 |
read(CLOSE_PAREN); |
| 1784 |
return command; |
| 1785 |
} |
| 1786 |
command.setColumns(parseColumnList(table)); |
| 1787 |
} |
| 1788 |
if (readIf(VALUES)) { |
| 1789 |
parseValuesForCommand(command); |
| 1790 |
} else { |
| 1791 |
command.setQuery(parseQuery()); |
| 1792 |
} |
| 1793 |
setSQL(command, start); |
| 1794 |
return command; |
| 1795 |
} |
| 1796 |
|
| 1797 |
/** |
| 1798 |
* REFRESH MATERIALIZED VIEW |
| 1799 |
*/ |
| 1800 |
private RefreshMaterializedView parseRefresh(int start) { |
| 1801 |
read("MATERIALIZED"); |
| 1802 |
read("VIEW"); |
| 1803 |
Table table = readTableOrView(/*resolveMaterializedView*/false); |
| 1804 |
if (!(table instanceof MaterializedView)) { |
| 1805 |
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, table.getName()); |
| 1806 |
} |
| 1807 |
RefreshMaterializedView command = new RefreshMaterializedView(session, getSchema()); |
| 1808 |
currentPrepared = command; |
| 1809 |
command.setView((MaterializedView) table); |
| 1810 |
setSQL(command, start); |
| 1811 |
return command; |
| 1812 |
} |
| 1813 |
|
| 1814 |
private void parseValuesForCommand(CommandWithValues command) { |
| 1815 |
ArrayList<Expression> values = Utils.newSmallArrayList(); |
| 1816 |
do { |
| 1817 |
values.clear(); |
| 1818 |
boolean multiColumn; |
| 1819 |
if (readIf(ROW, OPEN_PAREN)) { |
| 1820 |
multiColumn = true; |
| 1821 |
} else { |
| 1822 |
multiColumn = readIf(OPEN_PAREN); |
| 1823 |
} |
| 1824 |
if (multiColumn) { |
| 1825 |
if (!readIf(CLOSE_PAREN)) { |
| 1826 |
do { |
| 1827 |
values.add(readExpressionOrDefault()); |
| 1828 |
} while (readIfMore()); |
| 1829 |
} |
| 1830 |
} else { |
| 1831 |
values.add(readExpressionOrDefault()); |
| 1832 |
} |
| 1833 |
command.addRow(values.toArray(new Expression[0])); |
| 1834 |
} while (readIf(COMMA)); |
| 1835 |
} |
| 1836 |
|
| 1837 |
private TableFilter readTablePrimary() { |
| 1838 |
Table table; |
| 1839 |
String alias = null; |
| 1840 |
label: if (readIf(OPEN_PAREN)) { |
| 1841 |
if (isDerivedTable()) { |
| 1842 |
// Derived table |
| 1843 |
return readDerivedTableWithCorrelation(); |
| 1844 |
} else { |
| 1845 |
// Parenthesized joined table |
| 1846 |
TableFilter tableFilter = readTableReference(); |
| 1847 |
read(CLOSE_PAREN); |
| 1848 |
return readCorrelation(tableFilter); |
| 1849 |
} |
| 1850 |
} else if (readIf(VALUES)) { |
| 1851 |
BitSet outerUsedParameters = initParametersScope(); |
| 1852 |
TableValueConstructor query = parseValues(); |
| 1853 |
alias = session.getNextSystemIdentifier(sqlCommand); |
| 1854 |
table = query.toTable(alias, null, getUsedParameters(outerUsedParameters), createView != null, |
| 1855 |
currentSelect); |
| 1856 |
} else if (readIf(TABLE, OPEN_PAREN)) { |
| 1857 |
// Table function derived table |
| 1858 |
ArrayTableFunction function = readTableFunction(ArrayTableFunction.TABLE); |
| 1859 |
table = new FunctionTable(database.getMainSchema(), session, function); |
| 1860 |
} else { |
| 1861 |
boolean quoted = token.isQuoted(); |
| 1862 |
String tableName = readIdentifier(); |
| 1863 |
int backupIndex = tokenIndex; |
| 1864 |
schemaName = null; |
| 1865 |
if (readIf(DOT)) { |
| 1866 |
tableName = readIdentifierWithSchema2(tableName); |
| 1867 |
} else if (!quoted && readIf(TABLE, OPEN_PAREN)) { |
| 1868 |
table = readDataChangeDeltaTable(upperName(tableName), backupIndex); |
| 1869 |
break label; |
| 1870 |
} |
| 1871 |
Schema schema; |
| 1872 |
if (schemaName == null) { |
| 1873 |
schema = null; |
| 1874 |
} else { |
| 1875 |
schema = findSchema(schemaName); |
| 1876 |
if (schema == null) { |
| 1877 |
if (isDualTable(tableName)) { |
| 1878 |
table = new DualTable(database); |
| 1879 |
break label; |
| 1880 |
} |
| 1881 |
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName); |
| 1882 |
} |
| 1883 |
} |
| 1884 |
boolean foundLeftParen = readIf(OPEN_PAREN); |
| 1885 |
if (foundLeftParen && readIf("INDEX")) { |
| 1886 |
// Sybase compatibility with |
| 1887 |
// "select * from test (index table1_index)" |
| 1888 |
readIdentifierWithSchema(null); |
| 1889 |
read(CLOSE_PAREN); |
| 1890 |
foundLeftParen = false; |
| 1891 |
} |
| 1892 |
if (foundLeftParen) { |
| 1893 |
Schema mainSchema = database.getMainSchema(); |
| 1894 |
if (equalsToken(tableName, RangeTable.NAME) |
| 1895 |
|| equalsToken(tableName, RangeTable.ALIAS)) { |
| 1896 |
Expression min = readExpression(); |
| 1897 |
read(COMMA); |
| 1898 |
Expression max = readExpression(); |
| 1899 |
if (readIf(COMMA)) { |
| 1900 |
Expression step = readExpression(); |
| 1901 |
read(CLOSE_PAREN); |
| 1902 |
table = new RangeTable(mainSchema, min, max, step); |
| 1903 |
} else { |
| 1904 |
read(CLOSE_PAREN); |
| 1905 |
table = new RangeTable(mainSchema, min, max); |
| 1906 |
} |
| 1907 |
} else { |
| 1908 |
table = new FunctionTable(mainSchema, session, readTableFunction(tableName, schema)); |
| 1909 |
} |
| 1910 |
} else { |
| 1911 |
table = readTableOrView(tableName, /*resolveMaterializedView*/true); |
| 1912 |
} |
| 1913 |
} |
| 1914 |
ArrayList<String> derivedColumnNames = null; |
| 1915 |
IndexHints indexHints = null; |
| 1916 |
if (readIfUseIndex()) { |
| 1917 |
indexHints = parseIndexHints(table); |
| 1918 |
} else { |
| 1919 |
alias = readFromAlias(alias); |
| 1920 |
if (alias != null) { |
| 1921 |
derivedColumnNames = readDerivedColumnNames(); |
| 1922 |
if (readIfUseIndex()) { |
| 1923 |
indexHints = parseIndexHints(table); |
| 1924 |
} |
| 1925 |
} |
| 1926 |
} |
| 1927 |
return buildTableFilter(table, alias, derivedColumnNames, indexHints); |
| 1928 |
} |
| 1929 |
|
| 1930 |
private TableFilter readCorrelation(TableFilter tableFilter) { |
| 1931 |
String alias = readFromAlias(null); |
| 1932 |
if (alias != null) { |
| 1933 |
tableFilter.setAlias(alias); |
| 1934 |
ArrayList<String> derivedColumnNames = readDerivedColumnNames(); |
| 1935 |
if (derivedColumnNames != null) { |
| 1936 |
tableFilter.setDerivedColumns(derivedColumnNames); |
| 1937 |
} |
| 1938 |
} |
| 1939 |
return tableFilter; |
| 1940 |
} |
| 1941 |
|
| 1942 |
private TableFilter readDerivedTableWithCorrelation() { |
| 1943 |
BitSet outerUsedParameters = initParametersScope(); |
| 1944 |
Query query = parseQueryExpression(); |
| 1945 |
ArrayList<Parameter> queryParameters = getUsedParameters(outerUsedParameters); |
| 1946 |
read(CLOSE_PAREN); |
| 1947 |
Table table; |
| 1948 |
String alias; |
| 1949 |
ArrayList<String> derivedColumnNames = null; |
| 1950 |
IndexHints indexHints = null; |
| 1951 |
if (readIfUseIndex()) { |
| 1952 |
alias = session.getNextSystemIdentifier(sqlCommand); |
| 1953 |
table = query.toTable(alias, null, queryParameters, createView != null, currentSelect); |
| 1954 |
indexHints = parseIndexHints(table); |
| 1955 |
} else { |
| 1956 |
alias = readFromAlias(null); |
| 1957 |
if (alias != null) { |
| 1958 |
derivedColumnNames = readDerivedColumnNames(); |
| 1959 |
Column[] columnTemplates = null; |
| 1960 |
if (derivedColumnNames != null) { |
| 1961 |
query.init(); |
| 1962 |
columnTemplates = QueryExpressionTable.createQueryColumnTemplateList( |
| 1963 |
derivedColumnNames.toArray(new String[0]), query, new String[1]) |
| 1964 |
.toArray(new Column[0]); |
| 1965 |
} |
| 1966 |
table = query.toTable(alias, columnTemplates, queryParameters, createView != null, currentSelect); |
| 1967 |
if (readIfUseIndex()) { |
| 1968 |
indexHints = parseIndexHints(table); |
| 1969 |
} |
| 1970 |
} else { |
| 1971 |
alias = session.getNextSystemIdentifier(sqlCommand); |
| 1972 |
table = query.toTable(alias, null, queryParameters, createView != null, currentSelect); |
| 1973 |
} |
| 1974 |
} |
| 1975 |
return buildTableFilter(table, alias, derivedColumnNames, indexHints); |
| 1976 |
} |
| 1977 |
|
| 1978 |
private TableFilter buildTableFilter(Table table, String alias, ArrayList<String> derivedColumnNames, |
| 1979 |
IndexHints indexHints) { |
| 1980 |
if (database.getMode().discardWithTableHints) { |
| 1981 |
discardWithTableHints(); |
| 1982 |
} |
| 1983 |
// inherit alias for CTE as views from table name |
| 1984 |
if (alias == null && table.isView() && table.isTableExpression()) { |
| 1985 |
alias = table.getName(); |
| 1986 |
} |
| 1987 |
TableFilter filter = new TableFilter(session, table, alias, rightsChecked, |
| 1988 |
currentSelect, orderInFrom++, indexHints); |
| 1989 |
if (derivedColumnNames != null) { |
| 1990 |
filter.setDerivedColumns(derivedColumnNames); |
| 1991 |
} |
| 1992 |
return filter; |
| 1993 |
} |
| 1994 |
|
| 1995 |
private Table readDataChangeDeltaTable(String resultOptionName, int backupIndex) { |
| 1996 |
int start = tokenIndex; |
| 1997 |
DataChangeStatement statement; |
| 1998 |
ResultOption resultOption = ResultOption.FINAL; |
| 1999 |
switch (resultOptionName) { |
| 2000 |
case "OLD": |
| 2001 |
resultOption = ResultOption.OLD; |
| 2002 |
if (readIf("UPDATE")) { |
| 2003 |
statement = parseUpdate(start); |
| 2004 |
} else if (readIf("DELETE")) { |
| 2005 |
statement = parseDelete(start); |
| 2006 |
} else if (readIf("MERGE")) { |
| 2007 |
statement = (DataChangeStatement) parseMerge(start); |
| 2008 |
} else if (database.getMode().replaceInto && readIf("REPLACE")) { |
| 2009 |
statement = parseReplace(start); |
| 2010 |
} else { |
| 2011 |
throw getSyntaxError(); |
| 2012 |
} |
| 2013 |
break; |
| 2014 |
case "NEW": |
| 2015 |
resultOption = ResultOption.NEW; |
| 2016 |
//$FALL-THROUGH$ |
| 2017 |
case "FINAL": |
| 2018 |
if (readIf("INSERT")) { |
| 2019 |
statement = parseInsert(start); |
| 2020 |
} else if (readIf("UPDATE")) { |
| 2021 |
statement = parseUpdate(start); |
| 2022 |
} else if (readIf("MERGE")) { |
| 2023 |
statement = (DataChangeStatement) parseMerge(start); |
| 2024 |
} else if (database.getMode().replaceInto && readIf("REPLACE")) { |
| 2025 |
statement = parseReplace(start); |
| 2026 |
} else { |
| 2027 |
throw getSyntaxError(); |
| 2028 |
} |
| 2029 |
break; |
| 2030 |
default: |
| 2031 |
setTokenIndex(backupIndex); |
| 2032 |
addExpected("OLD TABLE"); |
| 2033 |
addExpected("NEW TABLE"); |
| 2034 |
addExpected("FINAL TABLE"); |
| 2035 |
throw getSyntaxError(); |
| 2036 |
} |
| 2037 |
read(CLOSE_PAREN); |
| 2038 |
if (currentSelect != null) { |
| 2039 |
// Lobs aren't copied, so use it for more safety |
| 2040 |
currentSelect.setNeverLazy(true); |
| 2041 |
} |
| 2042 |
return new DataChangeDeltaTable(getSchemaWithDefault(), session, statement, resultOption); |
| 2043 |
} |
| 2044 |
|
| 2045 |
private TableFunction readTableFunction(String name, Schema schema) { |
| 2046 |
if (schema == null) { |
| 2047 |
switch (upperName(name)) { |
| 2048 |
case "UNNEST": |
| 2049 |
return readUnnestFunction(); |
| 2050 |
case "TABLE_DISTINCT": |
| 2051 |
return readTableFunction(ArrayTableFunction.TABLE_DISTINCT); |
| 2052 |
case "CSVREAD": |
| 2053 |
recompileAlways = true; |
| 2054 |
return readParameters(new CSVReadFunction()); |
| 2055 |
case "LINK_SCHEMA": |
| 2056 |
recompileAlways = true; |
| 2057 |
return readParameters(new LinkSchemaFunction()); |
| 2058 |
} |
| 2059 |
} |
| 2060 |
FunctionAlias functionAlias = getFunctionAliasWithinPath(name, schema); |
| 2061 |
if (!functionAlias.isDeterministic()) { |
| 2062 |
recompileAlways = true; |
| 2063 |
} |
| 2064 |
ArrayList<Expression> argList = Utils.newSmallArrayList(); |
| 2065 |
if (!readIf(CLOSE_PAREN)) { |
| 2066 |
do { |
| 2067 |
argList.add(readExpression()); |
| 2068 |
} while (readIfMore()); |
| 2069 |
} |
| 2070 |
return new JavaTableFunction(functionAlias, argList.toArray(new Expression[0])); |
| 2071 |
} |
| 2072 |
|
| 2073 |
private boolean readIfUseIndex() { |
| 2074 |
int start = tokenIndex; |
| 2075 |
if (!readIf("USE")) { |
| 2076 |
return false; |
| 2077 |
} |
| 2078 |
if (!readIf("INDEX")) { |
| 2079 |
setTokenIndex(start); |
| 2080 |
return false; |
| 2081 |
} |
| 2082 |
return true; |
| 2083 |
} |
| 2084 |
|
| 2085 |
private IndexHints parseIndexHints(Table table) { |
| 2086 |
read(OPEN_PAREN); |
| 2087 |
LinkedHashSet<String> indexNames = new LinkedHashSet<>(); |
| 2088 |
if (!readIf(CLOSE_PAREN)) { |
| 2089 |
do { |
| 2090 |
String indexName = readIdentifierWithSchema(); |
| 2091 |
Index index = table.getIndex(indexName); |
| 2092 |
indexNames.add(index.getName()); |
| 2093 |
} while (readIfMore()); |
| 2094 |
} |
| 2095 |
return IndexHints.createUseIndexHints(indexNames); |
| 2096 |
} |
| 2097 |
|
| 2098 |
private String readFromAlias(String alias) { |
| 2099 |
if (readIf(AS) || isIdentifier()) { |
| 2100 |
alias = readIdentifier(); |
| 2101 |
} |
| 2102 |
return alias; |
| 2103 |
} |
| 2104 |
|
| 2105 |
private ArrayList<String> readDerivedColumnNames() { |
| 2106 |
if (readIf(OPEN_PAREN)) { |
| 2107 |
ArrayList<String> derivedColumnNames = new ArrayList<>(); |
| 2108 |
do { |
| 2109 |
derivedColumnNames.add(readIdentifier()); |
| 2110 |
} while (readIfMore()); |
| 2111 |
return derivedColumnNames; |
| 2112 |
} |
| 2113 |
return null; |
| 2114 |
} |
| 2115 |
|
| 2116 |
private void discardWithTableHints() { |
| 2117 |
if (readIf(WITH, OPEN_PAREN)) { |
| 2118 |
do { |
| 2119 |
discardTableHint(); |
| 2120 |
} while (readIfMore()); |
| 2121 |
} |
| 2122 |
} |
| 2123 |
|
| 2124 |
private void discardTableHint() { |
| 2125 |
if (readIf("INDEX")) { |
| 2126 |
if (readIf(OPEN_PAREN)) { |
| 2127 |
do { |
| 2128 |
readExpression(); |
| 2129 |
} while (readIfMore()); |
| 2130 |
} else { |
| 2131 |
read(EQUAL); |
| 2132 |
readExpression(); |
| 2133 |
} |
| 2134 |
} else { |
| 2135 |
readExpression(); |
| 2136 |
} |
| 2137 |
} |
| 2138 |
|
| 2139 |
private Prepared parseTruncate() { |
| 2140 |
read(TABLE); |
| 2141 |
Table table = readTableOrView(); |
| 2142 |
boolean restart = database.getMode().truncateTableRestartIdentity; |
| 2143 |
if (readIf("CONTINUE", "IDENTITY")) { |
| 2144 |
restart = false; |
| 2145 |
} else if (readIf("RESTART", "IDENTITY")) { |
| 2146 |
restart = true; |
| 2147 |
} |
| 2148 |
TruncateTable command = new TruncateTable(session); |
| 2149 |
command.setTable(table); |
| 2150 |
command.setRestart(restart); |
| 2151 |
return command; |
| 2152 |
} |
| 2153 |
|
| 2154 |
private boolean readIfExists(boolean ifExists) { |
| 2155 |
if (readIf(IF, EXISTS)) { |
| 2156 |
ifExists = true; |
| 2157 |
} |
| 2158 |
return ifExists; |
| 2159 |
} |
| 2160 |
|
| 2161 |
private Prepared parseComment() { |
| 2162 |
int type = 0; |
| 2163 |
read(ON); |
| 2164 |
boolean column = false; |
| 2165 |
if (readIf(TABLE) || readIf("VIEW")) { |
| 2166 |
type = DbObject.TABLE_OR_VIEW; |
| 2167 |
} else if (readIf("COLUMN")) { |
| 2168 |
column = true; |
| 2169 |
type = DbObject.TABLE_OR_VIEW; |
| 2170 |
} else if (readIf("CONSTANT")) { |
| 2171 |
type = DbObject.CONSTANT; |
| 2172 |
} else if (readIf(CONSTRAINT)) { |
| 2173 |
type = DbObject.CONSTRAINT; |
| 2174 |
} else if (readIf("ALIAS")) { |
| 2175 |
type = DbObject.FUNCTION_ALIAS; |
| 2176 |
} else if (readIf("INDEX")) { |
| 2177 |
type = DbObject.INDEX; |
| 2178 |
} else if (readIf("ROLE")) { |
| 2179 |
type = DbObject.ROLE; |
| 2180 |
} else if (readIf("SCHEMA")) { |
| 2181 |
type = DbObject.SCHEMA; |
| 2182 |
} else if (readIf("SEQUENCE")) { |
| 2183 |
type = DbObject.SEQUENCE; |
| 2184 |
} else if (readIf("TRIGGER")) { |
| 2185 |
type = DbObject.TRIGGER; |
| 2186 |
} else if (readIf(USER)) { |
| 2187 |
type = DbObject.USER; |
| 2188 |
} else if (readIf("DOMAIN")) { |
| 2189 |
type = DbObject.DOMAIN; |
| 2190 |
} else { |
| 2191 |
throw getSyntaxError(); |
| 2192 |
} |
| 2193 |
SetComment command = new SetComment(session); |
| 2194 |
String objectName; |
| 2195 |
if (column) { |
| 2196 |
// can't use readIdentifierWithSchema() because |
| 2197 |
// it would not read [catalog.]schema.table.column correctly |
| 2198 |
objectName = readIdentifier(); |
| 2199 |
String tmpSchemaName = null; |
| 2200 |
read(DOT); |
| 2201 |
boolean allowEmpty = database.getMode().allowEmptySchemaValuesAsDefaultSchema; |
| 2202 |
String columnName = allowEmpty && currentTokenType == DOT ? null : readIdentifier(); |
| 2203 |
if (readIf(DOT)) { |
| 2204 |
tmpSchemaName = objectName; |
| 2205 |
objectName = columnName; |
| 2206 |
columnName = allowEmpty && currentTokenType == DOT ? null : readIdentifier(); |
| 2207 |
if (readIf(DOT)) { |
| 2208 |
checkDatabaseName(tmpSchemaName); |
| 2209 |
tmpSchemaName = objectName; |
| 2210 |
objectName = columnName; |
| 2211 |
columnName = readIdentifier(); |
| 2212 |
} |
| 2213 |
} |
| 2214 |
if (columnName == null || objectName == null) { |
| 2215 |
throw DbException.getSyntaxError(sqlCommand, token.start(), "table.column"); |
| 2216 |
} |
| 2217 |
schemaName = tmpSchemaName != null ? tmpSchemaName : session.getCurrentSchemaName(); |
| 2218 |
command.setColumn(true); |
| 2219 |
command.setColumnName(columnName); |
| 2220 |
} else { |
| 2221 |
objectName = readIdentifierWithSchema(); |
| 2222 |
} |
| 2223 |
command.setSchemaName(schemaName); |
| 2224 |
command.setObjectName(objectName); |
| 2225 |
command.setObjectType(type); |
| 2226 |
read(IS); |
| 2227 |
command.setCommentExpression(readExpression()); |
| 2228 |
return command; |
| 2229 |
} |
| 2230 |
|
| 2231 |
private Prepared parseDrop() { |
| 2232 |
if (readIf(TABLE)) { |
| 2233 |
boolean ifExists = readIfExists(false); |
| 2234 |
DropTable command = new DropTable(session); |
| 2235 |
do { |
| 2236 |
String tableName = readIdentifierWithSchema(); |
| 2237 |
command.addTable(getSchema(), tableName); |
| 2238 |
} while (readIf(COMMA)); |
| 2239 |
ifExists = readIfExists(ifExists); |
| 2240 |
command.setIfExists(ifExists); |
| 2241 |
if (readIf("CASCADE")) { |
| 2242 |
command.setDropAction(ConstraintActionType.CASCADE); |
| 2243 |
readIf("CONSTRAINTS"); |
| 2244 |
} else if (readIf("RESTRICT")) { |
| 2245 |
command.setDropAction(ConstraintActionType.RESTRICT); |
| 2246 |
} else if (readIf("IGNORE")) { |
| 2247 |
// TODO SET_DEFAULT works in the same way as CASCADE |
| 2248 |
command.setDropAction(ConstraintActionType.SET_DEFAULT); |
| 2249 |
} |
| 2250 |
return command; |
| 2251 |
} else if (readIf("INDEX")) { |
| 2252 |
boolean ifExists = readIfExists(false); |
| 2253 |
String indexName = readIdentifierWithSchema(); |
| 2254 |
DropIndex command = new DropIndex(session, getSchema()); |
| 2255 |
command.setIndexName(indexName); |
| 2256 |
ifExists = readIfExists(ifExists); |
| 2257 |
command.setIfExists(ifExists); |
| 2258 |
//Support for MySQL: DROP INDEX index_name ON tbl_name |
| 2259 |
if (readIf(ON)) { |
| 2260 |
readIdentifierWithSchema(); |
| 2261 |
} |
| 2262 |
return command; |
| 2263 |
} else if (readIf(USER)) { |
| 2264 |
boolean ifExists = readIfExists(false); |
| 2265 |
DropUser command = new DropUser(session); |
| 2266 |
command.setUserName(readIdentifier()); |
| 2267 |
ifExists = readIfExists(ifExists); |
| 2268 |
readIf("CASCADE"); |
| 2269 |
command.setIfExists(ifExists); |
| 2270 |
return command; |
| 2271 |
} else if (readIf("SEQUENCE")) { |
| 2272 |
boolean ifExists = readIfExists(false); |
| 2273 |
String sequenceName = readIdentifierWithSchema(); |
| 2274 |
DropSequence command = new DropSequence(session, getSchema()); |
| 2275 |
command.setSequenceName(sequenceName); |
| 2276 |
ifExists = readIfExists(ifExists); |
| 2277 |
command.setIfExists(ifExists); |
| 2278 |
return command; |
| 2279 |
} else if (readIf("CONSTANT")) { |
| 2280 |
boolean ifExists = readIfExists(false); |
| 2281 |
String constantName = readIdentifierWithSchema(); |
| 2282 |
DropConstant command = new DropConstant(session, getSchema()); |
| 2283 |
command.setConstantName(constantName); |
| 2284 |
ifExists = readIfExists(ifExists); |
| 2285 |
command.setIfExists(ifExists); |
| 2286 |
return command; |
| 2287 |
} else if (readIf("TRIGGER")) { |
| 2288 |
boolean ifExists = readIfExists(false); |
| 2289 |
String triggerName = readIdentifierWithSchema(); |
| 2290 |
DropTrigger command = new DropTrigger(session, getSchema()); |
| 2291 |
command.setTriggerName(triggerName); |
| 2292 |
ifExists = readIfExists(ifExists); |
| 2293 |
command.setIfExists(ifExists); |
| 2294 |
return command; |
| 2295 |
} else if (readIf("MATERIALIZED")) { |
| 2296 |
read("VIEW"); |
| 2297 |
boolean ifExists = readIfExists(false); |
| 2298 |
String viewName = readIdentifierWithSchema(); |
| 2299 |
DropMaterializedView command = new DropMaterializedView(session, getSchema()); |
| 2300 |
command.setViewName(viewName); |
| 2301 |
ifExists = readIfExists(ifExists); |
| 2302 |
command.setIfExists(ifExists); |
| 2303 |
return command; |
| 2304 |
} else if (readIf("VIEW")) { |
| 2305 |
boolean ifExists = readIfExists(false); |
| 2306 |
String viewName = readIdentifierWithSchema(); |
| 2307 |
DropView command = new DropView(session, getSchema()); |
| 2308 |
command.setViewName(viewName); |
| 2309 |
ifExists = readIfExists(ifExists); |
| 2310 |
command.setIfExists(ifExists); |
| 2311 |
ConstraintActionType dropAction = parseCascadeOrRestrict(); |
| 2312 |
if (dropAction != null) { |
| 2313 |
command.setDropAction(dropAction); |
| 2314 |
} |
| 2315 |
return command; |
| 2316 |
} else if (readIf("ROLE")) { |
| 2317 |
boolean ifExists = readIfExists(false); |
| 2318 |
DropRole command = new DropRole(session); |
| 2319 |
command.setRoleName(readIdentifier()); |
| 2320 |
ifExists = readIfExists(ifExists); |
| 2321 |
command.setIfExists(ifExists); |
| 2322 |
return command; |
| 2323 |
} else if (readIf("ALIAS")) { |
| 2324 |
boolean ifExists = readIfExists(false); |
| 2325 |
String aliasName = readIdentifierWithSchema(); |
| 2326 |
DropFunctionAlias command = new DropFunctionAlias(session, |
| 2327 |
getSchema()); |
| 2328 |
command.setAliasName(aliasName); |
| 2329 |
ifExists = readIfExists(ifExists); |
| 2330 |
command.setIfExists(ifExists); |
| 2331 |
return command; |
| 2332 |
} else if (readIf("SCHEMA")) { |
| 2333 |
boolean ifExists = readIfExists(false); |
| 2334 |
DropSchema command = new DropSchema(session); |
| 2335 |
command.setSchemaName(readIdentifier()); |
| 2336 |
ifExists = readIfExists(ifExists); |
| 2337 |
command.setIfExists(ifExists); |
| 2338 |
ConstraintActionType dropAction = parseCascadeOrRestrict(); |
| 2339 |
if (dropAction != null) { |
| 2340 |
command.setDropAction(dropAction); |
| 2341 |
} |
| 2342 |
return command; |
| 2343 |
} else if (readIf(ALL, "OBJECTS")) { |
| 2344 |
DropDatabase command = new DropDatabase(session); |
| 2345 |
command.setDropAllObjects(true); |
| 2346 |
if (readIf("DELETE", "FILES")) { |
| 2347 |
command.setDeleteFiles(true); |
| 2348 |
} |
| 2349 |
return command; |
| 2350 |
} else if (readIf("DOMAIN") || readIf("TYPE") || readIf("DATATYPE")) { |
| 2351 |
return parseDropDomain(); |
| 2352 |
} else if (readIf("AGGREGATE")) { |
| 2353 |
return parseDropAggregate(); |
| 2354 |
} else if (readIf("SYNONYM")) { |
| 2355 |
boolean ifExists = readIfExists(false); |
| 2356 |
String synonymName = readIdentifierWithSchema(); |
| 2357 |
DropSynonym command = new DropSynonym(session, getSchema()); |
| 2358 |
command.setSynonymName(synonymName); |
| 2359 |
ifExists = readIfExists(ifExists); |
| 2360 |
command.setIfExists(ifExists); |
| 2361 |
return command; |
| 2362 |
} |
| 2363 |
throw getSyntaxError(); |
| 2364 |
} |
| 2365 |
|
| 2366 |
private DropDomain parseDropDomain() { |
| 2367 |
boolean ifExists = readIfExists(false); |
| 2368 |
String domainName = readIdentifierWithSchema(); |
| 2369 |
DropDomain command = new DropDomain(session, getSchema()); |
| 2370 |
command.setDomainName(domainName); |
| 2371 |
ifExists = readIfExists(ifExists); |
| 2372 |
command.setIfDomainExists(ifExists); |
| 2373 |
ConstraintActionType dropAction = parseCascadeOrRestrict(); |
| 2374 |
if (dropAction != null) { |
| 2375 |
command.setDropAction(dropAction); |
| 2376 |
} |
| 2377 |
return command; |
| 2378 |
} |
| 2379 |
|
| 2380 |
private DropAggregate parseDropAggregate() { |
| 2381 |
boolean ifExists = readIfExists(false); |
| 2382 |
String name = readIdentifierWithSchema(); |
| 2383 |
DropAggregate command = new DropAggregate(session, getSchema()); |
| 2384 |
command.setName(name); |
| 2385 |
ifExists = readIfExists(ifExists); |
| 2386 |
command.setIfExists(ifExists); |
| 2387 |
return command; |
| 2388 |
} |
| 2389 |
|
| 2390 |
private TableFilter readTableReference() { |
| 2391 |
for (TableFilter top, last = top = readTablePrimary(), join;; last = join) { |
| 2392 |
switch (currentTokenType) { |
| 2393 |
case RIGHT: { |
| 2394 |
read(); |
| 2395 |
readIf("OUTER"); |
| 2396 |
read(JOIN); |
| 2397 |
// the right hand side is the 'inner' table usually |
| 2398 |
join = readTableReference(); |
| 2399 |
Expression on = readJoinSpecification(top, join, true); |
| 2400 |
addJoin(join, top, true, on); |
| 2401 |
top = join; |
| 2402 |
break; |
| 2403 |
} |
| 2404 |
case LEFT: { |
| 2405 |
read(); |
| 2406 |
readIf("OUTER"); |
| 2407 |
read(JOIN); |
| 2408 |
join = readTableReference(); |
| 2409 |
Expression on = readJoinSpecification(top, join, false); |
| 2410 |
addJoin(top, join, true, on); |
| 2411 |
break; |
| 2412 |
} |
| 2413 |
case FULL: |
| 2414 |
read(); |
| 2415 |
throw getSyntaxError(); |
| 2416 |
case INNER: { |
| 2417 |
read(); |
| 2418 |
read(JOIN); |
| 2419 |
join = readTableReference(); |
| 2420 |
Expression on = readJoinSpecification(top, join, false); |
| 2421 |
addJoin(top, join, false, on); |
| 2422 |
break; |
| 2423 |
} |
| 2424 |
case JOIN: { |
| 2425 |
read(); |
| 2426 |
join = readTableReference(); |
| 2427 |
Expression on = readJoinSpecification(top, join, false); |
| 2428 |
addJoin(top, join, false, on); |
| 2429 |
break; |
| 2430 |
} |
| 2431 |
case CROSS: { |
| 2432 |
read(); |
| 2433 |
read(JOIN); |
| 2434 |
join = readTablePrimary(); |
| 2435 |
addJoin(top, join, false, null); |
| 2436 |
break; |
| 2437 |
} |
| 2438 |
case NATURAL: { |
| 2439 |
read(); |
| 2440 |
read(JOIN); |
| 2441 |
join = readTablePrimary(); |
| 2442 |
Expression on = null; |
| 2443 |
for (Column column1 : last.getTable().getColumns()) { |
| 2444 |
Column column2 = join.getColumn(last.getColumnName(column1), true); |
| 2445 |
if (column2 != null) { |
| 2446 |
on = addJoinColumn(on, last, join, column1, column2, false); |
| 2447 |
} |
| 2448 |
} |
| 2449 |
addJoin(top, join, false, on); |
| 2450 |
break; |
| 2451 |
} |
| 2452 |
default: |
| 2453 |
if (expectedList != null) { |
| 2454 |
// FULL is intentionally excluded |
| 2455 |
addMultipleExpected(RIGHT, LEFT, INNER, JOIN, CROSS, NATURAL); |
| 2456 |
} |
| 2457 |
return top; |
| 2458 |
} |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
private Expression readJoinSpecification(TableFilter filter1, TableFilter filter2, boolean rightJoin) { |
| 2463 |
Expression on = null; |
| 2464 |
if (readIf(ON)) { |
| 2465 |
on = readExpression(); |
| 2466 |
} else if (readIf(USING, OPEN_PAREN)) { |
| 2467 |
do { |
| 2468 |
String columnName = readIdentifier(); |
| 2469 |
on = addJoinColumn(on, filter1, filter2, filter1.getColumn(columnName, false), |
| 2470 |
filter2.getColumn(columnName, false), rightJoin); |
| 2471 |
} while (readIfMore()); |
| 2472 |
} |
| 2473 |
return on; |
| 2474 |
} |
| 2475 |
|
| 2476 |
private Expression addJoinColumn(Expression on, TableFilter filter1, TableFilter filter2, Column column1, |
| 2477 |
Column column2, boolean rightJoin) { |
| 2478 |
if (rightJoin) { |
| 2479 |
filter1.addCommonJoinColumns(column1, column2, filter2); |
| 2480 |
filter2.addCommonJoinColumnToExclude(column2); |
| 2481 |
} else { |
| 2482 |
filter1.addCommonJoinColumns(column1, column1, filter1); |
| 2483 |
filter2.addCommonJoinColumnToExclude(column2); |
| 2484 |
} |
| 2485 |
Expression tableExpr = new ExpressionColumn(database, filter1.getSchemaName(), filter1.getTableAlias(), |
| 2486 |
filter1.getColumnName(column1)); |
| 2487 |
Expression joinExpr = new ExpressionColumn(database, filter2.getSchemaName(), filter2.getTableAlias(), |
| 2488 |
filter2.getColumnName(column2)); |
| 2489 |
Expression equal = new Comparison(Comparison.EQUAL, tableExpr, joinExpr, false); |
| 2490 |
if (on == null) { |
| 2491 |
on = equal; |
| 2492 |
} else { |
| 2493 |
on = new ConditionAndOr(ConditionAndOr.AND, on, equal); |
| 2494 |
} |
| 2495 |
return on; |
| 2496 |
} |
| 2497 |
|
| 2498 |
/** |
| 2499 |
* Add one join to another. This method creates nested join between them if |
| 2500 |
* required. |
| 2501 |
* |
| 2502 |
* @param top parent join |
| 2503 |
* @param join child join |
| 2504 |
* @param outer if child join is an outer join |
| 2505 |
* @param on the join condition |
| 2506 |
* @see TableFilter#addJoin(TableFilter, boolean, Expression) |
| 2507 |
*/ |
| 2508 |
private void addJoin(TableFilter top, TableFilter join, boolean outer, Expression on) { |
| 2509 |
if (join.getJoin() != null) { |
| 2510 |
String joinTable = Constants.PREFIX_JOIN + token.start(); |
| 2511 |
TableFilter n = new TableFilter(session, new DualTable(database), |
| 2512 |
joinTable, rightsChecked, currentSelect, join.getOrderInFrom(), |
| 2513 |
null); |
| 2514 |
n.setNestedJoin(join); |
| 2515 |
join = n; |
| 2516 |
} |
| 2517 |
top.addJoin(join, outer, on); |
| 2518 |
} |
| 2519 |
|
| 2520 |
private Prepared parseExecutePostgre() { |
| 2521 |
if (readIf("IMMEDIATE")) { |
| 2522 |
return new ExecuteImmediate(session, readExpression()); |
| 2523 |
} |
| 2524 |
ExecuteProcedure command = new ExecuteProcedure(session); |
| 2525 |
String procedureName = readIdentifier(); |
| 2526 |
Procedure p = session.getProcedure(procedureName); |
| 2527 |
if (p == null) { |
| 2528 |
throw DbException.get(ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1, |
| 2529 |
procedureName); |
| 2530 |
} |
| 2531 |
command.setProcedure(p); |
| 2532 |
if (readIf(OPEN_PAREN)) { |
| 2533 |
for (int i = 0;; i++) { |
| 2534 |
command.setExpression(i, readExpression()); |
| 2535 |
if (!readIfMore()) { |
| 2536 |
break; |
| 2537 |
} |
| 2538 |
} |
| 2539 |
} |
| 2540 |
return command; |
| 2541 |
} |
| 2542 |
|
| 2543 |
private Prepared parseExecuteSQLServer() { |
| 2544 |
Call command = new Call(session); |
| 2545 |
currentPrepared = command; |
| 2546 |
String schemaName = null; |
| 2547 |
String name = readIdentifier(); |
| 2548 |
if (readIf(DOT)) { |
| 2549 |
schemaName = name; |
| 2550 |
name = readIdentifier(); |
| 2551 |
if (readIf(DOT)) { |
| 2552 |
checkDatabaseName(schemaName); |
| 2553 |
schemaName = name; |
| 2554 |
name = readIdentifier(); |
| 2555 |
} |
| 2556 |
} |
| 2557 |
FunctionAlias functionAlias = getFunctionAliasWithinPath(name, |
| 2558 |
schemaName != null ? database.getSchema(schemaName) : null); |
| 2559 |
Expression[] args; |
| 2560 |
ArrayList<Expression> argList = Utils.newSmallArrayList(); |
| 2561 |
if (currentTokenType != SEMICOLON && currentTokenType != END_OF_INPUT) { |
| 2562 |
do { |
| 2563 |
argList.add(readExpression()); |
| 2564 |
} while (readIf(COMMA)); |
| 2565 |
} |
| 2566 |
args = argList.toArray(new Expression[0]); |
| 2567 |
command.setExpression(new JavaFunction(functionAlias, args)); |
| 2568 |
return command; |
| 2569 |
} |
| 2570 |
|
| 2571 |
private FunctionAlias getFunctionAliasWithinPath(String name, Schema schema) { |
| 2572 |
UserDefinedFunction userDefinedFunction = findUserDefinedFunctionWithinPath(schema, name); |
| 2573 |
if (userDefinedFunction instanceof FunctionAlias) { |
| 2574 |
return (FunctionAlias) userDefinedFunction; |
| 2575 |
} |
| 2576 |
throw DbException.get(ErrorCode.FUNCTION_NOT_FOUND_1, name); |
| 2577 |
} |
| 2578 |
|
| 2579 |
private DeallocateProcedure parseDeallocate() { |
| 2580 |
readIf("PLAN"); |
| 2581 |
DeallocateProcedure command = new DeallocateProcedure(session); |
| 2582 |
command.setProcedureName(readIdentifier()); |
| 2583 |
return command; |
| 2584 |
} |
| 2585 |
|
| 2586 |
private Explain parseExplain() { |
| 2587 |
Explain command = new Explain(session); |
| 2588 |
if (readIf("ANALYZE")) { |
| 2589 |
command.setExecuteCommand(true); |
| 2590 |
} else { |
| 2591 |
if (readIf("PLAN")) { |
| 2592 |
readIf(FOR); |
| 2593 |
} |
| 2594 |
} |
| 2595 |
switch (currentTokenType) { |
| 2596 |
case SELECT: |
| 2597 |
case TABLE: |
| 2598 |
case VALUES: |
| 2599 |
case WITH: |
| 2600 |
case OPEN_PAREN: |
| 2601 |
Query query = parseQuery(); |
| 2602 |
query.setNeverLazy(true); |
| 2603 |
command.setCommand(query); |
| 2604 |
break; |
| 2605 |
default: |
| 2606 |
int start = tokenIndex; |
| 2607 |
if (readIf("DELETE")) { |
| 2608 |
command.setCommand(parseDelete(start)); |
| 2609 |
} else if (readIf("UPDATE")) { |
| 2610 |
command.setCommand(parseUpdate(start)); |
| 2611 |
} else if (readIf("INSERT")) { |
| 2612 |
command.setCommand(parseInsert(start)); |
| 2613 |
} else if (readIf("MERGE")) { |
| 2614 |
command.setCommand(parseMerge(start)); |
| 2615 |
} else { |
| 2616 |
throw getSyntaxError(); |
| 2617 |
} |
| 2618 |
} |
| 2619 |
return command; |
| 2620 |
} |
| 2621 |
|
| 2622 |
private Query parseQuery() { |
| 2623 |
BitSet outerUsedParameters = initParametersScope(); |
| 2624 |
Query command = parseQueryExpression(); |
| 2625 |
ArrayList<Parameter> params = getUsedParameters(outerUsedParameters); |
| 2626 |
command.setParameterList(params); |
| 2627 |
command.init(); |
| 2628 |
return command; |
| 2629 |
} |
| 2630 |
|
| 2631 |
private Prepared parseWithStatementOrQuery(int start) { |
| 2632 |
BitSet outerUsedParameters = initParametersScope(); |
| 2633 |
Prepared command = parseWith(); |
| 2634 |
ArrayList<Parameter> params = getUsedParameters(outerUsedParameters); |
| 2635 |
command.setParameterList(params); |
| 2636 |
if (command instanceof Query) { |
| 2637 |
Query query = (Query) command; |
| 2638 |
query.init(); |
| 2639 |
} |
| 2640 |
setSQL(command, start); |
| 2641 |
return command; |
| 2642 |
} |
| 2643 |
|
| 2644 |
private Query parseQueryExpression() { |
| 2645 |
Query query; |
| 2646 |
if (readIf(WITH)) { |
| 2647 |
try { |
| 2648 |
query = (Query) parseWith(); |
| 2649 |
} catch (ClassCastException e) { |
| 2650 |
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, "WITH statement supports only query in this context"); |
| 2651 |
} |
| 2652 |
// recursive can not be lazy |
| 2653 |
query.setNeverLazy(true); |
| 2654 |
} else { |
| 2655 |
query = parseQueryExpressionBodyAndEndOfQuery(); |
| 2656 |
} |
| 2657 |
return query; |
| 2658 |
} |
| 2659 |
|
| 2660 |
private Query parseQueryExpressionBodyAndEndOfQuery() { |
| 2661 |
int start = tokenIndex; |
| 2662 |
Query command = parseQueryExpressionBody(); |
| 2663 |
parseEndOfQuery(command); |
| 2664 |
setSQL(command, start); |
| 2665 |
return command; |
| 2666 |
} |
| 2667 |
|
| 2668 |
private Query parseQueryExpressionBody() { |
| 2669 |
Query command = parseQueryTerm(); |
| 2670 |
for (;;) { |
| 2671 |
SelectUnion.UnionType type; |
| 2672 |
if (readIf(UNION)) { |
| 2673 |
if (readIf(ALL)) { |
| 2674 |
type = SelectUnion.UnionType.UNION_ALL; |
| 2675 |
} else { |
| 2676 |
readIf(DISTINCT); |
| 2677 |
type = SelectUnion.UnionType.UNION; |
| 2678 |
} |
| 2679 |
} else if (readIf(EXCEPT) || readIf(MINUS)) { |
| 2680 |
type = SelectUnion.UnionType.EXCEPT; |
| 2681 |
} else { |
| 2682 |
break; |
| 2683 |
} |
| 2684 |
command = new SelectUnion(session, type, command, parseQueryTerm()); |
| 2685 |
} |
| 2686 |
return command; |
| 2687 |
} |
| 2688 |
|
| 2689 |
private Query parseQueryTerm() { |
| 2690 |
Query command = parseQueryPrimary(); |
| 2691 |
while (readIf(INTERSECT)) { |
| 2692 |
command = new SelectUnion(session, SelectUnion.UnionType.INTERSECT, command, parseQueryPrimary()); |
| 2693 |
} |
| 2694 |
return command; |
| 2695 |
} |
| 2696 |
|
| 2697 |
private void parseEndOfQuery(Query command) { |
| 2698 |
if (readIf(ORDER, "BY")) { |
| 2699 |
Select oldSelect = currentSelect; |
| 2700 |
if (command instanceof Select) { |
| 2701 |
currentSelect = (Select) command; |
| 2702 |
} |
| 2703 |
ArrayList<QueryOrderBy> orderList = Utils.newSmallArrayList(); |
| 2704 |
do { |
| 2705 |
boolean canBeNumber = currentTokenType == LITERAL; |
| 2706 |
QueryOrderBy order = new QueryOrderBy(); |
| 2707 |
Expression expr = readExpression(); |
| 2708 |
if (canBeNumber && expr instanceof ValueExpression && expr.getType().getValueType() == Value.INTEGER) { |
| 2709 |
order.columnIndexExpr = expr; |
| 2710 |
} else if (expr instanceof Parameter) { |
| 2711 |
recompileAlways = true; |
| 2712 |
order.columnIndexExpr = expr; |
| 2713 |
} else { |
| 2714 |
order.expression = expr; |
| 2715 |
} |
| 2716 |
order.sortType = parseSortType(); |
| 2717 |
orderList.add(order); |
| 2718 |
} while (readIf(COMMA)); |
| 2719 |
command.setOrder(orderList); |
| 2720 |
currentSelect = oldSelect; |
| 2721 |
} |
| 2722 |
if (command.getFetch() == null) { |
| 2723 |
// make sure aggregate functions will not work here |
| 2724 |
Select temp = currentSelect; |
| 2725 |
currentSelect = null; |
| 2726 |
boolean hasOffsetOrFetch = false; |
| 2727 |
// Standard SQL OFFSET / FETCH |
| 2728 |
if (readIf(OFFSET)) { |
| 2729 |
hasOffsetOrFetch = true; |
| 2730 |
command.setOffset(readExpression().optimize(session)); |
| 2731 |
if (!readIf(ROW)) { |
| 2732 |
readIf("ROWS"); |
| 2733 |
} |
| 2734 |
} |
| 2735 |
if (readIf(FETCH)) { |
| 2736 |
hasOffsetOrFetch = true; |
| 2737 |
if (!readIf("FIRST")) { |
| 2738 |
read("NEXT"); |
| 2739 |
} |
| 2740 |
if (readIf(ROW) || readIf("ROWS")) { |
| 2741 |
command.setFetch(ValueExpression.get(ValueInteger.get(1))); |
| 2742 |
} else { |
| 2743 |
command.setFetch(readExpression().optimize(session)); |
| 2744 |
if (readIf("PERCENT")) { |
| 2745 |
command.setFetchPercent(true); |
| 2746 |
} |
| 2747 |
if (!readIf(ROW)) { |
| 2748 |
read("ROWS"); |
| 2749 |
} |
| 2750 |
} |
| 2751 |
if (readIf(WITH, "TIES")) { |
| 2752 |
command.setWithTies(true); |
| 2753 |
} else { |
| 2754 |
read("ONLY"); |
| 2755 |
} |
| 2756 |
} |
| 2757 |
// MySQL-style LIMIT / OFFSET |
| 2758 |
if (!hasOffsetOrFetch && database.getMode().limit && readIf(LIMIT)) { |
| 2759 |
Expression limit = readExpression().optimize(session); |
| 2760 |
if (readIf(OFFSET)) { |
| 2761 |
command.setOffset(readExpression().optimize(session)); |
| 2762 |
} else if (readIf(COMMA)) { |
| 2763 |
// MySQL: [offset, ] rowcount |
| 2764 |
Expression offset = limit; |
| 2765 |
limit = readExpression().optimize(session); |
| 2766 |
command.setOffset(offset); |
| 2767 |
} |
| 2768 |
command.setFetch(limit); |
| 2769 |
} |
| 2770 |
currentSelect = temp; |
| 2771 |
} |
| 2772 |
if (readIf(FOR)) { |
| 2773 |
if (readIf("UPDATE")) { |
| 2774 |
if (readIf("OF")) { |
| 2775 |
do { |
| 2776 |
readIdentifierWithSchema(); |
| 2777 |
} while (readIf(COMMA)); |
| 2778 |
} |
| 2779 |
ForUpdate forUpdate; |
| 2780 |
if (readIf("NOWAIT")) { |
| 2781 |
forUpdate = ForUpdate.NOWAIT; |
| 2782 |
} else if (readIf("WAIT")) { |
| 2783 |
BigDecimal timeout; |
| 2784 |
if (currentTokenType != LITERAL || (timeout = token.value(session).getBigDecimal()) == null |
| 2785 |
|| timeout.signum() < 0 |
| 2786 |
|| timeout.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE, 3)) > 0) { |
| 2787 |
throw DbException.getSyntaxError(sqlCommand, token.start(), "timeout (0..2147483.647)"); |
| 2788 |
} |
| 2789 |
read(); |
| 2790 |
forUpdate = ForUpdate.wait(timeout.movePointRight(3).intValue()); |
| 2791 |
} else if (readIf("SKIP", "LOCKED")) { |
| 2792 |
forUpdate = ForUpdate.SKIP_LOCKED; |
| 2793 |
} else { |
| 2794 |
forUpdate = ForUpdate.DEFAULT; |
| 2795 |
} |
| 2796 |
command.setForUpdate(forUpdate); |
| 2797 |
} else if (readIf("READ") || readIf(FETCH)) { |
| 2798 |
read("ONLY"); |
| 2799 |
} |
| 2800 |
} |
| 2801 |
if (database.getMode().isolationLevelInSelectOrInsertStatement) { |
| 2802 |
parseIsolationClause(); |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
/** |
| 2807 |
* DB2 isolation clause |
| 2808 |
*/ |
| 2809 |
private void parseIsolationClause() { |
| 2810 |
if (readIf(WITH)) { |
| 2811 |
if (readIf("RR") || readIf("RS")) { |
| 2812 |
// concurrent-access-resolution clause |
| 2813 |
if (readIf("USE", AND, "KEEP")) { |
| 2814 |
if (readIf("SHARE") || readIf("UPDATE") || |
| 2815 |
readIf("EXCLUSIVE")) { |
| 2816 |
// ignore |
| 2817 |
} |
| 2818 |
read("LOCKS"); |
| 2819 |
} |
| 2820 |
} else if (readIf("CS") || readIf("UR")) { |
| 2821 |
// ignore |
| 2822 |
} |
| 2823 |
} |
| 2824 |
} |
| 2825 |
|
| 2826 |
private Query parseQueryPrimary() { |
| 2827 |
if (readIf(OPEN_PAREN)) { |
| 2828 |
Query command = parseQueryExpressionBodyAndEndOfQuery(); |
| 2829 |
read(CLOSE_PAREN); |
| 2830 |
return command; |
| 2831 |
} |
| 2832 |
int start = tokenIndex; |
| 2833 |
if (readIf(SELECT)) { |
| 2834 |
return parseSelect(start); |
| 2835 |
} else if (readIf(TABLE)) { |
| 2836 |
return parseExplicitTable(start); |
| 2837 |
} |
| 2838 |
read(VALUES); |
| 2839 |
return parseValues(); |
| 2840 |
} |
| 2841 |
|
| 2842 |
private void parseSelectFromPart(Select command) { |
| 2843 |
do { |
| 2844 |
TableFilter top = readTableReference(); |
| 2845 |
command.addTableFilter(top, true); |
| 2846 |
boolean isOuter = false; |
| 2847 |
for (;;) { |
| 2848 |
TableFilter n = top.getNestedJoin(); |
| 2849 |
if (n != null) { |
| 2850 |
n.visit(f -> command.addTableFilter(f, false)); |
| 2851 |
} |
| 2852 |
TableFilter join = top.getJoin(); |
| 2853 |
if (join == null) { |
| 2854 |
break; |
| 2855 |
} |
| 2856 |
isOuter = isOuter | join.isJoinOuter(); |
| 2857 |
if (isOuter) { |
| 2858 |
command.addTableFilter(join, false); |
| 2859 |
} else { |
| 2860 |
// make flat so the optimizer can work better |
| 2861 |
Expression on = join.getJoinCondition(); |
| 2862 |
if (on != null) { |
| 2863 |
command.addCondition(on); |
| 2864 |
} |
| 2865 |
join.removeJoinCondition(); |
| 2866 |
top.removeJoin(); |
| 2867 |
command.addTableFilter(join, true); |
| 2868 |
} |
| 2869 |
top = join; |
| 2870 |
} |
| 2871 |
} while (readIf(COMMA)); |
| 2872 |
} |
| 2873 |
|
| 2874 |
private void parseSelectExpressions(Select command) { |
| 2875 |
if (database.getMode().topInSelect && readIf("TOP")) { |
| 2876 |
Select temp = currentSelect; |
| 2877 |
// make sure aggregate functions will not work in TOP and LIMIT |
| 2878 |
currentSelect = null; |
| 2879 |
// can't read more complex expressions here because |
| 2880 |
// SELECT TOP 1 +? A FROM TEST could mean |
| 2881 |
// SELECT TOP (1+?) A FROM TEST or |
| 2882 |
// SELECT TOP 1 (+?) AS A FROM TEST |
| 2883 |
command.setFetch(readTerm().optimize(session)); |
| 2884 |
if (readIf("PERCENT")) { |
| 2885 |
command.setFetchPercent(true); |
| 2886 |
} |
| 2887 |
if (readIf(WITH, "TIES")) { |
| 2888 |
command.setWithTies(true); |
| 2889 |
} |
| 2890 |
currentSelect = temp; |
| 2891 |
} |
| 2892 |
if (readIf(DISTINCT)) { |
| 2893 |
if (readIf(ON, OPEN_PAREN)) { |
| 2894 |
ArrayList<Expression> distinctExpressions = Utils.newSmallArrayList(); |
| 2895 |
do { |
| 2896 |
distinctExpressions.add(readExpression()); |
| 2897 |
} while (readIfMore()); |
| 2898 |
command.setDistinct(distinctExpressions.toArray(new Expression[0])); |
| 2899 |
} else { |
| 2900 |
command.setDistinct(); |
| 2901 |
} |
| 2902 |
} else { |
| 2903 |
readIf(ALL); |
| 2904 |
} |
| 2905 |
ArrayList<Expression> expressions = Utils.newSmallArrayList(); |
| 2906 |
do { |
| 2907 |
if (readIf(ASTERISK)) { |
| 2908 |
expressions.add(parseWildcard(null, null)); |
| 2909 |
} else { |
| 2910 |
switch (currentTokenType) { |
| 2911 |
case FROM: |
| 2912 |
case WHERE: |
| 2913 |
case GROUP: |
| 2914 |
case HAVING: |
| 2915 |
case WINDOW: |
| 2916 |
case QUALIFY: |
| 2917 |
case ORDER: |
| 2918 |
case OFFSET: |
| 2919 |
case FETCH: |
| 2920 |
case CLOSE_PAREN: |
| 2921 |
case SEMICOLON: |
| 2922 |
case END_OF_INPUT: |
| 2923 |
break; |
| 2924 |
default: |
| 2925 |
Expression expr = readExpression(); |
| 2926 |
if (readIf(AS) || isIdentifier()) { |
| 2927 |
expr = new Alias(expr, readIdentifier(), database.getMode().aliasColumnName); |
| 2928 |
} |
| 2929 |
expressions.add(expr); |
| 2930 |
} |
| 2931 |
} |
| 2932 |
} while (readIf(COMMA)); |
| 2933 |
command.setExpressions(expressions); |
| 2934 |
} |
| 2935 |
|
| 2936 |
private Select parseSelect(int start) { |
| 2937 |
Select command = new Select(session, currentSelect); |
| 2938 |
Select oldSelect = currentSelect; |
| 2939 |
Prepared oldPrepared = currentPrepared; |
| 2940 |
BitSet outerUsedParameters = initParametersScope(); |
| 2941 |
currentSelect = command; |
| 2942 |
currentPrepared = command; |
| 2943 |
parseSelectExpressions(command); |
| 2944 |
if (!readIf(FROM)) { |
| 2945 |
// select without FROM |
| 2946 |
TableFilter filter = new TableFilter(session, new DualTable(database), null, rightsChecked, |
| 2947 |
currentSelect, 0, null); |
| 2948 |
command.addTableFilter(filter, true); |
| 2949 |
} else { |
| 2950 |
parseSelectFromPart(command); |
| 2951 |
} |
| 2952 |
if (readIf(WHERE)) { |
| 2953 |
command.addCondition(readExpressionWithGlobalConditions()); |
| 2954 |
} |
| 2955 |
// the group by is read for the outer select (or not a select) |
| 2956 |
// so that columns that are not grouped can be used |
| 2957 |
currentSelect = oldSelect; |
| 2958 |
if (readIf(GROUP, "BY")) { |
| 2959 |
command.setGroupQuery(); |
| 2960 |
ArrayList<Expression> list = Utils.newSmallArrayList(); |
| 2961 |
do { |
| 2962 |
if (isToken(OPEN_PAREN) && isOrdinaryGroupingSet()) { |
| 2963 |
if (!readIf(CLOSE_PAREN)) { |
| 2964 |
do { |
| 2965 |
list.add(readExpression()); |
| 2966 |
} while (readIfMore()); |
| 2967 |
} |
| 2968 |
} else { |
| 2969 |
Expression expr = readExpression(); |
| 2970 |
if (database.getMode().groupByColumnIndex && expr instanceof ValueExpression && |
| 2971 |
expr.getType().getValueType() == Value.INTEGER) { |
| 2972 |
ArrayList<Expression> expressions = command.getExpressions(); |
| 2973 |
for (Expression e : expressions) { |
| 2974 |
if (e instanceof Wildcard) { |
| 2975 |
throw getSyntaxError(); |
| 2976 |
} |
| 2977 |
} |
| 2978 |
int idx = expr.getValue(session).getInt(); |
| 2979 |
if (idx < 1 || idx > expressions.size()) { |
| 2980 |
throw DbException.get(ErrorCode.GROUP_BY_NOT_IN_THE_RESULT, Integer.toString(idx), |
| 2981 |
Integer.toString(expressions.size())); |
| 2982 |
} |
| 2983 |
list.add(expressions.get(idx - 1)); |
| 2984 |
} else { |
| 2985 |
list.add(expr); |
| 2986 |
} |
| 2987 |
} |
| 2988 |
} while (readIf(COMMA)); |
| 2989 |
if (!list.isEmpty()) { |
| 2990 |
command.setGroupBy(list); |
| 2991 |
} |
| 2992 |
} |
| 2993 |
currentSelect = command; |
| 2994 |
if (readIf(HAVING)) { |
| 2995 |
command.setGroupQuery(); |
| 2996 |
command.setHaving(readExpressionWithGlobalConditions()); |
| 2997 |
} |
| 2998 |
if (readIf(WINDOW)) { |
| 2999 |
do { |
| 3000 |
int sqlIndex = token.start(); |
| 3001 |
String name = readIdentifier(); |
| 3002 |
read(AS); |
| 3003 |
Window w = readWindowSpecification(); |
| 3004 |
if (!currentSelect.addWindow(name, w)) { |
| 3005 |
throw DbException.getSyntaxError(sqlCommand, sqlIndex, "unique identifier"); |
| 3006 |
} |
| 3007 |
} while (readIf(COMMA)); |
| 3008 |
} |
| 3009 |
if (readIf(QUALIFY)) { |
| 3010 |
command.setWindowQuery(); |
| 3011 |
command.setQualify(readExpressionWithGlobalConditions()); |
| 3012 |
} |
| 3013 |
command.setParameterList(getUsedParameters(outerUsedParameters)); |
| 3014 |
currentSelect = oldSelect; |
| 3015 |
currentPrepared = oldPrepared; |
| 3016 |
setSQL(command, start); |
| 3017 |
return command; |
| 3018 |
} |
| 3019 |
|
| 3020 |
/** |
| 3021 |
* Checks whether current opening parenthesis can be a start of ordinary |
| 3022 |
* grouping set. This method reads this parenthesis if it is. |
| 3023 |
* |
| 3024 |
* @return whether current opening parenthesis can be a start of ordinary |
| 3025 |
* grouping set |
| 3026 |
*/ |
| 3027 |
private boolean isOrdinaryGroupingSet() { |
| 3028 |
int offset = scanToCloseParen(tokenIndex + 1); |
| 3029 |
if (offset < 0) { |
| 3030 |
// Try to parse as expression to get better syntax error |
| 3031 |
return false; |
| 3032 |
} |
| 3033 |
switch (tokens.get(offset).tokenType()) { |
| 3034 |
// End of query |
| 3035 |
case CLOSE_PAREN: |
| 3036 |
case SEMICOLON: |
| 3037 |
case END_OF_INPUT: |
| 3038 |
// Next grouping element |
| 3039 |
case COMMA: |
| 3040 |
// Next select clause |
| 3041 |
case HAVING: |
| 3042 |
case WINDOW: |
| 3043 |
case QUALIFY: |
| 3044 |
// Next query expression body clause |
| 3045 |
case UNION: |
| 3046 |
case EXCEPT: |
| 3047 |
case MINUS: |
| 3048 |
case INTERSECT: |
| 3049 |
// Next query expression clause |
| 3050 |
case ORDER: |
| 3051 |
case OFFSET: |
| 3052 |
case FETCH: |
| 3053 |
case LIMIT: |
| 3054 |
case FOR: |
| 3055 |
setTokenIndex(tokenIndex + 1); |
| 3056 |
return true; |
| 3057 |
default: |
| 3058 |
return false; |
| 3059 |
} |
| 3060 |
} |
| 3061 |
|
| 3062 |
private Query parseExplicitTable(int start) { |
| 3063 |
Table table = readTableOrView(); |
| 3064 |
Select command = new Select(session, currentSelect); |
| 3065 |
TableFilter filter = new TableFilter(session, table, null, rightsChecked, |
| 3066 |
command, orderInFrom++, null); |
| 3067 |
command.addTableFilter(filter, true); |
| 3068 |
command.setExplicitTable(); |
| 3069 |
setSQL(command, start); |
| 3070 |
return command; |
| 3071 |
} |
| 3072 |
|
| 3073 |
private void setSQL(Prepared command, int start) { |
| 3074 |
String s = sqlCommand; |
| 3075 |
int beginIndex = tokens.get(start).start(); |
| 3076 |
int endIndex = token.start(); |
| 3077 |
while (beginIndex < endIndex && s.charAt(beginIndex) <= ' ') { |
| 3078 |
beginIndex++; |
| 3079 |
} |
| 3080 |
while (beginIndex < endIndex && s.charAt(endIndex - 1) <= ' ') { |
| 3081 |
endIndex--; |
| 3082 |
} |
| 3083 |
s = s.substring(beginIndex, endIndex); |
| 3084 |
ArrayList<Token> commandTokens; |
| 3085 |
if (start == 0 && currentTokenType == END_OF_INPUT) { |
| 3086 |
commandTokens = tokens; |
| 3087 |
if (beginIndex != 0) { |
| 3088 |
for (int i = 0, l = commandTokens.size() - 1; i < l; i++) { |
| 3089 |
commandTokens.get(i).subtractFromStart(beginIndex); |
| 3090 |
} |
| 3091 |
} |
| 3092 |
token.setStart(s.length()); |
| 3093 |
sqlCommand = s; |
| 3094 |
} else { |
| 3095 |
List<Token> subList = tokens.subList(start, tokenIndex); |
| 3096 |
commandTokens = new ArrayList<>(subList.size() + 1); |
| 3097 |
for (int i = start; i < tokenIndex; i++) { |
| 3098 |
Token t = tokens.get(i).clone(); |
| 3099 |
t.subtractFromStart(beginIndex); |
| 3100 |
commandTokens.add(t); |
| 3101 |
} |
| 3102 |
commandTokens.add(new Token.EndOfInputToken(s.length())); |
| 3103 |
} |
| 3104 |
command.setSQL(s, commandTokens); |
| 3105 |
} |
| 3106 |
|
| 3107 |
private Expression readExpressionOrDefault() { |
| 3108 |
if (readIf(DEFAULT)) { |
| 3109 |
return ValueExpression.DEFAULT; |
| 3110 |
} |
| 3111 |
return readExpression(); |
| 3112 |
} |
| 3113 |
|
| 3114 |
private Expression readExpressionWithGlobalConditions() { |
| 3115 |
Expression r = readCondition(); |
| 3116 |
if (readIf(AND)) { |
| 3117 |
r = readAnd(new ConditionAndOr(ConditionAndOr.AND, r, readCondition())); |
| 3118 |
} else if (readIf("_LOCAL_AND_GLOBAL_")) { |
| 3119 |
r = readAnd(new ConditionLocalAndGlobal(r, readCondition())); |
| 3120 |
} |
| 3121 |
return readExpressionPart2(r); |
| 3122 |
} |
| 3123 |
|
| 3124 |
private Expression readExpression() { |
| 3125 |
return readExpressionPart2(readAnd(readCondition())); |
| 3126 |
} |
| 3127 |
|
| 3128 |
private Expression readExpressionPart2(Expression r1) { |
| 3129 |
if (!readIf(OR)) { |
| 3130 |
return r1; |
| 3131 |
} |
| 3132 |
Expression r2 = readAnd(readCondition()); |
| 3133 |
if (!readIf(OR)) { |
| 3134 |
return new ConditionAndOr(ConditionAndOr.OR, r1, r2); |
| 3135 |
} |
| 3136 |
// Above logic to avoid allocating an ArrayList for the common case. |
| 3137 |
// We combine into ConditionAndOrN here rather than letting the optimisation |
| 3138 |
// pass do it, to avoid StackOverflowError during stuff like mapColumns. |
| 3139 |
final ArrayList<Expression> expressions = new ArrayList<>(); |
| 3140 |
expressions.add(r1); |
| 3141 |
expressions.add(r2); |
| 3142 |
do { |
| 3143 |
expressions.add(readAnd(readCondition())); |
| 3144 |
} |
| 3145 |
while (readIf(OR)); |
| 3146 |
return new ConditionAndOrN(ConditionAndOr.OR, expressions); |
| 3147 |
} |
| 3148 |
|
| 3149 |
private Expression readAnd(Expression r) { |
| 3150 |
if (!readIf(AND)) { |
| 3151 |
return r; |
| 3152 |
} |
| 3153 |
Expression expr2 = readCondition(); |
| 3154 |
if (!readIf(AND)) { |
| 3155 |
return new ConditionAndOr(ConditionAndOr.AND, r, expr2); |
| 3156 |
} |
| 3157 |
// Above logic to avoid allocating an ArrayList for the common case. |
| 3158 |
// We combine into ConditionAndOrN here rather than letting the optimisation |
| 3159 |
// pass do it, to avoid StackOverflowError during stuff like mapColumns. |
| 3160 |
final ArrayList<Expression> expressions = new ArrayList<>(); |
| 3161 |
expressions.add(r); |
| 3162 |
expressions.add(expr2); |
| 3163 |
do { |
| 3164 |
expressions.add(readCondition()); |
| 3165 |
} |
| 3166 |
while (readIf(AND)); |
| 3167 |
return new ConditionAndOrN(ConditionAndOr.AND, expressions); |
| 3168 |
} |
| 3169 |
|
| 3170 |
private Expression readCondition() { |
| 3171 |
switch (currentTokenType) { |
| 3172 |
case NOT: |
| 3173 |
read(); |
| 3174 |
return new ConditionNot(readCondition()); |
| 3175 |
case EXISTS: { |
| 3176 |
read(); |
| 3177 |
read(OPEN_PAREN); |
| 3178 |
Query query = parseQuery(); |
| 3179 |
// can not reduce expression because it might be a union except |
| 3180 |
// query with distinct |
| 3181 |
read(CLOSE_PAREN); |
| 3182 |
return new ExistsPredicate(query); |
| 3183 |
} |
| 3184 |
case UNIQUE: { |
| 3185 |
read(); |
| 3186 |
NullsDistinct nullsDistinct = readNullsDistinct(NullsDistinct.DISTINCT); |
| 3187 |
read(OPEN_PAREN); |
| 3188 |
Query query = parseQuery(); |
| 3189 |
read(CLOSE_PAREN); |
| 3190 |
return new UniquePredicate(query, nullsDistinct); |
| 3191 |
} |
| 3192 |
default: |
| 3193 |
if (readIf("INTERSECTS", OPEN_PAREN)) { |
| 3194 |
Expression r1 = readConcat(); |
| 3195 |
read(COMMA); |
| 3196 |
Expression r2 = readConcat(); |
| 3197 |
read(CLOSE_PAREN); |
| 3198 |
return new Comparison(Comparison.SPATIAL_INTERSECTS, r1, r2, false); |
| 3199 |
} |
| 3200 |
if (expectedList != null) { |
| 3201 |
addMultipleExpected(NOT, EXISTS, UNIQUE); |
| 3202 |
addExpected("INTERSECTS"); |
| 3203 |
} |
| 3204 |
} |
| 3205 |
Expression l, c = readConcat(); |
| 3206 |
do { |
| 3207 |
l = c; |
| 3208 |
// special case: NOT NULL is not part of an expression (as in CREATE |
| 3209 |
// TABLE TEST(ID INT DEFAULT 0 NOT NULL)) |
| 3210 |
int backup = tokenIndex; |
| 3211 |
boolean not = readIf(NOT); |
| 3212 |
if (not && isToken(NULL)) { |
| 3213 |
// this really only works for NOT NULL! |
| 3214 |
setTokenIndex(backup); |
| 3215 |
break; |
| 3216 |
} |
| 3217 |
c = readConditionRightHandSide(l, not, false); |
| 3218 |
} while (c != null); |
| 3219 |
return l; |
| 3220 |
} |
| 3221 |
|
| 3222 |
private Expression readConditionRightHandSide(Expression r, boolean not, boolean whenOperand) { |
| 3223 |
if (!not && readIf(IS)) { |
| 3224 |
r = readConditionIs(r, whenOperand); |
| 3225 |
} else { |
| 3226 |
switch (currentTokenType) { |
| 3227 |
case BETWEEN: { |
| 3228 |
read(); |
| 3229 |
boolean symmetric = readIf(SYMMETRIC); |
| 3230 |
if (!symmetric) { |
| 3231 |
readIf(ASYMMETRIC); |
| 3232 |
} |
| 3233 |
Expression a = readConcat(); |
| 3234 |
read(AND); |
| 3235 |
r = new BetweenPredicate(r, not, whenOperand, symmetric, a, readConcat()); |
| 3236 |
break; |
| 3237 |
} |
| 3238 |
case IN: |
| 3239 |
read(); |
| 3240 |
r = readInPredicate(r, not, whenOperand); |
| 3241 |
break; |
| 3242 |
case LIKE: { |
| 3243 |
read(); |
| 3244 |
r = readLikePredicate(r, LikeType.LIKE, not, whenOperand); |
| 3245 |
break; |
| 3246 |
} |
| 3247 |
default: |
| 3248 |
if (readIf("ILIKE")) { |
| 3249 |
r = readLikePredicate(r, LikeType.ILIKE, not, whenOperand); |
| 3250 |
} else if (readIf("REGEXP")) { |
| 3251 |
Expression b = readConcat(); |
| 3252 |
recompileAlways = true; |
| 3253 |
r = new CompareLike(database, r, not, whenOperand, b, null, LikeType.REGEXP); |
| 3254 |
} else if (not) { |
| 3255 |
if (whenOperand) { |
| 3256 |
return null; |
| 3257 |
} |
| 3258 |
if (expectedList != null) { |
| 3259 |
addMultipleExpected(BETWEEN, IN, LIKE); |
| 3260 |
} |
| 3261 |
throw getSyntaxError(); |
| 3262 |
} else { |
| 3263 |
int compareType = getCompareType(currentTokenType); |
| 3264 |
if (compareType < 0) { |
| 3265 |
return null; |
| 3266 |
} |
| 3267 |
read(); |
| 3268 |
r = readComparison(r, compareType, whenOperand); |
| 3269 |
} |
| 3270 |
} |
| 3271 |
} |
| 3272 |
return r; |
| 3273 |
} |
| 3274 |
|
| 3275 |
private Expression readConditionIs(Expression left, boolean whenOperand) { |
| 3276 |
boolean isNot = readIf(NOT); |
| 3277 |
switch (currentTokenType) { |
| 3278 |
case NULL: |
| 3279 |
read(); |
| 3280 |
left = new NullPredicate(left, isNot, whenOperand); |
| 3281 |
break; |
| 3282 |
case DISTINCT: |
| 3283 |
read(); |
| 3284 |
read(FROM); |
| 3285 |
left = readComparison(left, isNot ? Comparison.EQUAL_NULL_SAFE : Comparison.NOT_EQUAL_NULL_SAFE, |
| 3286 |
whenOperand); |
| 3287 |
break; |
| 3288 |
case TRUE: |
| 3289 |
read(); |
| 3290 |
left = new BooleanTest(left, isNot, whenOperand, true); |
| 3291 |
break; |
| 3292 |
case FALSE: |
| 3293 |
read(); |
| 3294 |
left = new BooleanTest(left, isNot, whenOperand, false); |
| 3295 |
break; |
| 3296 |
case UNKNOWN: |
| 3297 |
read(); |
| 3298 |
left = new BooleanTest(left, isNot, whenOperand, null); |
| 3299 |
break; |
| 3300 |
default: |
| 3301 |
if (readIf("OF")) { |
| 3302 |
left = readTypePredicate(left, isNot, whenOperand); |
| 3303 |
} else if (readIf("JSON")) { |
| 3304 |
left = readJsonPredicate(left, isNot, whenOperand); |
| 3305 |
} else { |
| 3306 |
if (expectedList != null) { |
| 3307 |
addMultipleExpected(NULL, DISTINCT, TRUE, FALSE, UNKNOWN); |
| 3308 |
} |
| 3309 |
/* |
| 3310 |
* Databases that were created in 1.4.199 and older |
| 3311 |
* versions can contain invalid generated IS [ NOT ] |
| 3312 |
* expressions. |
| 3313 |
*/ |
| 3314 |
if (whenOperand || !session.isQuirksMode()) { |
| 3315 |
throw getSyntaxError(); |
| 3316 |
} |
| 3317 |
left = new Comparison(isNot ? Comparison.NOT_EQUAL_NULL_SAFE : Comparison.EQUAL_NULL_SAFE, left, |
| 3318 |
readConcat(), false); |
| 3319 |
} |
| 3320 |
} |
| 3321 |
return left; |
| 3322 |
} |
| 3323 |
|
| 3324 |
private TypePredicate readTypePredicate(Expression left, boolean not, boolean whenOperand) { |
| 3325 |
read(OPEN_PAREN); |
| 3326 |
ArrayList<TypeInfo> typeList = Utils.newSmallArrayList(); |
| 3327 |
do { |
| 3328 |
typeList.add(parseDataType()); |
| 3329 |
} while (readIfMore()); |
| 3330 |
return new TypePredicate(left, not, whenOperand, typeList.toArray(new TypeInfo[0])); |
| 3331 |
} |
| 3332 |
|
| 3333 |
private Expression readInPredicate(Expression left, boolean not, boolean whenOperand) { |
| 3334 |
read(OPEN_PAREN); |
| 3335 |
if (!whenOperand && database.getMode().allowEmptyInPredicate && readIf(CLOSE_PAREN)) { |
| 3336 |
return ValueExpression.getBoolean(not); |
| 3337 |
} |
| 3338 |
ArrayList<Expression> v; |
| 3339 |
if (isQuery()) { |
| 3340 |
Query query = parseQuery(); |
| 3341 |
if (!readIfMore()) { |
| 3342 |
return new ConditionInQuery(left, not, whenOperand, query, false, Comparison.EQUAL); |
| 3343 |
} |
| 3344 |
v = Utils.newSmallArrayList(); |
| 3345 |
v.add(new Subquery(query)); |
| 3346 |
} else { |
| 3347 |
v = Utils.newSmallArrayList(); |
| 3348 |
} |
| 3349 |
do { |
| 3350 |
v.add(readExpression()); |
| 3351 |
} while (readIfMore()); |
| 3352 |
return new ConditionIn(left, not, whenOperand, v); |
| 3353 |
} |
| 3354 |
|
| 3355 |
private IsJsonPredicate readJsonPredicate(Expression left, boolean not, boolean whenOperand) { |
| 3356 |
JSONItemType itemType; |
| 3357 |
if (readIf(VALUE)) { |
| 3358 |
itemType = JSONItemType.VALUE; |
| 3359 |
} else if (readIf(ARRAY)) { |
| 3360 |
itemType = JSONItemType.ARRAY; |
| 3361 |
} else if (readIf("OBJECT")) { |
| 3362 |
itemType = JSONItemType.OBJECT; |
| 3363 |
} else if (readIf("SCALAR")) { |
| 3364 |
itemType = JSONItemType.SCALAR; |
| 3365 |
} else { |
| 3366 |
itemType = JSONItemType.VALUE; |
| 3367 |
} |
| 3368 |
boolean unique = false; |
| 3369 |
if (readIf(WITH, UNIQUE)) { |
| 3370 |
readIf("KEYS"); |
| 3371 |
unique = true; |
| 3372 |
} else if (readIf("WITHOUT", UNIQUE)) { |
| 3373 |
readIf("KEYS"); |
| 3374 |
} |
| 3375 |
return new IsJsonPredicate(left, not, whenOperand, unique, itemType); |
| 3376 |
} |
| 3377 |
|
| 3378 |
private Expression readLikePredicate(Expression left, LikeType likeType, boolean not, boolean whenOperand) { |
| 3379 |
Expression right = readConcat(); |
| 3380 |
Expression esc = readIf("ESCAPE") ? readConcat() : null; |
| 3381 |
recompileAlways = true; |
| 3382 |
return new CompareLike(database, left, not, whenOperand, right, esc, likeType); |
| 3383 |
} |
| 3384 |
|
| 3385 |
private Expression readComparison(Expression left, int compareType, boolean whenOperand) { |
| 3386 |
int start = tokenIndex; |
| 3387 |
if (readIf(ALL, OPEN_PAREN)) { |
| 3388 |
if (isQuery()) { |
| 3389 |
left = new ConditionInQuery(left, false, whenOperand, parseQuery(), true, compareType); |
| 3390 |
} else { |
| 3391 |
left = new ConditionInArray(left, whenOperand, readExpression(), true, compareType); |
| 3392 |
} |
| 3393 |
read(CLOSE_PAREN); |
| 3394 |
} else if (readIf(ANY, OPEN_PAREN)) { |
| 3395 |
left = readAnyComparison(left, compareType, whenOperand, start); |
| 3396 |
} else if (readIf(SOME, OPEN_PAREN)) { |
| 3397 |
left = readAnyComparison(left, compareType, whenOperand, start); |
| 3398 |
} else { |
| 3399 |
left = new Comparison(compareType, left, readConcat(), whenOperand); |
| 3400 |
} |
| 3401 |
return left; |
| 3402 |
} |
| 3403 |
|
| 3404 |
private Expression readAnyComparison(Expression left, int compareType, boolean whenOperand, int start) { |
| 3405 |
if (isQuery()) { |
| 3406 |
left = new ConditionInQuery(left, false, whenOperand, parseQuery(), false, compareType); |
| 3407 |
} else { |
| 3408 |
left = new ConditionInArray(left, whenOperand, readExpression(), false, compareType); |
| 3409 |
} |
| 3410 |
read(CLOSE_PAREN); |
| 3411 |
return left; |
| 3412 |
} |
| 3413 |
|
| 3414 |
private Expression readConcat() { |
| 3415 |
Expression op1 = readSum(); |
| 3416 |
for (;;) { |
| 3417 |
switch (currentTokenType) { |
| 3418 |
case CONCATENATION: { |
| 3419 |
read(); |
| 3420 |
Expression op2 = readSum(); |
| 3421 |
if (readIf(CONCATENATION)) { |
| 3422 |
ConcatenationOperation c = new ConcatenationOperation(); |
| 3423 |
c.addParameter(op1); |
| 3424 |
c.addParameter(op2); |
| 3425 |
do { |
| 3426 |
c.addParameter(readSum()); |
| 3427 |
} while (readIf(CONCATENATION)); |
| 3428 |
c.doneWithParameters(); |
| 3429 |
op1 = c; |
| 3430 |
} else { |
| 3431 |
op1 = new ConcatenationOperation(op1, op2); |
| 3432 |
} |
| 3433 |
break; |
| 3434 |
} |
| 3435 |
case TILDE: // PostgreSQL compatibility |
| 3436 |
op1 = readTildeCondition(op1, false); |
| 3437 |
break; |
| 3438 |
case NOT_TILDE: // PostgreSQL compatibility |
| 3439 |
op1 = readTildeCondition(op1, true); |
| 3440 |
break; |
| 3441 |
default: |
| 3442 |
// Don't add compatibility operators |
| 3443 |
addExpected(CONCATENATION); |
| 3444 |
return op1; |
| 3445 |
} |
| 3446 |
} |
| 3447 |
} |
| 3448 |
|
| 3449 |
private Expression readSum() { |
| 3450 |
Expression r = readFactor(); |
| 3451 |
while (true) { |
| 3452 |
if (readIf(PLUS_SIGN)) { |
| 3453 |
r = new BinaryOperation(OpType.PLUS, r, readFactor()); |
| 3454 |
} else if (readIf(MINUS_SIGN)) { |
| 3455 |
r = new BinaryOperation(OpType.MINUS, r, readFactor()); |
| 3456 |
} else { |
| 3457 |
return r; |
| 3458 |
} |
| 3459 |
} |
| 3460 |
} |
| 3461 |
|
| 3462 |
private Expression readFactor() { |
| 3463 |
Expression r = readTerm(); |
| 3464 |
while (true) { |
| 3465 |
if (readIf(ASTERISK)) { |
| 3466 |
r = new BinaryOperation(OpType.MULTIPLY, r, readTerm()); |
| 3467 |
} else if (readIf(SLASH)) { |
| 3468 |
r = new BinaryOperation(OpType.DIVIDE, r, readTerm()); |
| 3469 |
} else if (readIf(PERCENT)) { |
| 3470 |
r = new MathFunction(r, readTerm(), MathFunction.MOD); |
| 3471 |
} else { |
| 3472 |
return r; |
| 3473 |
} |
| 3474 |
} |
| 3475 |
} |
| 3476 |
|
| 3477 |
private Expression readTildeCondition(Expression r, boolean not) { |
| 3478 |
read(); |
| 3479 |
if (readIf(ASTERISK)) { |
| 3480 |
r = new CastSpecification(r, TypeInfo.TYPE_VARCHAR_IGNORECASE); |
| 3481 |
} |
| 3482 |
return new CompareLike(database, r, not, false, readSum(), null, LikeType.REGEXP); |
| 3483 |
} |
| 3484 |
|
| 3485 |
private Expression readAggregate(AggregateType aggregateType, String aggregateName) { |
| 3486 |
if (currentSelect == null) { |
| 3487 |
expectedList = null; |
| 3488 |
throw getSyntaxError(); |
| 3489 |
} |
| 3490 |
Aggregate r; |
| 3491 |
switch (aggregateType) { |
| 3492 |
case COUNT: |
| 3493 |
if (readIf(ASTERISK)) { |
| 3494 |
r = new Aggregate(AggregateType.COUNT_ALL, new Expression[0], currentSelect, false); |
| 3495 |
} else { |
| 3496 |
boolean distinct = readDistinctAgg(); |
| 3497 |
Expression on = readExpression(); |
| 3498 |
if (on instanceof Wildcard && !distinct) { |
| 3499 |
// PostgreSQL compatibility: count(t.*) |
| 3500 |
r = new Aggregate(AggregateType.COUNT_ALL, new Expression[0], currentSelect, false); |
| 3501 |
} else { |
| 3502 |
r = new Aggregate(AggregateType.COUNT, new Expression[] { on }, currentSelect, distinct); |
| 3503 |
} |
| 3504 |
} |
| 3505 |
break; |
| 3506 |
case COVAR_POP: |
| 3507 |
case COVAR_SAMP: |
| 3508 |
case CORR: |
| 3509 |
case REGR_SLOPE: |
| 3510 |
case REGR_INTERCEPT: |
| 3511 |
case REGR_COUNT: |
| 3512 |
case REGR_R2: |
| 3513 |
case REGR_AVGX: |
| 3514 |
case REGR_AVGY: |
| 3515 |
case REGR_SXX: |
| 3516 |
case REGR_SYY: |
| 3517 |
case REGR_SXY: |
| 3518 |
r = new Aggregate(aggregateType, new Expression[] { readExpression(), readNextArgument() }, |
| 3519 |
currentSelect, false); |
| 3520 |
break; |
| 3521 |
case HISTOGRAM: |
| 3522 |
r = new Aggregate(aggregateType, new Expression[] { readExpression() }, currentSelect, false); |
| 3523 |
break; |
| 3524 |
case LISTAGG: { |
| 3525 |
boolean distinct = readDistinctAgg(); |
| 3526 |
Expression arg = readExpression(); |
| 3527 |
ListaggArguments extraArguments = new ListaggArguments(); |
| 3528 |
ArrayList<QueryOrderBy> orderByList; |
| 3529 |
if ("STRING_AGG".equals(aggregateName)) { |
| 3530 |
// PostgreSQL compatibility: string_agg(expression, delimiter) |
| 3531 |
read(COMMA); |
| 3532 |
extraArguments.setSeparator(readStringOrParameter()); |
| 3533 |
orderByList = readIfOrderBy(); |
| 3534 |
} else if ("GROUP_CONCAT".equals(aggregateName)) { |
| 3535 |
orderByList = readIfOrderBy(); |
| 3536 |
if (readIf("SEPARATOR")) { |
| 3537 |
extraArguments.setSeparator(readStringOrParameter()); |
| 3538 |
} |
| 3539 |
} else { |
| 3540 |
if (readIf(COMMA)) { |
| 3541 |
extraArguments.setSeparator(readStringOrParameter()); |
| 3542 |
} |
| 3543 |
if (readIf(ON)) { |
| 3544 |
read("OVERFLOW"); |
| 3545 |
if (readIf("TRUNCATE")) { |
| 3546 |
extraArguments.setOnOverflowTruncate(true); |
| 3547 |
if (currentTokenType == LITERAL) { |
| 3548 |
extraArguments.setFilter(readStringOrParameter()); |
| 3549 |
} |
| 3550 |
if (!readIf(WITH)) { |
| 3551 |
read("WITHOUT"); |
| 3552 |
extraArguments.setWithoutCount(true); |
| 3553 |
} |
| 3554 |
read("COUNT"); |
| 3555 |
} else { |
| 3556 |
read("ERROR"); |
| 3557 |
} |
| 3558 |
} |
| 3559 |
orderByList = null; |
| 3560 |
} |
| 3561 |
Expression[] args = new Expression[] { arg }; |
| 3562 |
int index = tokenIndex; |
| 3563 |
read(CLOSE_PAREN); |
| 3564 |
if (orderByList == null && isToken("WITHIN")) { |
| 3565 |
r = readWithinGroup(aggregateType, args, distinct, extraArguments, false, false); |
| 3566 |
} else { |
| 3567 |
setTokenIndex(index); |
| 3568 |
r = new Aggregate(AggregateType.LISTAGG, args, currentSelect, distinct); |
| 3569 |
r.setExtraArguments(extraArguments); |
| 3570 |
if (orderByList != null) { |
| 3571 |
r.setOrderByList(orderByList); |
| 3572 |
} |
| 3573 |
} |
| 3574 |
break; |
| 3575 |
} |
| 3576 |
case ARRAY_AGG: { |
| 3577 |
boolean distinct = readDistinctAgg(); |
| 3578 |
r = new Aggregate(AggregateType.ARRAY_AGG, new Expression[] { readExpression() }, currentSelect, distinct); |
| 3579 |
r.setOrderByList(readIfOrderBy()); |
| 3580 |
break; |
| 3581 |
} |
| 3582 |
case RANK: |
| 3583 |
case DENSE_RANK: |
| 3584 |
case PERCENT_RANK: |
| 3585 |
case CUME_DIST: { |
| 3586 |
if (isToken(CLOSE_PAREN)) { |
| 3587 |
return readWindowFunction(aggregateName); |
| 3588 |
} |
| 3589 |
ArrayList<Expression> expressions = Utils.newSmallArrayList(); |
| 3590 |
do { |
| 3591 |
expressions.add(readExpression()); |
| 3592 |
} while (readIfMore()); |
| 3593 |
r = readWithinGroup(aggregateType, expressions.toArray(new Expression[0]), false, null, true, false); |
| 3594 |
break; |
| 3595 |
} |
| 3596 |
case PERCENTILE_CONT: |
| 3597 |
case PERCENTILE_DISC: { |
| 3598 |
Expression num = readExpression(); |
| 3599 |
read(CLOSE_PAREN); |
| 3600 |
r = readWithinGroup(aggregateType, new Expression[] { num }, false, null, false, true); |
| 3601 |
break; |
| 3602 |
} |
| 3603 |
case MODE: { |
| 3604 |
if (readIf(CLOSE_PAREN)) { |
| 3605 |
r = readWithinGroup(AggregateType.MODE, new Expression[0], false, null, false, true); |
| 3606 |
} else { |
| 3607 |
Expression expr = readExpression(); |
| 3608 |
r = new Aggregate(AggregateType.MODE, new Expression[0], currentSelect, false); |
| 3609 |
if (readIf(ORDER)) { |
| 3610 |
read("BY"); |
| 3611 |
Expression expr2 = readExpression(); |
| 3612 |
String sql = expr.getSQL(HasSQL.DEFAULT_SQL_FLAGS), sql2 = expr2.getSQL(HasSQL.DEFAULT_SQL_FLAGS); |
| 3613 |
if (!sql.equals(sql2)) { |
| 3614 |
throw DbException.getSyntaxError(ErrorCode.IDENTICAL_EXPRESSIONS_SHOULD_BE_USED, sqlCommand, |
| 3615 |
token.start(), sql, sql2); |
| 3616 |
} |
| 3617 |
readAggregateOrder(r, expr, true); |
| 3618 |
} else { |
| 3619 |
readAggregateOrder(r, expr, false); |
| 3620 |
} |
| 3621 |
} |
| 3622 |
break; |
| 3623 |
} |
| 3624 |
case JSON_OBJECTAGG: { |
| 3625 |
boolean withKey = readIf(KEY); |
| 3626 |
Expression key = readExpression(); |
| 3627 |
if (withKey) { |
| 3628 |
read(VALUE); |
| 3629 |
} else if (!(readIf(VALUE) || (database.getMode().acceptsCommaAsJsonKeyValueSeparator && readIf(COMMA)))) { |
| 3630 |
read(COLON); |
| 3631 |
} |
| 3632 |
Expression value = readExpression(); |
| 3633 |
r = new Aggregate(AggregateType.JSON_OBJECTAGG, new Expression[] { key, value }, currentSelect, false); |
| 3634 |
readJsonObjectFunctionFlags(r, false); |
| 3635 |
break; |
| 3636 |
} |
| 3637 |
case JSON_ARRAYAGG: { |
| 3638 |
boolean distinct = readDistinctAgg(); |
| 3639 |
r = new Aggregate(AggregateType.JSON_ARRAYAGG, new Expression[] { readExpression() }, currentSelect, |
| 3640 |
distinct); |
| 3641 |
r.setOrderByList(readIfOrderBy()); |
| 3642 |
r.setFlags(JsonConstructorUtils.JSON_ABSENT_ON_NULL); |
| 3643 |
readJsonObjectFunctionFlags(r, true); |
| 3644 |
break; |
| 3645 |
} |
| 3646 |
default: |
| 3647 |
boolean distinct = readDistinctAgg(); |
| 3648 |
r = new Aggregate(aggregateType, new Expression[] { readExpression() }, currentSelect, distinct); |
| 3649 |
break; |
| 3650 |
} |
| 3651 |
read(CLOSE_PAREN); |
| 3652 |
readFilterAndOver(r); |
| 3653 |
return r; |
| 3654 |
} |
| 3655 |
|
| 3656 |
private Aggregate readWithinGroup(AggregateType aggregateType, Expression[] args, boolean distinct, |
| 3657 |
Object extraArguments, boolean forHypotheticalSet, boolean simple) { |
| 3658 |
read("WITHIN"); |
| 3659 |
read(GROUP); |
| 3660 |
read(OPEN_PAREN); |
| 3661 |
read(ORDER); |
| 3662 |
read("BY"); |
| 3663 |
Aggregate r = new Aggregate(aggregateType, args, currentSelect, distinct); |
| 3664 |
r.setExtraArguments(extraArguments); |
| 3665 |
if (forHypotheticalSet) { |
| 3666 |
int count = args.length; |
| 3667 |
ArrayList<QueryOrderBy> orderList = new ArrayList<>(count); |
| 3668 |
for (int i = 0; i < count; i++) { |
| 3669 |
if (i > 0) { |
| 3670 |
read(COMMA); |
| 3671 |
} |
| 3672 |
orderList.add(parseSortSpecification()); |
| 3673 |
} |
| 3674 |
r.setOrderByList(orderList); |
| 3675 |
} else if (simple) { |
| 3676 |
readAggregateOrder(r, readExpression(), true); |
| 3677 |
} else { |
| 3678 |
r.setOrderByList(parseSortSpecificationList()); |
| 3679 |
} |
| 3680 |
return r; |
| 3681 |
} |
| 3682 |
|
| 3683 |
private void readAggregateOrder(Aggregate r, Expression expr, boolean parseSortType) { |
| 3684 |
ArrayList<QueryOrderBy> orderList = new ArrayList<>(1); |
| 3685 |
QueryOrderBy order = new QueryOrderBy(); |
| 3686 |
order.expression = expr; |
| 3687 |
if (parseSortType) { |
| 3688 |
order.sortType = parseSortType(); |
| 3689 |
} |
| 3690 |
orderList.add(order); |
| 3691 |
r.setOrderByList(orderList); |
| 3692 |
} |
| 3693 |
|
| 3694 |
private ArrayList<QueryOrderBy> readIfOrderBy() { |
| 3695 |
if (readIf(ORDER, "BY")) { |
| 3696 |
return parseSortSpecificationList(); |
| 3697 |
} |
| 3698 |
return null; |
| 3699 |
} |
| 3700 |
|
| 3701 |
private ArrayList<QueryOrderBy> parseSortSpecificationList() { |
| 3702 |
ArrayList<QueryOrderBy> orderList = Utils.newSmallArrayList(); |
| 3703 |
do { |
| 3704 |
orderList.add(parseSortSpecification()); |
| 3705 |
} while (readIf(COMMA)); |
| 3706 |
return orderList; |
| 3707 |
} |
| 3708 |
|
| 3709 |
private QueryOrderBy parseSortSpecification() { |
| 3710 |
QueryOrderBy order = new QueryOrderBy(); |
| 3711 |
order.expression = readExpression(); |
| 3712 |
order.sortType = parseSortType(); |
| 3713 |
return order; |
| 3714 |
} |
| 3715 |
|
| 3716 |
private Expression readUserDefinedFunctionIf(Schema schema, String functionName) { |
| 3717 |
UserDefinedFunction userDefinedFunction = findUserDefinedFunctionWithinPath(schema, functionName); |
| 3718 |
if (userDefinedFunction == null) { |
| 3719 |
return null; |
| 3720 |
} else if (userDefinedFunction instanceof FunctionAlias) { |
| 3721 |
FunctionAlias functionAlias = (FunctionAlias) userDefinedFunction; |
| 3722 |
ArrayList<Expression> argList = Utils.newSmallArrayList(); |
| 3723 |
if (!readIf(CLOSE_PAREN)) { |
| 3724 |
do { |
| 3725 |
argList.add(readExpression()); |
| 3726 |
} while (readIfMore()); |
| 3727 |
} |
| 3728 |
return new JavaFunction(functionAlias, argList.toArray(new Expression[0])); |
| 3729 |
} else { |
| 3730 |
UserAggregate aggregate = (UserAggregate) userDefinedFunction; |
| 3731 |
boolean distinct = readDistinctAgg(); |
| 3732 |
ArrayList<Expression> params = Utils.newSmallArrayList(); |
| 3733 |
do { |
| 3734 |
params.add(readExpression()); |
| 3735 |
} while (readIfMore()); |
| 3736 |
Expression[] list = params.toArray(new Expression[0]); |
| 3737 |
JavaAggregate agg = new JavaAggregate(aggregate, list, currentSelect, distinct); |
| 3738 |
readFilterAndOver(agg); |
| 3739 |
return agg; |
| 3740 |
} |
| 3741 |
} |
| 3742 |
|
| 3743 |
private boolean readDistinctAgg() { |
| 3744 |
if (readIf(DISTINCT)) { |
| 3745 |
return true; |
| 3746 |
} |
| 3747 |
readIf(ALL); |
| 3748 |
return false; |
| 3749 |
} |
| 3750 |
|
| 3751 |
private void readFilterAndOver(AbstractAggregate aggregate) { |
| 3752 |
if (readIf("FILTER", OPEN_PAREN, WHERE)) { |
| 3753 |
Expression filterCondition = readExpression(); |
| 3754 |
read(CLOSE_PAREN); |
| 3755 |
aggregate.setFilterCondition(filterCondition); |
| 3756 |
} |
| 3757 |
readOver(aggregate); |
| 3758 |
} |
| 3759 |
|
| 3760 |
private void readOver(DataAnalysisOperation operation) { |
| 3761 |
if (readIf("OVER")) { |
| 3762 |
operation.setOverCondition(readWindowNameOrSpecification()); |
| 3763 |
currentSelect.setWindowQuery(); |
| 3764 |
} else if (operation.isAggregate()) { |
| 3765 |
currentSelect.setGroupQuery(); |
| 3766 |
} else { |
| 3767 |
throw getSyntaxError(); |
| 3768 |
} |
| 3769 |
} |
| 3770 |
|
| 3771 |
private Window readWindowNameOrSpecification() { |
| 3772 |
return isToken(OPEN_PAREN) ? readWindowSpecification() : new Window(readIdentifier(), null, null, null); |
| 3773 |
} |
| 3774 |
|
| 3775 |
private Window readWindowSpecification() { |
| 3776 |
read(OPEN_PAREN); |
| 3777 |
String parent = null; |
| 3778 |
if (currentTokenType == IDENTIFIER) { |
| 3779 |
String current = currentToken; |
| 3780 |
if (token.isQuoted() || ( // |
| 3781 |
!equalsToken(current, "PARTITION") // |
| 3782 |
&& !equalsToken(current, "ROWS") // |
| 3783 |
&& !equalsToken(current, "RANGE") // |
| 3784 |
&& !equalsToken(current, "GROUPS"))) { |
| 3785 |
parent = current; |
| 3786 |
read(); |
| 3787 |
} |
| 3788 |
} |
| 3789 |
ArrayList<Expression> partitionBy = null; |
| 3790 |
if (readIf("PARTITION", "BY")) { |
| 3791 |
partitionBy = Utils.newSmallArrayList(); |
| 3792 |
do { |
| 3793 |
Expression expr = readExpression(); |
| 3794 |
partitionBy.add(expr); |
| 3795 |
} while (readIf(COMMA)); |
| 3796 |
} |
| 3797 |
ArrayList<QueryOrderBy> orderBy = readIfOrderBy(); |
| 3798 |
WindowFrame frame = readWindowFrame(); |
| 3799 |
read(CLOSE_PAREN); |
| 3800 |
return new Window(parent, partitionBy, orderBy, frame); |
| 3801 |
} |
| 3802 |
|
| 3803 |
private WindowFrame readWindowFrame() { |
| 3804 |
WindowFrameUnits units; |
| 3805 |
if (readIf("ROWS")) { |
| 3806 |
units = WindowFrameUnits.ROWS; |
| 3807 |
} else if (readIf("RANGE")) { |
| 3808 |
units = WindowFrameUnits.RANGE; |
| 3809 |
} else if (readIf("GROUPS")) { |
| 3810 |
units = WindowFrameUnits.GROUPS; |
| 3811 |
} else { |
| 3812 |
return null; |
| 3813 |
} |
| 3814 |
WindowFrameBound starting, following; |
| 3815 |
if (readIf(BETWEEN)) { |
| 3816 |
starting = readWindowFrameRange(); |
| 3817 |
read(AND); |
| 3818 |
following = readWindowFrameRange(); |
| 3819 |
} else { |
| 3820 |
starting = readWindowFrameStarting(); |
| 3821 |
following = null; |
| 3822 |
} |
| 3823 |
int sqlIndex = token.start(); |
| 3824 |
WindowFrameExclusion exclusion = WindowFrameExclusion.EXCLUDE_NO_OTHERS; |
| 3825 |
if (readIf("EXCLUDE")) { |
| 3826 |
if (readIf("CURRENT", ROW)) { |
| 3827 |
exclusion = WindowFrameExclusion.EXCLUDE_CURRENT_ROW; |
| 3828 |
} else if (readIf(GROUP)) { |
| 3829 |
exclusion = WindowFrameExclusion.EXCLUDE_GROUP; |
| 3830 |
} else if (readIf("TIES")) { |
| 3831 |
exclusion = WindowFrameExclusion.EXCLUDE_TIES; |
| 3832 |
} else { |
| 3833 |
read("NO"); |
| 3834 |
read("OTHERS"); |
| 3835 |
} |
| 3836 |
} |
| 3837 |
WindowFrame frame = new WindowFrame(units, starting, following, exclusion); |
| 3838 |
if (!frame.isValid()) { |
| 3839 |
throw DbException.getSyntaxError(sqlCommand, sqlIndex); |
| 3840 |
} |
| 3841 |
return frame; |
| 3842 |
} |
| 3843 |
|
| 3844 |
private WindowFrameBound readWindowFrameStarting() { |
| 3845 |
if (readIf("UNBOUNDED")) { |
| 3846 |
read("PRECEDING"); |
| 3847 |
return new WindowFrameBound(WindowFrameBoundType.UNBOUNDED_PRECEDING, null); |
| 3848 |
} |
| 3849 |
if (readIf("CURRENT")) { |
| 3850 |
read(ROW); |
| 3851 |
return new WindowFrameBound(WindowFrameBoundType.CURRENT_ROW, null); |
| 3852 |
} |
| 3853 |
Expression value = readExpression(); |
| 3854 |
read("PRECEDING"); |
| 3855 |
return new WindowFrameBound(WindowFrameBoundType.PRECEDING, value); |
| 3856 |
} |
| 3857 |
|
| 3858 |
private WindowFrameBound readWindowFrameRange() { |
| 3859 |
if (readIf("UNBOUNDED")) { |
| 3860 |
if (readIf("PRECEDING")) { |
| 3861 |
return new WindowFrameBound(WindowFrameBoundType.UNBOUNDED_PRECEDING, null); |
| 3862 |
} |
| 3863 |
read("FOLLOWING"); |
| 3864 |
return new WindowFrameBound(WindowFrameBoundType.UNBOUNDED_FOLLOWING, null); |
| 3865 |
} |
| 3866 |
if (readIf("CURRENT")) { |
| 3867 |
read(ROW); |
| 3868 |
return new WindowFrameBound(WindowFrameBoundType.CURRENT_ROW, null); |
| 3869 |
} |
| 3870 |
Expression value = readExpression(); |
| 3871 |
if (readIf("PRECEDING")) { |
| 3872 |
return new WindowFrameBound(WindowFrameBoundType.PRECEDING, value); |
| 3873 |
} |
| 3874 |
read("FOLLOWING"); |
| 3875 |
return new WindowFrameBound(WindowFrameBoundType.FOLLOWING, value); |
| 3876 |
} |
| 3877 |
|
| 3878 |
private Expression readFunction(Schema schema, String name) { |
| 3879 |
String upperName = upperName(name); |
| 3880 |
if (schema != null) { |
| 3881 |
return readFunctionWithSchema(schema, name, upperName); |
| 3882 |
} |
| 3883 |
boolean allowOverride = database.isAllowBuiltinAliasOverride(); |
| 3884 |
if (allowOverride) { |
| 3885 |
Expression e = readUserDefinedFunctionIf(null, name); |
| 3886 |
if (e != null) { |
| 3887 |
return e; |
| 3888 |
} |
| 3889 |
} |
| 3890 |
AggregateType agg = Aggregate.getAggregateType(upperName); |
| 3891 |
if (agg != null) { |
| 3892 |
return readAggregate(agg, upperName); |
| 3893 |
} |
| 3894 |
Expression e = readBuiltinFunctionIf(upperName); |
| 3895 |
if (e != null) { |
| 3896 |
return e; |
| 3897 |
} |
| 3898 |
e = readWindowFunction(upperName); |
| 3899 |
if (e != null) { |
| 3900 |
return e; |
| 3901 |
} |
| 3902 |
e = readCompatibilityFunction(upperName); |
| 3903 |
if (e != null) { |
| 3904 |
return e; |
| 3905 |
} |
| 3906 |
if (!allowOverride) { |
| 3907 |
e = readUserDefinedFunctionIf(null, name); |
| 3908 |
if (e != null) { |
| 3909 |
return e; |
| 3910 |
} |
| 3911 |
} |
| 3912 |
throw DbException.get(ErrorCode.FUNCTION_NOT_FOUND_1, name); |
| 3913 |
} |
| 3914 |
|
| 3915 |
private Expression readFunctionWithSchema(Schema schema, String name, String upperName) { |
| 3916 |
if (database.getMode().getEnum() == ModeEnum.PostgreSQL |
| 3917 |
&& schema.getName().equals(database.sysIdentifier("PG_CATALOG"))) { |
| 3918 |
FunctionsPostgreSQL function = FunctionsPostgreSQL.getFunction(upperName); |
| 3919 |
if (function != null) { |
| 3920 |
return readParameters(function); |
| 3921 |
} |
| 3922 |
} |
| 3923 |
Expression function = readUserDefinedFunctionIf(schema, name); |
| 3924 |
if (function != null) { |
| 3925 |
return function; |
| 3926 |
} |
| 3927 |
throw DbException.get(ErrorCode.FUNCTION_NOT_FOUND_1, name); |
| 3928 |
} |
| 3929 |
|
| 3930 |
private Expression readCompatibilityFunction(String name) { |
| 3931 |
switch (name) { |
| 3932 |
// || |
| 3933 |
case "ARRAY_APPEND": |
| 3934 |
case "ARRAY_CAT": |
| 3935 |
return new ConcatenationOperation(readExpression(), readLastArgument()); |
| 3936 |
// [] |
| 3937 |
case "ARRAY_GET": |
| 3938 |
return new ArrayElementReference(readExpression(), readLastArgument()); |
| 3939 |
// CARDINALITY |
| 3940 |
case "ARRAY_LENGTH": |
| 3941 |
return new CardinalityExpression(readSingleArgument(), false); |
| 3942 |
// Simple case |
| 3943 |
case "DECODE": { |
| 3944 |
Expression caseOperand = readExpression(); |
| 3945 |
boolean canOptimize = caseOperand.isConstant() && !caseOperand.getValue(session).containsNull(); |
| 3946 |
Expression a = readNextArgument(), b = readNextArgument(); |
| 3947 |
SimpleCase.SimpleWhen when = decodeToWhen(caseOperand, canOptimize, a, b), current = when; |
| 3948 |
Expression elseResult = null; |
| 3949 |
while (readIf(COMMA)) { |
| 3950 |
a = readExpression(); |
| 3951 |
if (readIf(COMMA)) { |
| 3952 |
b = readExpression(); |
| 3953 |
SimpleCase.SimpleWhen next = decodeToWhen(caseOperand, canOptimize, a, b); |
| 3954 |
current.setWhen(next); |
| 3955 |
current = next; |
| 3956 |
} else { |
| 3957 |
elseResult = a; |
| 3958 |
break; |
| 3959 |
} |
| 3960 |
} |
| 3961 |
read(CLOSE_PAREN); |
| 3962 |
return new SimpleCase(caseOperand, when, elseResult); |
| 3963 |
} |
| 3964 |
// Searched case |
| 3965 |
case "CASEWHEN": |
| 3966 |
return readCompatibilityCase(readExpression()); |
| 3967 |
case "NVL2": |
| 3968 |
return readCompatibilityCase(new NullPredicate(readExpression(), true, false)); |
| 3969 |
// Cast specification |
| 3970 |
case "CONVERT": { |
| 3971 |
Expression arg; |
| 3972 |
Column column; |
| 3973 |
if (database.getMode().swapConvertFunctionParameters) { |
| 3974 |
column = parseColumnWithType(null); |
| 3975 |
arg = readNextArgument(); |
| 3976 |
} else { |
| 3977 |
arg = readExpression(); |
| 3978 |
read(COMMA); |
| 3979 |
column = parseColumnWithType(null); |
| 3980 |
} |
| 3981 |
read(CLOSE_PAREN); |
| 3982 |
return new CastSpecification(arg, column); |
| 3983 |
} |
| 3984 |
// COALESCE |
| 3985 |
case "IFNULL": |
| 3986 |
return new CoalesceFunction(CoalesceFunction.COALESCE, readExpression(), readLastArgument()); |
| 3987 |
case "NVL": |
| 3988 |
return readCoalesceFunction(CoalesceFunction.COALESCE); |
| 3989 |
// CURRENT_CATALOG |
| 3990 |
case "DATABASE": |
| 3991 |
read(CLOSE_PAREN); |
| 3992 |
return new CurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_CATALOG); |
| 3993 |
// CURRENT_DATE |
| 3994 |
case "CURDATE": |
| 3995 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_DATE, true, name); |
| 3996 |
case "TODAY": |
| 3997 |
read(CLOSE_PAREN); |
| 3998 |
return ModeFunction.getCompatibilityDateTimeValueFunction(database, "TODAY", -1); |
| 3999 |
// CURRENT_SCHEMA |
| 4000 |
case "SCHEMA": |
| 4001 |
read(CLOSE_PAREN); |
| 4002 |
return new CurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_SCHEMA); |
| 4003 |
// CURRENT_TIMESTAMP |
| 4004 |
case "SYSTIMESTAMP": { |
| 4005 |
int scale = -1; |
| 4006 |
if (!readIf(CLOSE_PAREN)) { |
| 4007 |
scale = readInt(); |
| 4008 |
if (scale < 0 || scale > ValueTime.MAXIMUM_SCALE) { |
| 4009 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 4010 |
/* compile-time constant */ "" + ValueTime.MAXIMUM_SCALE); |
| 4011 |
} |
| 4012 |
read(CLOSE_PAREN); |
| 4013 |
} |
| 4014 |
return ModeFunction.getCompatibilityDateTimeValueFunction(database, "SYSTIMESTAMP", scale); |
| 4015 |
} |
| 4016 |
// EXTRACT |
| 4017 |
case "DAY": |
| 4018 |
case "DAY_OF_MONTH": |
| 4019 |
case "DAYOFMONTH": |
| 4020 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.DAY, readSingleArgument(), null); |
| 4021 |
case "DAY_OF_WEEK": |
| 4022 |
case "DAYOFWEEK": |
| 4023 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.DAY_OF_WEEK, readSingleArgument(), |
| 4024 |
null); |
| 4025 |
case "DAY_OF_YEAR": |
| 4026 |
case "DAYOFYEAR": |
| 4027 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.DAY_OF_YEAR, readSingleArgument(), |
| 4028 |
null); |
| 4029 |
case "HOUR": |
| 4030 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.HOUR, readSingleArgument(), null); |
| 4031 |
case "ISO_DAY_OF_WEEK": |
| 4032 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.ISO_DAY_OF_WEEK, |
| 4033 |
readSingleArgument(), null); |
| 4034 |
case "ISO_WEEK": |
| 4035 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.ISO_WEEK, readSingleArgument(), |
| 4036 |
null); |
| 4037 |
case "ISO_YEAR": |
| 4038 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.ISO_WEEK_YEAR, readSingleArgument(), |
| 4039 |
null); |
| 4040 |
case "MINUTE": |
| 4041 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.MINUTE, readSingleArgument(), null); |
| 4042 |
case "MONTH": |
| 4043 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.MONTH, readSingleArgument(), null); |
| 4044 |
case "QUARTER": |
| 4045 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.QUARTER, readSingleArgument(), // |
| 4046 |
null); |
| 4047 |
case "SECOND": |
| 4048 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.SECOND, readSingleArgument(), null); |
| 4049 |
case "WEEK": |
| 4050 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.WEEK, readSingleArgument(), null); |
| 4051 |
case "YEAR": |
| 4052 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, DateTimeFunction.YEAR, readSingleArgument(), null); |
| 4053 |
// LOCALTIME |
| 4054 |
case "CURTIME": |
| 4055 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIME, true, "CURTIME"); |
| 4056 |
// LOCALTIMESTAMP |
| 4057 |
case "NOW": |
| 4058 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIMESTAMP, true, "NOW"); |
| 4059 |
case "SYSDATE": |
| 4060 |
read(CLOSE_PAREN); |
| 4061 |
return ModeFunction.getCompatibilityDateTimeValueFunction(database, "SYSDATE", -1); |
| 4062 |
// LOCATE |
| 4063 |
case "INSTR": { |
| 4064 |
Expression arg1 = readExpression(); |
| 4065 |
return new StringFunction(readNextArgument(), arg1, readIfArgument(), StringFunction.LOCATE); |
| 4066 |
} |
| 4067 |
case "POSITION": { |
| 4068 |
// can't read expression because IN would be read too early |
| 4069 |
Expression arg1 = readConcat(); |
| 4070 |
if (!readIf(COMMA)) { |
| 4071 |
read(IN); |
| 4072 |
} |
| 4073 |
return new StringFunction(arg1, readSingleArgument(), null, StringFunction.LOCATE); |
| 4074 |
} |
| 4075 |
// LOWER |
| 4076 |
case "LCASE": |
| 4077 |
return new StringFunction1(readSingleArgument(), StringFunction1.LOWER); |
| 4078 |
// SUBSTRING |
| 4079 |
case "SUBSTR": |
| 4080 |
return readSubstringFunction(); |
| 4081 |
// UPPER |
| 4082 |
case "UCASE": |
| 4083 |
return new StringFunction1(readSingleArgument(), StringFunction1.UPPER); |
| 4084 |
// Sequence value |
| 4085 |
case "CURRVAL": |
| 4086 |
return readCompatibilitySequenceValueFunction(true); |
| 4087 |
case "NEXTVAL": |
| 4088 |
return readCompatibilitySequenceValueFunction(false); |
| 4089 |
default: |
| 4090 |
return null; |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
private <T extends ExpressionWithVariableParameters> T readParameters(T expression) { |
| 4095 |
if (!readIf(CLOSE_PAREN)) { |
| 4096 |
do { |
| 4097 |
expression.addParameter(readExpression()); |
| 4098 |
} while (readIfMore()); |
| 4099 |
} |
| 4100 |
expression.doneWithParameters(); |
| 4101 |
return expression; |
| 4102 |
} |
| 4103 |
|
| 4104 |
private SimpleCase.SimpleWhen decodeToWhen(Expression caseOperand, boolean canOptimize, Expression whenOperand, |
| 4105 |
Expression result) { |
| 4106 |
if (!canOptimize && (!whenOperand.isConstant() || whenOperand.getValue(session).containsNull())) { |
| 4107 |
whenOperand = new Comparison(Comparison.EQUAL_NULL_SAFE, caseOperand, whenOperand, true); |
| 4108 |
} |
| 4109 |
return new SimpleCase.SimpleWhen(whenOperand, result); |
| 4110 |
} |
| 4111 |
|
| 4112 |
private Expression readCompatibilityCase(Expression when) { |
| 4113 |
return new SearchedCase(new Expression[] { when, readNextArgument(), readLastArgument() }); |
| 4114 |
} |
| 4115 |
|
| 4116 |
private Expression readCompatibilitySequenceValueFunction(boolean current) { |
| 4117 |
Expression arg1 = readExpression(), arg2 = readIf(COMMA) ? readExpression() : null; |
| 4118 |
read(CLOSE_PAREN); |
| 4119 |
return new CompatibilitySequenceValueFunction(arg1, arg2, current); |
| 4120 |
} |
| 4121 |
|
| 4122 |
private Expression readBuiltinFunctionIf(String upperName) { |
| 4123 |
switch (upperName) { |
| 4124 |
case "ABS": |
| 4125 |
return new MathFunction(readSingleArgument(), null, MathFunction.ABS); |
| 4126 |
case "MOD": |
| 4127 |
return new MathFunction(readExpression(), readLastArgument(), MathFunction.MOD); |
| 4128 |
case "SIN": |
| 4129 |
return new MathFunction1(readSingleArgument(), MathFunction1.SIN); |
| 4130 |
case "COS": |
| 4131 |
return new MathFunction1(readSingleArgument(), MathFunction1.COS); |
| 4132 |
case "TAN": |
| 4133 |
return new MathFunction1(readSingleArgument(), MathFunction1.TAN); |
| 4134 |
case "COT": |
| 4135 |
return new MathFunction1(readSingleArgument(), MathFunction1.COT); |
| 4136 |
case "SINH": |
| 4137 |
return new MathFunction1(readSingleArgument(), MathFunction1.SINH); |
| 4138 |
case "COSH": |
| 4139 |
return new MathFunction1(readSingleArgument(), MathFunction1.COSH); |
| 4140 |
case "TANH": |
| 4141 |
return new MathFunction1(readSingleArgument(), MathFunction1.TANH); |
| 4142 |
case "ASIN": |
| 4143 |
return new MathFunction1(readSingleArgument(), MathFunction1.ASIN); |
| 4144 |
case "ACOS": |
| 4145 |
return new MathFunction1(readSingleArgument(), MathFunction1.ACOS); |
| 4146 |
case "ATAN": |
| 4147 |
return new MathFunction1(readSingleArgument(), MathFunction1.ATAN); |
| 4148 |
case "ATAN2": |
| 4149 |
return new MathFunction2(readExpression(), readLastArgument(), MathFunction2.ATAN2); |
| 4150 |
case "LOG": { |
| 4151 |
Expression arg1 = readExpression(); |
| 4152 |
if (readIf(COMMA)) { |
| 4153 |
return new MathFunction2(arg1, readSingleArgument(), MathFunction2.LOG); |
| 4154 |
} else { |
| 4155 |
read(CLOSE_PAREN); |
| 4156 |
return new MathFunction1(arg1, |
| 4157 |
database.getMode().logIsLogBase10 ? MathFunction1.LOG10 : MathFunction1.LN); |
| 4158 |
} |
| 4159 |
} |
| 4160 |
case "LOG10": |
| 4161 |
return new MathFunction1(readSingleArgument(), MathFunction1.LOG10); |
| 4162 |
case "LN": |
| 4163 |
return new MathFunction1(readSingleArgument(), MathFunction1.LN); |
| 4164 |
case "EXP": |
| 4165 |
return new MathFunction1(readSingleArgument(), MathFunction1.EXP); |
| 4166 |
case "POWER": |
| 4167 |
return new MathFunction2(readExpression(), readLastArgument(), MathFunction2.POWER); |
| 4168 |
case "SQRT": |
| 4169 |
return new MathFunction1(readSingleArgument(), MathFunction1.SQRT); |
| 4170 |
case "FLOOR": |
| 4171 |
return new MathFunction(readSingleArgument(), null, MathFunction.FLOOR); |
| 4172 |
case "CEIL": |
| 4173 |
case "CEILING": |
| 4174 |
return new MathFunction(readSingleArgument(), null, MathFunction.CEIL); |
| 4175 |
case "ROUND": |
| 4176 |
return new MathFunction(readExpression(), readIfArgument(), MathFunction.ROUND); |
| 4177 |
case "ROUNDMAGIC": |
| 4178 |
return new MathFunction(readSingleArgument(), null, MathFunction.ROUNDMAGIC); |
| 4179 |
case "SIGN": |
| 4180 |
return new MathFunction(readSingleArgument(), null, MathFunction.SIGN); |
| 4181 |
case "TRUNC": |
| 4182 |
case "TRUNCATE": |
| 4183 |
return new MathFunction(readExpression(), readIfArgument(), MathFunction.TRUNC); |
| 4184 |
case "DEGREES": |
| 4185 |
return new MathFunction1(readSingleArgument(), MathFunction1.DEGREES); |
| 4186 |
case "RADIANS": |
| 4187 |
return new MathFunction1(readSingleArgument(), MathFunction1.RADIANS); |
| 4188 |
case "BITAND": |
| 4189 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITAND); |
| 4190 |
case "BITOR": |
| 4191 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITOR); |
| 4192 |
case "BITXOR": |
| 4193 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITXOR); |
| 4194 |
case "BITNOT": |
| 4195 |
return new BitFunction(readSingleArgument(), null, BitFunction.BITNOT); |
| 4196 |
case "BITNAND": |
| 4197 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITNAND); |
| 4198 |
case "BITNOR": |
| 4199 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITNOR); |
| 4200 |
case "BITXNOR": |
| 4201 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITXNOR); |
| 4202 |
case "BITGET": |
| 4203 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.BITGET); |
| 4204 |
case "BITCOUNT": |
| 4205 |
return new BitFunction(readSingleArgument(), null, BitFunction.BITCOUNT); |
| 4206 |
case "LSHIFT": |
| 4207 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.LSHIFT); |
| 4208 |
case "RSHIFT": |
| 4209 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.RSHIFT); |
| 4210 |
case "ULSHIFT": |
| 4211 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.ULSHIFT); |
| 4212 |
case "URSHIFT": |
| 4213 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.URSHIFT); |
| 4214 |
case "ROTATELEFT": |
| 4215 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.ROTATELEFT); |
| 4216 |
case "ROTATERIGHT": |
| 4217 |
return new BitFunction(readExpression(), readLastArgument(), BitFunction.ROTATERIGHT); |
| 4218 |
case "EXTRACT": { |
| 4219 |
int field = readDateTimeField(); |
| 4220 |
read(FROM); |
| 4221 |
return new DateTimeFunction(DateTimeFunction.EXTRACT, field, readSingleArgument(), null); |
| 4222 |
} |
| 4223 |
case "DATE_TRUNC": |
| 4224 |
return new DateTimeFunction(DateTimeFunction.DATE_TRUNC, readDateTimeField(), readLastArgument(), null); |
| 4225 |
case "DATEADD": |
| 4226 |
case "TIMESTAMPADD": |
| 4227 |
return new DateTimeFunction(DateTimeFunction.DATEADD, readDateTimeField(), readNextArgument(), |
| 4228 |
readLastArgument()); |
| 4229 |
case "DATEDIFF": |
| 4230 |
case "TIMESTAMPDIFF": |
| 4231 |
return new DateTimeFunction(DateTimeFunction.DATEDIFF, readDateTimeField(), readNextArgument(), |
| 4232 |
readLastArgument()); |
| 4233 |
case "LAST_DAY": |
| 4234 |
return new DateTimeFunction(DateTimeFunction.LAST_DAY, -1, readSingleArgument(), null); |
| 4235 |
case "FORMATDATETIME": |
| 4236 |
return readDateTimeFormatFunction(DateTimeFormatFunction.FORMATDATETIME); |
| 4237 |
case "PARSEDATETIME": |
| 4238 |
return readDateTimeFormatFunction(DateTimeFormatFunction.PARSEDATETIME); |
| 4239 |
case "DAYNAME": |
| 4240 |
return new DayMonthNameFunction(readSingleArgument(), DayMonthNameFunction.DAYNAME); |
| 4241 |
case "MONTHNAME": |
| 4242 |
return new DayMonthNameFunction(readSingleArgument(), DayMonthNameFunction.MONTHNAME); |
| 4243 |
case "CARDINALITY": |
| 4244 |
return new CardinalityExpression(readSingleArgument(), false); |
| 4245 |
case "ARRAY_MAX_CARDINALITY": |
| 4246 |
return new CardinalityExpression(readSingleArgument(), true); |
| 4247 |
case "LOCATE": |
| 4248 |
return new StringFunction(readExpression(), readNextArgument(), readIfArgument(), StringFunction.LOCATE); |
| 4249 |
case "INSERT": |
| 4250 |
return new StringFunction(readExpression(), readNextArgument(), readNextArgument(), readLastArgument(), |
| 4251 |
StringFunction.INSERT); |
| 4252 |
case "REPLACE": |
| 4253 |
return new StringFunction(readExpression(), readNextArgument(), readIfArgument(), StringFunction.REPLACE); |
| 4254 |
case "LPAD": |
| 4255 |
return new StringFunction(readExpression(), readNextArgument(), readIfArgument(), StringFunction.LPAD); |
| 4256 |
case "RPAD": |
| 4257 |
return new StringFunction(readExpression(), readNextArgument(), readIfArgument(), StringFunction.RPAD); |
| 4258 |
case "TRANSLATE": |
| 4259 |
return new StringFunction(readExpression(), readNextArgument(), readLastArgument(), |
| 4260 |
StringFunction.TRANSLATE); |
| 4261 |
case "UPPER": |
| 4262 |
return new StringFunction1(readSingleArgument(), StringFunction1.UPPER); |
| 4263 |
case "LOWER": |
| 4264 |
return new StringFunction1(readSingleArgument(), StringFunction1.LOWER); |
| 4265 |
case "ASCII": |
| 4266 |
return new StringFunction1(readSingleArgument(), StringFunction1.ASCII); |
| 4267 |
case "CHAR": |
| 4268 |
case "CHR": |
| 4269 |
return new StringFunction1(readSingleArgument(), StringFunction1.CHAR); |
| 4270 |
case "STRINGENCODE": |
| 4271 |
return new StringFunction1(readSingleArgument(), StringFunction1.STRINGENCODE); |
| 4272 |
case "STRINGDECODE": |
| 4273 |
return new StringFunction1(readSingleArgument(), StringFunction1.STRINGDECODE); |
| 4274 |
case "STRINGTOUTF8": |
| 4275 |
return new StringFunction1(readSingleArgument(), StringFunction1.STRINGTOUTF8); |
| 4276 |
case "UTF8TOSTRING": |
| 4277 |
return new StringFunction1(readSingleArgument(), StringFunction1.UTF8TOSTRING); |
| 4278 |
case "HEXTORAW": |
| 4279 |
return new StringFunction1(readSingleArgument(), StringFunction1.HEXTORAW); |
| 4280 |
case "RAWTOHEX": |
| 4281 |
return new StringFunction1(readSingleArgument(), StringFunction1.RAWTOHEX); |
| 4282 |
case "SPACE": |
| 4283 |
return new StringFunction1(readSingleArgument(), StringFunction1.SPACE); |
| 4284 |
case "QUOTE_IDENT": |
| 4285 |
return new StringFunction1(readSingleArgument(), StringFunction1.QUOTE_IDENT); |
| 4286 |
case "SUBSTRING": |
| 4287 |
return readSubstringFunction(); |
| 4288 |
case "TO_CHAR": { |
| 4289 |
Expression arg1 = readExpression(), arg2, arg3; |
| 4290 |
if (readIf(COMMA)) { |
| 4291 |
arg2 = readExpression(); |
| 4292 |
arg3 = readIf(COMMA) ? readExpression() : null; |
| 4293 |
} else { |
| 4294 |
arg3 = arg2 = null; |
| 4295 |
} |
| 4296 |
read(CLOSE_PAREN); |
| 4297 |
return new ToCharFunction(arg1, arg2, arg3); |
| 4298 |
} |
| 4299 |
case "REPEAT": |
| 4300 |
return new StringFunction2(readExpression(), readLastArgument(), StringFunction2.REPEAT); |
| 4301 |
case "CHAR_LENGTH": |
| 4302 |
case "CHARACTER_LENGTH": |
| 4303 |
case "LENGTH": |
| 4304 |
return new LengthFunction(readIfSingleArgument(), LengthFunction.CHAR_LENGTH); |
| 4305 |
case "OCTET_LENGTH": |
| 4306 |
return new LengthFunction(readIfSingleArgument(), LengthFunction.OCTET_LENGTH); |
| 4307 |
case "BIT_LENGTH": |
| 4308 |
return new LengthFunction(readIfSingleArgument(), LengthFunction.BIT_LENGTH); |
| 4309 |
case "TRIM": |
| 4310 |
return readTrimFunction(); |
| 4311 |
case "LTRIM": |
| 4312 |
return new TrimFunction(readExpression(), readIfArgument(), |
| 4313 |
TrimFunction.LEADING | TrimFunction.MULTI_CHARACTER); |
| 4314 |
case "RTRIM": |
| 4315 |
return new TrimFunction(readExpression(), readIfArgument(), |
| 4316 |
TrimFunction.TRAILING | TrimFunction.MULTI_CHARACTER); |
| 4317 |
case "BTRIM": |
| 4318 |
return new TrimFunction(readExpression(), readIfArgument(), |
| 4319 |
TrimFunction.LEADING | TrimFunction.TRAILING | TrimFunction.MULTI_CHARACTER); |
| 4320 |
case "REGEXP_LIKE": |
| 4321 |
return readParameters(new RegexpFunction(RegexpFunction.REGEXP_LIKE)); |
| 4322 |
case "REGEXP_REPLACE": |
| 4323 |
return readParameters(new RegexpFunction(RegexpFunction.REGEXP_REPLACE)); |
| 4324 |
case "REGEXP_SUBSTR": |
| 4325 |
return readParameters(new RegexpFunction(RegexpFunction.REGEXP_SUBSTR)); |
| 4326 |
case "XMLATTR": |
| 4327 |
return readParameters(new XMLFunction(XMLFunction.XMLATTR)); |
| 4328 |
case "XMLCDATA": |
| 4329 |
return readParameters(new XMLFunction(XMLFunction.XMLCDATA)); |
| 4330 |
case "XMLCOMMENT": |
| 4331 |
return readParameters(new XMLFunction(XMLFunction.XMLCOMMENT)); |
| 4332 |
case "XMLNODE": |
| 4333 |
return readParameters(new XMLFunction(XMLFunction.XMLNODE)); |
| 4334 |
case "XMLSTARTDOC": |
| 4335 |
return readParameters(new XMLFunction(XMLFunction.XMLSTARTDOC)); |
| 4336 |
case "XMLTEXT": |
| 4337 |
return readParameters(new XMLFunction(XMLFunction.XMLTEXT)); |
| 4338 |
case "TRIM_ARRAY": |
| 4339 |
return new ArrayFunction(readExpression(), readLastArgument(), null, ArrayFunction.TRIM_ARRAY); |
| 4340 |
case "ARRAY_CONTAINS": |
| 4341 |
return new ArrayFunction(readExpression(), readLastArgument(), null, ArrayFunction.ARRAY_CONTAINS); |
| 4342 |
case "ARRAY_SLICE": |
| 4343 |
return new ArrayFunction(readExpression(), readNextArgument(), readLastArgument(), |
| 4344 |
ArrayFunction.ARRAY_SLICE); |
| 4345 |
case "COMPRESS": |
| 4346 |
return new CompressFunction(readExpression(), readIfArgument(), CompressFunction.COMPRESS); |
| 4347 |
case "EXPAND": |
| 4348 |
return new CompressFunction(readSingleArgument(), null, CompressFunction.EXPAND); |
| 4349 |
case "SOUNDEX": |
| 4350 |
return new SoundexFunction(readSingleArgument(), null, SoundexFunction.SOUNDEX); |
| 4351 |
case "DIFFERENCE": |
| 4352 |
return new SoundexFunction(readExpression(), readLastArgument(), SoundexFunction.DIFFERENCE); |
| 4353 |
case "JSON_OBJECT": { |
| 4354 |
JsonConstructorFunction function = new JsonConstructorFunction(false); |
| 4355 |
if (currentTokenType != CLOSE_PAREN && !readJsonObjectFunctionFlags(function, false)) { |
| 4356 |
do { |
| 4357 |
boolean withKey = readIf(KEY); |
| 4358 |
function.addParameter(readExpression()); |
| 4359 |
if (withKey) { |
| 4360 |
read(VALUE); |
| 4361 |
} else { |
| 4362 |
if (!(readIf(VALUE) || |
| 4363 |
(database.getMode().acceptsCommaAsJsonKeyValueSeparator && readIf(COMMA)))) { |
| 4364 |
read(COLON); |
| 4365 |
} |
| 4366 |
} |
| 4367 |
function.addParameter(readExpression()); |
| 4368 |
} while (readIf(COMMA)); |
| 4369 |
readJsonObjectFunctionFlags(function, false); |
| 4370 |
} |
| 4371 |
read(CLOSE_PAREN); |
| 4372 |
function.doneWithParameters(); |
| 4373 |
return function; |
| 4374 |
} |
| 4375 |
case "JSON_ARRAY": { |
| 4376 |
JsonConstructorFunction function = new JsonConstructorFunction(true); |
| 4377 |
function.setFlags(JsonConstructorUtils.JSON_ABSENT_ON_NULL); |
| 4378 |
if (currentTokenType != CLOSE_PAREN && !readJsonObjectFunctionFlags(function, true)) { |
| 4379 |
do { |
| 4380 |
function.addParameter(readExpression()); |
| 4381 |
} while (readIf(COMMA)); |
| 4382 |
readJsonObjectFunctionFlags(function, true); |
| 4383 |
} |
| 4384 |
read(CLOSE_PAREN); |
| 4385 |
function.doneWithParameters(); |
| 4386 |
return function; |
| 4387 |
} |
| 4388 |
case "ENCRYPT": |
| 4389 |
return new CryptFunction(readExpression(), readNextArgument(), readLastArgument(), CryptFunction.ENCRYPT); |
| 4390 |
case "DECRYPT": |
| 4391 |
return new CryptFunction(readExpression(), readNextArgument(), readLastArgument(), CryptFunction.DECRYPT); |
| 4392 |
case "COALESCE": |
| 4393 |
return readCoalesceFunction(CoalesceFunction.COALESCE); |
| 4394 |
case "GREATEST": |
| 4395 |
return readCoalesceFunction(CoalesceFunction.GREATEST); |
| 4396 |
case "LEAST": |
| 4397 |
return readCoalesceFunction(CoalesceFunction.LEAST); |
| 4398 |
case "NULLIF": |
| 4399 |
return new NullIfFunction(readExpression(), readLastArgument()); |
| 4400 |
case "CONCAT": |
| 4401 |
return readConcatFunction(ConcatFunction.CONCAT); |
| 4402 |
case "CONCAT_WS": |
| 4403 |
return readConcatFunction(ConcatFunction.CONCAT_WS); |
| 4404 |
case "HASH": |
| 4405 |
return new HashFunction(readExpression(), readNextArgument(), readIfArgument(), HashFunction.HASH); |
| 4406 |
case "ORA_HASH": { |
| 4407 |
Expression arg1 = readExpression(); |
| 4408 |
if (readIfMore()) { |
| 4409 |
return new HashFunction(arg1, readExpression(), readIfArgument(), HashFunction.ORA_HASH); |
| 4410 |
} |
| 4411 |
return new HashFunction(arg1, HashFunction.ORA_HASH); |
| 4412 |
} |
| 4413 |
case "RAND": |
| 4414 |
case "RANDOM": |
| 4415 |
return new RandFunction(readIfSingleArgument(), RandFunction.RAND); |
| 4416 |
case "SECURE_RAND": |
| 4417 |
return new RandFunction(readSingleArgument(), RandFunction.SECURE_RAND); |
| 4418 |
case "RANDOM_UUID": |
| 4419 |
case "UUID": |
| 4420 |
read(CLOSE_PAREN); |
| 4421 |
return new RandFunction(null, RandFunction.RANDOM_UUID); |
| 4422 |
case "ABORT_SESSION": |
| 4423 |
return new SessionControlFunction(readIfSingleArgument(), SessionControlFunction.ABORT_SESSION); |
| 4424 |
case "CANCEL_SESSION": |
| 4425 |
return new SessionControlFunction(readIfSingleArgument(), SessionControlFunction.CANCEL_SESSION); |
| 4426 |
case "AUTOCOMMIT": |
| 4427 |
read(CLOSE_PAREN); |
| 4428 |
return new SysInfoFunction(SysInfoFunction.AUTOCOMMIT); |
| 4429 |
case "DATABASE_PATH": |
| 4430 |
read(CLOSE_PAREN); |
| 4431 |
return new SysInfoFunction(SysInfoFunction.DATABASE_PATH); |
| 4432 |
case "H2VERSION": |
| 4433 |
read(CLOSE_PAREN); |
| 4434 |
return new SysInfoFunction(SysInfoFunction.H2VERSION); |
| 4435 |
case "LOCK_MODE": |
| 4436 |
read(CLOSE_PAREN); |
| 4437 |
return new SysInfoFunction(SysInfoFunction.LOCK_MODE); |
| 4438 |
case "LOCK_TIMEOUT": |
| 4439 |
read(CLOSE_PAREN); |
| 4440 |
return new SysInfoFunction(SysInfoFunction.LOCK_TIMEOUT); |
| 4441 |
case "MEMORY_FREE": |
| 4442 |
read(CLOSE_PAREN); |
| 4443 |
return new SysInfoFunction(SysInfoFunction.MEMORY_FREE); |
| 4444 |
case "MEMORY_USED": |
| 4445 |
read(CLOSE_PAREN); |
| 4446 |
return new SysInfoFunction(SysInfoFunction.MEMORY_USED); |
| 4447 |
case "READONLY": |
| 4448 |
read(CLOSE_PAREN); |
| 4449 |
return new SysInfoFunction(SysInfoFunction.READONLY); |
| 4450 |
case "SESSION_ID": |
| 4451 |
read(CLOSE_PAREN); |
| 4452 |
return new SysInfoFunction(SysInfoFunction.SESSION_ID); |
| 4453 |
case "TRANSACTION_ID": |
| 4454 |
read(CLOSE_PAREN); |
| 4455 |
return new SysInfoFunction(SysInfoFunction.TRANSACTION_ID); |
| 4456 |
case "DISK_SPACE_USED": |
| 4457 |
return new TableInfoFunction(readIfSingleArgument(), null, TableInfoFunction.DISK_SPACE_USED); |
| 4458 |
case "ESTIMATED_ENVELOPE": |
| 4459 |
return new TableInfoFunction(readExpression(), readLastArgument(), TableInfoFunction.ESTIMATED_ENVELOPE); |
| 4460 |
case "FILE_READ": |
| 4461 |
return new FileFunction(readExpression(), readIfArgument(), FileFunction.FILE_READ); |
| 4462 |
case "FILE_WRITE": |
| 4463 |
return new FileFunction(readExpression(), readLastArgument(), FileFunction.FILE_WRITE); |
| 4464 |
case "DATA_TYPE_SQL": |
| 4465 |
return new DataTypeSQLFunction(readExpression(), readNextArgument(), readNextArgument(), |
| 4466 |
readLastArgument()); |
| 4467 |
case "DB_OBJECT_ID": |
| 4468 |
return new DBObjectFunction(readExpression(), readNextArgument(), readIfArgument(), |
| 4469 |
DBObjectFunction.DB_OBJECT_ID); |
| 4470 |
case "DB_OBJECT_SQL": |
| 4471 |
return new DBObjectFunction(readExpression(), readNextArgument(), readIfArgument(), |
| 4472 |
DBObjectFunction.DB_OBJECT_SQL); |
| 4473 |
case "CSVWRITE": |
| 4474 |
return readParameters(new CSVWriteFunction()); |
| 4475 |
case "SIGNAL": |
| 4476 |
return new SignalFunction(readExpression(), readLastArgument()); |
| 4477 |
case "TRUNCATE_VALUE": |
| 4478 |
return new TruncateValueFunction(readExpression(), readNextArgument(), readLastArgument()); |
| 4479 |
case "ZERO": |
| 4480 |
read(CLOSE_PAREN); |
| 4481 |
return ValueExpression.get(ValueInteger.get(0)); |
| 4482 |
case "PI": |
| 4483 |
read(CLOSE_PAREN); |
| 4484 |
return ValueExpression.get(ValueDouble.get(Math.PI)); |
| 4485 |
} |
| 4486 |
ModeFunction function = ModeFunction.getFunction(database, upperName); |
| 4487 |
return function != null ? readParameters(function) : null; |
| 4488 |
} |
| 4489 |
|
| 4490 |
private Expression readDateTimeFormatFunction(int function) { |
| 4491 |
DateTimeFormatFunction f = new DateTimeFormatFunction(function); |
| 4492 |
f.addParameter(readExpression()); |
| 4493 |
read(COMMA); |
| 4494 |
f.addParameter(readExpression()); |
| 4495 |
if (readIf(COMMA)) { |
| 4496 |
f.addParameter(readExpression()); |
| 4497 |
if (readIf(COMMA)) { |
| 4498 |
f.addParameter(readExpression()); |
| 4499 |
} |
| 4500 |
} |
| 4501 |
read(CLOSE_PAREN); |
| 4502 |
f.doneWithParameters(); |
| 4503 |
return f; |
| 4504 |
} |
| 4505 |
|
| 4506 |
private Expression readTrimFunction() { |
| 4507 |
int flags; |
| 4508 |
boolean needFrom = false; |
| 4509 |
if (readIf("LEADING")) { |
| 4510 |
flags = TrimFunction.LEADING; |
| 4511 |
needFrom = true; |
| 4512 |
} else if (readIf("TRAILING")) { |
| 4513 |
flags = TrimFunction.TRAILING; |
| 4514 |
needFrom = true; |
| 4515 |
} else { |
| 4516 |
needFrom = readIf("BOTH"); |
| 4517 |
flags = TrimFunction.LEADING | TrimFunction.TRAILING; |
| 4518 |
} |
| 4519 |
Expression from, space = null; |
| 4520 |
if (needFrom) { |
| 4521 |
if (!readIf(FROM)) { |
| 4522 |
space = readExpression(); |
| 4523 |
read(FROM); |
| 4524 |
} |
| 4525 |
from = readExpression(); |
| 4526 |
} else { |
| 4527 |
if (readIf(FROM)) { |
| 4528 |
from = readExpression(); |
| 4529 |
} else { |
| 4530 |
from = readExpression(); |
| 4531 |
if (readIf(FROM)) { |
| 4532 |
space = from; |
| 4533 |
from = readExpression(); |
| 4534 |
} else if (readIf(COMMA)) { |
| 4535 |
space = readExpression(); |
| 4536 |
} |
| 4537 |
} |
| 4538 |
} |
| 4539 |
read(CLOSE_PAREN); |
| 4540 |
return new TrimFunction(from, space, flags); |
| 4541 |
} |
| 4542 |
|
| 4543 |
private ArrayTableFunction readUnnestFunction() { |
| 4544 |
ArrayTableFunction f = new ArrayTableFunction(ArrayTableFunction.UNNEST); |
| 4545 |
ArrayList<Column> columns = Utils.newSmallArrayList(); |
| 4546 |
if (!readIf(CLOSE_PAREN)) { |
| 4547 |
int i = 0; |
| 4548 |
do { |
| 4549 |
Expression expr = readExpression(); |
| 4550 |
TypeInfo columnType = TypeInfo.TYPE_NULL; |
| 4551 |
boolean constant = expr.isConstant(); |
| 4552 |
if (constant || expr instanceof CastSpecification) { |
| 4553 |
if (constant) { |
| 4554 |
expr = expr.optimize(session); |
| 4555 |
} |
| 4556 |
TypeInfo exprType = expr.getType(); |
| 4557 |
switch (exprType.getValueType()) { |
| 4558 |
case Value.JSON: |
| 4559 |
columnType = TypeInfo.TYPE_JSON; |
| 4560 |
break; |
| 4561 |
case Value.ARRAY: |
| 4562 |
columnType = (TypeInfo) exprType.getExtTypeInfo(); |
| 4563 |
break; |
| 4564 |
} |
| 4565 |
} |
| 4566 |
f.addParameter(expr); |
| 4567 |
columns.add(new Column("C" + ++i, columnType)); |
| 4568 |
} while (readIfMore()); |
| 4569 |
} |
| 4570 |
if (readIf(WITH, "ORDINALITY")) { |
| 4571 |
columns.add(new Column("NORD", TypeInfo.TYPE_INTEGER)); |
| 4572 |
} |
| 4573 |
f.setColumns(columns); |
| 4574 |
f.doneWithParameters(); |
| 4575 |
return f; |
| 4576 |
} |
| 4577 |
|
| 4578 |
private ArrayTableFunction readTableFunction(int functionType) { |
| 4579 |
ArrayTableFunction f = new ArrayTableFunction(functionType); |
| 4580 |
ArrayList<Column> columns = Utils.newSmallArrayList(); |
| 4581 |
do { |
| 4582 |
columns.add(parseColumnWithType(readIdentifier())); |
| 4583 |
read(EQUAL); |
| 4584 |
f.addParameter(readExpression()); |
| 4585 |
} while (readIfMore()); |
| 4586 |
f.setColumns(columns); |
| 4587 |
f.doneWithParameters(); |
| 4588 |
return f; |
| 4589 |
} |
| 4590 |
|
| 4591 |
private Expression readSingleArgument() { |
| 4592 |
Expression arg = readExpression(); |
| 4593 |
read(CLOSE_PAREN); |
| 4594 |
return arg; |
| 4595 |
} |
| 4596 |
|
| 4597 |
private Expression readNextArgument() { |
| 4598 |
read(COMMA); |
| 4599 |
return readExpression(); |
| 4600 |
} |
| 4601 |
|
| 4602 |
private Expression readLastArgument() { |
| 4603 |
read(COMMA); |
| 4604 |
Expression arg = readExpression(); |
| 4605 |
read(CLOSE_PAREN); |
| 4606 |
return arg; |
| 4607 |
} |
| 4608 |
|
| 4609 |
private Expression readIfSingleArgument() { |
| 4610 |
Expression arg; |
| 4611 |
if (readIf(CLOSE_PAREN)) { |
| 4612 |
arg = null; |
| 4613 |
} else { |
| 4614 |
arg = readExpression(); |
| 4615 |
read(CLOSE_PAREN); |
| 4616 |
} |
| 4617 |
return arg; |
| 4618 |
} |
| 4619 |
|
| 4620 |
private Expression readIfArgument() { |
| 4621 |
Expression arg = readIf(COMMA) ? readExpression() : null; |
| 4622 |
read(CLOSE_PAREN); |
| 4623 |
return arg; |
| 4624 |
} |
| 4625 |
|
| 4626 |
private Expression readCoalesceFunction(int function) { |
| 4627 |
CoalesceFunction f = new CoalesceFunction(function); |
| 4628 |
f.addParameter(readExpression()); |
| 4629 |
while (readIfMore()) { |
| 4630 |
f.addParameter(readExpression()); |
| 4631 |
} |
| 4632 |
f.doneWithParameters(); |
| 4633 |
return f; |
| 4634 |
} |
| 4635 |
|
| 4636 |
private Expression readConcatFunction(int function) { |
| 4637 |
ConcatFunction f = new ConcatFunction(function); |
| 4638 |
f.addParameter(readExpression()); |
| 4639 |
f.addParameter(readNextArgument()); |
| 4640 |
if (function == ConcatFunction.CONCAT_WS) { |
| 4641 |
f.addParameter(readNextArgument()); |
| 4642 |
} |
| 4643 |
while (readIfMore()) { |
| 4644 |
f.addParameter(readExpression()); |
| 4645 |
} |
| 4646 |
f.doneWithParameters(); |
| 4647 |
return f; |
| 4648 |
} |
| 4649 |
|
| 4650 |
private Expression readSubstringFunction() { |
| 4651 |
// Standard variants are: |
| 4652 |
// SUBSTRING(X FROM 1) |
| 4653 |
// SUBSTRING(X FROM 1 FOR 1) |
| 4654 |
// Different non-standard variants include: |
| 4655 |
// SUBSTRING(X,1) |
| 4656 |
// SUBSTRING(X,1,1) |
| 4657 |
// SUBSTRING(X FOR 1) -- Postgres |
| 4658 |
SubstringFunction function = new SubstringFunction(); |
| 4659 |
function.addParameter(readExpression()); |
| 4660 |
if (readIf(FROM)) { |
| 4661 |
function.addParameter(readExpression()); |
| 4662 |
if (readIf(FOR)) { |
| 4663 |
function.addParameter(readExpression()); |
| 4664 |
} |
| 4665 |
} else if (readIf(FOR)) { |
| 4666 |
function.addParameter(ValueExpression.get(ValueInteger.get(1))); |
| 4667 |
function.addParameter(readExpression()); |
| 4668 |
} else { |
| 4669 |
read(COMMA); |
| 4670 |
function.addParameter(readExpression()); |
| 4671 |
if (readIf(COMMA)) { |
| 4672 |
function.addParameter(readExpression()); |
| 4673 |
} |
| 4674 |
} |
| 4675 |
read(CLOSE_PAREN); |
| 4676 |
function.doneWithParameters(); |
| 4677 |
return function; |
| 4678 |
} |
| 4679 |
|
| 4680 |
private int readDateTimeField() { |
| 4681 |
int field = -1; |
| 4682 |
switch (currentTokenType) { |
| 4683 |
case IDENTIFIER: |
| 4684 |
if (!token.isQuoted()) { |
| 4685 |
field = DateTimeFunction.getField(currentToken); |
| 4686 |
} |
| 4687 |
break; |
| 4688 |
case LITERAL: |
| 4689 |
if (token.value(session).getValueType() == Value.VARCHAR) { |
| 4690 |
field = DateTimeFunction.getField(token.value(session).getString()); |
| 4691 |
} |
| 4692 |
break; |
| 4693 |
case YEAR: |
| 4694 |
field = DateTimeFunction.YEAR; |
| 4695 |
break; |
| 4696 |
case MONTH: |
| 4697 |
field = DateTimeFunction.MONTH; |
| 4698 |
break; |
| 4699 |
case DAY: |
| 4700 |
field = DateTimeFunction.DAY; |
| 4701 |
break; |
| 4702 |
case HOUR: |
| 4703 |
field = DateTimeFunction.HOUR; |
| 4704 |
break; |
| 4705 |
case MINUTE: |
| 4706 |
field = DateTimeFunction.MINUTE; |
| 4707 |
break; |
| 4708 |
case SECOND: |
| 4709 |
field = DateTimeFunction.SECOND; |
| 4710 |
} |
| 4711 |
if (field < 0) { |
| 4712 |
addExpected("date-time field"); |
| 4713 |
throw getSyntaxError(); |
| 4714 |
} |
| 4715 |
read(); |
| 4716 |
return field; |
| 4717 |
} |
| 4718 |
|
| 4719 |
private WindowFunction readWindowFunction(String name) { |
| 4720 |
WindowFunctionType type = WindowFunctionType.get(name); |
| 4721 |
if (type == null) { |
| 4722 |
return null; |
| 4723 |
} |
| 4724 |
if (currentSelect == null) { |
| 4725 |
throw getSyntaxError(); |
| 4726 |
} |
| 4727 |
int numArgs = WindowFunction.getMinArgumentCount(type); |
| 4728 |
Expression[] args = null; |
| 4729 |
if (numArgs > 0) { |
| 4730 |
// There is no functions with numArgs == 0 && numArgsMax > 0 |
| 4731 |
int numArgsMax = WindowFunction.getMaxArgumentCount(type); |
| 4732 |
args = new Expression[numArgsMax]; |
| 4733 |
if (numArgs == numArgsMax) { |
| 4734 |
for (int i = 0; i < numArgs; i++) { |
| 4735 |
if (i > 0) { |
| 4736 |
read(COMMA); |
| 4737 |
} |
| 4738 |
args[i] = readExpression(); |
| 4739 |
} |
| 4740 |
} else { |
| 4741 |
int i = 0; |
| 4742 |
while (i < numArgsMax) { |
| 4743 |
if (i > 0 && !readIf(COMMA)) { |
| 4744 |
break; |
| 4745 |
} |
| 4746 |
args[i] = readExpression(); |
| 4747 |
i++; |
| 4748 |
} |
| 4749 |
if (i < numArgs) { |
| 4750 |
throw getSyntaxError(); |
| 4751 |
} |
| 4752 |
if (i != numArgsMax) { |
| 4753 |
args = Arrays.copyOf(args, i); |
| 4754 |
} |
| 4755 |
} |
| 4756 |
} |
| 4757 |
read(CLOSE_PAREN); |
| 4758 |
WindowFunction function = new WindowFunction(type, currentSelect, args); |
| 4759 |
switch (type) { |
| 4760 |
case NTH_VALUE: |
| 4761 |
readFromFirstOrLast(function); |
| 4762 |
//$FALL-THROUGH$ |
| 4763 |
case LEAD: |
| 4764 |
case LAG: |
| 4765 |
case FIRST_VALUE: |
| 4766 |
case LAST_VALUE: |
| 4767 |
readRespectOrIgnoreNulls(function); |
| 4768 |
//$FALL-THROUGH$ |
| 4769 |
default: |
| 4770 |
// Avoid warning |
| 4771 |
} |
| 4772 |
readOver(function); |
| 4773 |
return function; |
| 4774 |
} |
| 4775 |
|
| 4776 |
private void readFromFirstOrLast(WindowFunction function) { |
| 4777 |
if (readIf(FROM, "LAST")) { |
| 4778 |
function.setFromLast(true); |
| 4779 |
} else { |
| 4780 |
readIf(FROM, "FIRST"); |
| 4781 |
} |
| 4782 |
} |
| 4783 |
|
| 4784 |
private void readRespectOrIgnoreNulls(WindowFunction function) { |
| 4785 |
if (readIf("IGNORE", "NULLS")) { |
| 4786 |
function.setIgnoreNulls(true); |
| 4787 |
} else { |
| 4788 |
readIf("RESPECT", "NULLS"); |
| 4789 |
} |
| 4790 |
} |
| 4791 |
|
| 4792 |
private boolean readJsonObjectFunctionFlags(ExpressionWithFlags function, boolean forArray) { |
| 4793 |
boolean result = false; |
| 4794 |
int flags = function.getFlags(); |
| 4795 |
if (readIf(NULL, ON, NULL)) { |
| 4796 |
flags &= ~JsonConstructorUtils.JSON_ABSENT_ON_NULL; |
| 4797 |
result = true; |
| 4798 |
} else if (readIf("ABSENT", ON, NULL)) { |
| 4799 |
flags |= JsonConstructorUtils.JSON_ABSENT_ON_NULL; |
| 4800 |
result = true; |
| 4801 |
} |
| 4802 |
if (!forArray) { |
| 4803 |
if (readIf(WITH, UNIQUE, "KEYS")) { |
| 4804 |
flags |= JsonConstructorUtils.JSON_WITH_UNIQUE_KEYS; |
| 4805 |
result = true; |
| 4806 |
} else if (readIf("WITHOUT", UNIQUE, "KEYS")) { |
| 4807 |
flags &= ~JsonConstructorUtils.JSON_WITH_UNIQUE_KEYS; |
| 4808 |
result = true; |
| 4809 |
} |
| 4810 |
} |
| 4811 |
if (result) { |
| 4812 |
function.setFlags(flags); |
| 4813 |
} |
| 4814 |
return result; |
| 4815 |
} |
| 4816 |
|
| 4817 |
private Expression readKeywordCompatibilityFunctionOrColumn() { |
| 4818 |
boolean nonKeyword = nonKeywords != null && nonKeywords.get(currentTokenType); |
| 4819 |
String name = currentToken; |
| 4820 |
read(); |
| 4821 |
if (readIf(OPEN_PAREN)) { |
| 4822 |
return readCompatibilityFunction(upperName(name)); |
| 4823 |
} else if (nonKeyword) { |
| 4824 |
return readIf(DOT) ? readTermObjectDot(name) : new ExpressionColumn(database, null, null, name); |
| 4825 |
} |
| 4826 |
throw getSyntaxError(); |
| 4827 |
} |
| 4828 |
|
| 4829 |
private Expression readCurrentDateTimeValueFunction(int function, boolean hasParen, String name) { |
| 4830 |
int scale = -1; |
| 4831 |
if (hasParen) { |
| 4832 |
if (function != CurrentDateTimeValueFunction.CURRENT_DATE && currentTokenType != CLOSE_PAREN) { |
| 4833 |
scale = readInt(); |
| 4834 |
if (scale < 0 || scale > ValueTime.MAXIMUM_SCALE) { |
| 4835 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 4836 |
/* compile-time constant */ "" + ValueTime.MAXIMUM_SCALE); |
| 4837 |
} |
| 4838 |
} |
| 4839 |
read(CLOSE_PAREN); |
| 4840 |
} |
| 4841 |
if (database.isAllowBuiltinAliasOverride()) { |
| 4842 |
FunctionAlias functionAlias = database.getSchema(session.getCurrentSchemaName()) |
| 4843 |
.findFunction(name != null ? name : CurrentDateTimeValueFunction.getName(function)); |
| 4844 |
if (functionAlias != null) { |
| 4845 |
return new JavaFunction(functionAlias, |
| 4846 |
scale >= 0 ? new Expression[] { ValueExpression.get(ValueInteger.get(scale)) } |
| 4847 |
: new Expression[0]); |
| 4848 |
} |
| 4849 |
} |
| 4850 |
return new CurrentDateTimeValueFunction(function, scale); |
| 4851 |
} |
| 4852 |
|
| 4853 |
private Expression readIfWildcardRowidOrSequencePseudoColumn(String schema, String objectName) { |
| 4854 |
if (readIf(ASTERISK)) { |
| 4855 |
return parseWildcard(schema, objectName); |
| 4856 |
} |
| 4857 |
if (readIf(_ROWID_)) { |
| 4858 |
return new ExpressionColumn(database, schema, objectName); |
| 4859 |
} |
| 4860 |
if (database.getMode().nextvalAndCurrvalPseudoColumns) { |
| 4861 |
return readIfSequencePseudoColumn(schema, objectName); |
| 4862 |
} |
| 4863 |
return null; |
| 4864 |
} |
| 4865 |
|
| 4866 |
private Wildcard parseWildcard(String schema, String objectName) { |
| 4867 |
Wildcard wildcard = new Wildcard(schema, objectName); |
| 4868 |
if (readIf(EXCEPT, OPEN_PAREN)) { |
| 4869 |
ArrayList<ExpressionColumn> exceptColumns = Utils.newSmallArrayList(); |
| 4870 |
do { |
| 4871 |
String s = null, t = null; |
| 4872 |
String name = readIdentifier(); |
| 4873 |
if (readIf(DOT)) { |
| 4874 |
t = name; |
| 4875 |
name = readIdentifier(); |
| 4876 |
if (readIf(DOT)) { |
| 4877 |
s = t; |
| 4878 |
t = name; |
| 4879 |
name = readIdentifier(); |
| 4880 |
if (readIf(DOT)) { |
| 4881 |
checkDatabaseName(s); |
| 4882 |
s = t; |
| 4883 |
t = name; |
| 4884 |
name = readIdentifier(); |
| 4885 |
} |
| 4886 |
} |
| 4887 |
} |
| 4888 |
exceptColumns.add(new ExpressionColumn(database, s, t, name)); |
| 4889 |
} while (readIfMore()); |
| 4890 |
wildcard.setExceptColumns(exceptColumns); |
| 4891 |
} |
| 4892 |
return wildcard; |
| 4893 |
} |
| 4894 |
|
| 4895 |
private SequenceValue readIfSequencePseudoColumn(String schema, String objectName) { |
| 4896 |
if (schema == null) { |
| 4897 |
schema = session.getCurrentSchemaName(); |
| 4898 |
} |
| 4899 |
if (isToken("NEXTVAL")) { |
| 4900 |
Sequence sequence = findSequence(schema, objectName); |
| 4901 |
if (sequence != null) { |
| 4902 |
read(); |
| 4903 |
return new SequenceValue(sequence, getCurrentPreparedOrSelect()); |
| 4904 |
} |
| 4905 |
} else if (isToken("CURRVAL")) { |
| 4906 |
Sequence sequence = findSequence(schema, objectName); |
| 4907 |
if (sequence != null) { |
| 4908 |
read(); |
| 4909 |
return new SequenceValue(sequence); |
| 4910 |
} |
| 4911 |
} |
| 4912 |
return null; |
| 4913 |
} |
| 4914 |
|
| 4915 |
private Expression readTermObjectDot(String objectName) { |
| 4916 |
Expression expr = readIfWildcardRowidOrSequencePseudoColumn(null, objectName); |
| 4917 |
if (expr != null) { |
| 4918 |
return expr; |
| 4919 |
} |
| 4920 |
String name = readIdentifier(); |
| 4921 |
if (readIf(OPEN_PAREN)) { |
| 4922 |
return readFunction(database.getSchema(objectName), name); |
| 4923 |
} else if (readIf(DOT)) { |
| 4924 |
String schema = objectName; |
| 4925 |
objectName = name; |
| 4926 |
expr = readIfWildcardRowidOrSequencePseudoColumn(schema, objectName); |
| 4927 |
if (expr != null) { |
| 4928 |
return expr; |
| 4929 |
} |
| 4930 |
name = readIdentifier(); |
| 4931 |
if (readIf(OPEN_PAREN)) { |
| 4932 |
checkDatabaseName(schema); |
| 4933 |
return readFunction(database.getSchema(objectName), name); |
| 4934 |
} else if (readIf(DOT)) { |
| 4935 |
checkDatabaseName(schema); |
| 4936 |
schema = objectName; |
| 4937 |
objectName = name; |
| 4938 |
expr = readIfWildcardRowidOrSequencePseudoColumn(schema, objectName); |
| 4939 |
if (expr != null) { |
| 4940 |
return expr; |
| 4941 |
} |
| 4942 |
name = readIdentifier(); |
| 4943 |
} |
| 4944 |
return new ExpressionColumn(database, schema, objectName, name); |
| 4945 |
} |
| 4946 |
return new ExpressionColumn(database, null, objectName, name); |
| 4947 |
} |
| 4948 |
|
| 4949 |
private void checkDatabaseName(String databaseName) { |
| 4950 |
if (!database.getIgnoreCatalogs() && !equalsToken(database.getShortName(), databaseName)) { |
| 4951 |
throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, databaseName); |
| 4952 |
} |
| 4953 |
} |
| 4954 |
|
| 4955 |
private Parameter readParameter() { |
| 4956 |
int index = ((Token.ParameterToken) token).index() - 1; |
| 4957 |
read(); |
| 4958 |
usedParameters.set(index); |
| 4959 |
return parameters.get(index); |
| 4960 |
} |
| 4961 |
|
| 4962 |
private BitSet initParametersScope() { |
| 4963 |
BitSet outerUsedParameters = usedParameters; |
| 4964 |
usedParameters = new BitSet(); |
| 4965 |
return outerUsedParameters; |
| 4966 |
} |
| 4967 |
|
| 4968 |
private ArrayList<Parameter> getUsedParameters(BitSet outerUsedParameters) { |
| 4969 |
BitSet innerUsedParameters = usedParameters; |
| 4970 |
int size = innerUsedParameters.cardinality(); |
| 4971 |
ArrayList<Parameter> params = new ArrayList<>(size); |
| 4972 |
if (size > 0) { |
| 4973 |
for (int i = -1; (i = innerUsedParameters.nextSetBit(i + 1)) >= 0;) { |
| 4974 |
params.add(parameters.get(i)); |
| 4975 |
} |
| 4976 |
} |
| 4977 |
outerUsedParameters.or(innerUsedParameters); |
| 4978 |
usedParameters = outerUsedParameters; |
| 4979 |
return params; |
| 4980 |
} |
| 4981 |
|
| 4982 |
private Expression readTerm() { |
| 4983 |
Expression r = currentTokenType == IDENTIFIER ? readTermWithIdentifier() : readTermWithoutIdentifier(); |
| 4984 |
for (;;) { |
| 4985 |
if (readIf(OPEN_BRACKET)) { |
| 4986 |
r = new ArrayElementReference(r, readExpression()); |
| 4987 |
read(CLOSE_BRACKET); |
| 4988 |
continue; |
| 4989 |
} |
| 4990 |
if (readIf(DOT)) { |
| 4991 |
r = new FieldReference(r, readIdentifier()); |
| 4992 |
continue; |
| 4993 |
} |
| 4994 |
if (readIf(COLON_COLON)) { |
| 4995 |
r = readColonColonAfterTerm(r); |
| 4996 |
continue; |
| 4997 |
} |
| 4998 |
TypeInfo ti = readIntervalQualifier(); |
| 4999 |
if (ti != null) { |
| 5000 |
r = new CastSpecification(r, ti); |
| 5001 |
continue; |
| 5002 |
} |
| 5003 |
int index = tokenIndex; |
| 5004 |
if (readIf("AT")) { |
| 5005 |
if (readIf("TIME", "ZONE")) { |
| 5006 |
r = new TimeZoneOperation(r, readExpression()); |
| 5007 |
continue; |
| 5008 |
} else if (readIf("LOCAL")) { |
| 5009 |
r = new TimeZoneOperation(r, null); |
| 5010 |
continue; |
| 5011 |
} else { |
| 5012 |
setTokenIndex(index); |
| 5013 |
} |
| 5014 |
} else if (readIf("FORMAT")) { |
| 5015 |
if (readIf("JSON")) { |
| 5016 |
r = new Format(r, FormatEnum.JSON); |
| 5017 |
continue; |
| 5018 |
} else { |
| 5019 |
setTokenIndex(index); |
| 5020 |
} |
| 5021 |
} |
| 5022 |
break; |
| 5023 |
} |
| 5024 |
return r; |
| 5025 |
} |
| 5026 |
|
| 5027 |
private Expression readTermWithoutIdentifier() { |
| 5028 |
Expression r; |
| 5029 |
switch (currentTokenType) { |
| 5030 |
case AT: |
| 5031 |
read(); |
| 5032 |
r = new Variable(session, readIdentifier()); |
| 5033 |
if (readIf(COLON_EQ)) { |
| 5034 |
r = new SetFunction(r, readExpression()); |
| 5035 |
} |
| 5036 |
break; |
| 5037 |
case PARAMETER: |
| 5038 |
r = readParameter(); |
| 5039 |
break; |
| 5040 |
case TABLE: |
| 5041 |
case SELECT: |
| 5042 |
case WITH: |
| 5043 |
r = new Subquery(parseQuery()); |
| 5044 |
break; |
| 5045 |
case MINUS_SIGN: |
| 5046 |
read(); |
| 5047 |
if (currentTokenType == LITERAL) { |
| 5048 |
r = ValueExpression.get(token.value(session).negate()); |
| 5049 |
int rType = r.getType().getValueType(); |
| 5050 |
if (rType == Value.BIGINT && |
| 5051 |
r.getValue(session).getLong() == Integer.MIN_VALUE) { |
| 5052 |
// convert Integer.MIN_VALUE to type 'int' |
| 5053 |
// (Integer.MAX_VALUE+1 is of type 'long') |
| 5054 |
r = ValueExpression.get(ValueInteger.get(Integer.MIN_VALUE)); |
| 5055 |
} else if (rType == Value.NUMERIC && |
| 5056 |
r.getValue(session).getBigDecimal().compareTo(Value.MIN_LONG_DECIMAL) == 0) { |
| 5057 |
// convert Long.MIN_VALUE to type 'long' |
| 5058 |
// (Long.MAX_VALUE+1 is of type 'decimal') |
| 5059 |
r = ValueExpression.get(ValueBigint.MIN); |
| 5060 |
} |
| 5061 |
read(); |
| 5062 |
} else { |
| 5063 |
r = new UnaryOperation(readTerm()); |
| 5064 |
} |
| 5065 |
break; |
| 5066 |
case PLUS_SIGN: |
| 5067 |
read(); |
| 5068 |
r = readTerm(); |
| 5069 |
break; |
| 5070 |
case OPEN_PAREN: |
| 5071 |
read(); |
| 5072 |
if (readIf(CLOSE_PAREN)) { |
| 5073 |
r = ValueExpression.get(ValueRow.EMPTY); |
| 5074 |
} else if (isQuery()) { |
| 5075 |
r = new Subquery(parseQuery()); |
| 5076 |
read(CLOSE_PAREN); |
| 5077 |
} else { |
| 5078 |
r = readExpression(); |
| 5079 |
if (readIfMore()) { |
| 5080 |
ArrayList<Expression> list = Utils.newSmallArrayList(); |
| 5081 |
list.add(r); |
| 5082 |
do { |
| 5083 |
list.add(readExpression()); |
| 5084 |
} while (readIfMore()); |
| 5085 |
r = new ExpressionList(list.toArray(new Expression[0]), false); |
| 5086 |
} else if (r instanceof BinaryOperation) { |
| 5087 |
BinaryOperation binaryOperation = (BinaryOperation) r; |
| 5088 |
if (binaryOperation.getOperationType() == OpType.MINUS) { |
| 5089 |
TypeInfo ti = readIntervalQualifier(); |
| 5090 |
if (ti != null) { |
| 5091 |
binaryOperation.setForcedType(ti); |
| 5092 |
} |
| 5093 |
} |
| 5094 |
} |
| 5095 |
} |
| 5096 |
break; |
| 5097 |
case ARRAY: |
| 5098 |
read(); |
| 5099 |
if (readIf(OPEN_BRACKET)) { |
| 5100 |
if (readIf(CLOSE_BRACKET)) { |
| 5101 |
r = ValueExpression.get(ValueArray.EMPTY); |
| 5102 |
} else { |
| 5103 |
ArrayList<Expression> list = Utils.newSmallArrayList(); |
| 5104 |
do { |
| 5105 |
list.add(readExpression()); |
| 5106 |
} while (readIf(COMMA)); |
| 5107 |
read(CLOSE_BRACKET); |
| 5108 |
r = new ExpressionList(list.toArray(new Expression[0]), true); |
| 5109 |
} |
| 5110 |
} else { |
| 5111 |
read(OPEN_PAREN); |
| 5112 |
Query q = parseQuery(); |
| 5113 |
read(CLOSE_PAREN); |
| 5114 |
r = new ArrayConstructorByQuery(q); |
| 5115 |
} |
| 5116 |
break; |
| 5117 |
case INTERVAL: |
| 5118 |
read(); |
| 5119 |
r = readInterval(); |
| 5120 |
break; |
| 5121 |
case ROW: |
| 5122 |
if (readIf(ROW, OPEN_PAREN)) { |
| 5123 |
if (readIf(CLOSE_PAREN)) { |
| 5124 |
r = ValueExpression.get(ValueRow.EMPTY); |
| 5125 |
} else { |
| 5126 |
ArrayList<Expression> list = Utils.newSmallArrayList(); |
| 5127 |
do { |
| 5128 |
list.add(readExpression()); |
| 5129 |
} while (readIfMore()); |
| 5130 |
r = new ExpressionList(list.toArray(new Expression[0]), false); |
| 5131 |
} |
| 5132 |
} else { |
| 5133 |
r = readTermWithIdentifier(); |
| 5134 |
} |
| 5135 |
break; |
| 5136 |
case TRUE: |
| 5137 |
read(); |
| 5138 |
r = ValueExpression.TRUE; |
| 5139 |
break; |
| 5140 |
case FALSE: |
| 5141 |
read(); |
| 5142 |
r = ValueExpression.FALSE; |
| 5143 |
break; |
| 5144 |
case UNKNOWN: |
| 5145 |
read(); |
| 5146 |
r = TypedValueExpression.UNKNOWN; |
| 5147 |
break; |
| 5148 |
case ROWNUM: |
| 5149 |
read(); |
| 5150 |
if (readIf(OPEN_PAREN)) { |
| 5151 |
read(CLOSE_PAREN); |
| 5152 |
} |
| 5153 |
if (currentSelect == null && currentPrepared == null) { |
| 5154 |
throw getSyntaxError(); |
| 5155 |
} |
| 5156 |
r = new Rownum(getCurrentPreparedOrSelect()); |
| 5157 |
break; |
| 5158 |
case NULL: |
| 5159 |
read(); |
| 5160 |
r = ValueExpression.NULL; |
| 5161 |
break; |
| 5162 |
case _ROWID_: |
| 5163 |
read(); |
| 5164 |
r = new ExpressionColumn(database, null, null); |
| 5165 |
break; |
| 5166 |
case LITERAL: |
| 5167 |
r = ValueExpression.get(token.value(session)); |
| 5168 |
read(); |
| 5169 |
break; |
| 5170 |
case VALUES: |
| 5171 |
if (database.getMode().onDuplicateKeyUpdate) { |
| 5172 |
if (currentPrepared instanceof Insert) { |
| 5173 |
r = readOnDuplicateKeyValues(((Insert) currentPrepared).getTable(), null); |
| 5174 |
break; |
| 5175 |
} else if (currentPrepared instanceof Update) { |
| 5176 |
Update update = (Update) currentPrepared; |
| 5177 |
r = readOnDuplicateKeyValues(update.getTable(), update); |
| 5178 |
break; |
| 5179 |
} |
| 5180 |
} |
| 5181 |
r = new Subquery(parseQuery()); |
| 5182 |
break; |
| 5183 |
case CASE: |
| 5184 |
read(); |
| 5185 |
r = readCase(); |
| 5186 |
break; |
| 5187 |
case CAST: { |
| 5188 |
read(); |
| 5189 |
read(OPEN_PAREN); |
| 5190 |
Expression arg = readExpression(); |
| 5191 |
read(AS); |
| 5192 |
Column column = parseColumnWithType(null); |
| 5193 |
Expression template = readIf("FORMAT") ? readExpression() : null; |
| 5194 |
read(CLOSE_PAREN); |
| 5195 |
r = new CastSpecification(arg, column, template); |
| 5196 |
break; |
| 5197 |
} |
| 5198 |
case CURRENT_CATALOG: |
| 5199 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_CATALOG); |
| 5200 |
break; |
| 5201 |
case CURRENT_DATE: |
| 5202 |
read(); |
| 5203 |
r = readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_DATE, readIf(OPEN_PAREN), null); |
| 5204 |
break; |
| 5205 |
case CURRENT_PATH: |
| 5206 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_PATH); |
| 5207 |
break; |
| 5208 |
case CURRENT_ROLE: |
| 5209 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_ROLE); |
| 5210 |
break; |
| 5211 |
case CURRENT_SCHEMA: |
| 5212 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_SCHEMA); |
| 5213 |
break; |
| 5214 |
case CURRENT_TIME: |
| 5215 |
read(); |
| 5216 |
r = readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_TIME, readIf(OPEN_PAREN), null); |
| 5217 |
break; |
| 5218 |
case CURRENT_TIMESTAMP: |
| 5219 |
read(); |
| 5220 |
r = readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_TIMESTAMP, readIf(OPEN_PAREN), |
| 5221 |
null); |
| 5222 |
break; |
| 5223 |
case CURRENT_USER: |
| 5224 |
case USER: |
| 5225 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.CURRENT_USER); |
| 5226 |
break; |
| 5227 |
case SESSION_USER: |
| 5228 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.SESSION_USER); |
| 5229 |
break; |
| 5230 |
case SYSTEM_USER: |
| 5231 |
r = readCurrentGeneralValueSpecification(CurrentGeneralValueSpecification.SYSTEM_USER); |
| 5232 |
break; |
| 5233 |
case ANY: |
| 5234 |
case SOME: |
| 5235 |
read(); |
| 5236 |
read(OPEN_PAREN); |
| 5237 |
r = readAggregate(AggregateType.ANY, "ANY"); |
| 5238 |
break; |
| 5239 |
case DAY: |
| 5240 |
case HOUR: |
| 5241 |
case MINUTE: |
| 5242 |
case MONTH: |
| 5243 |
case SECOND: |
| 5244 |
case YEAR: |
| 5245 |
r = readKeywordCompatibilityFunctionOrColumn(); |
| 5246 |
break; |
| 5247 |
case LEFT: |
| 5248 |
r = readColumnIfNotFunction(); |
| 5249 |
if (r == null) { |
| 5250 |
r = new StringFunction2(readExpression(), readLastArgument(), StringFunction2.LEFT); |
| 5251 |
} |
| 5252 |
break; |
| 5253 |
case LOCALTIME: |
| 5254 |
read(); |
| 5255 |
r = readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIME, readIf(OPEN_PAREN), null); |
| 5256 |
break; |
| 5257 |
case LOCALTIMESTAMP: |
| 5258 |
read(); |
| 5259 |
r = readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIMESTAMP, readIf(OPEN_PAREN), // |
| 5260 |
null); |
| 5261 |
break; |
| 5262 |
case RIGHT: |
| 5263 |
r = readColumnIfNotFunction(); |
| 5264 |
if (r == null) { |
| 5265 |
r = new StringFunction2(readExpression(), readLastArgument(), StringFunction2.RIGHT); |
| 5266 |
} |
| 5267 |
break; |
| 5268 |
case SET: |
| 5269 |
r = readColumnIfNotFunction(); |
| 5270 |
if (r == null) { |
| 5271 |
r = readSetFunction(); |
| 5272 |
} |
| 5273 |
break; |
| 5274 |
case VALUE: |
| 5275 |
if (parseDomainConstraint) { |
| 5276 |
read(); |
| 5277 |
r = new DomainValueExpression(); |
| 5278 |
break; |
| 5279 |
} |
| 5280 |
//$FALL-THROUGH$ |
| 5281 |
default: |
| 5282 |
if (!isIdentifier()) { |
| 5283 |
throw getSyntaxError(); |
| 5284 |
} |
| 5285 |
r = readTermWithIdentifier(); |
| 5286 |
break; |
| 5287 |
} |
| 5288 |
return r; |
| 5289 |
} |
| 5290 |
|
| 5291 |
private Expression readTermWithIdentifier() { |
| 5292 |
Expression r; |
| 5293 |
String name = currentToken; |
| 5294 |
boolean quoted = token.isQuoted(); |
| 5295 |
read(); |
| 5296 |
if (readIf(OPEN_PAREN)) { |
| 5297 |
r = readFunction(null, name); |
| 5298 |
} else if (readIf(DOT)) { |
| 5299 |
r = readTermObjectDot(name); |
| 5300 |
} else if (quoted) { |
| 5301 |
r = new ExpressionColumn(database, null, null, name); |
| 5302 |
} else { |
| 5303 |
r = readTermWithIdentifier(name, quoted); |
| 5304 |
} |
| 5305 |
return r; |
| 5306 |
} |
| 5307 |
|
| 5308 |
private Expression readColonColonAfterTerm(Expression r) { |
| 5309 |
if (database.getMode().getEnum() == ModeEnum.PostgreSQL) { |
| 5310 |
// PostgreSQL compatibility |
| 5311 |
if (readIf("PG_CATALOG")) { |
| 5312 |
read(DOT); |
| 5313 |
} |
| 5314 |
if (readIf("REGCLASS")) { |
| 5315 |
return new Regclass(r); |
| 5316 |
} |
| 5317 |
} |
| 5318 |
return new CastSpecification(r, parseColumnWithType(null)); |
| 5319 |
} |
| 5320 |
|
| 5321 |
private Expression readCurrentGeneralValueSpecification(int specification) { |
| 5322 |
read(); |
| 5323 |
if (readIf(OPEN_PAREN)) { |
| 5324 |
read(CLOSE_PAREN); |
| 5325 |
} |
| 5326 |
return new CurrentGeneralValueSpecification(specification); |
| 5327 |
} |
| 5328 |
|
| 5329 |
private Expression readColumnIfNotFunction() { |
| 5330 |
boolean nonKeyword = nonKeywords != null && nonKeywords.get(currentTokenType); |
| 5331 |
String name = currentToken; |
| 5332 |
read(); |
| 5333 |
if (readIf(OPEN_PAREN)) { |
| 5334 |
return null; |
| 5335 |
} else if (nonKeyword) { |
| 5336 |
return readIf(DOT) ? readTermObjectDot(name) : new ExpressionColumn(database, null, null, name); |
| 5337 |
} |
| 5338 |
throw getSyntaxError(); |
| 5339 |
} |
| 5340 |
|
| 5341 |
private Expression readSetFunction() { |
| 5342 |
SetFunction function = new SetFunction(readExpression(), readLastArgument()); |
| 5343 |
if (database.isAllowBuiltinAliasOverride()) { |
| 5344 |
FunctionAlias functionAlias = database.getSchema(session.getCurrentSchemaName()).findFunction( |
| 5345 |
function.getName()); |
| 5346 |
if (functionAlias != null) { |
| 5347 |
return new JavaFunction(functionAlias, |
| 5348 |
new Expression[] { function.getSubexpression(0), function.getSubexpression(1) }); |
| 5349 |
} |
| 5350 |
} |
| 5351 |
return function; |
| 5352 |
} |
| 5353 |
|
| 5354 |
private Expression readOnDuplicateKeyValues(Table table, Update update) { |
| 5355 |
read(); |
| 5356 |
read(OPEN_PAREN); |
| 5357 |
Column c = readTableColumn(new TableFilter(session, table, null, rightsChecked, null, 0, null)); |
| 5358 |
read(CLOSE_PAREN); |
| 5359 |
return new OnDuplicateKeyValues(c, update); |
| 5360 |
} |
| 5361 |
|
| 5362 |
private Expression readTermWithIdentifier(String name, boolean quoted) { |
| 5363 |
/* |
| 5364 |
* Convert a-z to A-Z. This method is safe, because only A-Z |
| 5365 |
* characters are considered below. |
| 5366 |
* |
| 5367 |
* Unquoted identifier is never empty. |
| 5368 |
*/ |
| 5369 |
switch (name.charAt(0) & 0xffdf) { |
| 5370 |
case 'C': |
| 5371 |
if (equalsToken("CURRENT", name)) { |
| 5372 |
if (readIf(VALUE, FOR)) { |
| 5373 |
return new SequenceValue(readSequence()); |
| 5374 |
} |
| 5375 |
if (database.getMode().getEnum() == ModeEnum.DB2) { |
| 5376 |
return parseDB2SpecialRegisters(name); |
| 5377 |
} |
| 5378 |
} |
| 5379 |
break; |
| 5380 |
case 'D': |
| 5381 |
if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR && |
| 5382 |
(equalsToken("DATE", name) || equalsToken("D", name))) { |
| 5383 |
String date = token.value(session).getString(); |
| 5384 |
read(); |
| 5385 |
return ValueExpression.get(ValueDate.parse(date)); |
| 5386 |
} |
| 5387 |
break; |
| 5388 |
case 'E': |
| 5389 |
if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR // |
| 5390 |
&& equalsToken("E", name)) { |
| 5391 |
String text = token.value(session).getString(); |
| 5392 |
// the PostgreSQL ODBC driver uses |
| 5393 |
// LIKE E'PROJECT\\_DATA' instead of LIKE |
| 5394 |
// 'PROJECT\_DATA' |
| 5395 |
// N: SQL-92 "National Language" strings |
| 5396 |
text = StringUtils.replaceAll(text, "\\\\", "\\"); |
| 5397 |
read(); |
| 5398 |
return ValueExpression.get(ValueVarchar.get(text)); |
| 5399 |
} |
| 5400 |
break; |
| 5401 |
case 'G': |
| 5402 |
if (currentTokenType == LITERAL) { |
| 5403 |
int t = token.value(session).getValueType(); |
| 5404 |
if (t == Value.VARCHAR && equalsToken("GEOMETRY", name)) { |
| 5405 |
ValueExpression v = ValueExpression.get(ValueGeometry.get(token.value(session).getString())); |
| 5406 |
read(); |
| 5407 |
return v; |
| 5408 |
} else if (t == Value.VARBINARY && equalsToken("GEOMETRY", name)) { |
| 5409 |
ValueExpression v = ValueExpression |
| 5410 |
.get(ValueGeometry.getFromEWKB(token.value(session).getBytesNoCopy())); |
| 5411 |
read(); |
| 5412 |
return v; |
| 5413 |
} |
| 5414 |
} |
| 5415 |
break; |
| 5416 |
case 'J': |
| 5417 |
if (currentTokenType == LITERAL) { |
| 5418 |
int t = token.value(session).getValueType(); |
| 5419 |
if (t == Value.VARCHAR && equalsToken("JSON", name)) { |
| 5420 |
ValueExpression v = ValueExpression.get(ValueJson.fromJson(token.value(session).getString())); |
| 5421 |
read(); |
| 5422 |
return v; |
| 5423 |
} else if (t == Value.VARBINARY && equalsToken("JSON", name)) { |
| 5424 |
ValueExpression v = ValueExpression.get(ValueJson.fromJson(token.value(session).getBytesNoCopy())); |
| 5425 |
read(); |
| 5426 |
return v; |
| 5427 |
} |
| 5428 |
} |
| 5429 |
break; |
| 5430 |
case 'N': |
| 5431 |
if (equalsToken("NEXT", name)) { |
| 5432 |
if (readIf(VALUE, FOR)) { |
| 5433 |
return new SequenceValue(readSequence(), getCurrentPreparedOrSelect()); |
| 5434 |
} |
| 5435 |
} |
| 5436 |
break; |
| 5437 |
case 'T': |
| 5438 |
if (equalsToken("TIME", name)) { |
| 5439 |
if (readIf(WITH, "TIME", "ZONE")) { |
| 5440 |
if (currentTokenType != LITERAL || token.value(session).getValueType() != Value.VARCHAR) { |
| 5441 |
throw getSyntaxError(); |
| 5442 |
} |
| 5443 |
String time = token.value(session).getString(); |
| 5444 |
read(); |
| 5445 |
return ValueExpression.get(ValueTimeTimeZone.parse(time, session)); |
| 5446 |
} else { |
| 5447 |
boolean without = readIf("WITHOUT", "TIME", "ZONE"); |
| 5448 |
if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR) { |
| 5449 |
String time = token.value(session).getString(); |
| 5450 |
read(); |
| 5451 |
return ValueExpression.get(ValueTime.parse(time, session)); |
| 5452 |
} else if (without) { |
| 5453 |
throw getSyntaxError(); |
| 5454 |
} |
| 5455 |
} |
| 5456 |
} else if (equalsToken("TIMESTAMP", name)) { |
| 5457 |
if (readIf(WITH, "TIME", "ZONE")) { |
| 5458 |
if (currentTokenType != LITERAL || token.value(session).getValueType() != Value.VARCHAR) { |
| 5459 |
throw getSyntaxError(); |
| 5460 |
} |
| 5461 |
String timestamp = token.value(session).getString(); |
| 5462 |
read(); |
| 5463 |
return ValueExpression.get(ValueTimestampTimeZone.parse(timestamp, session)); |
| 5464 |
} else { |
| 5465 |
boolean without = readIf("WITHOUT", "TIME", "ZONE"); |
| 5466 |
if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR) { |
| 5467 |
String timestamp = token.value(session).getString(); |
| 5468 |
read(); |
| 5469 |
return ValueExpression.get(ValueTimestamp.parse(timestamp, session)); |
| 5470 |
} else if (without) { |
| 5471 |
throw getSyntaxError(); |
| 5472 |
} |
| 5473 |
} |
| 5474 |
} else if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR) { |
| 5475 |
if (equalsToken("T", name)) { |
| 5476 |
String time = token.value(session).getString(); |
| 5477 |
read(); |
| 5478 |
return ValueExpression.get(ValueTime.parse(time, session)); |
| 5479 |
} else if (equalsToken("TS", name)) { |
| 5480 |
String timestamp = token.value(session).getString(); |
| 5481 |
read(); |
| 5482 |
return ValueExpression.get(ValueTimestamp.parse(timestamp, session)); |
| 5483 |
} |
| 5484 |
} |
| 5485 |
break; |
| 5486 |
case 'U': |
| 5487 |
if (currentTokenType == LITERAL && token.value(session).getValueType() == Value.VARCHAR |
| 5488 |
&& equalsToken("UUID", name)) { |
| 5489 |
String uuid = token.value(session).getString(); |
| 5490 |
read(); |
| 5491 |
return ValueExpression.get(ValueUuid.get(uuid)); |
| 5492 |
} |
| 5493 |
break; |
| 5494 |
} |
| 5495 |
return new ExpressionColumn(database, null, null, name, quoted); |
| 5496 |
} |
| 5497 |
|
| 5498 |
private Prepared getCurrentPreparedOrSelect() { |
| 5499 |
Prepared p = currentPrepared; |
| 5500 |
return p != null ? p : currentSelect; |
| 5501 |
} |
| 5502 |
|
| 5503 |
private Expression readInterval() { |
| 5504 |
boolean negative = readIf(MINUS_SIGN); |
| 5505 |
if (!negative) { |
| 5506 |
readIf(PLUS_SIGN); |
| 5507 |
} |
| 5508 |
if (currentTokenType != LITERAL || token.value(session).getValueType() != Value.VARCHAR) { |
| 5509 |
addExpected("string"); |
| 5510 |
throw getSyntaxError(); |
| 5511 |
} |
| 5512 |
String s = token.value(session).getString(); |
| 5513 |
read(); |
| 5514 |
TypeInfo typeInfo = readIntervalQualifier(); |
| 5515 |
try { |
| 5516 |
ValueInterval interval = IntervalUtils.parseInterval( |
| 5517 |
IntervalQualifier.valueOf(typeInfo.getValueType() - Value.INTERVAL_YEAR), negative, s); |
| 5518 |
if (typeInfo.getDeclaredPrecision() != -1L || typeInfo.getDeclaredScale() != -1) { |
| 5519 |
return TypedValueExpression.get(interval.castTo(typeInfo, session), typeInfo); |
| 5520 |
} |
| 5521 |
return ValueExpression.get(interval); |
| 5522 |
} catch (Exception e) { |
| 5523 |
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s); |
| 5524 |
} |
| 5525 |
} |
| 5526 |
|
| 5527 |
private Expression parseDB2SpecialRegisters(String name) { |
| 5528 |
// Only "CURRENT" name is supported |
| 5529 |
if (readIf("TIMESTAMP")) { |
| 5530 |
if (readIf(WITH, "TIME", "ZONE")) { |
| 5531 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_TIMESTAMP, |
| 5532 |
readIf(OPEN_PAREN), null); |
| 5533 |
} |
| 5534 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIMESTAMP, readIf(OPEN_PAREN), |
| 5535 |
null); |
| 5536 |
} else if (readIf("TIME")) { |
| 5537 |
// Time with fractional seconds is not supported by DB2 |
| 5538 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIME, false, null); |
| 5539 |
} else if (readIf("DATE")) { |
| 5540 |
return readCurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_DATE, false, null); |
| 5541 |
} |
| 5542 |
// No match, parse CURRENT as a column |
| 5543 |
return new ExpressionColumn(database, null, null, name); |
| 5544 |
} |
| 5545 |
|
| 5546 |
private Expression readCase() { |
| 5547 |
Expression c; |
| 5548 |
if (readIf(WHEN)) { |
| 5549 |
SearchedCase searched = new SearchedCase(); |
| 5550 |
do { |
| 5551 |
Expression condition = readExpression(); |
| 5552 |
read("THEN"); |
| 5553 |
searched.addParameter(condition); |
| 5554 |
searched.addParameter(readExpression()); |
| 5555 |
} while (readIf(WHEN)); |
| 5556 |
if (readIf(ELSE)) { |
| 5557 |
searched.addParameter(readExpression()); |
| 5558 |
} |
| 5559 |
searched.doneWithParameters(); |
| 5560 |
c = searched; |
| 5561 |
} else { |
| 5562 |
Expression caseOperand = readExpression(); |
| 5563 |
read(WHEN); |
| 5564 |
SimpleCase.SimpleWhen when = readSimpleWhenClause(caseOperand), current = when; |
| 5565 |
while (readIf(WHEN)) { |
| 5566 |
SimpleCase.SimpleWhen next = readSimpleWhenClause(caseOperand); |
| 5567 |
current.setWhen(next); |
| 5568 |
current = next; |
| 5569 |
} |
| 5570 |
c = new SimpleCase(caseOperand, when, readIf(ELSE) ? readExpression() : null); |
| 5571 |
} |
| 5572 |
read(END); |
| 5573 |
return c; |
| 5574 |
} |
| 5575 |
|
| 5576 |
private SimpleCase.SimpleWhen readSimpleWhenClause(Expression caseOperand) { |
| 5577 |
Expression whenOperand = readWhenOperand(caseOperand); |
| 5578 |
if (readIf(COMMA)) { |
| 5579 |
ArrayList<Expression> operands = Utils.newSmallArrayList(); |
| 5580 |
operands.add(whenOperand); |
| 5581 |
do { |
| 5582 |
operands.add(readWhenOperand(caseOperand)); |
| 5583 |
} while (readIf(COMMA)); |
| 5584 |
read("THEN"); |
| 5585 |
return new SimpleCase.SimpleWhen(operands.toArray(new Expression[0]), readExpression()); |
| 5586 |
} |
| 5587 |
read("THEN"); |
| 5588 |
return new SimpleCase.SimpleWhen(whenOperand, readExpression()); |
| 5589 |
} |
| 5590 |
|
| 5591 |
private Expression readWhenOperand(Expression caseOperand) { |
| 5592 |
int backup = tokenIndex; |
| 5593 |
boolean not = readIf(NOT); |
| 5594 |
Expression whenOperand = readConditionRightHandSide(caseOperand, not, true); |
| 5595 |
if (whenOperand == null) { |
| 5596 |
if (not) { |
| 5597 |
setTokenIndex(backup); |
| 5598 |
} |
| 5599 |
whenOperand = readExpression(); |
| 5600 |
} |
| 5601 |
return whenOperand; |
| 5602 |
} |
| 5603 |
|
| 5604 |
private int readNonNegativeInt() { |
| 5605 |
int v = readInt(); |
| 5606 |
if (v < 0) { |
| 5607 |
throw DbException.getInvalidValueException("non-negative integer", v); |
| 5608 |
} |
| 5609 |
return v; |
| 5610 |
} |
| 5611 |
|
| 5612 |
private int readInt() { |
| 5613 |
boolean minus = false; |
| 5614 |
if (currentTokenType == MINUS_SIGN) { |
| 5615 |
minus = true; |
| 5616 |
read(); |
| 5617 |
} else if (currentTokenType == PLUS_SIGN) { |
| 5618 |
read(); |
| 5619 |
} |
| 5620 |
if (currentTokenType != LITERAL) { |
| 5621 |
throw DbException.getSyntaxError(sqlCommand, token.start(), "integer"); |
| 5622 |
} |
| 5623 |
Value value = token.value(session); |
| 5624 |
if (minus) { |
| 5625 |
// must do that now, otherwise Integer.MIN_VALUE would not work |
| 5626 |
value = value.negate(); |
| 5627 |
} |
| 5628 |
int i = value.getInt(); |
| 5629 |
read(); |
| 5630 |
return i; |
| 5631 |
} |
| 5632 |
|
| 5633 |
private long readPositiveLong() { |
| 5634 |
long v = readLong(); |
| 5635 |
if (v <= 0) { |
| 5636 |
throw DbException.getInvalidValueException("positive long", v); |
| 5637 |
} |
| 5638 |
return v; |
| 5639 |
} |
| 5640 |
|
| 5641 |
private long readLong() { |
| 5642 |
boolean minus = false; |
| 5643 |
if (currentTokenType == MINUS_SIGN) { |
| 5644 |
minus = true; |
| 5645 |
read(); |
| 5646 |
} else if (currentTokenType == PLUS_SIGN) { |
| 5647 |
read(); |
| 5648 |
} |
| 5649 |
if (currentTokenType != LITERAL) { |
| 5650 |
throw DbException.getSyntaxError(sqlCommand, token.start(), "long"); |
| 5651 |
} |
| 5652 |
Value value = token.value(session); |
| 5653 |
if (minus) { |
| 5654 |
// must do that now, otherwise Long.MIN_VALUE would not work |
| 5655 |
value = value.negate(); |
| 5656 |
} |
| 5657 |
long i = value.getLong(); |
| 5658 |
read(); |
| 5659 |
return i; |
| 5660 |
} |
| 5661 |
|
| 5662 |
private boolean readBooleanSetting() { |
| 5663 |
switch (currentTokenType) { |
| 5664 |
case ON: |
| 5665 |
case TRUE: |
| 5666 |
read(); |
| 5667 |
return true; |
| 5668 |
case FALSE: |
| 5669 |
read(); |
| 5670 |
return false; |
| 5671 |
case LITERAL: |
| 5672 |
boolean result = token.value(session).getBoolean(); |
| 5673 |
read(); |
| 5674 |
return result; |
| 5675 |
} |
| 5676 |
if (readIf("OFF")) { |
| 5677 |
return false; |
| 5678 |
} else { |
| 5679 |
if (expectedList != null) { |
| 5680 |
addMultipleExpected(ON, TRUE, FALSE); |
| 5681 |
} |
| 5682 |
throw getSyntaxError(); |
| 5683 |
} |
| 5684 |
} |
| 5685 |
|
| 5686 |
private String readString() { |
| 5687 |
int sqlIndex = token.start(); |
| 5688 |
Expression expr = readExpression(); |
| 5689 |
try { |
| 5690 |
String s = expr.optimize(session).getValue(session).getString(); |
| 5691 |
if (s == null || s.length() <= Constants.MAX_STRING_LENGTH) { |
| 5692 |
return s; |
| 5693 |
} |
| 5694 |
} catch (DbException e) { |
| 5695 |
} |
| 5696 |
throw DbException.getSyntaxError(sqlCommand, sqlIndex, "character string"); |
| 5697 |
} |
| 5698 |
|
| 5699 |
private Expression readStringOrParameter() { |
| 5700 |
int sqlIndex = token.start(); |
| 5701 |
Expression expr = readExpression(); |
| 5702 |
try { |
| 5703 |
expr = expr.optimize(session); |
| 5704 |
if (expr instanceof Parameter) { |
| 5705 |
return expr; |
| 5706 |
} |
| 5707 |
Value v = expr.getValue(session); |
| 5708 |
int valueType = v.getValueType(); |
| 5709 |
if ((valueType == NULL || valueType == Value.VARCHAR) && expr instanceof ValueExpression) { |
| 5710 |
return expr; |
| 5711 |
} |
| 5712 |
String s = v.getString(); |
| 5713 |
if (s == null || s.length() <= Constants.MAX_STRING_LENGTH) { |
| 5714 |
return s == null ? ValueExpression.NULL : ValueExpression.get(ValueVarchar.get(s, database)); |
| 5715 |
} |
| 5716 |
} catch (DbException e) { |
| 5717 |
} |
| 5718 |
throw DbException.getSyntaxError(sqlCommand, sqlIndex, "character string"); |
| 5719 |
} |
| 5720 |
|
| 5721 |
// TODO: why does this function allow defaultSchemaName=null - which resets |
| 5722 |
// the parser schemaName for everyone ? |
| 5723 |
private String readIdentifierWithSchema(String defaultSchemaName) { |
| 5724 |
String s = readIdentifier(); |
| 5725 |
schemaName = defaultSchemaName; |
| 5726 |
if (readIf(DOT)) { |
| 5727 |
s = readIdentifierWithSchema2(s); |
| 5728 |
} |
| 5729 |
return s; |
| 5730 |
} |
| 5731 |
|
| 5732 |
private String readIdentifierWithSchema2(String s) { |
| 5733 |
schemaName = s; |
| 5734 |
if (database.getMode().allowEmptySchemaValuesAsDefaultSchema && readIf(DOT)) { |
| 5735 |
if (equalsToken(schemaName, database.getShortName()) || database.getIgnoreCatalogs()) { |
| 5736 |
schemaName = session.getCurrentSchemaName(); |
| 5737 |
s = readIdentifier(); |
| 5738 |
} |
| 5739 |
} else { |
| 5740 |
s = readIdentifier(); |
| 5741 |
if (currentTokenType == DOT) { |
| 5742 |
if (equalsToken(schemaName, database.getShortName()) || database.getIgnoreCatalogs()) { |
| 5743 |
read(); |
| 5744 |
schemaName = s; |
| 5745 |
s = readIdentifier(); |
| 5746 |
} |
| 5747 |
} |
| 5748 |
} |
| 5749 |
return s; |
| 5750 |
} |
| 5751 |
|
| 5752 |
private String readIdentifierWithSchema() { |
| 5753 |
return readIdentifierWithSchema(session.getCurrentSchemaName()); |
| 5754 |
} |
| 5755 |
|
| 5756 |
private String readIdentifier() { |
| 5757 |
if (!isIdentifier()) { |
| 5758 |
/* |
| 5759 |
* Sometimes a new keywords are introduced. During metadata |
| 5760 |
* initialization phase keywords are accepted as identifiers to |
| 5761 |
* allow migration from older versions. |
| 5762 |
*/ |
| 5763 |
if (!session.isQuirksMode() || !isKeyword(currentTokenType)) { |
| 5764 |
throw DbException.getSyntaxError(sqlCommand, token.start(), "identifier"); |
| 5765 |
} |
| 5766 |
} |
| 5767 |
String s = currentToken; |
| 5768 |
read(); |
| 5769 |
return s; |
| 5770 |
} |
| 5771 |
|
| 5772 |
private String readIdentifierOrKeyword() { |
| 5773 |
if (currentTokenType < IDENTIFIER || currentTokenType > LAST_KEYWORD) { |
| 5774 |
addExpected("identifier or keyword"); |
| 5775 |
throw getSyntaxError(); |
| 5776 |
} |
| 5777 |
String s = currentToken; |
| 5778 |
read(); |
| 5779 |
return s; |
| 5780 |
} |
| 5781 |
|
| 5782 |
private void read(String expected) { |
| 5783 |
if (!testToken(expected, token)) { |
| 5784 |
addExpected(expected); |
| 5785 |
throw getSyntaxError(); |
| 5786 |
} |
| 5787 |
read(); |
| 5788 |
} |
| 5789 |
|
| 5790 |
private void read(int tokenType) { |
| 5791 |
if (tokenType != currentTokenType) { |
| 5792 |
addExpected(tokenType); |
| 5793 |
throw getSyntaxError(); |
| 5794 |
} |
| 5795 |
read(); |
| 5796 |
} |
| 5797 |
|
| 5798 |
private boolean readIf(String tokenName) { |
| 5799 |
if (testToken(tokenName, token)) { |
| 5800 |
read(); |
| 5801 |
return true; |
| 5802 |
} |
| 5803 |
addExpected(tokenName); |
| 5804 |
return false; |
| 5805 |
} |
| 5806 |
|
| 5807 |
private boolean readIf(String tokenName1, String tokenName2) { |
| 5808 |
int i = tokenIndex + 1; |
| 5809 |
if (i + 1 < tokens.size() && testToken(tokenName1, token) && testToken(tokenName2, tokens.get(i))) { |
| 5810 |
setTokenIndex(i + 1); |
| 5811 |
return true; |
| 5812 |
} |
| 5813 |
addExpected(tokenName1, tokenName2); |
| 5814 |
return false; |
| 5815 |
} |
| 5816 |
|
| 5817 |
private boolean readIf(String tokenName1, int tokenType2) { |
| 5818 |
int i = tokenIndex + 1; |
| 5819 |
if (i + 1 < tokens.size() && tokens.get(i).tokenType() == tokenType2 && testToken(tokenName1, token)) { |
| 5820 |
setTokenIndex(i + 1); |
| 5821 |
return true; |
| 5822 |
} |
| 5823 |
addExpected(tokenName1, TOKENS[tokenType2]); |
| 5824 |
return false; |
| 5825 |
} |
| 5826 |
|
| 5827 |
private boolean readIf(int tokenType) { |
| 5828 |
if (tokenType == currentTokenType) { |
| 5829 |
read(); |
| 5830 |
return true; |
| 5831 |
} |
| 5832 |
addExpected(tokenType); |
| 5833 |
return false; |
| 5834 |
} |
| 5835 |
|
| 5836 |
private boolean readIf(int tokenType1, int tokenType2) { |
| 5837 |
if (tokenType1 == currentTokenType) { |
| 5838 |
int i = tokenIndex + 1; |
| 5839 |
if (tokens.get(i).tokenType() == tokenType2) { |
| 5840 |
setTokenIndex(i + 1); |
| 5841 |
return true; |
| 5842 |
} |
| 5843 |
} |
| 5844 |
addExpected(tokenType1, tokenType2); |
| 5845 |
return false; |
| 5846 |
} |
| 5847 |
|
| 5848 |
private boolean readIf(int tokenType1, String tokenName2) { |
| 5849 |
if (tokenType1 == currentTokenType) { |
| 5850 |
int i = tokenIndex + 1; |
| 5851 |
if (testToken(tokenName2, tokens.get(i))) { |
| 5852 |
setTokenIndex(i + 1); |
| 5853 |
return true; |
| 5854 |
} |
| 5855 |
} |
| 5856 |
addExpected(TOKENS[tokenType1], tokenName2); |
| 5857 |
return false; |
| 5858 |
} |
| 5859 |
|
| 5860 |
private boolean readIf(Object... tokensTypesOrNames) { |
| 5861 |
int count = tokensTypesOrNames.length; |
| 5862 |
int size = tokens.size(); |
| 5863 |
int i = tokenIndex; |
| 5864 |
check: if (i + count < size) { |
| 5865 |
for (Object tokenTypeOrName : tokensTypesOrNames) { |
| 5866 |
if (!testToken(tokenTypeOrName, tokens.get(i++))) { |
| 5867 |
break check; |
| 5868 |
} |
| 5869 |
} |
| 5870 |
setTokenIndex(i); |
| 5871 |
return true; |
| 5872 |
} |
| 5873 |
addExpected(tokensTypesOrNames); |
| 5874 |
return false; |
| 5875 |
} |
| 5876 |
|
| 5877 |
private boolean isToken(String tokenName) { |
| 5878 |
if (testToken(tokenName, token)) { |
| 5879 |
return true; |
| 5880 |
} |
| 5881 |
addExpected(tokenName); |
| 5882 |
return false; |
| 5883 |
} |
| 5884 |
|
| 5885 |
private boolean testToken(Object expected, Token token) { |
| 5886 |
return expected instanceof Integer ? (int) expected == token.tokenType() : testToken((String) expected, token); |
| 5887 |
} |
| 5888 |
|
| 5889 |
private boolean testToken(String tokenName, Token token) { |
| 5890 |
if (!token.isQuoted()) { |
| 5891 |
String s = token.asIdentifier(); |
| 5892 |
return identifiersToUpper ? tokenName.equals(s) : tokenName.equalsIgnoreCase(s); |
| 5893 |
} |
| 5894 |
return false; |
| 5895 |
} |
| 5896 |
|
| 5897 |
private boolean isToken(int tokenType) { |
| 5898 |
if (tokenType == currentTokenType) { |
| 5899 |
return true; |
| 5900 |
} |
| 5901 |
addExpected(tokenType); |
| 5902 |
return false; |
| 5903 |
} |
| 5904 |
|
| 5905 |
private boolean equalsToken(String a, String b) { |
| 5906 |
if (a == null) { |
| 5907 |
return b == null; |
| 5908 |
} else |
| 5909 |
return a.equals(b) || !identifiersToUpper && a.equalsIgnoreCase(b); |
| 5910 |
} |
| 5911 |
|
| 5912 |
private boolean isIdentifier() { |
| 5913 |
return currentTokenType == IDENTIFIER || nonKeywords != null && nonKeywords.get(currentTokenType); |
| 5914 |
} |
| 5915 |
|
| 5916 |
private void addExpected(String token) { |
| 5917 |
if (expectedList != null) { |
| 5918 |
expectedList.add(token); |
| 5919 |
} |
| 5920 |
} |
| 5921 |
|
| 5922 |
private void addExpected(int tokenType) { |
| 5923 |
if (expectedList != null) { |
| 5924 |
expectedList.add(TOKENS[tokenType]); |
| 5925 |
} |
| 5926 |
} |
| 5927 |
|
| 5928 |
private void addExpected(int tokenType1, int tokenType2) { |
| 5929 |
if (expectedList != null) { |
| 5930 |
expectedList.add(TOKENS[tokenType1] + ' ' + TOKENS[tokenType2]); |
| 5931 |
} |
| 5932 |
} |
| 5933 |
|
| 5934 |
private void addExpected(String tokenType1, String tokenType2) { |
| 5935 |
if (expectedList != null) { |
| 5936 |
expectedList.add(tokenType1 + ' ' + tokenType2); |
| 5937 |
} |
| 5938 |
} |
| 5939 |
|
| 5940 |
private void addExpected(Object... tokens) { |
| 5941 |
if (expectedList != null) { |
| 5942 |
StringJoiner j = new StringJoiner(" "); |
| 5943 |
for (Object token : tokens) { |
| 5944 |
j.add(token instanceof Integer ? TOKENS[(int) token] : (String) token); |
| 5945 |
} |
| 5946 |
expectedList.add(j.toString()); |
| 5947 |
} |
| 5948 |
} |
| 5949 |
|
| 5950 |
private void addMultipleExpected(int ... tokenTypes) { |
| 5951 |
for (int tokenType : tokenTypes) { |
| 5952 |
expectedList.add(TOKENS[tokenType]); |
| 5953 |
} |
| 5954 |
} |
| 5955 |
|
| 5956 |
private void read() { |
| 5957 |
if (expectedList != null) { |
| 5958 |
expectedList.clear(); |
| 5959 |
} |
| 5960 |
int size = tokens.size(); |
| 5961 |
if (tokenIndex + 1 < size) { |
| 5962 |
token = tokens.get(++tokenIndex); |
| 5963 |
currentTokenType = token.tokenType(); |
| 5964 |
currentToken = token.asIdentifier(); |
| 5965 |
if (currentToken != null && currentToken.length() > Constants.MAX_IDENTIFIER_LENGTH) { |
| 5966 |
throw DbException.get(ErrorCode.NAME_TOO_LONG_2, currentToken.substring(0, 32), |
| 5967 |
"" + Constants.MAX_IDENTIFIER_LENGTH); |
| 5968 |
} else if (currentTokenType == LITERAL) { |
| 5969 |
checkLiterals(); |
| 5970 |
} |
| 5971 |
} else { |
| 5972 |
throw getSyntaxError(); |
| 5973 |
} |
| 5974 |
} |
| 5975 |
|
| 5976 |
private void checkLiterals() { |
| 5977 |
if (!literalsChecked && session != null && !session.getAllowLiterals()) { |
| 5978 |
int allowed = database.getAllowLiterals(); |
| 5979 |
if (allowed == Constants.ALLOW_LITERALS_NONE |
| 5980 |
|| ((token instanceof Token.CharacterStringToken || token instanceof Token.BinaryStringToken) |
| 5981 |
&& allowed != Constants.ALLOW_LITERALS_ALL)) { |
| 5982 |
throw DbException.get(ErrorCode.LITERALS_ARE_NOT_ALLOWED); |
| 5983 |
} |
| 5984 |
} |
| 5985 |
} |
| 5986 |
|
| 5987 |
private void initialize(String sql, ArrayList<Token> tokens, boolean stopOnCloseParen) { |
| 5988 |
if (sql == null) { |
| 5989 |
sql = ""; |
| 5990 |
} |
| 5991 |
sqlCommand = sql; |
| 5992 |
if (tokens == null) { |
| 5993 |
BitSet usedParameters = new BitSet(); |
| 5994 |
this.tokens = new Tokenizer(database, identifiersToUpper, identifiersToLower, nonKeywords) |
| 5995 |
.tokenize(sql, stopOnCloseParen, usedParameters); |
| 5996 |
if (parameters == null) { |
| 5997 |
int l = usedParameters.length(); |
| 5998 |
if (l > Constants.MAX_PARAMETER_INDEX) { |
| 5999 |
throw DbException.getInvalidValueException("parameter index", l); |
| 6000 |
} |
| 6001 |
if (l > 0) { |
| 6002 |
parameters = new ArrayList<>(l); |
| 6003 |
for (int i = 0; i < l; i++) { |
| 6004 |
/* |
| 6005 |
* We need to create parameters even when they aren't |
| 6006 |
* actually used, for example, VALUES ?1, ?3 needs |
| 6007 |
* parameters ?1, ?2, and ?3. |
| 6008 |
*/ |
| 6009 |
parameters.add(new Parameter(i)); |
| 6010 |
} |
| 6011 |
} else { |
| 6012 |
parameters = new ArrayList<>(); |
| 6013 |
} |
| 6014 |
} |
| 6015 |
} else { |
| 6016 |
this.tokens = tokens; |
| 6017 |
} |
| 6018 |
resetTokenIndex(); |
| 6019 |
} |
| 6020 |
|
| 6021 |
private void resetTokenIndex() { |
| 6022 |
tokenIndex = -1; |
| 6023 |
token = null; |
| 6024 |
currentTokenType = -1; |
| 6025 |
currentToken = null; |
| 6026 |
} |
| 6027 |
|
| 6028 |
void setTokenIndex(int index) { |
| 6029 |
if (index != tokenIndex) { |
| 6030 |
if (expectedList != null) { |
| 6031 |
expectedList.clear(); |
| 6032 |
} |
| 6033 |
token = tokens.get(index); |
| 6034 |
tokenIndex = index; |
| 6035 |
currentTokenType = token.tokenType(); |
| 6036 |
currentToken = token.asIdentifier(); |
| 6037 |
} |
| 6038 |
} |
| 6039 |
|
| 6040 |
private static boolean isKeyword(int tokenType) { |
| 6041 |
return tokenType >= FIRST_KEYWORD && tokenType <= LAST_KEYWORD; |
| 6042 |
} |
| 6043 |
|
| 6044 |
private boolean isKeyword(String s) { |
| 6045 |
return ParserUtil.isKeyword(s, !identifiersToUpper); |
| 6046 |
} |
| 6047 |
|
| 6048 |
private String upperName(String name) { |
| 6049 |
return identifiersToUpper ? name : StringUtils.toUpperEnglish(name); |
| 6050 |
} |
| 6051 |
|
| 6052 |
private Column parseColumnForTable(String columnName, boolean defaultNullable) { |
| 6053 |
Column column; |
| 6054 |
Mode mode = database.getMode(); |
| 6055 |
if (mode.identityDataType && readIf("IDENTITY")) { |
| 6056 |
column = new Column(columnName, TypeInfo.TYPE_BIGINT); |
| 6057 |
parseCompatibilityIdentityOptions(column); |
| 6058 |
column.setPrimaryKey(true); |
| 6059 |
} else if (mode.serialDataTypes && readIf("BIGSERIAL")) { |
| 6060 |
column = new Column(columnName, TypeInfo.TYPE_BIGINT); |
| 6061 |
column.setIdentityOptions(new SequenceOptions(), false); |
| 6062 |
} else if (mode.serialDataTypes && readIf("SERIAL")) { |
| 6063 |
column = new Column(columnName, TypeInfo.TYPE_INTEGER); |
| 6064 |
column.setIdentityOptions(new SequenceOptions(), false); |
| 6065 |
} else { |
| 6066 |
column = parseColumnWithType(columnName); |
| 6067 |
} |
| 6068 |
if (readIf("INVISIBLE")) { |
| 6069 |
column.setVisible(false); |
| 6070 |
} else if (readIf("VISIBLE")) { |
| 6071 |
column.setVisible(true); |
| 6072 |
} |
| 6073 |
boolean defaultOnNull = false; |
| 6074 |
NullConstraintType nullConstraint = parseNotNullConstraint(); |
| 6075 |
defaultIdentityGeneration: if (!column.isIdentity()) { |
| 6076 |
if (readIf(AS)) { |
| 6077 |
column.setGeneratedExpression(readExpression()); |
| 6078 |
} else if (readIf(DEFAULT)) { |
| 6079 |
if (readIf(ON, NULL)) { |
| 6080 |
defaultOnNull = true; |
| 6081 |
break defaultIdentityGeneration; |
| 6082 |
} |
| 6083 |
column.setDefaultExpression(session, readExpression()); |
| 6084 |
} else if (readIf("GENERATED")) { |
| 6085 |
boolean always = readIf("ALWAYS"); |
| 6086 |
if (!always) { |
| 6087 |
read("BY"); |
| 6088 |
read(DEFAULT); |
| 6089 |
} |
| 6090 |
read(AS); |
| 6091 |
if (readIf("IDENTITY")) { |
| 6092 |
SequenceOptions options = new SequenceOptions(); |
| 6093 |
if (readIf(OPEN_PAREN)) { |
| 6094 |
parseSequenceOptions(options, null, false, false); |
| 6095 |
read(CLOSE_PAREN); |
| 6096 |
} |
| 6097 |
column.setIdentityOptions(options, always); |
| 6098 |
break defaultIdentityGeneration; |
| 6099 |
} else if (!always) { |
| 6100 |
throw getSyntaxError(); |
| 6101 |
} else { |
| 6102 |
column.setGeneratedExpression(readExpression()); |
| 6103 |
} |
| 6104 |
} |
| 6105 |
if (!column.isGenerated() && readIf(ON, "UPDATE")) { |
| 6106 |
column.setOnUpdateExpression(session, readExpression()); |
| 6107 |
} |
| 6108 |
nullConstraint = parseNotNullConstraint(nullConstraint); |
| 6109 |
if (parseCompatibilityIdentity(column, mode)) { |
| 6110 |
nullConstraint = parseNotNullConstraint(nullConstraint); |
| 6111 |
} |
| 6112 |
} |
| 6113 |
switch (nullConstraint) { |
| 6114 |
case NULL_IS_ALLOWED: |
| 6115 |
if (column.isIdentity()) { |
| 6116 |
throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, column.getName()); |
| 6117 |
} |
| 6118 |
column.setNullable(true); |
| 6119 |
break; |
| 6120 |
case NULL_IS_NOT_ALLOWED: |
| 6121 |
column.setNullable(false); |
| 6122 |
break; |
| 6123 |
case NO_NULL_CONSTRAINT_FOUND: |
| 6124 |
if (!column.isIdentity()) { |
| 6125 |
column.setNullable(defaultNullable); |
| 6126 |
} |
| 6127 |
break; |
| 6128 |
default: |
| 6129 |
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, |
| 6130 |
"Internal Error - unhandled case: " + nullConstraint.name()); |
| 6131 |
} |
| 6132 |
if (!defaultOnNull) { |
| 6133 |
if (readIf(DEFAULT, ON, NULL)) { |
| 6134 |
defaultOnNull = true; |
| 6135 |
} else if (readIf("NULL_TO_DEFAULT")) { |
| 6136 |
defaultOnNull = true; |
| 6137 |
} |
| 6138 |
} |
| 6139 |
if (defaultOnNull) { |
| 6140 |
column.setDefaultOnNull(true); |
| 6141 |
} |
| 6142 |
if (!column.isGenerated()) { |
| 6143 |
if (readIf("SEQUENCE")) { |
| 6144 |
column.setSequence(readSequence(), column.isGeneratedAlways()); |
| 6145 |
} |
| 6146 |
} |
| 6147 |
if (readIf("SELECTIVITY")) { |
| 6148 |
column.setSelectivity(readNonNegativeInt()); |
| 6149 |
} |
| 6150 |
if (mode.mySqlTableOptions) { |
| 6151 |
if (readIf("CHARACTER")) { |
| 6152 |
readIf(SET); |
| 6153 |
readMySQLCharset(); |
| 6154 |
} |
| 6155 |
if (readIf("COLLATE")) { |
| 6156 |
readMySQLCharset(); |
| 6157 |
} |
| 6158 |
} |
| 6159 |
String comment = readCommentIf(); |
| 6160 |
if (comment != null) { |
| 6161 |
column.setComment(comment); |
| 6162 |
} |
| 6163 |
return column; |
| 6164 |
} |
| 6165 |
|
| 6166 |
private void parseCompatibilityIdentityOptions(Column column) { |
| 6167 |
SequenceOptions options = new SequenceOptions(); |
| 6168 |
if (readIf(OPEN_PAREN)) { |
| 6169 |
options.setStartValue(ValueExpression.get(ValueBigint.get(readLong()))); |
| 6170 |
if (readIf(COMMA)) { |
| 6171 |
options.setIncrement(ValueExpression.get(ValueBigint.get(readLong()))); |
| 6172 |
} |
| 6173 |
read(CLOSE_PAREN); |
| 6174 |
} |
| 6175 |
column.setIdentityOptions(options, false); |
| 6176 |
} |
| 6177 |
|
| 6178 |
private String readCommentIf() { |
| 6179 |
if (readIf("COMMENT")) { |
| 6180 |
readIf(IS); |
| 6181 |
return readString(); |
| 6182 |
} |
| 6183 |
return null; |
| 6184 |
} |
| 6185 |
|
| 6186 |
private Column parseColumnWithType(String columnName) { |
| 6187 |
TypeInfo typeInfo = readIfDataType(); |
| 6188 |
if (typeInfo == null) { |
| 6189 |
String domainName = readIdentifierWithSchema(); |
| 6190 |
return getColumnWithDomain(columnName, getSchema().getDomain(domainName)); |
| 6191 |
} |
| 6192 |
return new Column(columnName, typeInfo); |
| 6193 |
} |
| 6194 |
|
| 6195 |
private TypeInfo parseDataType() { |
| 6196 |
TypeInfo typeInfo = readIfDataType(); |
| 6197 |
if (typeInfo == null) { |
| 6198 |
addExpected("data type"); |
| 6199 |
throw getSyntaxError(); |
| 6200 |
} |
| 6201 |
return typeInfo; |
| 6202 |
} |
| 6203 |
|
| 6204 |
private TypeInfo readIfDataType() { |
| 6205 |
TypeInfo typeInfo = readIfDataType1(); |
| 6206 |
if (typeInfo != null) { |
| 6207 |
while (readIf(ARRAY)) { |
| 6208 |
typeInfo = parseArrayType(typeInfo); |
| 6209 |
} |
| 6210 |
} |
| 6211 |
return typeInfo; |
| 6212 |
} |
| 6213 |
|
| 6214 |
private TypeInfo readIfDataType1() { |
| 6215 |
switch (currentTokenType) { |
| 6216 |
case IDENTIFIER: |
| 6217 |
if (token.isQuoted()) { |
| 6218 |
return null; |
| 6219 |
} |
| 6220 |
break; |
| 6221 |
case INTERVAL: { |
| 6222 |
read(); |
| 6223 |
TypeInfo typeInfo = readIntervalQualifier(); |
| 6224 |
if (typeInfo == null) { |
| 6225 |
throw intervalQualifierError(); |
| 6226 |
} |
| 6227 |
return typeInfo; |
| 6228 |
} |
| 6229 |
case NULL: |
| 6230 |
read(); |
| 6231 |
return TypeInfo.TYPE_NULL; |
| 6232 |
case ROW: |
| 6233 |
read(); |
| 6234 |
return parseRowType(); |
| 6235 |
case ARRAY: |
| 6236 |
// Partial compatibility with 1.4.200 and older versions |
| 6237 |
if (session.isQuirksMode()) { |
| 6238 |
read(); |
| 6239 |
return parseArrayType(TypeInfo.TYPE_VARCHAR); |
| 6240 |
} |
| 6241 |
addExpected("data type"); |
| 6242 |
throw getSyntaxError(); |
| 6243 |
default: |
| 6244 |
if (isKeyword(currentTokenType)) { |
| 6245 |
break; |
| 6246 |
} |
| 6247 |
addExpected("data type"); |
| 6248 |
throw getSyntaxError(); |
| 6249 |
} |
| 6250 |
int index = tokenIndex; |
| 6251 |
String originalCase = currentToken; |
| 6252 |
read(); |
| 6253 |
if (currentTokenType == DOT) { |
| 6254 |
setTokenIndex(index); |
| 6255 |
return null; |
| 6256 |
} |
| 6257 |
String original = upperName(originalCase); |
| 6258 |
switch (original) { |
| 6259 |
case "BINARY": |
| 6260 |
if (readIf("VARYING")) { |
| 6261 |
original = "BINARY VARYING"; |
| 6262 |
} else if (readIf("LARGE")) { |
| 6263 |
read("OBJECT"); |
| 6264 |
original = "BINARY LARGE OBJECT"; |
| 6265 |
} else if (variableBinary) { |
| 6266 |
original = "VARBINARY"; |
| 6267 |
} |
| 6268 |
break; |
| 6269 |
case "CHAR": |
| 6270 |
if (readIf("VARYING")) { |
| 6271 |
original = "CHAR VARYING"; |
| 6272 |
} else if (readIf("LARGE")) { |
| 6273 |
read("OBJECT"); |
| 6274 |
original = "CHAR LARGE OBJECT"; |
| 6275 |
} |
| 6276 |
break; |
| 6277 |
case "CHARACTER": |
| 6278 |
if (readIf("VARYING")) { |
| 6279 |
original = "CHARACTER VARYING"; |
| 6280 |
} else if (readIf("LARGE")) { |
| 6281 |
read("OBJECT"); |
| 6282 |
original = "CHARACTER LARGE OBJECT"; |
| 6283 |
} |
| 6284 |
break; |
| 6285 |
case "DATE": |
| 6286 |
return database.getMode().dateIsTimestamp0 ? TypeInfo.getTypeInfo(Value.TIMESTAMP, -1L, 0, null) |
| 6287 |
: TypeInfo.TYPE_DATE; |
| 6288 |
case "DATETIME": |
| 6289 |
case "DATETIME2": |
| 6290 |
return parseDateTimeType(false); |
| 6291 |
case "DEC": |
| 6292 |
case "DECIMAL": |
| 6293 |
return parseNumericType(true); |
| 6294 |
case "DECFLOAT": |
| 6295 |
return parseDecfloatType(); |
| 6296 |
case "DOUBLE": |
| 6297 |
if (readIf("PRECISION")) { |
| 6298 |
original = "DOUBLE PRECISION"; |
| 6299 |
} |
| 6300 |
break; |
| 6301 |
case "ENUM": |
| 6302 |
return parseEnumType(); |
| 6303 |
case "FLOAT": |
| 6304 |
return parseFloatType(); |
| 6305 |
case "GEOMETRY": |
| 6306 |
return parseGeometryType(); |
| 6307 |
case "LONG": |
| 6308 |
if (readIf("RAW")) { |
| 6309 |
original = "LONG RAW"; |
| 6310 |
} |
| 6311 |
break; |
| 6312 |
case "NATIONAL": |
| 6313 |
if (readIf("CHARACTER")) { |
| 6314 |
if (readIf("VARYING")) { |
| 6315 |
original = "NATIONAL CHARACTER VARYING"; |
| 6316 |
} else if (readIf("LARGE")) { |
| 6317 |
read("OBJECT"); |
| 6318 |
original = "NATIONAL CHARACTER LARGE OBJECT"; |
| 6319 |
} else { |
| 6320 |
original = "NATIONAL CHARACTER"; |
| 6321 |
} |
| 6322 |
} else { |
| 6323 |
read("CHAR"); |
| 6324 |
if (readIf("VARYING")) { |
| 6325 |
original = "NATIONAL CHAR VARYING"; |
| 6326 |
} else { |
| 6327 |
original = "NATIONAL CHAR"; |
| 6328 |
} |
| 6329 |
} |
| 6330 |
break; |
| 6331 |
case "NCHAR": |
| 6332 |
if (readIf("VARYING")) { |
| 6333 |
original = "NCHAR VARYING"; |
| 6334 |
} else if (readIf("LARGE")) { |
| 6335 |
read("OBJECT"); |
| 6336 |
original = "NCHAR LARGE OBJECT"; |
| 6337 |
} |
| 6338 |
break; |
| 6339 |
case "NUMBER": |
| 6340 |
if (database.getMode().disallowedTypes.contains("NUMBER")) { |
| 6341 |
throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, "NUMBER"); |
| 6342 |
} |
| 6343 |
if (!isToken(OPEN_PAREN)) { |
| 6344 |
return TypeInfo.getTypeInfo(Value.DECFLOAT, 40, -1, null); |
| 6345 |
} |
| 6346 |
//$FALL-THROUGH$ |
| 6347 |
case "NUMERIC": |
| 6348 |
return parseNumericType(false); |
| 6349 |
case "SMALLDATETIME": |
| 6350 |
return parseDateTimeType(true); |
| 6351 |
case "TIME": |
| 6352 |
return parseTimeType(); |
| 6353 |
case "TIMESTAMP": |
| 6354 |
return parseTimestampType(); |
| 6355 |
} |
| 6356 |
// Domain names can't have multiple words without quotes |
| 6357 |
if (originalCase.length() == original.length()) { |
| 6358 |
Domain domain = database.getSchema(session.getCurrentSchemaName()).findDomain(originalCase); |
| 6359 |
if (domain != null) { |
| 6360 |
setTokenIndex(index); |
| 6361 |
return null; |
| 6362 |
} |
| 6363 |
} |
| 6364 |
Mode mode = database.getMode(); |
| 6365 |
DataType dataType = DataType.getTypeByName(original, mode); |
| 6366 |
if (dataType == null || mode.disallowedTypes.contains(original)) { |
| 6367 |
throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, original); |
| 6368 |
} |
| 6369 |
long precision; |
| 6370 |
int scale; |
| 6371 |
if (dataType.specialPrecisionScale) { |
| 6372 |
precision = dataType.defaultPrecision; |
| 6373 |
scale = dataType.defaultScale; |
| 6374 |
} else { |
| 6375 |
precision = -1L; |
| 6376 |
scale = -1; |
| 6377 |
} |
| 6378 |
int t = dataType.type; |
| 6379 |
if (database.getIgnoreCase() && t == Value.VARCHAR && !equalsToken("VARCHAR_CASESENSITIVE", original)) { |
| 6380 |
dataType = DataType.getDataType(t = Value.VARCHAR_IGNORECASE); |
| 6381 |
} |
| 6382 |
if ((dataType.supportsPrecision || dataType.supportsScale) && readIf(OPEN_PAREN)) { |
| 6383 |
if (!readIf("MAX")) { |
| 6384 |
if (dataType.supportsPrecision) { |
| 6385 |
precision = readPrecision(t); |
| 6386 |
if (precision < dataType.minPrecision) { |
| 6387 |
throw getInvalidPrecisionException(dataType, precision); |
| 6388 |
} else if (precision > dataType.maxPrecision) |
| 6389 |
badPrecision: { |
| 6390 |
if (session.isQuirksMode() || session.isTruncateLargeLength()) { |
| 6391 |
switch (dataType.type) { |
| 6392 |
case Value.CHAR: |
| 6393 |
case Value.VARCHAR: |
| 6394 |
case Value.VARCHAR_IGNORECASE: |
| 6395 |
case Value.BINARY: |
| 6396 |
case Value.VARBINARY: |
| 6397 |
case Value.JAVA_OBJECT: |
| 6398 |
case Value.JSON: |
| 6399 |
precision = dataType.maxPrecision; |
| 6400 |
break badPrecision; |
| 6401 |
} |
| 6402 |
} |
| 6403 |
throw getInvalidPrecisionException(dataType, precision); |
| 6404 |
} |
| 6405 |
if (dataType.supportsScale) { |
| 6406 |
if (readIf(COMMA)) { |
| 6407 |
scale = readInt(); |
| 6408 |
if (scale < dataType.minScale || scale > dataType.maxScale) { |
| 6409 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), |
| 6410 |
Integer.toString(dataType.minScale), Integer.toString(dataType.maxScale)); |
| 6411 |
} |
| 6412 |
} |
| 6413 |
} |
| 6414 |
} else { |
| 6415 |
scale = readInt(); |
| 6416 |
if (scale < dataType.minScale || scale > dataType.maxScale) { |
| 6417 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), |
| 6418 |
Integer.toString(dataType.minScale), Integer.toString(dataType.maxScale)); |
| 6419 |
} |
| 6420 |
} |
| 6421 |
} |
| 6422 |
read(CLOSE_PAREN); |
| 6423 |
} |
| 6424 |
if (mode.allNumericTypesHavePrecision |
| 6425 |
&& (DataType.isNumericType(dataType.type) || dataType.type == Value.BOOLEAN)) { |
| 6426 |
if (readIf(OPEN_PAREN)) { |
| 6427 |
// Support for MySQL: INT(11), MEDIUMINT(8) and so on. |
| 6428 |
// Just ignore the precision. |
| 6429 |
readNonNegativeInt(); |
| 6430 |
read(CLOSE_PAREN); |
| 6431 |
} |
| 6432 |
readIf("UNSIGNED"); |
| 6433 |
} |
| 6434 |
if (mode.forBitData && DataType.isStringType(t)) { |
| 6435 |
if (readIf(FOR, "BIT", "DATA")) { |
| 6436 |
dataType = DataType.getDataType(t = Value.VARBINARY); |
| 6437 |
} |
| 6438 |
} |
| 6439 |
return TypeInfo.getTypeInfo(t, precision, scale, null); |
| 6440 |
} |
| 6441 |
|
| 6442 |
private static DbException getInvalidPrecisionException(DataType dataType, long precision) { |
| 6443 |
return DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Long.toString(precision), |
| 6444 |
Long.toString(dataType.minPrecision), Long.toString(dataType.maxPrecision)); |
| 6445 |
} |
| 6446 |
|
| 6447 |
private static Column getColumnWithDomain(String columnName, Domain domain) { |
| 6448 |
Column column = new Column(columnName, domain.getDataType()); |
| 6449 |
column.setComment(domain.getComment()); |
| 6450 |
column.setDomain(domain); |
| 6451 |
return column; |
| 6452 |
} |
| 6453 |
|
| 6454 |
private TypeInfo parseFloatType() { |
| 6455 |
int type = Value.DOUBLE; |
| 6456 |
int precision; |
| 6457 |
if (readIf(OPEN_PAREN)) { |
| 6458 |
precision = readNonNegativeInt(); |
| 6459 |
read(CLOSE_PAREN); |
| 6460 |
if (precision < 1 || precision > 53) { |
| 6461 |
throw DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Integer.toString(precision), "1", "53"); |
| 6462 |
} |
| 6463 |
if (precision <= 24) { |
| 6464 |
type = Value.REAL; |
| 6465 |
} |
| 6466 |
} else { |
| 6467 |
precision = 0; |
| 6468 |
} |
| 6469 |
return TypeInfo.getTypeInfo(type, precision, -1, null); |
| 6470 |
} |
| 6471 |
|
| 6472 |
private TypeInfo parseNumericType(boolean decimal) { |
| 6473 |
long precision = -1L; |
| 6474 |
int scale = -1; |
| 6475 |
if (readIf(OPEN_PAREN)) { |
| 6476 |
precision = readPrecision(Value.NUMERIC); |
| 6477 |
if (precision < 1) { |
| 6478 |
throw getInvalidNumericPrecisionException(precision); |
| 6479 |
} else if (precision > Constants.MAX_NUMERIC_PRECISION) { |
| 6480 |
if (session.isQuirksMode() || session.isTruncateLargeLength()) { |
| 6481 |
precision = Constants.MAX_NUMERIC_PRECISION; |
| 6482 |
} else { |
| 6483 |
throw getInvalidNumericPrecisionException(precision); |
| 6484 |
} |
| 6485 |
} |
| 6486 |
if (readIf(COMMA)) { |
| 6487 |
scale = readInt(); |
| 6488 |
if (scale < 0 || scale > ValueNumeric.MAXIMUM_SCALE) { |
| 6489 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), |
| 6490 |
"0", "" + ValueNumeric.MAXIMUM_SCALE); |
| 6491 |
} |
| 6492 |
} |
| 6493 |
read(CLOSE_PAREN); |
| 6494 |
} else if (database.getMode().numericIsDecfloat) { |
| 6495 |
return TypeInfo.TYPE_DECFLOAT; |
| 6496 |
} |
| 6497 |
return TypeInfo.getTypeInfo(Value.NUMERIC, precision, scale, decimal ? ExtTypeInfoNumeric.DECIMAL : null); |
| 6498 |
} |
| 6499 |
|
| 6500 |
private TypeInfo parseDecfloatType() { |
| 6501 |
long precision = -1L; |
| 6502 |
if (readIf(OPEN_PAREN)) { |
| 6503 |
precision = readPrecision(Value.DECFLOAT); |
| 6504 |
if (precision < 1 || precision > Constants.MAX_NUMERIC_PRECISION) { |
| 6505 |
throw getInvalidNumericPrecisionException(precision); |
| 6506 |
} |
| 6507 |
read(CLOSE_PAREN); |
| 6508 |
} |
| 6509 |
return TypeInfo.getTypeInfo(Value.DECFLOAT, precision, -1, null); |
| 6510 |
} |
| 6511 |
|
| 6512 |
private static DbException getInvalidNumericPrecisionException(long precision) { |
| 6513 |
return DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Long.toString(precision), "1", |
| 6514 |
"" + Constants.MAX_NUMERIC_PRECISION); |
| 6515 |
} |
| 6516 |
|
| 6517 |
private TypeInfo parseTimeType() { |
| 6518 |
int scale = -1; |
| 6519 |
if (readIf(OPEN_PAREN)) { |
| 6520 |
scale = readNonNegativeInt(); |
| 6521 |
if (scale > ValueTime.MAXIMUM_SCALE) { |
| 6522 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 6523 |
/* Folds to a constant */ "" + ValueTime.MAXIMUM_SCALE); |
| 6524 |
} |
| 6525 |
read(CLOSE_PAREN); |
| 6526 |
} |
| 6527 |
int type = Value.TIME; |
| 6528 |
if (readIf(WITH, "TIME", "ZONE")) { |
| 6529 |
type = Value.TIME_TZ; |
| 6530 |
} else { |
| 6531 |
readIf("WITHOUT", "TIME", "ZONE"); |
| 6532 |
} |
| 6533 |
return TypeInfo.getTypeInfo(type, -1L, scale, null); |
| 6534 |
} |
| 6535 |
|
| 6536 |
private TypeInfo parseTimestampType() { |
| 6537 |
int scale = -1; |
| 6538 |
if (readIf(OPEN_PAREN)) { |
| 6539 |
scale = readNonNegativeInt(); |
| 6540 |
// Allow non-standard TIMESTAMP(..., ...) syntax |
| 6541 |
if (readIf(COMMA)) { |
| 6542 |
scale = readNonNegativeInt(); |
| 6543 |
} |
| 6544 |
if (scale > ValueTimestamp.MAXIMUM_SCALE) { |
| 6545 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 6546 |
/* Folds to a constant */ "" + ValueTimestamp.MAXIMUM_SCALE); |
| 6547 |
} |
| 6548 |
read(CLOSE_PAREN); |
| 6549 |
} |
| 6550 |
int type = Value.TIMESTAMP; |
| 6551 |
if (readIf(WITH, "TIME", "ZONE")) { |
| 6552 |
type = Value.TIMESTAMP_TZ; |
| 6553 |
} else { |
| 6554 |
readIf("WITHOUT", "TIME", "ZONE"); |
| 6555 |
} |
| 6556 |
return TypeInfo.getTypeInfo(type, -1L, scale, null); |
| 6557 |
} |
| 6558 |
|
| 6559 |
private TypeInfo parseDateTimeType(boolean smallDateTime) { |
| 6560 |
int scale; |
| 6561 |
if (smallDateTime) { |
| 6562 |
scale = 0; |
| 6563 |
} else { |
| 6564 |
scale = -1; |
| 6565 |
if (readIf(OPEN_PAREN)) { |
| 6566 |
scale = readNonNegativeInt(); |
| 6567 |
if (scale > ValueTimestamp.MAXIMUM_SCALE) { |
| 6568 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 6569 |
/* folds to a constant */ "" + ValueTimestamp.MAXIMUM_SCALE); |
| 6570 |
} |
| 6571 |
read(CLOSE_PAREN); |
| 6572 |
} |
| 6573 |
} |
| 6574 |
return TypeInfo.getTypeInfo(Value.TIMESTAMP, -1L, scale, null); |
| 6575 |
} |
| 6576 |
|
| 6577 |
private TypeInfo readIntervalQualifier() { |
| 6578 |
IntervalQualifier qualifier; |
| 6579 |
int precision = -1, scale = -1; |
| 6580 |
switch (currentTokenType) { |
| 6581 |
case YEAR: |
| 6582 |
read(); |
| 6583 |
if (readIf(OPEN_PAREN)) { |
| 6584 |
precision = readNonNegativeInt(); |
| 6585 |
read(CLOSE_PAREN); |
| 6586 |
} |
| 6587 |
if (readIf(TO, MONTH)) { |
| 6588 |
qualifier = IntervalQualifier.YEAR_TO_MONTH; |
| 6589 |
} else { |
| 6590 |
qualifier = IntervalQualifier.YEAR; |
| 6591 |
} |
| 6592 |
break; |
| 6593 |
case MONTH: |
| 6594 |
read(); |
| 6595 |
if (readIf(OPEN_PAREN)) { |
| 6596 |
precision = readNonNegativeInt(); |
| 6597 |
read(CLOSE_PAREN); |
| 6598 |
} |
| 6599 |
qualifier = IntervalQualifier.MONTH; |
| 6600 |
break; |
| 6601 |
case DAY: |
| 6602 |
read(); |
| 6603 |
if (readIf(OPEN_PAREN)) { |
| 6604 |
precision = readNonNegativeInt(); |
| 6605 |
read(CLOSE_PAREN); |
| 6606 |
} |
| 6607 |
if (readIf(TO)) { |
| 6608 |
switch (currentTokenType) { |
| 6609 |
case HOUR: |
| 6610 |
read(); |
| 6611 |
qualifier = IntervalQualifier.DAY_TO_HOUR; |
| 6612 |
break; |
| 6613 |
case MINUTE: |
| 6614 |
read(); |
| 6615 |
qualifier = IntervalQualifier.DAY_TO_MINUTE; |
| 6616 |
break; |
| 6617 |
case SECOND: |
| 6618 |
read(); |
| 6619 |
if (readIf(OPEN_PAREN)) { |
| 6620 |
scale = readNonNegativeInt(); |
| 6621 |
read(CLOSE_PAREN); |
| 6622 |
} |
| 6623 |
qualifier = IntervalQualifier.DAY_TO_SECOND; |
| 6624 |
break; |
| 6625 |
default: |
| 6626 |
throw intervalDayError(); |
| 6627 |
} |
| 6628 |
} else { |
| 6629 |
qualifier = IntervalQualifier.DAY; |
| 6630 |
} |
| 6631 |
break; |
| 6632 |
case HOUR: |
| 6633 |
read(); |
| 6634 |
if (readIf(OPEN_PAREN)) { |
| 6635 |
precision = readNonNegativeInt(); |
| 6636 |
read(CLOSE_PAREN); |
| 6637 |
} |
| 6638 |
if (readIf(TO)) { |
| 6639 |
switch (currentTokenType) { |
| 6640 |
case MINUTE: |
| 6641 |
read(); |
| 6642 |
qualifier = IntervalQualifier.HOUR_TO_MINUTE; |
| 6643 |
break; |
| 6644 |
case SECOND: |
| 6645 |
read(); |
| 6646 |
if (readIf(OPEN_PAREN)) { |
| 6647 |
scale = readNonNegativeInt(); |
| 6648 |
read(CLOSE_PAREN); |
| 6649 |
} |
| 6650 |
qualifier = IntervalQualifier.HOUR_TO_SECOND; |
| 6651 |
break; |
| 6652 |
default: |
| 6653 |
throw intervalHourError(); |
| 6654 |
} |
| 6655 |
} else { |
| 6656 |
qualifier = IntervalQualifier.HOUR; |
| 6657 |
} |
| 6658 |
break; |
| 6659 |
case MINUTE: |
| 6660 |
read(); |
| 6661 |
if (readIf(OPEN_PAREN)) { |
| 6662 |
precision = readNonNegativeInt(); |
| 6663 |
read(CLOSE_PAREN); |
| 6664 |
} |
| 6665 |
if (readIf(TO, SECOND)) { |
| 6666 |
if (readIf(OPEN_PAREN)) { |
| 6667 |
scale = readNonNegativeInt(); |
| 6668 |
read(CLOSE_PAREN); |
| 6669 |
} |
| 6670 |
qualifier = IntervalQualifier.MINUTE_TO_SECOND; |
| 6671 |
} else { |
| 6672 |
qualifier = IntervalQualifier.MINUTE; |
| 6673 |
} |
| 6674 |
break; |
| 6675 |
case SECOND: |
| 6676 |
read(); |
| 6677 |
if (readIf(OPEN_PAREN)) { |
| 6678 |
precision = readNonNegativeInt(); |
| 6679 |
if (readIf(COMMA)) { |
| 6680 |
scale = readNonNegativeInt(); |
| 6681 |
} |
| 6682 |
read(CLOSE_PAREN); |
| 6683 |
} |
| 6684 |
qualifier = IntervalQualifier.SECOND; |
| 6685 |
break; |
| 6686 |
default: |
| 6687 |
return null; |
| 6688 |
} |
| 6689 |
if (precision >= 0) { |
| 6690 |
if (precision == 0 || precision > ValueInterval.MAXIMUM_PRECISION) { |
| 6691 |
throw DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Integer.toString(precision), "1", |
| 6692 |
/* Folds to a constant */ "" + ValueInterval.MAXIMUM_PRECISION); |
| 6693 |
} |
| 6694 |
} |
| 6695 |
if (scale >= 0) { |
| 6696 |
if (scale > ValueInterval.MAXIMUM_SCALE) { |
| 6697 |
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE, Integer.toString(scale), "0", |
| 6698 |
/* Folds to a constant */ "" + ValueInterval.MAXIMUM_SCALE); |
| 6699 |
} |
| 6700 |
} |
| 6701 |
return TypeInfo.getTypeInfo(qualifier.ordinal() + Value.INTERVAL_YEAR, precision, scale, null); |
| 6702 |
} |
| 6703 |
|
| 6704 |
private DbException intervalQualifierError() { |
| 6705 |
if (expectedList != null) { |
| 6706 |
addMultipleExpected(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND); |
| 6707 |
} |
| 6708 |
return getSyntaxError(); |
| 6709 |
} |
| 6710 |
|
| 6711 |
private DbException intervalDayError() { |
| 6712 |
if (expectedList != null) { |
| 6713 |
addMultipleExpected(HOUR, MINUTE, SECOND); |
| 6714 |
} |
| 6715 |
return getSyntaxError(); |
| 6716 |
} |
| 6717 |
|
| 6718 |
private DbException intervalHourError() { |
| 6719 |
if (expectedList != null) { |
| 6720 |
addMultipleExpected(MINUTE, SECOND); |
| 6721 |
} |
| 6722 |
return getSyntaxError(); |
| 6723 |
} |
| 6724 |
|
| 6725 |
private TypeInfo parseArrayType(TypeInfo componentType) { |
| 6726 |
int precision = -1; |
| 6727 |
if (readIf(OPEN_BRACKET)) { |
| 6728 |
// Maximum cardinality may be zero |
| 6729 |
precision = readNonNegativeInt(); |
| 6730 |
if (precision > Constants.MAX_ARRAY_CARDINALITY) { |
| 6731 |
throw DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Integer.toString(precision), "0", |
| 6732 |
/* Folds to a constant */ "" + Constants.MAX_ARRAY_CARDINALITY); |
| 6733 |
} |
| 6734 |
read(CLOSE_BRACKET); |
| 6735 |
} |
| 6736 |
return TypeInfo.getTypeInfo(Value.ARRAY, precision, -1, componentType); |
| 6737 |
} |
| 6738 |
|
| 6739 |
private TypeInfo parseEnumType() { |
| 6740 |
read(OPEN_PAREN); |
| 6741 |
ArrayList<String> enumeratorList = new ArrayList<>(); |
| 6742 |
do { |
| 6743 |
enumeratorList.add(readString()); |
| 6744 |
} while (readIfMore()); |
| 6745 |
return TypeInfo.getTypeInfo(Value.ENUM, -1L, -1, new ExtTypeInfoEnum(enumeratorList.toArray(new String[0]))); |
| 6746 |
} |
| 6747 |
|
| 6748 |
private TypeInfo parseGeometryType() { |
| 6749 |
ExtTypeInfoGeometry extTypeInfo; |
| 6750 |
if (readIf(OPEN_PAREN)) { |
| 6751 |
int type = 0; |
| 6752 |
if (currentTokenType != IDENTIFIER || token.isQuoted()) { |
| 6753 |
throw getSyntaxError(); |
| 6754 |
} |
| 6755 |
if (!readIf("GEOMETRY")) { |
| 6756 |
try { |
| 6757 |
type = EWKTUtils.parseGeometryType(currentToken); |
| 6758 |
read(); |
| 6759 |
if (type / 1_000 == 0 && currentTokenType == IDENTIFIER && !token.isQuoted()) { |
| 6760 |
type += EWKTUtils.parseDimensionSystem(currentToken) * 1_000; |
| 6761 |
read(); |
| 6762 |
} |
| 6763 |
} catch (IllegalArgumentException ex) { |
| 6764 |
throw getSyntaxError(); |
| 6765 |
} |
| 6766 |
} |
| 6767 |
Integer srid = null; |
| 6768 |
if (readIf(COMMA)) { |
| 6769 |
srid = readInt(); |
| 6770 |
} |
| 6771 |
read(CLOSE_PAREN); |
| 6772 |
extTypeInfo = new ExtTypeInfoGeometry(type, srid); |
| 6773 |
} else { |
| 6774 |
extTypeInfo = null; |
| 6775 |
} |
| 6776 |
return TypeInfo.getTypeInfo(Value.GEOMETRY, -1L, -1, extTypeInfo); |
| 6777 |
} |
| 6778 |
|
| 6779 |
private TypeInfo parseRowType() { |
| 6780 |
read(OPEN_PAREN); |
| 6781 |
LinkedHashMap<String, TypeInfo> fields = new LinkedHashMap<>(); |
| 6782 |
do { |
| 6783 |
String name = readIdentifier(); |
| 6784 |
if (fields.putIfAbsent(name, parseDataType()) != null) { |
| 6785 |
throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, name); |
| 6786 |
} |
| 6787 |
} while (readIfMore()); |
| 6788 |
return TypeInfo.getTypeInfo(Value.ROW, -1L, -1, new ExtTypeInfoRow(fields)); |
| 6789 |
} |
| 6790 |
|
| 6791 |
private long readPrecision(int valueType) { |
| 6792 |
long p = readPositiveLong(); |
| 6793 |
if (currentTokenType != IDENTIFIER || token.isQuoted()) { |
| 6794 |
return p; |
| 6795 |
} |
| 6796 |
if ((valueType == Value.BLOB || valueType == Value.CLOB) && currentToken.length() == 1) { |
| 6797 |
long mul; |
| 6798 |
/* |
| 6799 |
* Convert a-z to A-Z. This method is safe, because only A-Z |
| 6800 |
* characters are considered below. |
| 6801 |
*/ |
| 6802 |
switch (currentToken.charAt(0) & 0xffdf) { |
| 6803 |
case 'K': |
| 6804 |
mul = 1L << 10; |
| 6805 |
break; |
| 6806 |
case 'M': |
| 6807 |
mul = 1L << 20; |
| 6808 |
break; |
| 6809 |
case 'G': |
| 6810 |
mul = 1L << 30; |
| 6811 |
break; |
| 6812 |
case 'T': |
| 6813 |
mul = 1L << 40; |
| 6814 |
break; |
| 6815 |
case 'P': |
| 6816 |
mul = 1L << 50; |
| 6817 |
break; |
| 6818 |
default: |
| 6819 |
throw getSyntaxError(); |
| 6820 |
} |
| 6821 |
if (p > Long.MAX_VALUE / mul) { |
| 6822 |
throw DbException.getInvalidValueException("precision", p + currentToken); |
| 6823 |
} |
| 6824 |
p *= mul; |
| 6825 |
read(); |
| 6826 |
if (currentTokenType != IDENTIFIER || token.isQuoted()) { |
| 6827 |
return p; |
| 6828 |
} |
| 6829 |
} |
| 6830 |
switch (valueType) { |
| 6831 |
case Value.VARCHAR: |
| 6832 |
case Value.VARCHAR_IGNORECASE: |
| 6833 |
case Value.CLOB: |
| 6834 |
case Value.CHAR: |
| 6835 |
if (!readIf("CHARACTERS") && !readIf("OCTETS")) { |
| 6836 |
if (database.getMode().charAndByteLengthUnits && !readIf("CHAR")) { |
| 6837 |
readIf("BYTE"); |
| 6838 |
} |
| 6839 |
} |
| 6840 |
} |
| 6841 |
return p; |
| 6842 |
} |
| 6843 |
|
| 6844 |
private Prepared parseCreate() { |
| 6845 |
boolean orReplace = false; |
| 6846 |
if (readIf(OR, "REPLACE")) { |
| 6847 |
orReplace = true; |
| 6848 |
} |
| 6849 |
boolean force = readIf("FORCE"); |
| 6850 |
if (readIf("VIEW")) { |
| 6851 |
return parseCreateView(force, orReplace); |
| 6852 |
} else if (readIf("MATERIALIZED")) { |
| 6853 |
read("VIEW"); |
| 6854 |
return parseCreateMaterializedView(force, orReplace); |
| 6855 |
} else if (readIf("ALIAS")) { |
| 6856 |
return parseCreateFunctionAlias(force); |
| 6857 |
} else if (readIf("SEQUENCE")) { |
| 6858 |
return parseCreateSequence(); |
| 6859 |
} else if (readIf(USER)) { |
| 6860 |
return parseCreateUser(); |
| 6861 |
} else if (readIf("TRIGGER")) { |
| 6862 |
return parseCreateTrigger(force); |
| 6863 |
} else if (readIf("ROLE")) { |
| 6864 |
return parseCreateRole(); |
| 6865 |
} else if (readIf("SCHEMA")) { |
| 6866 |
return parseCreateSchema(); |
| 6867 |
} else if (readIf("CONSTANT")) { |
| 6868 |
return parseCreateConstant(); |
| 6869 |
} else if (readIf("DOMAIN") || readIf("TYPE") || readIf("DATATYPE")) { |
| 6870 |
return parseCreateDomain(); |
| 6871 |
} else if (readIf("AGGREGATE")) { |
| 6872 |
return parseCreateAggregate(force); |
| 6873 |
} else if (readIf("LINKED")) { |
| 6874 |
return parseCreateLinkedTable(false, false, force); |
| 6875 |
} |
| 6876 |
// tables or linked tables |
| 6877 |
boolean memory = false, cached = false; |
| 6878 |
if (readIf("MEMORY")) { |
| 6879 |
memory = true; |
| 6880 |
} else if (readIf("CACHED")) { |
| 6881 |
cached = true; |
| 6882 |
} |
| 6883 |
if (readIf("LOCAL", "TEMPORARY")) { |
| 6884 |
if (readIf("LINKED")) { |
| 6885 |
return parseCreateLinkedTable(true, false, force); |
| 6886 |
} |
| 6887 |
read(TABLE); |
| 6888 |
return parseCreateTable(true, false, cached); |
| 6889 |
} else if (readIf("GLOBAL", "TEMPORARY")) { |
| 6890 |
if (readIf("LINKED")) { |
| 6891 |
return parseCreateLinkedTable(true, true, force); |
| 6892 |
} |
| 6893 |
read(TABLE); |
| 6894 |
return parseCreateTable(true, true, cached); |
| 6895 |
} else if (readIf("TEMP") || readIf("TEMPORARY")) { |
| 6896 |
if (readIf("LINKED")) { |
| 6897 |
return parseCreateLinkedTable(true, true, force); |
| 6898 |
} |
| 6899 |
read(TABLE); |
| 6900 |
return parseCreateTable(true, true, cached); |
| 6901 |
} else if (readIf(TABLE)) { |
| 6902 |
if (!cached && !memory) { |
| 6903 |
cached = database.getDefaultTableType() == Table.TYPE_CACHED; |
| 6904 |
} |
| 6905 |
return parseCreateTable(false, false, cached); |
| 6906 |
} else if (readIf("SYNONYM")) { |
| 6907 |
return parseCreateSynonym(orReplace); |
| 6908 |
} else { |
| 6909 |
boolean hash = false, primaryKey = false; |
| 6910 |
NullsDistinct nullsDistinct = null; |
| 6911 |
boolean spatial = false; |
| 6912 |
String indexName = null; |
| 6913 |
Schema oldSchema = null; |
| 6914 |
boolean ifNotExists = false; |
| 6915 |
if (session.isQuirksMode() && readIf(PRIMARY, KEY)) { |
| 6916 |
if (readIf("HASH")) { |
| 6917 |
hash = true; |
| 6918 |
} |
| 6919 |
primaryKey = true; |
| 6920 |
if (!isToken(ON)) { |
| 6921 |
ifNotExists = readIfNotExists(); |
| 6922 |
indexName = readIdentifierWithSchema(null); |
| 6923 |
oldSchema = getSchema(); |
| 6924 |
} |
| 6925 |
} else { |
| 6926 |
if (readIf(UNIQUE)) { |
| 6927 |
nullsDistinct = readNullsDistinct(database.getMode().nullsDistinct); |
| 6928 |
} |
| 6929 |
if (readIf("HASH")) { |
| 6930 |
hash = true; |
| 6931 |
} else if (nullsDistinct == null && readIf("SPATIAL")) { |
| 6932 |
spatial = true; |
| 6933 |
} |
| 6934 |
read("INDEX"); |
| 6935 |
if (!isToken(ON)) { |
| 6936 |
ifNotExists = readIfNotExists(); |
| 6937 |
indexName = readIdentifierWithSchema(null); |
| 6938 |
oldSchema = getSchema(); |
| 6939 |
} |
| 6940 |
} |
| 6941 |
read(ON); |
| 6942 |
String tableName = readIdentifierWithSchema(); |
| 6943 |
checkSchema(oldSchema); |
| 6944 |
String comment = readCommentIf(); |
| 6945 |
if (!readIf(OPEN_PAREN)) { |
| 6946 |
// PostgreSQL compatibility |
| 6947 |
if (hash || spatial) { |
| 6948 |
throw getSyntaxError(); |
| 6949 |
} |
| 6950 |
read(USING); |
| 6951 |
if (readIf("BTREE")) { |
| 6952 |
// default |
| 6953 |
} else if (readIf("HASH")) { |
| 6954 |
hash = true; |
| 6955 |
} else { |
| 6956 |
read("RTREE"); |
| 6957 |
spatial = true; |
| 6958 |
} |
| 6959 |
read(OPEN_PAREN); |
| 6960 |
} |
| 6961 |
CreateIndex command = new CreateIndex(session, getSchema()); |
| 6962 |
command.setIfNotExists(ifNotExists); |
| 6963 |
command.setPrimaryKey(primaryKey); |
| 6964 |
command.setTableName(tableName); |
| 6965 |
command.setHash(hash); |
| 6966 |
command.setSpatial(spatial); |
| 6967 |
command.setIndexName(indexName); |
| 6968 |
command.setComment(comment); |
| 6969 |
IndexColumn[] columns; |
| 6970 |
int uniqueColumnCount = 0; |
| 6971 |
if (spatial) { |
| 6972 |
columns = new IndexColumn[] { new IndexColumn(readIdentifier()) }; |
| 6973 |
if (nullsDistinct != null) { |
| 6974 |
uniqueColumnCount = 1; |
| 6975 |
} |
| 6976 |
read(CLOSE_PAREN); |
| 6977 |
} else { |
| 6978 |
columns = parseIndexColumnList(); |
| 6979 |
if (nullsDistinct != null) { |
| 6980 |
uniqueColumnCount = columns.length; |
| 6981 |
if (readIf("INCLUDE")) { |
| 6982 |
read(OPEN_PAREN); |
| 6983 |
IndexColumn[] columnsToInclude = parseIndexColumnList(); |
| 6984 |
int nonUniqueCount = columnsToInclude.length; |
| 6985 |
columns = Arrays.copyOf(columns, uniqueColumnCount + nonUniqueCount); |
| 6986 |
System.arraycopy(columnsToInclude, 0, columns, uniqueColumnCount, nonUniqueCount); |
| 6987 |
} |
| 6988 |
} else if (primaryKey) { |
| 6989 |
uniqueColumnCount = columns.length; |
| 6990 |
} |
| 6991 |
} |
| 6992 |
command.setIndexColumns(columns); |
| 6993 |
command.setUnique(nullsDistinct, uniqueColumnCount); |
| 6994 |
return command; |
| 6995 |
} |
| 6996 |
} |
| 6997 |
|
| 6998 |
private NullsDistinct readNullsDistinct(NullsDistinct defaultDistinct) { |
| 6999 |
if (readIf("NULLS")) { |
| 7000 |
if (readIf(DISTINCT)) { |
| 7001 |
return NullsDistinct.DISTINCT; |
| 7002 |
} |
| 7003 |
if (readIf(NOT, DISTINCT)) { |
| 7004 |
return NullsDistinct.NOT_DISTINCT; |
| 7005 |
} |
| 7006 |
if (readIf(ALL, DISTINCT)) { |
| 7007 |
return NullsDistinct.ALL_DISTINCT; |
| 7008 |
} |
| 7009 |
throw getSyntaxError(); |
| 7010 |
} |
| 7011 |
return defaultDistinct; |
| 7012 |
} |
| 7013 |
|
| 7014 |
/** |
| 7015 |
* @return true if we expect to see a TABLE clause |
| 7016 |
*/ |
| 7017 |
private boolean addRoleOrRight(GrantRevoke command) { |
| 7018 |
if (readIf(SELECT)) { |
| 7019 |
command.addRight(Right.SELECT); |
| 7020 |
return true; |
| 7021 |
} else if (readIf("DELETE")) { |
| 7022 |
command.addRight(Right.DELETE); |
| 7023 |
return true; |
| 7024 |
} else if (readIf("INSERT")) { |
| 7025 |
command.addRight(Right.INSERT); |
| 7026 |
return true; |
| 7027 |
} else if (readIf("UPDATE")) { |
| 7028 |
command.addRight(Right.UPDATE); |
| 7029 |
return true; |
| 7030 |
} else if (readIf("CONNECT")) { |
| 7031 |
// ignore this right |
| 7032 |
return true; |
| 7033 |
} else if (readIf("RESOURCE")) { |
| 7034 |
// ignore this right |
| 7035 |
return true; |
| 7036 |
} else { |
| 7037 |
command.addRoleName(readIdentifier()); |
| 7038 |
return false; |
| 7039 |
} |
| 7040 |
} |
| 7041 |
|
| 7042 |
private GrantRevoke parseGrantRevoke(int operationType) { |
| 7043 |
GrantRevoke command = new GrantRevoke(session); |
| 7044 |
command.setOperationType(operationType); |
| 7045 |
boolean tableClauseExpected; |
| 7046 |
if (readIf(ALL)) { |
| 7047 |
readIf("PRIVILEGES"); |
| 7048 |
command.addRight(Right.ALL); |
| 7049 |
tableClauseExpected = true; |
| 7050 |
} else if (readIf("ALTER")) { |
| 7051 |
read(ANY); |
| 7052 |
read("SCHEMA"); |
| 7053 |
command.addRight(Right.ALTER_ANY_SCHEMA); |
| 7054 |
command.addTable(null); |
| 7055 |
tableClauseExpected = false; |
| 7056 |
} else { |
| 7057 |
tableClauseExpected = addRoleOrRight(command); |
| 7058 |
while (readIf(COMMA)) { |
| 7059 |
if (addRoleOrRight(command) != tableClauseExpected) { |
| 7060 |
throw DbException.get(ErrorCode.ROLES_AND_RIGHT_CANNOT_BE_MIXED); |
| 7061 |
} |
| 7062 |
} |
| 7063 |
} |
| 7064 |
if (tableClauseExpected) { |
| 7065 |
if (readIf(ON)) { |
| 7066 |
if (readIf("SCHEMA")) { |
| 7067 |
command.setSchema(database.getSchema(readIdentifier())); |
| 7068 |
} else { |
| 7069 |
readIf(TABLE); |
| 7070 |
do { |
| 7071 |
Table table = readTableOrView(); |
| 7072 |
command.addTable(table); |
| 7073 |
} while (readIf(COMMA)); |
| 7074 |
} |
| 7075 |
} |
| 7076 |
} |
| 7077 |
read(operationType == CommandInterface.GRANT ? TO : FROM); |
| 7078 |
command.setGranteeName(readIdentifier()); |
| 7079 |
return command; |
| 7080 |
} |
| 7081 |
|
| 7082 |
private TableValueConstructor parseValues() { |
| 7083 |
ArrayList<ArrayList<Expression>> rows = Utils.newSmallArrayList(); |
| 7084 |
ArrayList<Expression> row = parseValuesRow(Utils.newSmallArrayList()); |
| 7085 |
rows.add(row); |
| 7086 |
int columnCount = row.size(); |
| 7087 |
while (readIf(COMMA)) { |
| 7088 |
row = parseValuesRow(new ArrayList<>(columnCount)); |
| 7089 |
if (row.size() != columnCount) { |
| 7090 |
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); |
| 7091 |
} |
| 7092 |
rows.add(row); |
| 7093 |
} |
| 7094 |
return new TableValueConstructor(session, rows); |
| 7095 |
} |
| 7096 |
|
| 7097 |
private ArrayList<Expression> parseValuesRow(ArrayList<Expression> row) { |
| 7098 |
if (!readIf(ROW, OPEN_PAREN) && !readIf(OPEN_PAREN)) { |
| 7099 |
row.add(readExpression()); |
| 7100 |
return row; |
| 7101 |
} |
| 7102 |
do { |
| 7103 |
row.add(readExpression()); |
| 7104 |
} while (readIfMore()); |
| 7105 |
return row; |
| 7106 |
} |
| 7107 |
|
| 7108 |
private Call parseCall() { |
| 7109 |
Call command = new Call(session); |
| 7110 |
currentPrepared = command; |
| 7111 |
if (readIf(TABLE, OPEN_PAREN)) { |
| 7112 |
command.setTableFunction(readTableFunction(ArrayTableFunction.TABLE)); |
| 7113 |
return command; |
| 7114 |
} |
| 7115 |
int index = tokenIndex; |
| 7116 |
boolean canBeFunction = isIdentifier(); |
| 7117 |
try { |
| 7118 |
command.setExpression(readExpression()); |
| 7119 |
} catch (DbException e) { |
| 7120 |
if (canBeFunction && e.getErrorCode() == ErrorCode.FUNCTION_NOT_FOUND_1) { |
| 7121 |
setTokenIndex(index); |
| 7122 |
String schemaName = null, name = readIdentifier(); |
| 7123 |
if (readIf(DOT)) { |
| 7124 |
schemaName = name; |
| 7125 |
name = readIdentifier(); |
| 7126 |
if (readIf(DOT)) { |
| 7127 |
checkDatabaseName(schemaName); |
| 7128 |
schemaName = name; |
| 7129 |
name = readIdentifier(); |
| 7130 |
} |
| 7131 |
} |
| 7132 |
read(OPEN_PAREN); |
| 7133 |
Schema schema = schemaName != null ? database.getSchema(schemaName) : null; |
| 7134 |
command.setTableFunction(readTableFunction(name, schema)); |
| 7135 |
return command; |
| 7136 |
} |
| 7137 |
throw e; |
| 7138 |
} |
| 7139 |
return command; |
| 7140 |
} |
| 7141 |
|
| 7142 |
private CreateRole parseCreateRole() { |
| 7143 |
CreateRole command = new CreateRole(session); |
| 7144 |
command.setIfNotExists(readIfNotExists()); |
| 7145 |
command.setRoleName(readIdentifier()); |
| 7146 |
return command; |
| 7147 |
} |
| 7148 |
|
| 7149 |
private CreateSchema parseCreateSchema() { |
| 7150 |
CreateSchema command = new CreateSchema(session); |
| 7151 |
command.setIfNotExists(readIfNotExists()); |
| 7152 |
String authorization; |
| 7153 |
if (readIf(AUTHORIZATION)) { |
| 7154 |
authorization = readIdentifier(); |
| 7155 |
command.setSchemaName(authorization); |
| 7156 |
command.setAuthorization(authorization); |
| 7157 |
} else { |
| 7158 |
command.setSchemaName(readIdentifier()); |
| 7159 |
if (readIf(AUTHORIZATION)) { |
| 7160 |
authorization = readIdentifier(); |
| 7161 |
} else { |
| 7162 |
authorization = session.getUser().getName(); |
| 7163 |
} |
| 7164 |
} |
| 7165 |
command.setAuthorization(authorization); |
| 7166 |
if (readIf(WITH)) { |
| 7167 |
command.setTableEngineParams(readTableEngineParams()); |
| 7168 |
} |
| 7169 |
return command; |
| 7170 |
} |
| 7171 |
|
| 7172 |
private ArrayList<String> readTableEngineParams() { |
| 7173 |
ArrayList<String> tableEngineParams = Utils.newSmallArrayList(); |
| 7174 |
do { |
| 7175 |
tableEngineParams.add(readIdentifier()); |
| 7176 |
} while (readIf(COMMA)); |
| 7177 |
return tableEngineParams; |
| 7178 |
} |
| 7179 |
|
| 7180 |
private CreateSequence parseCreateSequence() { |
| 7181 |
boolean ifNotExists = readIfNotExists(); |
| 7182 |
String sequenceName = readIdentifierWithSchema(); |
| 7183 |
CreateSequence command = new CreateSequence(session, getSchema()); |
| 7184 |
command.setIfNotExists(ifNotExists); |
| 7185 |
command.setSequenceName(sequenceName); |
| 7186 |
SequenceOptions options = new SequenceOptions(); |
| 7187 |
parseSequenceOptions(options, command, true, false); |
| 7188 |
command.setOptions(options); |
| 7189 |
return command; |
| 7190 |
} |
| 7191 |
|
| 7192 |
private boolean readIfNotExists() { |
| 7193 |
if (readIf(IF, NOT, EXISTS)) { |
| 7194 |
return true; |
| 7195 |
} |
| 7196 |
return false; |
| 7197 |
} |
| 7198 |
|
| 7199 |
private CreateConstant parseCreateConstant() { |
| 7200 |
boolean ifNotExists = readIfNotExists(); |
| 7201 |
String constantName = readIdentifierWithSchema(); |
| 7202 |
Schema schema = getSchema(); |
| 7203 |
if (isKeyword(constantName)) { |
| 7204 |
throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, |
| 7205 |
constantName); |
| 7206 |
} |
| 7207 |
read(VALUE); |
| 7208 |
Expression expr = readExpression(); |
| 7209 |
CreateConstant command = new CreateConstant(session, schema); |
| 7210 |
command.setConstantName(constantName); |
| 7211 |
command.setExpression(expr); |
| 7212 |
command.setIfNotExists(ifNotExists); |
| 7213 |
return command; |
| 7214 |
} |
| 7215 |
|
| 7216 |
private CreateAggregate parseCreateAggregate(boolean force) { |
| 7217 |
boolean ifNotExists = readIfNotExists(); |
| 7218 |
String name = readIdentifierWithSchema(), upperName; |
| 7219 |
if (isKeyword(name) || BuiltinFunctions.isBuiltinFunction(database, upperName = upperName(name)) |
| 7220 |
|| Aggregate.getAggregateType(upperName) != null) { |
| 7221 |
throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, name); |
| 7222 |
} |
| 7223 |
CreateAggregate command = new CreateAggregate(session, getSchema()); |
| 7224 |
command.setForce(force); |
| 7225 |
command.setName(name); |
| 7226 |
command.setIfNotExists(ifNotExists); |
| 7227 |
read(FOR); |
| 7228 |
command.setJavaClassMethod(readStringOrIdentifier()); |
| 7229 |
return command; |
| 7230 |
} |
| 7231 |
|
| 7232 |
private CreateDomain parseCreateDomain() { |
| 7233 |
boolean ifNotExists = readIfNotExists(); |
| 7234 |
String domainName = readIdentifierWithSchema(); |
| 7235 |
Schema schema = getSchema(); |
| 7236 |
CreateDomain command = new CreateDomain(session, schema); |
| 7237 |
command.setIfNotExists(ifNotExists); |
| 7238 |
command.setTypeName(domainName); |
| 7239 |
readIf(AS); |
| 7240 |
TypeInfo dataType = readIfDataType(); |
| 7241 |
if (dataType != null) { |
| 7242 |
command.setDataType(dataType); |
| 7243 |
} else { |
| 7244 |
String parentDomainName = readIdentifierWithSchema(); |
| 7245 |
command.setParentDomain(getSchema().getDomain(parentDomainName)); |
| 7246 |
} |
| 7247 |
if (readIf(DEFAULT)) { |
| 7248 |
command.setDefaultExpression(readExpression()); |
| 7249 |
} |
| 7250 |
if (readIf(ON, "UPDATE")) { |
| 7251 |
command.setOnUpdateExpression(readExpression()); |
| 7252 |
} |
| 7253 |
// Compatibility with 1.4.200 and older versions |
| 7254 |
if (readIf("SELECTIVITY")) { |
| 7255 |
readNonNegativeInt(); |
| 7256 |
} |
| 7257 |
String comment = readCommentIf(); |
| 7258 |
if (comment != null) { |
| 7259 |
command.setComment(comment); |
| 7260 |
} |
| 7261 |
for (;;) { |
| 7262 |
String constraintName; |
| 7263 |
if (readIf(CONSTRAINT)) { |
| 7264 |
constraintName = readIdentifier(); |
| 7265 |
read(CHECK); |
| 7266 |
} else if (readIf(CHECK)) { |
| 7267 |
constraintName = null; |
| 7268 |
} else { |
| 7269 |
break; |
| 7270 |
} |
| 7271 |
AlterDomainAddConstraint constraint = new AlterDomainAddConstraint(session, schema, ifNotExists); |
| 7272 |
constraint.setConstraintName(constraintName); |
| 7273 |
constraint.setDomainName(domainName); |
| 7274 |
parseDomainConstraint = true; |
| 7275 |
try { |
| 7276 |
constraint.setCheckExpression(readExpression()); |
| 7277 |
} finally { |
| 7278 |
parseDomainConstraint = false; |
| 7279 |
} |
| 7280 |
command.addConstraintCommand(constraint); |
| 7281 |
} |
| 7282 |
return command; |
| 7283 |
} |
| 7284 |
|
| 7285 |
private CreateTrigger parseCreateTrigger(boolean force) { |
| 7286 |
boolean ifNotExists = readIfNotExists(); |
| 7287 |
String triggerName = readIdentifierWithSchema(null); |
| 7288 |
Schema schema = getSchema(); |
| 7289 |
boolean insteadOf, isBefore; |
| 7290 |
if (readIf("INSTEAD", "OF")) { |
| 7291 |
isBefore = true; |
| 7292 |
insteadOf = true; |
| 7293 |
} else if (readIf("BEFORE")) { |
| 7294 |
insteadOf = false; |
| 7295 |
isBefore = true; |
| 7296 |
} else { |
| 7297 |
read("AFTER"); |
| 7298 |
insteadOf = false; |
| 7299 |
isBefore = false; |
| 7300 |
} |
| 7301 |
int typeMask = 0; |
| 7302 |
boolean onRollback = false; |
| 7303 |
boolean allowOr = database.getMode().getEnum() == ModeEnum.PostgreSQL; |
| 7304 |
do { |
| 7305 |
if (readIf("INSERT")) { |
| 7306 |
typeMask |= Trigger.INSERT; |
| 7307 |
} else if (readIf("UPDATE")) { |
| 7308 |
typeMask |= Trigger.UPDATE; |
| 7309 |
} else if (readIf("DELETE")) { |
| 7310 |
typeMask |= Trigger.DELETE; |
| 7311 |
} else if (readIf(SELECT)) { |
| 7312 |
typeMask |= Trigger.SELECT; |
| 7313 |
} else if (readIf("ROLLBACK")) { |
| 7314 |
onRollback = true; |
| 7315 |
} else { |
| 7316 |
throw getSyntaxError(); |
| 7317 |
} |
| 7318 |
} while (readIf(COMMA) || allowOr && readIf(OR)); |
| 7319 |
read(ON); |
| 7320 |
String tableName = readIdentifierWithSchema(); |
| 7321 |
checkSchema(schema); |
| 7322 |
CreateTrigger command = new CreateTrigger(session, getSchema()); |
| 7323 |
command.setForce(force); |
| 7324 |
command.setTriggerName(triggerName); |
| 7325 |
command.setIfNotExists(ifNotExists); |
| 7326 |
command.setInsteadOf(insteadOf); |
| 7327 |
command.setBefore(isBefore); |
| 7328 |
command.setOnRollback(onRollback); |
| 7329 |
command.setTypeMask(typeMask); |
| 7330 |
command.setTableName(tableName); |
| 7331 |
if (readIf(FOR, "EACH")) { |
| 7332 |
if (readIf(ROW)) { |
| 7333 |
command.setRowBased(true); |
| 7334 |
} else { |
| 7335 |
read("STATEMENT"); |
| 7336 |
} |
| 7337 |
} |
| 7338 |
if (readIf("QUEUE")) { |
| 7339 |
command.setQueueSize(readNonNegativeInt()); |
| 7340 |
} |
| 7341 |
command.setNoWait(readIf("NOWAIT")); |
| 7342 |
if (readIf(AS)) { |
| 7343 |
command.setTriggerSource(readString()); |
| 7344 |
} else { |
| 7345 |
read("CALL"); |
| 7346 |
command.setTriggerClassName(readStringOrIdentifier()); |
| 7347 |
} |
| 7348 |
return command; |
| 7349 |
} |
| 7350 |
|
| 7351 |
private CreateUser parseCreateUser() { |
| 7352 |
CreateUser command = new CreateUser(session); |
| 7353 |
command.setIfNotExists(readIfNotExists()); |
| 7354 |
command.setUserName(readIdentifier()); |
| 7355 |
command.setComment(readCommentIf()); |
| 7356 |
if (readIf("PASSWORD")) { |
| 7357 |
command.setPassword(readExpression()); |
| 7358 |
} else if (readIf("SALT")) { |
| 7359 |
command.setSalt(readExpression()); |
| 7360 |
read("HASH"); |
| 7361 |
command.setHash(readExpression()); |
| 7362 |
} else if (readIf("IDENTIFIED")) { |
| 7363 |
read("BY"); |
| 7364 |
// uppercase if not quoted |
| 7365 |
command.setPassword(ValueExpression.get(ValueVarchar.get(readIdentifier()))); |
| 7366 |
} else { |
| 7367 |
throw getSyntaxError(); |
| 7368 |
} |
| 7369 |
if (readIf("ADMIN")) { |
| 7370 |
command.setAdmin(true); |
| 7371 |
} |
| 7372 |
return command; |
| 7373 |
} |
| 7374 |
|
| 7375 |
private CreateFunctionAlias parseCreateFunctionAlias(boolean force) { |
| 7376 |
boolean ifNotExists = readIfNotExists(); |
| 7377 |
String aliasName; |
| 7378 |
if (currentTokenType == IDENTIFIER) { |
| 7379 |
aliasName = readIdentifierWithSchema(); |
| 7380 |
} else if (isKeyword(currentTokenType)) { |
| 7381 |
aliasName = currentToken; |
| 7382 |
read(); |
| 7383 |
schemaName = session.getCurrentSchemaName(); |
| 7384 |
} else { |
| 7385 |
addExpected("identifier"); |
| 7386 |
throw getSyntaxError(); |
| 7387 |
} |
| 7388 |
String upperName = upperName(aliasName); |
| 7389 |
if (isReservedFunctionName(upperName)) { |
| 7390 |
throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, aliasName); |
| 7391 |
} |
| 7392 |
CreateFunctionAlias command = new CreateFunctionAlias(session, getSchema()); |
| 7393 |
command.setForce(force); |
| 7394 |
command.setAliasName(aliasName); |
| 7395 |
command.setIfNotExists(ifNotExists); |
| 7396 |
command.setDeterministic(readIf("DETERMINISTIC")); |
| 7397 |
// Compatibility with old versions of H2 |
| 7398 |
readIf("NOBUFFER"); |
| 7399 |
if (readIf(AS)) { |
| 7400 |
command.setSource(readString()); |
| 7401 |
} else { |
| 7402 |
read(FOR); |
| 7403 |
command.setJavaClassMethod(readStringOrIdentifier()); |
| 7404 |
} |
| 7405 |
return command; |
| 7406 |
} |
| 7407 |
|
| 7408 |
private String readStringOrIdentifier() { |
| 7409 |
return currentTokenType != IDENTIFIER ? readString() : readIdentifier(); |
| 7410 |
} |
| 7411 |
|
| 7412 |
private boolean isReservedFunctionName(String name) { |
| 7413 |
int tokenType = ParserUtil.getTokenType(name, false, false); |
| 7414 |
if (tokenType != ParserUtil.IDENTIFIER) { |
| 7415 |
if (database.isAllowBuiltinAliasOverride()) { |
| 7416 |
switch (tokenType) { |
| 7417 |
case CURRENT_DATE: |
| 7418 |
case CURRENT_TIME: |
| 7419 |
case CURRENT_TIMESTAMP: |
| 7420 |
case DAY: |
| 7421 |
case HOUR: |
| 7422 |
case LOCALTIME: |
| 7423 |
case LOCALTIMESTAMP: |
| 7424 |
case MINUTE: |
| 7425 |
case MONTH: |
| 7426 |
case SECOND: |
| 7427 |
case YEAR: |
| 7428 |
return false; |
| 7429 |
} |
| 7430 |
} |
| 7431 |
return true; |
| 7432 |
} |
| 7433 |
return Aggregate.getAggregateType(name) != null |
| 7434 |
|| BuiltinFunctions.isBuiltinFunction(database, name) && !database.isAllowBuiltinAliasOverride(); |
| 7435 |
} |
| 7436 |
|
| 7437 |
private Prepared parseWith() { |
| 7438 |
List<TableView> viewsCreated = new ArrayList<>(); |
| 7439 |
try { |
| 7440 |
return parseWith1(viewsCreated); |
| 7441 |
} catch (Throwable t) { |
| 7442 |
CommandContainer.clearCTE(session, viewsCreated); |
| 7443 |
throw t; |
| 7444 |
} |
| 7445 |
} |
| 7446 |
|
| 7447 |
private Prepared parseWith1(List<TableView> viewsCreated) { |
| 7448 |
readIf("RECURSIVE"); |
| 7449 |
|
| 7450 |
// This WITH statement is not a temporary view - it is part of a persistent view |
| 7451 |
// as in CREATE VIEW abc AS WITH my_cte - this auto detects that condition. |
| 7452 |
final boolean isTemporary = !session.isParsingCreateView(); |
| 7453 |
|
| 7454 |
do { |
| 7455 |
viewsCreated.add(parseSingleCommonTableExpression(isTemporary)); |
| 7456 |
} while (readIf(COMMA)); |
| 7457 |
|
| 7458 |
Prepared p; |
| 7459 |
// Reverse the order of constructed CTE views - as the destruction order |
| 7460 |
// (since later created view may depend on previously created views - |
| 7461 |
// we preserve that dependency order in the destruction sequence ) |
| 7462 |
// used in setCteCleanups. |
| 7463 |
Collections.reverse(viewsCreated); |
| 7464 |
|
| 7465 |
int start = tokenIndex; |
| 7466 |
if (isQueryQuick()) { |
| 7467 |
p = parseWithQuery(); |
| 7468 |
} else if (readIf("INSERT")) { |
| 7469 |
p = parseInsert(start); |
| 7470 |
p.setPrepareAlways(true); |
| 7471 |
} else if (readIf("UPDATE")) { |
| 7472 |
p = parseUpdate(start); |
| 7473 |
p.setPrepareAlways(true); |
| 7474 |
} else if (readIf("MERGE")) { |
| 7475 |
p = parseMerge(start); |
| 7476 |
p.setPrepareAlways(true); |
| 7477 |
} else if (readIf("DELETE")) { |
| 7478 |
p = parseDelete(start); |
| 7479 |
p.setPrepareAlways(true); |
| 7480 |
} else if (readIf("CREATE")) { |
| 7481 |
if (!isToken(TABLE)) { |
| 7482 |
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, |
| 7483 |
WITH_STATEMENT_SUPPORTS_LIMITED_SUB_STATEMENTS); |
| 7484 |
} |
| 7485 |
p = parseCreate(); |
| 7486 |
p.setPrepareAlways(true); |
| 7487 |
} else { |
| 7488 |
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, |
| 7489 |
WITH_STATEMENT_SUPPORTS_LIMITED_SUB_STATEMENTS); |
| 7490 |
} |
| 7491 |
|
| 7492 |
// Clean up temporary views starting with last to first (in case of |
| 7493 |
// dependencies) - but only if they are not persistent. |
| 7494 |
if (isTemporary) { |
| 7495 |
if (cteCleanups == null) { |
| 7496 |
cteCleanups = new ArrayList<>(viewsCreated.size()); |
| 7497 |
} |
| 7498 |
cteCleanups.addAll(viewsCreated); |
| 7499 |
} |
| 7500 |
return p; |
| 7501 |
} |
| 7502 |
|
| 7503 |
private Prepared parseWithQuery() { |
| 7504 |
Query query = parseQueryExpressionBodyAndEndOfQuery(); |
| 7505 |
query.setPrepareAlways(true); |
| 7506 |
query.setNeverLazy(true); |
| 7507 |
return query; |
| 7508 |
} |
| 7509 |
|
| 7510 |
private TableView parseSingleCommonTableExpression(boolean isTemporary) { |
| 7511 |
String cteViewName = readIdentifierWithSchema(); |
| 7512 |
Schema schema = getSchema(); |
| 7513 |
ArrayList<Column> columns = Utils.newSmallArrayList(); |
| 7514 |
String[] cols = null; |
| 7515 |
|
| 7516 |
// column names are now optional - they can be inferred from the named |
| 7517 |
// query, if not supplied by user |
| 7518 |
if (readIf(OPEN_PAREN)) { |
| 7519 |
cols = parseColumnList(); |
| 7520 |
for (String c : cols) { |
| 7521 |
// we don't really know the type of the column, so STRING will |
| 7522 |
// have to do, UNKNOWN does not work here |
| 7523 |
columns.add(new Column(c, TypeInfo.TYPE_VARCHAR)); |
| 7524 |
} |
| 7525 |
} |
| 7526 |
|
| 7527 |
Table oldViewFound; |
| 7528 |
if (!isTemporary) { |
| 7529 |
oldViewFound = getSchema().findTableOrView(session, cteViewName); |
| 7530 |
} else { |
| 7531 |
oldViewFound = session.findLocalTempTable(cteViewName); |
| 7532 |
} |
| 7533 |
// this persistent check conflicts with check 10 lines down |
| 7534 |
if (oldViewFound != null) { |
| 7535 |
if (!(oldViewFound instanceof TableView)) { |
| 7536 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, |
| 7537 |
cteViewName); |
| 7538 |
} |
| 7539 |
TableView tv = (TableView) oldViewFound; |
| 7540 |
if (!tv.isTableExpression()) { |
| 7541 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, |
| 7542 |
cteViewName); |
| 7543 |
} |
| 7544 |
if (!isTemporary) { |
| 7545 |
oldViewFound.lock(session, Table.EXCLUSIVE_LOCK); |
| 7546 |
database.removeSchemaObject(session, oldViewFound); |
| 7547 |
|
| 7548 |
} else { |
| 7549 |
session.removeLocalTempTable(oldViewFound); |
| 7550 |
} |
| 7551 |
} |
| 7552 |
/* |
| 7553 |
* This table is created as a workaround because recursive table |
| 7554 |
* expressions need to reference something that look like themselves to |
| 7555 |
* work (its removed after creation in this method). Only create table |
| 7556 |
* data and table if we don't have a working CTE already. |
| 7557 |
*/ |
| 7558 |
Table recursiveTable = TableView.createShadowTableForRecursiveTableExpression( |
| 7559 |
isTemporary, session, cteViewName, schema, columns, database); |
| 7560 |
List<Column> columnTemplateList; |
| 7561 |
String[] querySQLOutput = new String[1]; |
| 7562 |
BitSet outerUsedParameters = initParametersScope(); |
| 7563 |
ArrayList<Parameter> queryParameters; |
| 7564 |
try { |
| 7565 |
read(AS); |
| 7566 |
read(OPEN_PAREN); |
| 7567 |
Query withQuery = parseQuery(); |
| 7568 |
if (!isTemporary) { |
| 7569 |
withQuery.session = session; |
| 7570 |
} |
| 7571 |
read(CLOSE_PAREN); |
| 7572 |
columnTemplateList = QueryExpressionTable.createQueryColumnTemplateList(cols, withQuery, querySQLOutput); |
| 7573 |
|
| 7574 |
} finally { |
| 7575 |
queryParameters = getUsedParameters(outerUsedParameters); |
| 7576 |
TableView.destroyShadowTableForRecursiveExpression(isTemporary, session, recursiveTable); |
| 7577 |
} |
| 7578 |
|
| 7579 |
return createCTEView(cteViewName, querySQLOutput[0], queryParameters, columnTemplateList, isTemporary); |
| 7580 |
} |
| 7581 |
|
| 7582 |
private TableView createCTEView(String cteViewName, String querySQL, ArrayList<Parameter> queryParameters, |
| 7583 |
List<Column> columnTemplateList, boolean isTemporary) { |
| 7584 |
Schema schema = getSchemaWithDefault(); |
| 7585 |
int id = database.allocateObjectId(); |
| 7586 |
Column[] columnTemplateArray = columnTemplateList.toArray(new Column[0]); |
| 7587 |
|
| 7588 |
// No easy way to determine if this is a recursive query up front, so we just compile |
| 7589 |
// it twice - once without the flag set, and if we didn't see a recursive term, |
| 7590 |
// then we just compile it again. |
| 7591 |
TableView view; |
| 7592 |
synchronized (session) { |
| 7593 |
view = new TableView(schema, id, cteViewName, querySQL, |
| 7594 |
queryParameters, columnTemplateArray, session, |
| 7595 |
true, false, true, |
| 7596 |
isTemporary); |
| 7597 |
if (!view.isRecursiveQueryDetected()) { |
| 7598 |
if (!isTemporary) { |
| 7599 |
database.addSchemaObject(session, view); |
| 7600 |
view.lock(session, Table.EXCLUSIVE_LOCK); |
| 7601 |
database.removeSchemaObject(session, view); |
| 7602 |
} else { |
| 7603 |
session.addLocalTempTable(view); |
| 7604 |
session.removeLocalTempTable(view); |
| 7605 |
} |
| 7606 |
id = database.allocateObjectId(); |
| 7607 |
view = new TableView(schema, id, cteViewName, querySQL, queryParameters, |
| 7608 |
columnTemplateArray, session, |
| 7609 |
false/* assume recursive */, false, true, |
| 7610 |
isTemporary); |
| 7611 |
} |
| 7612 |
// both removeSchemaObject and removeLocalTempTable hold meta locks |
| 7613 |
database.unlockMeta(session); |
| 7614 |
} |
| 7615 |
view.setTableExpression(true); |
| 7616 |
view.setTemporary(isTemporary); |
| 7617 |
view.setHidden(true); |
| 7618 |
view.setOnCommitDrop(false); |
| 7619 |
if (!isTemporary) { |
| 7620 |
database.addSchemaObject(session, view); |
| 7621 |
view.unlock(session); |
| 7622 |
database.unlockMeta(session); |
| 7623 |
} else { |
| 7624 |
session.addLocalTempTable(view); |
| 7625 |
} |
| 7626 |
return view; |
| 7627 |
} |
| 7628 |
|
| 7629 |
private CreateView parseCreateView(boolean force, boolean orReplace) { |
| 7630 |
boolean ifNotExists = readIfNotExists(); |
| 7631 |
boolean isTableExpression = readIf("TABLE_EXPRESSION"); |
| 7632 |
String viewName = readIdentifierWithSchema(); |
| 7633 |
CreateView command = new CreateView(session, getSchema()); |
| 7634 |
this.createView = command; |
| 7635 |
command.setViewName(viewName); |
| 7636 |
command.setIfNotExists(ifNotExists); |
| 7637 |
command.setComment(readCommentIf()); |
| 7638 |
command.setOrReplace(orReplace); |
| 7639 |
command.setForce(force); |
| 7640 |
command.setTableExpression(isTableExpression); |
| 7641 |
if (readIf(OPEN_PAREN)) { |
| 7642 |
String[] cols = parseColumnList(); |
| 7643 |
command.setColumnNames(cols); |
| 7644 |
} |
| 7645 |
read(AS); |
| 7646 |
String select = StringUtils.cache(sqlCommand.substring(token.start())); |
| 7647 |
try { |
| 7648 |
Query query; |
| 7649 |
session.setParsingCreateView(true); |
| 7650 |
try { |
| 7651 |
query = parseQuery(); |
| 7652 |
query.prepare(); |
| 7653 |
} finally { |
| 7654 |
session.setParsingCreateView(false); |
| 7655 |
} |
| 7656 |
command.setSelect(query); |
| 7657 |
} catch (DbException e) { |
| 7658 |
if (force) { |
| 7659 |
command.setSelectSQL(select); |
| 7660 |
while (currentTokenType != END_OF_INPUT) { |
| 7661 |
read(); |
| 7662 |
} |
| 7663 |
} else { |
| 7664 |
throw e; |
| 7665 |
} |
| 7666 |
} |
| 7667 |
return command; |
| 7668 |
} |
| 7669 |
|
| 7670 |
private CreateMaterializedView parseCreateMaterializedView(boolean force, boolean orReplace) { |
| 7671 |
boolean ifNotExists = readIfNotExists(); |
| 7672 |
String viewName = readIdentifierWithSchema(); |
| 7673 |
read(AS); |
| 7674 |
CreateMaterializedView command = new CreateMaterializedView(session, getSchema()); |
| 7675 |
command.setViewName(viewName); |
| 7676 |
command.setIfNotExists(ifNotExists); |
| 7677 |
command.setComment(readCommentIf()); |
| 7678 |
command.setOrReplace(orReplace); |
| 7679 |
if (force) { |
| 7680 |
throw new UnsupportedOperationException("not yet implemented"); |
| 7681 |
} |
| 7682 |
String select = StringUtils.cache(sqlCommand.substring(token.start())); |
| 7683 |
Query query; |
| 7684 |
session.setParsingCreateView(true); |
| 7685 |
try { |
| 7686 |
query = parseQuery(); |
| 7687 |
} finally { |
| 7688 |
session.setParsingCreateView(false); |
| 7689 |
} |
| 7690 |
command.setSelect(query); |
| 7691 |
command.setSelectSQL(select); |
| 7692 |
return command; |
| 7693 |
} |
| 7694 |
|
| 7695 |
private TransactionCommand parseCheckpoint() { |
| 7696 |
TransactionCommand command; |
| 7697 |
if (readIf("SYNC")) { |
| 7698 |
command = new TransactionCommand(session, |
| 7699 |
CommandInterface.CHECKPOINT_SYNC); |
| 7700 |
} else { |
| 7701 |
command = new TransactionCommand(session, |
| 7702 |
CommandInterface.CHECKPOINT); |
| 7703 |
} |
| 7704 |
return command; |
| 7705 |
} |
| 7706 |
|
| 7707 |
private Prepared parseAlter() { |
| 7708 |
if (readIf(TABLE)) { |
| 7709 |
return parseAlterTable(); |
| 7710 |
} else if (readIf(USER)) { |
| 7711 |
return parseAlterUser(); |
| 7712 |
} else if (readIf("INDEX")) { |
| 7713 |
return parseAlterIndex(); |
| 7714 |
} else if (readIf("SCHEMA")) { |
| 7715 |
return parseAlterSchema(); |
| 7716 |
} else if (readIf("SEQUENCE")) { |
| 7717 |
return parseAlterSequence(); |
| 7718 |
} else if (readIf("VIEW")) { |
| 7719 |
return parseAlterView(); |
| 7720 |
} else if (readIf("DOMAIN")) { |
| 7721 |
return parseAlterDomain(); |
| 7722 |
} |
| 7723 |
throw getSyntaxError(); |
| 7724 |
} |
| 7725 |
|
| 7726 |
private void checkSchema(Schema old) { |
| 7727 |
if (old != null && getSchema() != old) { |
| 7728 |
throw DbException.get(ErrorCode.SCHEMA_NAME_MUST_MATCH); |
| 7729 |
} |
| 7730 |
} |
| 7731 |
|
| 7732 |
private AlterIndexRename parseAlterIndex() { |
| 7733 |
boolean ifExists = readIfExists(false); |
| 7734 |
String indexName = readIdentifierWithSchema(); |
| 7735 |
Schema old = getSchema(); |
| 7736 |
AlterIndexRename command = new AlterIndexRename(session); |
| 7737 |
command.setOldSchema(old); |
| 7738 |
command.setOldName(indexName); |
| 7739 |
command.setIfExists(ifExists); |
| 7740 |
read("RENAME"); |
| 7741 |
read(TO); |
| 7742 |
String newName = readIdentifierWithSchema(old.getName()); |
| 7743 |
checkSchema(old); |
| 7744 |
command.setNewName(newName); |
| 7745 |
return command; |
| 7746 |
} |
| 7747 |
|
| 7748 |
private DefineCommand parseAlterDomain() { |
| 7749 |
boolean ifDomainExists = readIfExists(false); |
| 7750 |
String domainName = readIdentifierWithSchema(); |
| 7751 |
Schema schema = getSchema(); |
| 7752 |
if (readIf("ADD")) { |
| 7753 |
boolean ifNotExists = false; |
| 7754 |
String constraintName = null; |
| 7755 |
String comment = null; |
| 7756 |
if (readIf(CONSTRAINT)) { |
| 7757 |
ifNotExists = readIfNotExists(); |
| 7758 |
constraintName = readIdentifierWithSchema(schema.getName()); |
| 7759 |
checkSchema(schema); |
| 7760 |
comment = readCommentIf(); |
| 7761 |
} |
| 7762 |
read(CHECK); |
| 7763 |
AlterDomainAddConstraint command = new AlterDomainAddConstraint(session, schema, ifNotExists); |
| 7764 |
command.setDomainName(domainName); |
| 7765 |
command.setConstraintName(constraintName); |
| 7766 |
parseDomainConstraint = true; |
| 7767 |
try { |
| 7768 |
command.setCheckExpression(readExpression()); |
| 7769 |
} finally { |
| 7770 |
parseDomainConstraint = false; |
| 7771 |
} |
| 7772 |
command.setIfDomainExists(ifDomainExists); |
| 7773 |
command.setComment(comment); |
| 7774 |
if (readIf("NOCHECK")) { |
| 7775 |
command.setCheckExisting(false); |
| 7776 |
} else { |
| 7777 |
readIf(CHECK); |
| 7778 |
command.setCheckExisting(true); |
| 7779 |
} |
| 7780 |
return command; |
| 7781 |
} else if (readIf("DROP")) { |
| 7782 |
if (readIf(CONSTRAINT)) { |
| 7783 |
boolean ifConstraintExists = readIfExists(false); |
| 7784 |
String constraintName = readIdentifierWithSchema(schema.getName()); |
| 7785 |
checkSchema(schema); |
| 7786 |
AlterDomainDropConstraint command = new AlterDomainDropConstraint(session, getSchema(), |
| 7787 |
ifConstraintExists); |
| 7788 |
command.setConstraintName(constraintName); |
| 7789 |
command.setDomainName(domainName); |
| 7790 |
command.setIfDomainExists(ifDomainExists); |
| 7791 |
return command; |
| 7792 |
} else if (readIf(DEFAULT)) { |
| 7793 |
AlterDomainExpressions command = new AlterDomainExpressions(session, schema, |
| 7794 |
CommandInterface.ALTER_DOMAIN_DEFAULT); |
| 7795 |
command.setDomainName(domainName); |
| 7796 |
command.setIfDomainExists(ifDomainExists); |
| 7797 |
command.setExpression(null); |
| 7798 |
return command; |
| 7799 |
} else if (readIf(ON, "UPDATE")) { |
| 7800 |
AlterDomainExpressions command = new AlterDomainExpressions(session, schema, |
| 7801 |
CommandInterface.ALTER_DOMAIN_ON_UPDATE); |
| 7802 |
command.setDomainName(domainName); |
| 7803 |
command.setIfDomainExists(ifDomainExists); |
| 7804 |
command.setExpression(null); |
| 7805 |
return command; |
| 7806 |
} |
| 7807 |
} else if (readIf("RENAME")) { |
| 7808 |
if (readIf(CONSTRAINT)) { |
| 7809 |
String constraintName = readIdentifierWithSchema(schema.getName()); |
| 7810 |
checkSchema(schema); |
| 7811 |
read(TO); |
| 7812 |
AlterDomainRenameConstraint command = new AlterDomainRenameConstraint(session, schema); |
| 7813 |
command.setDomainName(domainName); |
| 7814 |
command.setIfDomainExists(ifDomainExists); |
| 7815 |
command.setConstraintName(constraintName); |
| 7816 |
command.setNewConstraintName(readIdentifier()); |
| 7817 |
return command; |
| 7818 |
} |
| 7819 |
read(TO); |
| 7820 |
String newName = readIdentifierWithSchema(schema.getName()); |
| 7821 |
checkSchema(schema); |
| 7822 |
AlterDomainRename command = new AlterDomainRename(session, getSchema()); |
| 7823 |
command.setDomainName(domainName); |
| 7824 |
command.setIfDomainExists(ifDomainExists); |
| 7825 |
command.setNewDomainName(newName); |
| 7826 |
return command; |
| 7827 |
} else { |
| 7828 |
read(SET); |
| 7829 |
if (readIf(DEFAULT)) { |
| 7830 |
AlterDomainExpressions command = new AlterDomainExpressions(session, schema, |
| 7831 |
CommandInterface.ALTER_DOMAIN_DEFAULT); |
| 7832 |
command.setDomainName(domainName); |
| 7833 |
command.setIfDomainExists(ifDomainExists); |
| 7834 |
command.setExpression(readExpression()); |
| 7835 |
return command; |
| 7836 |
} else if (readIf(ON, "UPDATE")) { |
| 7837 |
AlterDomainExpressions command = new AlterDomainExpressions(session, schema, |
| 7838 |
CommandInterface.ALTER_DOMAIN_ON_UPDATE); |
| 7839 |
command.setDomainName(domainName); |
| 7840 |
command.setIfDomainExists(ifDomainExists); |
| 7841 |
command.setExpression(readExpression()); |
| 7842 |
return command; |
| 7843 |
} |
| 7844 |
} |
| 7845 |
throw getSyntaxError(); |
| 7846 |
} |
| 7847 |
|
| 7848 |
private DefineCommand parseAlterView() { |
| 7849 |
boolean ifExists = readIfExists(false); |
| 7850 |
String viewName = readIdentifierWithSchema(); |
| 7851 |
Schema schema = getSchema(); |
| 7852 |
Table tableView = schema.findTableOrView(session, viewName); |
| 7853 |
if (!(tableView instanceof TableView) && !ifExists) { |
| 7854 |
throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); |
| 7855 |
} |
| 7856 |
if (readIf("RENAME", TO)) { |
| 7857 |
String newName = readIdentifierWithSchema(schema.getName()); |
| 7858 |
checkSchema(schema); |
| 7859 |
AlterTableRename command = new AlterTableRename(session, getSchema()); |
| 7860 |
command.setTableName(viewName); |
| 7861 |
command.setNewTableName(newName); |
| 7862 |
command.setIfTableExists(ifExists); |
| 7863 |
return command; |
| 7864 |
} else { |
| 7865 |
read("RECOMPILE"); |
| 7866 |
TableView view = (TableView) tableView; |
| 7867 |
AlterView command = new AlterView(session); |
| 7868 |
command.setIfExists(ifExists); |
| 7869 |
command.setView(view); |
| 7870 |
return command; |
| 7871 |
} |
| 7872 |
} |
| 7873 |
|
| 7874 |
private Prepared parseAlterSchema() { |
| 7875 |
boolean ifExists = readIfExists(false); |
| 7876 |
String schemaName = readIdentifierWithSchema(); |
| 7877 |
Schema old = getSchema(); |
| 7878 |
read("RENAME"); |
| 7879 |
read(TO); |
| 7880 |
String newName = readIdentifierWithSchema(old.getName()); |
| 7881 |
Schema schema = findSchema(schemaName); |
| 7882 |
if (schema == null) { |
| 7883 |
if (ifExists) { |
| 7884 |
return new NoOperation(session); |
| 7885 |
} |
| 7886 |
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName); |
| 7887 |
} |
| 7888 |
AlterSchemaRename command = new AlterSchemaRename(session); |
| 7889 |
command.setOldSchema(schema); |
| 7890 |
checkSchema(old); |
| 7891 |
command.setNewName(newName); |
| 7892 |
return command; |
| 7893 |
} |
| 7894 |
|
| 7895 |
private AlterSequence parseAlterSequence() { |
| 7896 |
boolean ifExists = readIfExists(false); |
| 7897 |
String sequenceName = readIdentifierWithSchema(); |
| 7898 |
AlterSequence command = new AlterSequence(session, getSchema()); |
| 7899 |
command.setSequenceName(sequenceName); |
| 7900 |
command.setIfExists(ifExists); |
| 7901 |
SequenceOptions options = new SequenceOptions(); |
| 7902 |
parseSequenceOptions(options, null, false, false); |
| 7903 |
command.setOptions(options); |
| 7904 |
return command; |
| 7905 |
} |
| 7906 |
|
| 7907 |
private boolean parseSequenceOptions(SequenceOptions options, CreateSequence command, boolean allowDataType, |
| 7908 |
boolean forAlterColumn) { |
| 7909 |
boolean result = false; |
| 7910 |
for (;;) { |
| 7911 |
if (allowDataType && readIf(AS)) { |
| 7912 |
TypeInfo dataType = parseDataType(); |
| 7913 |
if (!DataType.isNumericType(dataType.getValueType())) { |
| 7914 |
throw DbException.getUnsupportedException(dataType |
| 7915 |
.getSQL(new StringBuilder("CREATE SEQUENCE AS "), HasSQL.TRACE_SQL_FLAGS).toString()); |
| 7916 |
} |
| 7917 |
options.setDataType(dataType); |
| 7918 |
} else if (readIf("START", WITH) |
| 7919 |
|| (database.getMode().getEnum() == ModeEnum.PostgreSQL && readIf("START"))) { |
| 7920 |
options.setStartValue(readExpression()); |
| 7921 |
} else if (readIf("RESTART")) { |
| 7922 |
options.setRestartValue(readIf(WITH) ? readExpression() : ValueExpression.DEFAULT); |
| 7923 |
} else if (command != null && parseCreateSequenceOption(command)) { |
| 7924 |
// |
| 7925 |
} else if (forAlterColumn) { |
| 7926 |
int index = tokenIndex; |
| 7927 |
if (readIf(SET)) { |
| 7928 |
if (!parseBasicSequenceOption(options)) { |
| 7929 |
setTokenIndex(index); |
| 7930 |
break; |
| 7931 |
} |
| 7932 |
} else { |
| 7933 |
break; |
| 7934 |
} |
| 7935 |
} else if (!parseBasicSequenceOption(options)) { |
| 7936 |
break; |
| 7937 |
} |
| 7938 |
result = true; |
| 7939 |
} |
| 7940 |
return result; |
| 7941 |
} |
| 7942 |
|
| 7943 |
private boolean parseCreateSequenceOption(CreateSequence command) { |
| 7944 |
if (readIf("BELONGS_TO_TABLE")) { |
| 7945 |
command.setBelongsToTable(true); |
| 7946 |
} else if (readIf(ORDER)) { |
| 7947 |
// Oracle compatibility |
| 7948 |
} else { |
| 7949 |
return false; |
| 7950 |
} |
| 7951 |
return true; |
| 7952 |
} |
| 7953 |
|
| 7954 |
private boolean parseBasicSequenceOption(SequenceOptions options) { |
| 7955 |
if (readIf("INCREMENT")) { |
| 7956 |
// TODO Why BY is optional? |
| 7957 |
readIf("BY"); |
| 7958 |
options.setIncrement(readExpression()); |
| 7959 |
} else if (readIf("MINVALUE")) { |
| 7960 |
options.setMinValue(readExpression()); |
| 7961 |
} else if (readIf("MAXVALUE")) { |
| 7962 |
options.setMaxValue(readExpression()); |
| 7963 |
} else if (readIf("CYCLE")) { |
| 7964 |
options.setCycle(Sequence.Cycle.CYCLE); |
| 7965 |
} else if (readIf("NO")) { |
| 7966 |
if (readIf("MINVALUE")) { |
| 7967 |
options.setMinValue(ValueExpression.NULL); |
| 7968 |
} else if (readIf("MAXVALUE")) { |
| 7969 |
options.setMaxValue(ValueExpression.NULL); |
| 7970 |
} else if (readIf("CYCLE")) { |
| 7971 |
options.setCycle(Sequence.Cycle.NO_CYCLE); |
| 7972 |
} else if (readIf("CACHE")) { |
| 7973 |
options.setCacheSize(ValueExpression.get(ValueBigint.get(1))); |
| 7974 |
} else { |
| 7975 |
throw getSyntaxError(); |
| 7976 |
} |
| 7977 |
} else if (readIf("EXHAUSTED")) { |
| 7978 |
options.setCycle(Sequence.Cycle.EXHAUSTED); |
| 7979 |
} else if (readIf("CACHE")) { |
| 7980 |
options.setCacheSize(readExpression()); |
| 7981 |
// Various compatibility options |
| 7982 |
} else if (readIf("NOMINVALUE")) { |
| 7983 |
options.setMinValue(ValueExpression.NULL); |
| 7984 |
} else if (readIf("NOMAXVALUE")) { |
| 7985 |
options.setMaxValue(ValueExpression.NULL); |
| 7986 |
} else if (readIf("NOCYCLE")) { |
| 7987 |
options.setCycle(Sequence.Cycle.NO_CYCLE); |
| 7988 |
} else if (readIf("NOCACHE")) { |
| 7989 |
options.setCacheSize(ValueExpression.get(ValueBigint.get(1))); |
| 7990 |
} else { |
| 7991 |
return false; |
| 7992 |
} |
| 7993 |
return true; |
| 7994 |
} |
| 7995 |
|
| 7996 |
private AlterUser parseAlterUser() { |
| 7997 |
String userName = readIdentifier(); |
| 7998 |
if (readIf(SET)) { |
| 7999 |
AlterUser command = new AlterUser(session); |
| 8000 |
command.setType(CommandInterface.ALTER_USER_SET_PASSWORD); |
| 8001 |
command.setUser(database.getUser(userName)); |
| 8002 |
if (readIf("PASSWORD")) { |
| 8003 |
command.setPassword(readExpression()); |
| 8004 |
} else if (readIf("SALT")) { |
| 8005 |
command.setSalt(readExpression()); |
| 8006 |
read("HASH"); |
| 8007 |
command.setHash(readExpression()); |
| 8008 |
} else { |
| 8009 |
throw getSyntaxError(); |
| 8010 |
} |
| 8011 |
return command; |
| 8012 |
} else if (readIf("RENAME", TO)) { |
| 8013 |
AlterUser command = new AlterUser(session); |
| 8014 |
command.setType(CommandInterface.ALTER_USER_RENAME); |
| 8015 |
command.setUser(database.getUser(userName)); |
| 8016 |
command.setNewName(readIdentifier()); |
| 8017 |
return command; |
| 8018 |
} else if (readIf("ADMIN")) { |
| 8019 |
AlterUser command = new AlterUser(session); |
| 8020 |
command.setType(CommandInterface.ALTER_USER_ADMIN); |
| 8021 |
User user = database.getUser(userName); |
| 8022 |
command.setUser(user); |
| 8023 |
if (readIf(TRUE)) { |
| 8024 |
command.setAdmin(true); |
| 8025 |
} else if (readIf(FALSE)) { |
| 8026 |
command.setAdmin(false); |
| 8027 |
} else { |
| 8028 |
throw getSyntaxError(); |
| 8029 |
} |
| 8030 |
return command; |
| 8031 |
} |
| 8032 |
throw getSyntaxError(); |
| 8033 |
} |
| 8034 |
|
| 8035 |
private void readIfEqualOrTo() { |
| 8036 |
if (!readIf(EQUAL)) { |
| 8037 |
readIf(TO); |
| 8038 |
} |
| 8039 |
} |
| 8040 |
|
| 8041 |
private Prepared parseSet() { |
| 8042 |
if (readIf(AT)) { |
| 8043 |
Set command = new Set(session, SetTypes.VARIABLE); |
| 8044 |
command.setString(readIdentifier()); |
| 8045 |
readIfEqualOrTo(); |
| 8046 |
command.setExpression(readExpression()); |
| 8047 |
return command; |
| 8048 |
} else if (readIf("AUTOCOMMIT")) { |
| 8049 |
readIfEqualOrTo(); |
| 8050 |
return new TransactionCommand(session, readBooleanSetting() ? CommandInterface.SET_AUTOCOMMIT_TRUE |
| 8051 |
: CommandInterface.SET_AUTOCOMMIT_FALSE); |
| 8052 |
} else if (readIf("EXCLUSIVE")) { |
| 8053 |
readIfEqualOrTo(); |
| 8054 |
Set command = new Set(session, SetTypes.EXCLUSIVE); |
| 8055 |
command.setExpression(readExpression()); |
| 8056 |
return command; |
| 8057 |
} else if (readIf("IGNORECASE")) { |
| 8058 |
readIfEqualOrTo(); |
| 8059 |
Set command = new Set(session, SetTypes.IGNORECASE); |
| 8060 |
command.setInt(readBooleanSetting() ? 1 : 0); |
| 8061 |
return command; |
| 8062 |
} else if (readIf("PASSWORD")) { |
| 8063 |
readIfEqualOrTo(); |
| 8064 |
AlterUser command = new AlterUser(session); |
| 8065 |
command.setType(CommandInterface.ALTER_USER_SET_PASSWORD); |
| 8066 |
command.setUser(session.getUser()); |
| 8067 |
command.setPassword(readExpression()); |
| 8068 |
return command; |
| 8069 |
} else if (readIf("SALT")) { |
| 8070 |
readIfEqualOrTo(); |
| 8071 |
AlterUser command = new AlterUser(session); |
| 8072 |
command.setType(CommandInterface.ALTER_USER_SET_PASSWORD); |
| 8073 |
command.setUser(session.getUser()); |
| 8074 |
command.setSalt(readExpression()); |
| 8075 |
read("HASH"); |
| 8076 |
command.setHash(readExpression()); |
| 8077 |
return command; |
| 8078 |
} else if (readIf("MODE")) { |
| 8079 |
readIfEqualOrTo(); |
| 8080 |
Set command = new Set(session, SetTypes.MODE); |
| 8081 |
command.setString(readIdentifier()); |
| 8082 |
return command; |
| 8083 |
} else if (readIf("DATABASE")) { |
| 8084 |
readIfEqualOrTo(); |
| 8085 |
read("COLLATION"); |
| 8086 |
return parseSetCollation(); |
| 8087 |
} else if (readIf("COLLATION")) { |
| 8088 |
readIfEqualOrTo(); |
| 8089 |
return parseSetCollation(); |
| 8090 |
} else if (readIf("CLUSTER")) { |
| 8091 |
readIfEqualOrTo(); |
| 8092 |
Set command = new Set(session, SetTypes.CLUSTER); |
| 8093 |
command.setString(readString()); |
| 8094 |
return command; |
| 8095 |
} else if (readIf("DATABASE_EVENT_LISTENER")) { |
| 8096 |
readIfEqualOrTo(); |
| 8097 |
Set command = new Set(session, SetTypes.DATABASE_EVENT_LISTENER); |
| 8098 |
command.setString(readString()); |
| 8099 |
return command; |
| 8100 |
} else if (readIf("ALLOW_LITERALS")) { |
| 8101 |
readIfEqualOrTo(); |
| 8102 |
Set command = new Set(session, SetTypes.ALLOW_LITERALS); |
| 8103 |
int v; |
| 8104 |
if (readIf(ALL)) { |
| 8105 |
v = Constants.ALLOW_LITERALS_ALL; |
| 8106 |
} else if (readIf("NONE")) { |
| 8107 |
v = Constants.ALLOW_LITERALS_NONE; |
| 8108 |
} else if (readIf("NUMBERS")) { |
| 8109 |
v = Constants.ALLOW_LITERALS_NUMBERS; |
| 8110 |
} else { |
| 8111 |
v = readNonNegativeInt(); |
| 8112 |
} |
| 8113 |
command.setInt(v); |
| 8114 |
return command; |
| 8115 |
} else if (readIf("DEFAULT_TABLE_TYPE")) { |
| 8116 |
readIfEqualOrTo(); |
| 8117 |
Set command = new Set(session, SetTypes.DEFAULT_TABLE_TYPE); |
| 8118 |
int v; |
| 8119 |
if (readIf("MEMORY")) { |
| 8120 |
v = Table.TYPE_MEMORY; |
| 8121 |
} else if (readIf("CACHED")) { |
| 8122 |
v = Table.TYPE_CACHED; |
| 8123 |
} else { |
| 8124 |
v = readNonNegativeInt(); |
| 8125 |
} |
| 8126 |
command.setInt(v); |
| 8127 |
return command; |
| 8128 |
} else if (readIf("SCHEMA")) { |
| 8129 |
readIfEqualOrTo(); |
| 8130 |
Set command = new Set(session, SetTypes.SCHEMA); |
| 8131 |
command.setExpression(readExpressionOrIdentifier()); |
| 8132 |
return command; |
| 8133 |
} else if (readIf("CATALOG")) { |
| 8134 |
readIfEqualOrTo(); |
| 8135 |
Set command = new Set(session, SetTypes.CATALOG); |
| 8136 |
command.setExpression(readExpressionOrIdentifier()); |
| 8137 |
return command; |
| 8138 |
} else if (readIf(SetTypes.getTypeName(SetTypes.SCHEMA_SEARCH_PATH))) { |
| 8139 |
readIfEqualOrTo(); |
| 8140 |
Set command = new Set(session, SetTypes.SCHEMA_SEARCH_PATH); |
| 8141 |
ArrayList<String> list = Utils.newSmallArrayList(); |
| 8142 |
do { |
| 8143 |
list.add(readIdentifier()); |
| 8144 |
} while (readIf(COMMA)); |
| 8145 |
command.setStringArray(list.toArray(new String[0])); |
| 8146 |
return command; |
| 8147 |
} else if (readIf("JAVA_OBJECT_SERIALIZER")) { |
| 8148 |
readIfEqualOrTo(); |
| 8149 |
Set command = new Set(session, SetTypes.JAVA_OBJECT_SERIALIZER); |
| 8150 |
command.setString(readString()); |
| 8151 |
return command; |
| 8152 |
} else if (readIf("IGNORE_CATALOGS")) { |
| 8153 |
readIfEqualOrTo(); |
| 8154 |
Set command = new Set(session, SetTypes.IGNORE_CATALOGS); |
| 8155 |
command.setInt(readBooleanSetting() ? 1 : 0); |
| 8156 |
return command; |
| 8157 |
} else if (readIf("SESSION")) { |
| 8158 |
read("CHARACTERISTICS"); |
| 8159 |
read(AS); |
| 8160 |
read("TRANSACTION"); |
| 8161 |
return parseSetTransactionMode(); |
| 8162 |
} else if (readIf("TRANSACTION")) { |
| 8163 |
// TODO should affect only the current transaction |
| 8164 |
return parseSetTransactionMode(); |
| 8165 |
} else if (readIf("TIME")) { |
| 8166 |
read("ZONE"); |
| 8167 |
Set command = new Set(session, SetTypes.TIME_ZONE); |
| 8168 |
if (!readIf("LOCAL")) { |
| 8169 |
command.setExpression(readExpression()); |
| 8170 |
} |
| 8171 |
return command; |
| 8172 |
} else if (readIf("NON_KEYWORDS")) { |
| 8173 |
readIfEqualOrTo(); |
| 8174 |
Set command = new Set(session, SetTypes.NON_KEYWORDS); |
| 8175 |
ArrayList<String> list = Utils.newSmallArrayList(); |
| 8176 |
if (currentTokenType != END_OF_INPUT && currentTokenType != SEMICOLON) { |
| 8177 |
do { |
| 8178 |
list.add(StringUtils.toUpperEnglish(readIdentifierOrKeyword())); |
| 8179 |
} while (readIf(COMMA)); |
| 8180 |
} |
| 8181 |
command.setStringArray(list.toArray(new String[0])); |
| 8182 |
return command; |
| 8183 |
} else if (readIf("DEFAULT_NULL_ORDERING")) { |
| 8184 |
readIfEqualOrTo(); |
| 8185 |
Set command = new Set(session, SetTypes.DEFAULT_NULL_ORDERING); |
| 8186 |
command.setString(readIdentifier()); |
| 8187 |
return command; |
| 8188 |
} else if (readIf("LOG")) { |
| 8189 |
throw DbException.getUnsupportedException("LOG"); |
| 8190 |
} else { |
| 8191 |
String upperName = upperName(currentToken); |
| 8192 |
if (ConnectionInfo.isIgnoredByParser(upperName)) { |
| 8193 |
read(); |
| 8194 |
readIfEqualOrTo(); |
| 8195 |
read(); |
| 8196 |
return new NoOperation(session); |
| 8197 |
} |
| 8198 |
int type = SetTypes.getType(upperName); |
| 8199 |
if (type >= 0) { |
| 8200 |
read(); |
| 8201 |
readIfEqualOrTo(); |
| 8202 |
Set command = new Set(session, type); |
| 8203 |
command.setExpression(readExpression()); |
| 8204 |
return command; |
| 8205 |
} |
| 8206 |
ModeEnum modeEnum = database.getMode().getEnum(); |
| 8207 |
if (modeEnum != ModeEnum.REGULAR) { |
| 8208 |
Prepared command = readSetCompatibility(modeEnum); |
| 8209 |
if (command != null) { |
| 8210 |
return command; |
| 8211 |
} |
| 8212 |
} |
| 8213 |
if (session.isQuirksMode()) { |
| 8214 |
switch (upperName) { |
| 8215 |
case "BINARY_COLLATION": |
| 8216 |
case "UUID_COLLATION": |
| 8217 |
read(); |
| 8218 |
readIfEqualOrTo(); |
| 8219 |
readIdentifier(); |
| 8220 |
return new NoOperation(session); |
| 8221 |
} |
| 8222 |
} |
| 8223 |
throw getSyntaxError(); |
| 8224 |
} |
| 8225 |
} |
| 8226 |
|
| 8227 |
private Prepared parseSetTransactionMode() { |
| 8228 |
IsolationLevel isolationLevel; |
| 8229 |
read("ISOLATION"); |
| 8230 |
read("LEVEL"); |
| 8231 |
if (readIf("READ")) { |
| 8232 |
if (readIf("UNCOMMITTED")) { |
| 8233 |
isolationLevel = IsolationLevel.READ_UNCOMMITTED; |
| 8234 |
} else { |
| 8235 |
read("COMMITTED"); |
| 8236 |
isolationLevel = IsolationLevel.READ_COMMITTED; |
| 8237 |
} |
| 8238 |
} else if (readIf("REPEATABLE")) { |
| 8239 |
read("READ"); |
| 8240 |
isolationLevel = IsolationLevel.REPEATABLE_READ; |
| 8241 |
} else if (readIf("SNAPSHOT")) { |
| 8242 |
isolationLevel = IsolationLevel.SNAPSHOT; |
| 8243 |
} else { |
| 8244 |
read("SERIALIZABLE"); |
| 8245 |
isolationLevel = IsolationLevel.SERIALIZABLE; |
| 8246 |
} |
| 8247 |
return new SetSessionCharacteristics(session, isolationLevel); |
| 8248 |
} |
| 8249 |
|
| 8250 |
private Expression readExpressionOrIdentifier() { |
| 8251 |
if (isIdentifier()) { |
| 8252 |
return ValueExpression.get(ValueVarchar.get(readIdentifier())); |
| 8253 |
} |
| 8254 |
return readExpression(); |
| 8255 |
} |
| 8256 |
|
| 8257 |
private Prepared parseUse() { |
| 8258 |
readIfEqualOrTo(); |
| 8259 |
Set command = new Set(session, SetTypes.SCHEMA); |
| 8260 |
command.setExpression(ValueExpression.get(ValueVarchar.get(readIdentifier()))); |
| 8261 |
return command; |
| 8262 |
} |
| 8263 |
|
| 8264 |
private Set parseSetCollation() { |
| 8265 |
Set command = new Set(session, SetTypes.COLLATION); |
| 8266 |
String name = readIdentifier(); |
| 8267 |
command.setString(name); |
| 8268 |
if (equalsToken(name, CompareMode.OFF)) { |
| 8269 |
return command; |
| 8270 |
} |
| 8271 |
Collator coll = CompareMode.getCollator(name); |
| 8272 |
if (coll == null) { |
| 8273 |
throw DbException.getInvalidValueException("collation", name); |
| 8274 |
} |
| 8275 |
if (readIf("STRENGTH")) { |
| 8276 |
if (readIf(PRIMARY)) { |
| 8277 |
command.setInt(Collator.PRIMARY); |
| 8278 |
} else if (readIf("SECONDARY")) { |
| 8279 |
command.setInt(Collator.SECONDARY); |
| 8280 |
} else if (readIf("TERTIARY")) { |
| 8281 |
command.setInt(Collator.TERTIARY); |
| 8282 |
} else if (readIf("IDENTICAL")) { |
| 8283 |
command.setInt(Collator.IDENTICAL); |
| 8284 |
} |
| 8285 |
} else { |
| 8286 |
command.setInt(coll.getStrength()); |
| 8287 |
} |
| 8288 |
return command; |
| 8289 |
} |
| 8290 |
|
| 8291 |
private Prepared readSetCompatibility(ModeEnum modeEnum) { |
| 8292 |
switch (modeEnum) { |
| 8293 |
case Derby: |
| 8294 |
if (readIf("CREATE")) { |
| 8295 |
readIfEqualOrTo(); |
| 8296 |
// (CREATE=TRUE in the database URL) |
| 8297 |
read(); |
| 8298 |
return new NoOperation(session); |
| 8299 |
} |
| 8300 |
break; |
| 8301 |
case HSQLDB: |
| 8302 |
if (readIf("LOGSIZE")) { |
| 8303 |
readIfEqualOrTo(); |
| 8304 |
Set command = new Set(session, SetTypes.MAX_LOG_SIZE); |
| 8305 |
command.setExpression(readExpression()); |
| 8306 |
return command; |
| 8307 |
} |
| 8308 |
break; |
| 8309 |
case MariaDB: |
| 8310 |
case MySQL: |
| 8311 |
if (readIf("FOREIGN_KEY_CHECKS")) { |
| 8312 |
readIfEqualOrTo(); |
| 8313 |
Set command = new Set(session, SetTypes.REFERENTIAL_INTEGRITY); |
| 8314 |
command.setExpression(readExpression()); |
| 8315 |
return command; |
| 8316 |
} else if (readIf("NAMES")) { |
| 8317 |
// Quercus PHP MySQL driver compatibility |
| 8318 |
readIfEqualOrTo(); |
| 8319 |
read(); |
| 8320 |
return new NoOperation(session); |
| 8321 |
} |
| 8322 |
break; |
| 8323 |
case PostgreSQL: |
| 8324 |
if (readIf("STATEMENT_TIMEOUT")) { |
| 8325 |
readIfEqualOrTo(); |
| 8326 |
Set command = new Set(session, SetTypes.QUERY_TIMEOUT); |
| 8327 |
command.setInt(readNonNegativeInt()); |
| 8328 |
return command; |
| 8329 |
} else if (readIf("CLIENT_ENCODING") || readIf("CLIENT_MIN_MESSAGES") || readIf("JOIN_COLLAPSE_LIMIT")) { |
| 8330 |
readIfEqualOrTo(); |
| 8331 |
read(); |
| 8332 |
return new NoOperation(session); |
| 8333 |
} else if (readIf("DATESTYLE")) { |
| 8334 |
readIfEqualOrTo(); |
| 8335 |
if (!readIf("ISO")) { |
| 8336 |
String s = readString(); |
| 8337 |
if (!equalsToken(s, "ISO")) { |
| 8338 |
throw getSyntaxError(); |
| 8339 |
} |
| 8340 |
} |
| 8341 |
return new NoOperation(session); |
| 8342 |
} else if (readIf("SEARCH_PATH")) { |
| 8343 |
readIfEqualOrTo(); |
| 8344 |
Set command = new Set(session, SetTypes.SCHEMA_SEARCH_PATH); |
| 8345 |
ArrayList<String> list = Utils.newSmallArrayList(); |
| 8346 |
String pgCatalog = database.sysIdentifier("PG_CATALOG"); |
| 8347 |
boolean hasPgCatalog = false; |
| 8348 |
do { |
| 8349 |
// some PG clients will send single-quoted alias |
| 8350 |
String s = currentTokenType == LITERAL ? readString() : readIdentifier(); |
| 8351 |
if ("$user".equals(s)) { |
| 8352 |
continue; |
| 8353 |
} |
| 8354 |
if (pgCatalog.equals(s)) { |
| 8355 |
hasPgCatalog = true; |
| 8356 |
} |
| 8357 |
list.add(s); |
| 8358 |
} while (readIf(COMMA)); |
| 8359 |
// If "pg_catalog" is not in the path then it will be searched before |
| 8360 |
// searching any of the path items. See |
| 8361 |
// https://www.postgresql.org/docs/8.2/runtime-config-client.html |
| 8362 |
if (!hasPgCatalog) { |
| 8363 |
if (database.findSchema(pgCatalog) != null) { |
| 8364 |
list.add(0, pgCatalog); |
| 8365 |
} |
| 8366 |
} |
| 8367 |
command.setStringArray(list.toArray(new String[0])); |
| 8368 |
return command; |
| 8369 |
} |
| 8370 |
break; |
| 8371 |
default: |
| 8372 |
} |
| 8373 |
return null; |
| 8374 |
} |
| 8375 |
|
| 8376 |
private RunScriptCommand parseRunScript() { |
| 8377 |
RunScriptCommand command = new RunScriptCommand(session); |
| 8378 |
read(FROM); |
| 8379 |
command.setFileNameExpr(readExpression()); |
| 8380 |
if (readIf("COMPRESSION")) { |
| 8381 |
command.setCompressionAlgorithm(readIdentifier()); |
| 8382 |
} |
| 8383 |
if (readIf("CIPHER")) { |
| 8384 |
command.setCipher(readIdentifier()); |
| 8385 |
if (readIf("PASSWORD")) { |
| 8386 |
command.setPassword(readExpression()); |
| 8387 |
} |
| 8388 |
} |
| 8389 |
if (readIf("CHARSET")) { |
| 8390 |
command.setCharset(Charset.forName(readString())); |
| 8391 |
} |
| 8392 |
if (readIf("FROM_1X")) { |
| 8393 |
command.setFrom1X(); |
| 8394 |
} else { |
| 8395 |
if (readIf("QUIRKS_MODE")) { |
| 8396 |
command.setQuirksMode(true); |
| 8397 |
} |
| 8398 |
if (readIf("VARIABLE_BINARY")) { |
| 8399 |
command.setVariableBinary(true); |
| 8400 |
} |
| 8401 |
} |
| 8402 |
return command; |
| 8403 |
} |
| 8404 |
|
| 8405 |
private ScriptCommand parseScript() { |
| 8406 |
ScriptCommand command = new ScriptCommand(session); |
| 8407 |
boolean data = true, passwords = true, settings = true, version = true; |
| 8408 |
boolean dropTables = false, simple = false, withColumns = false; |
| 8409 |
if (readIf("NODATA")) { |
| 8410 |
data = false; |
| 8411 |
} else { |
| 8412 |
if (readIf("SIMPLE")) { |
| 8413 |
simple = true; |
| 8414 |
} |
| 8415 |
if (readIf("COLUMNS")) { |
| 8416 |
withColumns = true; |
| 8417 |
} |
| 8418 |
} |
| 8419 |
if (readIf("NOPASSWORDS")) { |
| 8420 |
passwords = false; |
| 8421 |
} |
| 8422 |
if (readIf("NOSETTINGS")) { |
| 8423 |
settings = false; |
| 8424 |
} |
| 8425 |
if (readIf("NOVERSION")) { |
| 8426 |
version = false; |
| 8427 |
} |
| 8428 |
if (readIf("DROP")) { |
| 8429 |
dropTables = true; |
| 8430 |
} |
| 8431 |
if (readIf("BLOCKSIZE")) { |
| 8432 |
long blockSize = readLong(); |
| 8433 |
command.setLobBlockSize(blockSize); |
| 8434 |
} |
| 8435 |
command.setData(data); |
| 8436 |
command.setPasswords(passwords); |
| 8437 |
command.setSettings(settings); |
| 8438 |
command.setVersion(version); |
| 8439 |
command.setDrop(dropTables); |
| 8440 |
command.setSimple(simple); |
| 8441 |
command.setWithColumns(withColumns); |
| 8442 |
if (readIf(TO)) { |
| 8443 |
command.setFileNameExpr(readExpression()); |
| 8444 |
if (readIf("COMPRESSION")) { |
| 8445 |
command.setCompressionAlgorithm(readIdentifier()); |
| 8446 |
} |
| 8447 |
if (readIf("CIPHER")) { |
| 8448 |
command.setCipher(readIdentifier()); |
| 8449 |
if (readIf("PASSWORD")) { |
| 8450 |
command.setPassword(readExpression()); |
| 8451 |
} |
| 8452 |
} |
| 8453 |
if (readIf("CHARSET")) { |
| 8454 |
command.setCharset(Charset.forName(readString())); |
| 8455 |
} |
| 8456 |
} |
| 8457 |
if (readIf("SCHEMA")) { |
| 8458 |
HashSet<String> schemaNames = new HashSet<>(); |
| 8459 |
do { |
| 8460 |
schemaNames.add(readIdentifier()); |
| 8461 |
} while (readIf(COMMA)); |
| 8462 |
command.setSchemaNames(schemaNames); |
| 8463 |
} else if (readIf(TABLE)) { |
| 8464 |
ArrayList<Table> tables = Utils.newSmallArrayList(); |
| 8465 |
do { |
| 8466 |
tables.add(readTableOrView()); |
| 8467 |
} while (readIf(COMMA)); |
| 8468 |
command.setTables(tables); |
| 8469 |
} |
| 8470 |
return command; |
| 8471 |
} |
| 8472 |
|
| 8473 |
/** |
| 8474 |
* Is this the Oracle DUAL table or the IBM/DB2 SYSIBM table? |
| 8475 |
* |
| 8476 |
* @param tableName table name. |
| 8477 |
* @return {@code true} if the table is DUAL special table. Otherwise returns {@code false}. |
| 8478 |
* @see <a href="https://en.wikipedia.org/wiki/DUAL_table">Wikipedia: DUAL table</a> |
| 8479 |
*/ |
| 8480 |
private boolean isDualTable(String tableName) { |
| 8481 |
return ((schemaName == null || equalsToken(schemaName, "SYS")) && equalsToken("DUAL", tableName)) |
| 8482 |
|| (database.getMode().sysDummy1 && (schemaName == null || equalsToken(schemaName, "SYSIBM")) |
| 8483 |
&& equalsToken("SYSDUMMY1", tableName)); |
| 8484 |
} |
| 8485 |
|
| 8486 |
private Table readTableOrView() { |
| 8487 |
return readTableOrView(readIdentifierWithSchema(null), /*resolveMaterializedView*/true); |
| 8488 |
} |
| 8489 |
|
| 8490 |
private Table readTableOrView(boolean resolveMaterializedView) { |
| 8491 |
return readTableOrView(readIdentifierWithSchema(null), resolveMaterializedView); |
| 8492 |
} |
| 8493 |
|
| 8494 |
private Table readTableOrView(String tableName, boolean resolveMaterializedView) { |
| 8495 |
if (schemaName != null) { |
| 8496 |
Table table = getSchema().resolveTableOrView(session, tableName, resolveMaterializedView); |
| 8497 |
if (table != null) { |
| 8498 |
return table; |
| 8499 |
} |
| 8500 |
} else { |
| 8501 |
Table table = database.getSchema(session.getCurrentSchemaName()) |
| 8502 |
.resolveTableOrView(session, tableName, resolveMaterializedView); |
| 8503 |
if (table != null) { |
| 8504 |
return table; |
| 8505 |
} |
| 8506 |
String[] schemaNames = session.getSchemaSearchPath(); |
| 8507 |
if (schemaNames != null) { |
| 8508 |
for (String name : schemaNames) { |
| 8509 |
Schema s = database.getSchema(name); |
| 8510 |
table = s.resolveTableOrView(session, tableName, resolveMaterializedView); |
| 8511 |
if (table != null) { |
| 8512 |
return table; |
| 8513 |
} |
| 8514 |
} |
| 8515 |
} |
| 8516 |
} |
| 8517 |
if (isDualTable(tableName)) { |
| 8518 |
return new DualTable(database); |
| 8519 |
} |
| 8520 |
|
| 8521 |
throw getTableOrViewNotFoundDbException(tableName); |
| 8522 |
} |
| 8523 |
|
| 8524 |
private DbException getTableOrViewNotFoundDbException(String tableName) { |
| 8525 |
if (schemaName != null) { |
| 8526 |
return getTableOrViewNotFoundDbException(schemaName, tableName); |
| 8527 |
} |
| 8528 |
|
| 8529 |
String currentSchemaName = session.getCurrentSchemaName(); |
| 8530 |
String[] schemaSearchPath = session.getSchemaSearchPath(); |
| 8531 |
if (schemaSearchPath == null) { |
| 8532 |
return getTableOrViewNotFoundDbException(Collections.singleton(currentSchemaName), tableName); |
| 8533 |
} |
| 8534 |
|
| 8535 |
LinkedHashSet<String> schemaNames = new LinkedHashSet<>(); |
| 8536 |
schemaNames.add(currentSchemaName); |
| 8537 |
schemaNames.addAll(Arrays.asList(schemaSearchPath)); |
| 8538 |
return getTableOrViewNotFoundDbException(schemaNames, tableName); |
| 8539 |
} |
| 8540 |
|
| 8541 |
private DbException getTableOrViewNotFoundDbException(String schemaName, String tableName) { |
| 8542 |
return getTableOrViewNotFoundDbException(Collections.singleton(schemaName), tableName); |
| 8543 |
} |
| 8544 |
|
| 8545 |
private DbException getTableOrViewNotFoundDbException( |
| 8546 |
java.util.Set<String> schemaNames, String tableName) { |
| 8547 |
if (database == null || database.getFirstUserTable() == null) { |
| 8548 |
return DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_DATABASE_EMPTY_1, tableName); |
| 8549 |
} |
| 8550 |
|
| 8551 |
if (database.getSettings().caseInsensitiveIdentifiers) { |
| 8552 |
return DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); |
| 8553 |
} |
| 8554 |
|
| 8555 |
java.util.Set<String> candidates = new TreeSet<>(); |
| 8556 |
for (String schemaName : schemaNames) { |
| 8557 |
findTableNameCandidates(schemaName, tableName, candidates); |
| 8558 |
} |
| 8559 |
|
| 8560 |
if (candidates.isEmpty()) { |
| 8561 |
return DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); |
| 8562 |
} |
| 8563 |
|
| 8564 |
return DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_WITH_CANDIDATES_2, |
| 8565 |
tableName, |
| 8566 |
String.join(", ", candidates)); |
| 8567 |
} |
| 8568 |
|
| 8569 |
private void findTableNameCandidates(String schemaName, String tableName, java.util.Set<String> candidates) { |
| 8570 |
Schema schema = database.getSchema(schemaName); |
| 8571 |
String ucTableName = StringUtils.toUpperEnglish(tableName); |
| 8572 |
Collection<Table> allTablesAndViews = schema.getAllTablesAndViews(session); |
| 8573 |
for (Table candidate : allTablesAndViews) { |
| 8574 |
String candidateName = candidate.getName(); |
| 8575 |
if (ucTableName.equals(StringUtils.toUpperEnglish(candidateName))) { |
| 8576 |
candidates.add(candidateName); |
| 8577 |
} |
| 8578 |
} |
| 8579 |
} |
| 8580 |
|
| 8581 |
private UserDefinedFunction findUserDefinedFunctionWithinPath(Schema schema, String name) { |
| 8582 |
if (schema != null) { |
| 8583 |
return schema.findFunctionOrAggregate(name); |
| 8584 |
} |
| 8585 |
schema = database.getSchema(session.getCurrentSchemaName()); |
| 8586 |
UserDefinedFunction userDefinedFunction = schema.findFunctionOrAggregate(name); |
| 8587 |
if (userDefinedFunction != null) { |
| 8588 |
return userDefinedFunction; |
| 8589 |
} |
| 8590 |
String[] schemaNames = session.getSchemaSearchPath(); |
| 8591 |
if (schemaNames != null) { |
| 8592 |
for (String schemaName : schemaNames) { |
| 8593 |
Schema schemaFromPath = database.getSchema(schemaName); |
| 8594 |
if (schemaFromPath != schema) { |
| 8595 |
userDefinedFunction = schemaFromPath.findFunctionOrAggregate(name); |
| 8596 |
if (userDefinedFunction != null) { |
| 8597 |
return userDefinedFunction; |
| 8598 |
} |
| 8599 |
} |
| 8600 |
} |
| 8601 |
} |
| 8602 |
return null; |
| 8603 |
} |
| 8604 |
|
| 8605 |
private Sequence findSequence(String schema, String sequenceName) { |
| 8606 |
Sequence sequence = database.getSchema(schema).findSequence( |
| 8607 |
sequenceName); |
| 8608 |
if (sequence != null) { |
| 8609 |
return sequence; |
| 8610 |
} |
| 8611 |
String[] schemaNames = session.getSchemaSearchPath(); |
| 8612 |
if (schemaNames != null) { |
| 8613 |
for (String n : schemaNames) { |
| 8614 |
sequence = database.getSchema(n).findSequence(sequenceName); |
| 8615 |
if (sequence != null) { |
| 8616 |
return sequence; |
| 8617 |
} |
| 8618 |
} |
| 8619 |
} |
| 8620 |
return null; |
| 8621 |
} |
| 8622 |
|
| 8623 |
private Sequence readSequence() { |
| 8624 |
// same algorithm as readTableOrView |
| 8625 |
String sequenceName = readIdentifierWithSchema(null); |
| 8626 |
if (schemaName != null) { |
| 8627 |
return getSchema().getSequence(sequenceName); |
| 8628 |
} |
| 8629 |
Sequence sequence = findSequence(session.getCurrentSchemaName(), |
| 8630 |
sequenceName); |
| 8631 |
if (sequence != null) { |
| 8632 |
return sequence; |
| 8633 |
} |
| 8634 |
throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName); |
| 8635 |
} |
| 8636 |
|
| 8637 |
private Prepared parseAlterTable() { |
| 8638 |
boolean ifTableExists = readIfExists(false); |
| 8639 |
String tableName = readIdentifierWithSchema(); |
| 8640 |
Schema schema = getSchema(); |
| 8641 |
if (readIf("ADD")) { |
| 8642 |
Prepared command = parseTableConstraintIf(tableName, schema, ifTableExists); |
| 8643 |
if (command != null) { |
| 8644 |
return command; |
| 8645 |
} |
| 8646 |
return parseAlterTableAddColumn(tableName, schema, ifTableExists); |
| 8647 |
} else if (readIf(SET)) { |
| 8648 |
return parseAlterTableSet(schema, tableName, ifTableExists); |
| 8649 |
} else if (readIf("RENAME")) { |
| 8650 |
return parseAlterTableRename(schema, tableName, ifTableExists); |
| 8651 |
} else if (readIf("DROP")) { |
| 8652 |
return parseAlterTableDrop(schema, tableName, ifTableExists); |
| 8653 |
} else if (readIf("ALTER")) { |
| 8654 |
return parseAlterTableAlter(schema, tableName, ifTableExists); |
| 8655 |
} else { |
| 8656 |
Mode mode = database.getMode(); |
| 8657 |
if (mode.alterTableExtensionsMySQL || mode.alterTableModifyColumn) { |
| 8658 |
return parseAlterTableCompatibility(schema, tableName, ifTableExists, mode); |
| 8659 |
} |
| 8660 |
} |
| 8661 |
throw getSyntaxError(); |
| 8662 |
} |
| 8663 |
|
| 8664 |
private Prepared parseAlterTableAlter(Schema schema, String tableName, boolean ifTableExists) { |
| 8665 |
readIf("COLUMN"); |
| 8666 |
boolean ifExists = readIfExists(false); |
| 8667 |
String columnName = readIdentifier(); |
| 8668 |
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists, ifExists); |
| 8669 |
if (readIf("RENAME")) { |
| 8670 |
read(TO); |
| 8671 |
AlterTableRenameColumn command = new AlterTableRenameColumn( |
| 8672 |
session, schema); |
| 8673 |
command.setTableName(tableName); |
| 8674 |
command.setIfTableExists(ifTableExists); |
| 8675 |
command.setIfExists(ifExists); |
| 8676 |
command.setOldColumnName(columnName); |
| 8677 |
String newName = readIdentifier(); |
| 8678 |
command.setNewColumnName(newName); |
| 8679 |
return command; |
| 8680 |
} else if (readIf("DROP")) { |
| 8681 |
if (readIf(DEFAULT)) { |
| 8682 |
if (readIf(ON, NULL)) { |
| 8683 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 8684 |
command.setTableName(tableName); |
| 8685 |
command.setIfTableExists(ifTableExists); |
| 8686 |
command.setOldColumn(column); |
| 8687 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT_ON_NULL); |
| 8688 |
command.setBooleanFlag(false); |
| 8689 |
return command; |
| 8690 |
} |
| 8691 |
return getAlterTableAlterColumnDropDefaultExpression(schema, tableName, ifTableExists, column, |
| 8692 |
CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT); |
| 8693 |
} else if (readIf("EXPRESSION")) { |
| 8694 |
return getAlterTableAlterColumnDropDefaultExpression(schema, tableName, ifTableExists, column, |
| 8695 |
CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_EXPRESSION); |
| 8696 |
} else if (readIf("IDENTITY")) { |
| 8697 |
return getAlterTableAlterColumnDropDefaultExpression(schema, tableName, ifTableExists, column, |
| 8698 |
CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_IDENTITY); |
| 8699 |
} |
| 8700 |
if (readIf(ON, "UPDATE")) { |
| 8701 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 8702 |
command.setTableName(tableName); |
| 8703 |
command.setIfTableExists(ifTableExists); |
| 8704 |
command.setOldColumn(column); |
| 8705 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE); |
| 8706 |
command.setDefaultExpression(null); |
| 8707 |
return command; |
| 8708 |
} |
| 8709 |
read(NOT); |
| 8710 |
read(NULL); |
| 8711 |
AlterTableAlterColumn command = new AlterTableAlterColumn( |
| 8712 |
session, schema); |
| 8713 |
command.setTableName(tableName); |
| 8714 |
command.setIfTableExists(ifTableExists); |
| 8715 |
command.setOldColumn(column); |
| 8716 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_NOT_NULL); |
| 8717 |
return command; |
| 8718 |
} else if (readIf("TYPE")) { |
| 8719 |
// PostgreSQL compatibility |
| 8720 |
return parseAlterTableAlterColumnDataType(schema, tableName, columnName, ifTableExists, ifExists); |
| 8721 |
} else if (readIf("SELECTIVITY")) { |
| 8722 |
AlterTableAlterColumn command = new AlterTableAlterColumn( |
| 8723 |
session, schema); |
| 8724 |
command.setTableName(tableName); |
| 8725 |
command.setIfTableExists(ifTableExists); |
| 8726 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY); |
| 8727 |
command.setOldColumn(column); |
| 8728 |
command.setSelectivity(readExpression()); |
| 8729 |
return command; |
| 8730 |
} |
| 8731 |
Prepared command = parseAlterTableAlterColumnIdentity(schema, tableName, ifTableExists, column); |
| 8732 |
if (command != null) { |
| 8733 |
return command; |
| 8734 |
} |
| 8735 |
if (readIf(SET)) { |
| 8736 |
return parseAlterTableAlterColumnSet(schema, tableName, ifTableExists, ifExists, columnName, column); |
| 8737 |
} |
| 8738 |
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists, ifExists, true); |
| 8739 |
} |
| 8740 |
|
| 8741 |
private Prepared getAlterTableAlterColumnDropDefaultExpression(Schema schema, String tableName, |
| 8742 |
boolean ifTableExists, Column column, int type) { |
| 8743 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 8744 |
command.setTableName(tableName); |
| 8745 |
command.setIfTableExists(ifTableExists); |
| 8746 |
command.setOldColumn(column); |
| 8747 |
command.setType(type); |
| 8748 |
command.setDefaultExpression(null); |
| 8749 |
return command; |
| 8750 |
} |
| 8751 |
|
| 8752 |
private Prepared parseAlterTableAlterColumnIdentity(Schema schema, String tableName, boolean ifTableExists, |
| 8753 |
Column column) { |
| 8754 |
Boolean always = null; |
| 8755 |
if (readIf(SET, "GENERATED")) { |
| 8756 |
if (readIf("ALWAYS")) { |
| 8757 |
always = true; |
| 8758 |
} else { |
| 8759 |
read("BY"); |
| 8760 |
read(DEFAULT); |
| 8761 |
always = false; |
| 8762 |
} |
| 8763 |
} |
| 8764 |
SequenceOptions options = new SequenceOptions(); |
| 8765 |
if (!parseSequenceOptions(options, null, false, true) && always == null) { |
| 8766 |
return null; |
| 8767 |
} |
| 8768 |
if (column == null) { |
| 8769 |
return new NoOperation(session); |
| 8770 |
} |
| 8771 |
if (!column.isIdentity()) { |
| 8772 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 8773 |
parseAlterColumnUsingIf(command); |
| 8774 |
command.setTableName(tableName); |
| 8775 |
command.setIfTableExists(ifTableExists); |
| 8776 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE); |
| 8777 |
command.setOldColumn(column); |
| 8778 |
Column newColumn = column.getClone(); |
| 8779 |
newColumn.setIdentityOptions(options, always != null && always); |
| 8780 |
command.setNewColumn(newColumn); |
| 8781 |
return command; |
| 8782 |
} |
| 8783 |
AlterSequence command = new AlterSequence(session, schema); |
| 8784 |
command.setColumn(column, always); |
| 8785 |
command.setOptions(options); |
| 8786 |
return commandIfTableExists(schema, tableName, ifTableExists, command); |
| 8787 |
} |
| 8788 |
|
| 8789 |
private Prepared parseAlterTableAlterColumnSet(Schema schema, String tableName, boolean ifTableExists, |
| 8790 |
boolean ifExists, String columnName, Column column) { |
| 8791 |
if (readIf("DATA", "TYPE")) { |
| 8792 |
return parseAlterTableAlterColumnDataType(schema, tableName, columnName, ifTableExists, ifExists); |
| 8793 |
} |
| 8794 |
AlterTableAlterColumn command = new AlterTableAlterColumn( |
| 8795 |
session, schema); |
| 8796 |
command.setTableName(tableName); |
| 8797 |
command.setIfTableExists(ifTableExists); |
| 8798 |
command.setOldColumn(column); |
| 8799 |
NullConstraintType nullConstraint = parseNotNullConstraint(); |
| 8800 |
switch (nullConstraint) { |
| 8801 |
case NULL_IS_ALLOWED: |
| 8802 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_NOT_NULL); |
| 8803 |
break; |
| 8804 |
case NULL_IS_NOT_ALLOWED: |
| 8805 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL); |
| 8806 |
break; |
| 8807 |
case NO_NULL_CONSTRAINT_FOUND: |
| 8808 |
if (readIf(DEFAULT)) { |
| 8809 |
if (readIf(ON, NULL)) { |
| 8810 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT_ON_NULL); |
| 8811 |
command.setBooleanFlag(true); |
| 8812 |
break; |
| 8813 |
} |
| 8814 |
Expression defaultExpression = readExpression(); |
| 8815 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT); |
| 8816 |
command.setDefaultExpression(defaultExpression); |
| 8817 |
} else if (readIf(ON, "UPDATE")) { |
| 8818 |
Expression onUpdateExpression = readExpression(); |
| 8819 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE); |
| 8820 |
command.setDefaultExpression(onUpdateExpression); |
| 8821 |
} else if (readIf("INVISIBLE")) { |
| 8822 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY); |
| 8823 |
command.setBooleanFlag(false); |
| 8824 |
} else if (readIf("VISIBLE")) { |
| 8825 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY); |
| 8826 |
command.setBooleanFlag(true); |
| 8827 |
} |
| 8828 |
break; |
| 8829 |
default: |
| 8830 |
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, |
| 8831 |
"Internal Error - unhandled case: " + nullConstraint.name()); |
| 8832 |
} |
| 8833 |
return command; |
| 8834 |
} |
| 8835 |
|
| 8836 |
private Prepared parseAlterTableDrop(Schema schema, String tableName, boolean ifTableExists) { |
| 8837 |
if (readIf(CONSTRAINT)) { |
| 8838 |
boolean ifExists = readIfExists(false); |
| 8839 |
String constraintName = readIdentifierWithSchema(schema.getName()); |
| 8840 |
ifExists = readIfExists(ifExists); |
| 8841 |
checkSchema(schema); |
| 8842 |
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists); |
| 8843 |
command.setTableName(tableName); |
| 8844 |
command.setIfTableExists(ifTableExists); |
| 8845 |
command.setConstraintName(constraintName); |
| 8846 |
ConstraintActionType dropAction = parseCascadeOrRestrict(); |
| 8847 |
if (dropAction != null) { |
| 8848 |
command.setDropAction(dropAction); |
| 8849 |
} |
| 8850 |
return command; |
| 8851 |
} else if (readIf(PRIMARY, KEY)) { |
| 8852 |
Table table = tableIfTableExists(schema, tableName, ifTableExists); |
| 8853 |
if (table == null) { |
| 8854 |
return new NoOperation(session); |
| 8855 |
} |
| 8856 |
Index idx = table.getPrimaryKey(); |
| 8857 |
DropIndex command = new DropIndex(session, schema); |
| 8858 |
command.setIndexName(idx.getName()); |
| 8859 |
return command; |
| 8860 |
} else if (database.getMode().alterTableExtensionsMySQL) { |
| 8861 |
Prepared command = parseAlterTableDropCompatibility(schema, tableName, ifTableExists); |
| 8862 |
if (command != null) { |
| 8863 |
return command; |
| 8864 |
} |
| 8865 |
} |
| 8866 |
readIf("COLUMN"); |
| 8867 |
boolean ifExists = readIfExists(false); |
| 8868 |
ArrayList<Column> columnsToRemove = new ArrayList<>(); |
| 8869 |
Table table = tableIfTableExists(schema, tableName, ifTableExists); |
| 8870 |
// For Oracle compatibility - open bracket required |
| 8871 |
boolean openingBracketDetected = readIf(OPEN_PAREN); |
| 8872 |
do { |
| 8873 |
String columnName = readIdentifier(); |
| 8874 |
if (table != null) { |
| 8875 |
Column column = table.getColumn(columnName, ifExists); |
| 8876 |
if (column != null) { |
| 8877 |
columnsToRemove.add(column); |
| 8878 |
} |
| 8879 |
} |
| 8880 |
} while (readIf(COMMA)); |
| 8881 |
if (openingBracketDetected) { |
| 8882 |
// For Oracle compatibility - close bracket |
| 8883 |
read(CLOSE_PAREN); |
| 8884 |
} |
| 8885 |
if (table == null || columnsToRemove.isEmpty()) { |
| 8886 |
return new NoOperation(session); |
| 8887 |
} |
| 8888 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 8889 |
command.setType(CommandInterface.ALTER_TABLE_DROP_COLUMN); |
| 8890 |
command.setTableName(tableName); |
| 8891 |
command.setIfTableExists(ifTableExists); |
| 8892 |
command.setColumnsToRemove(columnsToRemove); |
| 8893 |
return command; |
| 8894 |
} |
| 8895 |
|
| 8896 |
private Prepared parseAlterTableDropCompatibility(Schema schema, String tableName, boolean ifTableExists) { |
| 8897 |
if (readIf(FOREIGN, KEY)) { |
| 8898 |
// For MariaDB |
| 8899 |
boolean ifExists = readIfExists(false); |
| 8900 |
String constraintName = readIdentifierWithSchema(schema.getName()); |
| 8901 |
checkSchema(schema); |
| 8902 |
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists); |
| 8903 |
command.setTableName(tableName); |
| 8904 |
command.setIfTableExists(ifTableExists); |
| 8905 |
command.setConstraintName(constraintName); |
| 8906 |
return command; |
| 8907 |
} else if (readIf("INDEX")) { |
| 8908 |
// For MariaDB |
| 8909 |
boolean ifExists = readIfExists(false); |
| 8910 |
String indexOrConstraintName = readIdentifierWithSchema(schema.getName()); |
| 8911 |
if (schema.findIndex(session, indexOrConstraintName) != null) { |
| 8912 |
DropIndex dropIndexCommand = new DropIndex(session, getSchema()); |
| 8913 |
dropIndexCommand.setIndexName(indexOrConstraintName); |
| 8914 |
return commandIfTableExists(schema, tableName, ifTableExists, dropIndexCommand); |
| 8915 |
} else { |
| 8916 |
AlterTableDropConstraint dropCommand = new AlterTableDropConstraint(session, getSchema(), ifExists); |
| 8917 |
dropCommand.setTableName(tableName); |
| 8918 |
dropCommand.setIfTableExists(ifTableExists); |
| 8919 |
dropCommand.setConstraintName(indexOrConstraintName); |
| 8920 |
return dropCommand; |
| 8921 |
} |
| 8922 |
} |
| 8923 |
return null; |
| 8924 |
} |
| 8925 |
|
| 8926 |
private Prepared parseAlterTableRename(Schema schema, String tableName, boolean ifTableExists) { |
| 8927 |
if (readIf("COLUMN")) { |
| 8928 |
// PostgreSQL syntax |
| 8929 |
String columnName = readIdentifier(); |
| 8930 |
read(TO); |
| 8931 |
AlterTableRenameColumn command = new AlterTableRenameColumn( |
| 8932 |
session, schema); |
| 8933 |
command.setTableName(tableName); |
| 8934 |
command.setIfTableExists(ifTableExists); |
| 8935 |
command.setOldColumnName(columnName); |
| 8936 |
command.setNewColumnName(readIdentifier()); |
| 8937 |
return command; |
| 8938 |
} else if (readIf(CONSTRAINT)) { |
| 8939 |
String constraintName = readIdentifierWithSchema(schema.getName()); |
| 8940 |
checkSchema(schema); |
| 8941 |
read(TO); |
| 8942 |
AlterTableRenameConstraint command = new AlterTableRenameConstraint(session, schema); |
| 8943 |
command.setTableName(tableName); |
| 8944 |
command.setIfTableExists(ifTableExists); |
| 8945 |
command.setConstraintName(constraintName); |
| 8946 |
command.setNewConstraintName(readIdentifier()); |
| 8947 |
return command; |
| 8948 |
} else { |
| 8949 |
read(TO); |
| 8950 |
String newName = readIdentifierWithSchema(schema.getName()); |
| 8951 |
checkSchema(schema); |
| 8952 |
AlterTableRename command = new AlterTableRename(session, |
| 8953 |
getSchema()); |
| 8954 |
command.setTableName(tableName); |
| 8955 |
command.setNewTableName(newName); |
| 8956 |
command.setIfTableExists(ifTableExists); |
| 8957 |
command.setHidden(readIf("HIDDEN")); |
| 8958 |
return command; |
| 8959 |
} |
| 8960 |
} |
| 8961 |
|
| 8962 |
private Prepared parseAlterTableSet(Schema schema, String tableName, boolean ifTableExists) { |
| 8963 |
read("REFERENTIAL_INTEGRITY"); |
| 8964 |
int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY; |
| 8965 |
boolean value = readBooleanSetting(); |
| 8966 |
AlterTableSet command = new AlterTableSet(session, |
| 8967 |
schema, type, value); |
| 8968 |
command.setTableName(tableName); |
| 8969 |
command.setIfTableExists(ifTableExists); |
| 8970 |
if (readIf(CHECK)) { |
| 8971 |
command.setCheckExisting(true); |
| 8972 |
} else if (readIf("NOCHECK")) { |
| 8973 |
command.setCheckExisting(false); |
| 8974 |
} |
| 8975 |
return command; |
| 8976 |
} |
| 8977 |
|
| 8978 |
private Prepared parseAlterTableCompatibility(Schema schema, String tableName, boolean ifTableExists, Mode mode) { |
| 8979 |
if (mode.alterTableExtensionsMySQL) { |
| 8980 |
if (readIf("AUTO_INCREMENT")) { |
| 8981 |
readIf(EQUAL); |
| 8982 |
Expression restart = readExpression(); |
| 8983 |
Table table = tableIfTableExists(schema, tableName, ifTableExists); |
| 8984 |
if (table == null) { |
| 8985 |
return new NoOperation(session); |
| 8986 |
} |
| 8987 |
Index idx = table.findPrimaryKey(); |
| 8988 |
if (idx != null) { |
| 8989 |
for (IndexColumn ic : idx.getIndexColumns()) { |
| 8990 |
Column column = ic.column; |
| 8991 |
if (column.isIdentity()) { |
| 8992 |
AlterSequence command = new AlterSequence(session, schema); |
| 8993 |
command.setColumn(column, null); |
| 8994 |
SequenceOptions options = new SequenceOptions(); |
| 8995 |
options.setRestartValue(restart); |
| 8996 |
command.setOptions(options); |
| 8997 |
return command; |
| 8998 |
} |
| 8999 |
} |
| 9000 |
} |
| 9001 |
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, "AUTO_INCREMENT PRIMARY KEY"); |
| 9002 |
} else if (readIf("CHANGE")) { |
| 9003 |
readIf("COLUMN"); |
| 9004 |
String columnName = readIdentifier(); |
| 9005 |
String newColumnName = readIdentifier(); |
| 9006 |
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists, false); |
| 9007 |
boolean nullable = column == null ? true : column.isNullable(); |
| 9008 |
// new column type ignored. RENAME and MODIFY are |
| 9009 |
// a single command in MySQL but two different commands in H2. |
| 9010 |
parseColumnForTable(newColumnName, nullable); |
| 9011 |
AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema); |
| 9012 |
command.setTableName(tableName); |
| 9013 |
command.setIfTableExists(ifTableExists); |
| 9014 |
command.setOldColumnName(columnName); |
| 9015 |
command.setNewColumnName(newColumnName); |
| 9016 |
return command; |
| 9017 |
} else if (readIf("CONVERT")) { |
| 9018 |
readIf(TO); |
| 9019 |
readIf("CHARACTER"); |
| 9020 |
readIf(SET); |
| 9021 |
readMySQLCharset(); |
| 9022 |
|
| 9023 |
if (readIf("COLLATE")) { |
| 9024 |
readMySQLCharset(); |
| 9025 |
} |
| 9026 |
|
| 9027 |
return new NoOperation(session); |
| 9028 |
} |
| 9029 |
} |
| 9030 |
if (mode.alterTableModifyColumn && readIf("MODIFY")) { |
| 9031 |
// MySQL compatibility (optional) |
| 9032 |
readIf("COLUMN"); |
| 9033 |
// Oracle specifies (but will not require) an opening parenthesis |
| 9034 |
boolean hasOpeningBracket = readIf(OPEN_PAREN); |
| 9035 |
String columnName = readIdentifier(); |
| 9036 |
AlterTableAlterColumn command; |
| 9037 |
NullConstraintType nullConstraint = parseNotNullConstraint(); |
| 9038 |
switch (nullConstraint) { |
| 9039 |
case NULL_IS_ALLOWED: |
| 9040 |
case NULL_IS_NOT_ALLOWED: |
| 9041 |
command = new AlterTableAlterColumn(session, schema); |
| 9042 |
command.setTableName(tableName); |
| 9043 |
command.setIfTableExists(ifTableExists); |
| 9044 |
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists, false); |
| 9045 |
command.setOldColumn(column); |
| 9046 |
if (nullConstraint == NullConstraintType.NULL_IS_ALLOWED) { |
| 9047 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_NOT_NULL); |
| 9048 |
} else { |
| 9049 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL); |
| 9050 |
} |
| 9051 |
break; |
| 9052 |
case NO_NULL_CONSTRAINT_FOUND: |
| 9053 |
command = parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists, false, |
| 9054 |
mode.alterTableModifyColumnPreserveNullability); |
| 9055 |
break; |
| 9056 |
default: |
| 9057 |
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, |
| 9058 |
"Internal Error - unhandled case: " + nullConstraint.name()); |
| 9059 |
} |
| 9060 |
if (hasOpeningBracket) { |
| 9061 |
read(CLOSE_PAREN); |
| 9062 |
} |
| 9063 |
return command; |
| 9064 |
} |
| 9065 |
throw getSyntaxError(); |
| 9066 |
} |
| 9067 |
|
| 9068 |
private Table tableIfTableExists(Schema schema, String tableName, boolean ifTableExists) { |
| 9069 |
Table table = schema.resolveTableOrView(session, tableName); |
| 9070 |
if (table == null && !ifTableExists) { |
| 9071 |
throw getTableOrViewNotFoundDbException(schema.getName(), tableName); |
| 9072 |
} |
| 9073 |
return table; |
| 9074 |
} |
| 9075 |
|
| 9076 |
private Column columnIfTableExists(Schema schema, String tableName, |
| 9077 |
String columnName, boolean ifTableExists, boolean ifExists) { |
| 9078 |
Table table = tableIfTableExists(schema, tableName, ifTableExists); |
| 9079 |
if (table == null) { |
| 9080 |
return null; |
| 9081 |
} |
| 9082 |
return table.getColumn(columnName, ifExists); |
| 9083 |
} |
| 9084 |
|
| 9085 |
private Prepared commandIfTableExists(Schema schema, String tableName, |
| 9086 |
boolean ifTableExists, Prepared commandIfTableExists) { |
| 9087 |
return tableIfTableExists(schema, tableName, ifTableExists) == null |
| 9088 |
? new NoOperation(session) |
| 9089 |
: commandIfTableExists; |
| 9090 |
} |
| 9091 |
|
| 9092 |
private AlterTableAlterColumn parseAlterTableAlterColumnType(Schema schema, |
| 9093 |
String tableName, String columnName, boolean ifTableExists, boolean ifExists, boolean preserveNotNull) { |
| 9094 |
Column oldColumn = columnIfTableExists(schema, tableName, columnName, ifTableExists, ifExists); |
| 9095 |
Column newColumn = parseColumnForTable(columnName, |
| 9096 |
!preserveNotNull || oldColumn == null || oldColumn.isNullable()); |
| 9097 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 9098 |
parseAlterColumnUsingIf(command); |
| 9099 |
command.setTableName(tableName); |
| 9100 |
command.setIfTableExists(ifTableExists); |
| 9101 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE); |
| 9102 |
command.setOldColumn(oldColumn); |
| 9103 |
command.setNewColumn(newColumn); |
| 9104 |
return command; |
| 9105 |
} |
| 9106 |
|
| 9107 |
private AlterTableAlterColumn parseAlterTableAlterColumnDataType(Schema schema, |
| 9108 |
String tableName, String columnName, boolean ifTableExists, boolean ifExists) { |
| 9109 |
Column oldColumn = columnIfTableExists(schema, tableName, columnName, ifTableExists, ifExists); |
| 9110 |
Column newColumn = parseColumnWithType(columnName); |
| 9111 |
if (oldColumn != null) { |
| 9112 |
if (!oldColumn.isNullable()) { |
| 9113 |
newColumn.setNullable(false); |
| 9114 |
} |
| 9115 |
if (!oldColumn.getVisible()) { |
| 9116 |
newColumn.setVisible(false); |
| 9117 |
} |
| 9118 |
Expression e = oldColumn.getDefaultExpression(); |
| 9119 |
if (e != null) { |
| 9120 |
if (oldColumn.isGenerated()) { |
| 9121 |
newColumn.setGeneratedExpression(e); |
| 9122 |
} else { |
| 9123 |
newColumn.setDefaultExpression(session, e); |
| 9124 |
} |
| 9125 |
} |
| 9126 |
e = oldColumn.getOnUpdateExpression(); |
| 9127 |
if (e != null) { |
| 9128 |
newColumn.setOnUpdateExpression(session, e); |
| 9129 |
} |
| 9130 |
Sequence s = oldColumn.getSequence(); |
| 9131 |
if (s != null) { |
| 9132 |
newColumn.setIdentityOptions(new SequenceOptions(s, newColumn.getType()), |
| 9133 |
oldColumn.isGeneratedAlways()); |
| 9134 |
} |
| 9135 |
String c = oldColumn.getComment(); |
| 9136 |
if (c != null) { |
| 9137 |
newColumn.setComment(c); |
| 9138 |
} |
| 9139 |
} |
| 9140 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema); |
| 9141 |
parseAlterColumnUsingIf(command); |
| 9142 |
command.setTableName(tableName); |
| 9143 |
command.setIfTableExists(ifTableExists); |
| 9144 |
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE); |
| 9145 |
command.setOldColumn(oldColumn); |
| 9146 |
command.setNewColumn(newColumn); |
| 9147 |
return command; |
| 9148 |
} |
| 9149 |
|
| 9150 |
private AlterTableAlterColumn parseAlterTableAddColumn(String tableName, |
| 9151 |
Schema schema, boolean ifTableExists) { |
| 9152 |
readIf("COLUMN"); |
| 9153 |
AlterTableAlterColumn command = new AlterTableAlterColumn(session, |
| 9154 |
schema); |
| 9155 |
command.setType(CommandInterface.ALTER_TABLE_ADD_COLUMN); |
| 9156 |
command.setTableName(tableName); |
| 9157 |
command.setIfTableExists(ifTableExists); |
| 9158 |
if (readIf(OPEN_PAREN)) { |
| 9159 |
command.setIfNotExists(false); |
| 9160 |
do { |
| 9161 |
parseTableColumnDefinition(command, schema, tableName, false); |
| 9162 |
} while (readIfMore()); |
| 9163 |
} else { |
| 9164 |
boolean ifNotExists = readIfNotExists(); |
| 9165 |
command.setIfNotExists(ifNotExists); |
| 9166 |
parseTableColumnDefinition(command, schema, tableName, false); |
| 9167 |
parseAlterColumnUsingIf(command); |
| 9168 |
} |
| 9169 |
if (readIf("BEFORE")) { |
| 9170 |
command.setAddBefore(readIdentifier()); |
| 9171 |
} else if (readIf("AFTER")) { |
| 9172 |
command.setAddAfter(readIdentifier()); |
| 9173 |
} else if (readIf("FIRST")) { |
| 9174 |
command.setAddFirst(); |
| 9175 |
} |
| 9176 |
return command; |
| 9177 |
} |
| 9178 |
|
| 9179 |
private void parseAlterColumnUsingIf(AlterTableAlterColumn command) { |
| 9180 |
if (readIf(USING)) { |
| 9181 |
command.setUsingExpression(readExpression()); |
| 9182 |
} |
| 9183 |
} |
| 9184 |
|
| 9185 |
private ConstraintActionType parseAction() { |
| 9186 |
ConstraintActionType result = parseCascadeOrRestrict(); |
| 9187 |
if (result != null) { |
| 9188 |
return result; |
| 9189 |
} |
| 9190 |
if (readIf("NO", "ACTION")) { |
| 9191 |
return ConstraintActionType.RESTRICT; |
| 9192 |
} |
| 9193 |
read(SET); |
| 9194 |
if (readIf(NULL)) { |
| 9195 |
return ConstraintActionType.SET_NULL; |
| 9196 |
} |
| 9197 |
read(DEFAULT); |
| 9198 |
return ConstraintActionType.SET_DEFAULT; |
| 9199 |
} |
| 9200 |
|
| 9201 |
private ConstraintActionType parseCascadeOrRestrict() { |
| 9202 |
if (readIf("CASCADE")) { |
| 9203 |
return ConstraintActionType.CASCADE; |
| 9204 |
} else if (readIf("RESTRICT")) { |
| 9205 |
return ConstraintActionType.RESTRICT; |
| 9206 |
} else { |
| 9207 |
return null; |
| 9208 |
} |
| 9209 |
} |
| 9210 |
|
| 9211 |
private DefineCommand parseTableConstraintIf(String tableName, Schema schema, boolean ifTableExists) { |
| 9212 |
String constraintName = null, comment = null; |
| 9213 |
boolean ifNotExists = false; |
| 9214 |
if (readIf(CONSTRAINT)) { |
| 9215 |
ifNotExists = readIfNotExists(); |
| 9216 |
constraintName = readIdentifierWithSchema(schema.getName()); |
| 9217 |
checkSchema(schema); |
| 9218 |
comment = readCommentIf(); |
| 9219 |
} |
| 9220 |
AlterTableAddConstraint command; |
| 9221 |
switch (currentTokenType) { |
| 9222 |
case PRIMARY: |
| 9223 |
read(); |
| 9224 |
read(KEY); |
| 9225 |
command = new AlterTableAddConstraint(session, schema, |
| 9226 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY, ifNotExists); |
| 9227 |
if (readIf("HASH")) { |
| 9228 |
command.setPrimaryKeyHash(true); |
| 9229 |
} |
| 9230 |
read(OPEN_PAREN); |
| 9231 |
command.setIndexColumns(parseIndexColumnList()); |
| 9232 |
if (readIf("INDEX")) { |
| 9233 |
String indexName = readIdentifierWithSchema(); |
| 9234 |
command.setIndex(getSchema().findIndex(session, indexName)); |
| 9235 |
} |
| 9236 |
break; |
| 9237 |
case UNIQUE: { |
| 9238 |
read(); |
| 9239 |
NullsDistinct nullsDistinct = readNullsDistinct(database.getMode().nullsDistinct); |
| 9240 |
// MySQL compatibility |
| 9241 |
boolean compatibility = database.getMode().indexDefinitionInCreateTable; |
| 9242 |
if (compatibility) { |
| 9243 |
if (!readIf(KEY)) { |
| 9244 |
readIf("INDEX"); |
| 9245 |
} |
| 9246 |
if (!isToken(OPEN_PAREN)) { |
| 9247 |
constraintName = readIdentifier(); |
| 9248 |
} |
| 9249 |
} |
| 9250 |
read(OPEN_PAREN); |
| 9251 |
command = new AlterTableAddConstraint(session, schema, CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE, |
| 9252 |
ifNotExists); |
| 9253 |
command.setNullsDistinct(nullsDistinct); |
| 9254 |
if (readIf(VALUE, CLOSE_PAREN)) { |
| 9255 |
command.setIndexColumns(null); |
| 9256 |
} else { |
| 9257 |
command.setIndexColumns(parseIndexColumnList()); |
| 9258 |
} |
| 9259 |
if (readIf("INDEX")) { |
| 9260 |
String indexName = readIdentifierWithSchema(); |
| 9261 |
command.setIndex(getSchema().findIndex(session, indexName)); |
| 9262 |
} |
| 9263 |
if (compatibility) { |
| 9264 |
readIf(USING, "BTREE"); |
| 9265 |
} |
| 9266 |
break; |
| 9267 |
} |
| 9268 |
case FOREIGN: |
| 9269 |
read(); |
| 9270 |
read(KEY); |
| 9271 |
read(OPEN_PAREN); |
| 9272 |
command = new AlterTableAddConstraint(session, schema, |
| 9273 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL, ifNotExists); |
| 9274 |
command.setIndexColumns(parseIndexColumnList()); |
| 9275 |
if (readIf("INDEX")) { |
| 9276 |
String indexName = readIdentifierWithSchema(); |
| 9277 |
command.setIndex(schema.findIndex(session, indexName)); |
| 9278 |
} |
| 9279 |
read("REFERENCES"); |
| 9280 |
parseReferences(command, schema, tableName); |
| 9281 |
break; |
| 9282 |
case CHECK: |
| 9283 |
read(); |
| 9284 |
command = new AlterTableAddConstraint(session, schema, CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK, |
| 9285 |
ifNotExists); |
| 9286 |
command.setCheckExpression(readExpression()); |
| 9287 |
break; |
| 9288 |
default: |
| 9289 |
if (constraintName == null) { |
| 9290 |
Mode mode = database.getMode(); |
| 9291 |
if (mode.indexDefinitionInCreateTable) { |
| 9292 |
int start = tokenIndex; |
| 9293 |
if (readIf(KEY) || readIf("INDEX")) { |
| 9294 |
// MySQL |
| 9295 |
// need to read ahead, as it could be a column name |
| 9296 |
if (DataType.getTypeByName(currentToken, mode) == null) { |
| 9297 |
CreateIndex createIndex = new CreateIndex(session, schema); |
| 9298 |
createIndex.setComment(comment); |
| 9299 |
createIndex.setTableName(tableName); |
| 9300 |
createIndex.setIfTableExists(ifTableExists); |
| 9301 |
if (!readIf(OPEN_PAREN)) { |
| 9302 |
createIndex.setIndexName(readIdentifier()); |
| 9303 |
read(OPEN_PAREN); |
| 9304 |
} |
| 9305 |
createIndex.setIndexColumns(parseIndexColumnList()); |
| 9306 |
// MySQL compatibility |
| 9307 |
if (readIf(USING)) { |
| 9308 |
read("BTREE"); |
| 9309 |
} |
| 9310 |
return createIndex; |
| 9311 |
} else { |
| 9312 |
// known data type |
| 9313 |
setTokenIndex(start); |
| 9314 |
} |
| 9315 |
} |
| 9316 |
} |
| 9317 |
return null; |
| 9318 |
} else { |
| 9319 |
if (expectedList != null) { |
| 9320 |
addMultipleExpected(PRIMARY, UNIQUE, FOREIGN, CHECK); |
| 9321 |
} |
| 9322 |
throw getSyntaxError(); |
| 9323 |
} |
| 9324 |
} |
| 9325 |
if (command.getType() != CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) { |
| 9326 |
if (readIf("NOCHECK")) { |
| 9327 |
command.setCheckExisting(false); |
| 9328 |
} else { |
| 9329 |
readIf(CHECK); |
| 9330 |
command.setCheckExisting(true); |
| 9331 |
} |
| 9332 |
} |
| 9333 |
command.setTableName(tableName); |
| 9334 |
command.setIfTableExists(ifTableExists); |
| 9335 |
command.setConstraintName(constraintName); |
| 9336 |
command.setComment(comment); |
| 9337 |
return command; |
| 9338 |
} |
| 9339 |
|
| 9340 |
private void parseReferences(AlterTableAddConstraint command, |
| 9341 |
Schema schema, String tableName) { |
| 9342 |
if (readIf(OPEN_PAREN)) { |
| 9343 |
command.setRefTableName(schema, tableName); |
| 9344 |
command.setRefIndexColumns(parseIndexColumnList()); |
| 9345 |
} else { |
| 9346 |
String refTableName = readIdentifierWithSchema(schema.getName()); |
| 9347 |
command.setRefTableName(getSchema(), refTableName); |
| 9348 |
if (readIf(OPEN_PAREN)) { |
| 9349 |
command.setRefIndexColumns(parseIndexColumnList()); |
| 9350 |
} |
| 9351 |
} |
| 9352 |
if (readIf("INDEX")) { |
| 9353 |
String indexName = readIdentifierWithSchema(); |
| 9354 |
command.setRefIndex(getSchema().findIndex(session, indexName)); |
| 9355 |
} |
| 9356 |
while (readIf(ON)) { |
| 9357 |
if (readIf("DELETE")) { |
| 9358 |
command.setDeleteAction(parseAction()); |
| 9359 |
} else { |
| 9360 |
read("UPDATE"); |
| 9361 |
command.setUpdateAction(parseAction()); |
| 9362 |
} |
| 9363 |
} |
| 9364 |
if (!readIf(NOT, "DEFERRABLE")) { |
| 9365 |
readIf("DEFERRABLE"); |
| 9366 |
} |
| 9367 |
} |
| 9368 |
|
| 9369 |
private CreateLinkedTable parseCreateLinkedTable(boolean temp, |
| 9370 |
boolean globalTemp, boolean force) { |
| 9371 |
read(TABLE); |
| 9372 |
boolean ifNotExists = readIfNotExists(); |
| 9373 |
String tableName = readIdentifierWithSchema(); |
| 9374 |
CreateLinkedTable command = new CreateLinkedTable(session, getSchema()); |
| 9375 |
command.setTemporary(temp); |
| 9376 |
command.setGlobalTemporary(globalTemp); |
| 9377 |
command.setForce(force); |
| 9378 |
command.setIfNotExists(ifNotExists); |
| 9379 |
command.setTableName(tableName); |
| 9380 |
command.setComment(readCommentIf()); |
| 9381 |
read(OPEN_PAREN); |
| 9382 |
command.setDriver(readString()); |
| 9383 |
read(COMMA); |
| 9384 |
command.setUrl(readString()); |
| 9385 |
read(COMMA); |
| 9386 |
command.setUser(readString()); |
| 9387 |
read(COMMA); |
| 9388 |
command.setPassword(readString()); |
| 9389 |
read(COMMA); |
| 9390 |
String originalTable = readString(); |
| 9391 |
if (readIf(COMMA)) { |
| 9392 |
command.setOriginalSchema(originalTable); |
| 9393 |
originalTable = readString(); |
| 9394 |
} |
| 9395 |
command.setOriginalTable(originalTable); |
| 9396 |
read(CLOSE_PAREN); |
| 9397 |
if (readIf("EMIT", "UPDATES")) { |
| 9398 |
command.setEmitUpdates(true); |
| 9399 |
} else if (readIf("READONLY")) { |
| 9400 |
command.setReadOnly(true); |
| 9401 |
} |
| 9402 |
if (readIf("FETCH_SIZE")) { |
| 9403 |
command.setFetchSize(readNonNegativeInt()); |
| 9404 |
} |
| 9405 |
if (readIf("AUTOCOMMIT")) { |
| 9406 |
if (readIf("ON")) { |
| 9407 |
command.setAutoCommit(true); |
| 9408 |
} else if (readIf("OFF")) { |
| 9409 |
command.setAutoCommit(false); |
| 9410 |
} |
| 9411 |
} |
| 9412 |
return command; |
| 9413 |
} |
| 9414 |
|
| 9415 |
private CreateTable parseCreateTable(boolean temp, boolean globalTemp, |
| 9416 |
boolean persistIndexes) { |
| 9417 |
boolean ifNotExists = readIfNotExists(); |
| 9418 |
String tableName = readIdentifierWithSchema(); |
| 9419 |
if (temp && globalTemp && equalsToken("SESSION", schemaName)) { |
| 9420 |
// support weird syntax: declare global temporary table session.xy |
| 9421 |
// (...) not logged |
| 9422 |
schemaName = session.getCurrentSchemaName(); |
| 9423 |
globalTemp = false; |
| 9424 |
} |
| 9425 |
Schema schema = getSchema(); |
| 9426 |
CreateTable command = new CreateTable(session, schema); |
| 9427 |
command.setPersistIndexes(persistIndexes); |
| 9428 |
command.setTemporary(temp); |
| 9429 |
command.setGlobalTemporary(globalTemp); |
| 9430 |
command.setIfNotExists(ifNotExists); |
| 9431 |
command.setTableName(tableName); |
| 9432 |
command.setComment(readCommentIf()); |
| 9433 |
if (readIf(OPEN_PAREN)) { |
| 9434 |
if (!readIf(CLOSE_PAREN)) { |
| 9435 |
do { |
| 9436 |
parseTableColumnDefinition(command, schema, tableName, true); |
| 9437 |
} while (readIfMore()); |
| 9438 |
} |
| 9439 |
} |
| 9440 |
if (database.getMode().mySqlTableOptions) { |
| 9441 |
parseCreateTableMySQLTableOptions(command); |
| 9442 |
} |
| 9443 |
if (readIf("ENGINE")) { |
| 9444 |
command.setTableEngine(readIdentifier()); |
| 9445 |
} |
| 9446 |
if (readIf(WITH)) { |
| 9447 |
command.setTableEngineParams(readTableEngineParams()); |
| 9448 |
} |
| 9449 |
if (temp) { |
| 9450 |
if (readIf(ON, "COMMIT")) { |
| 9451 |
if (readIf("DROP")) { |
| 9452 |
command.setOnCommitDrop(); |
| 9453 |
} else if (readIf("DELETE")) { |
| 9454 |
read("ROWS"); |
| 9455 |
command.setOnCommitTruncate(); |
| 9456 |
} |
| 9457 |
} else if (readIf(NOT)) { |
| 9458 |
if (readIf("PERSISTENT")) { |
| 9459 |
command.setPersistData(false); |
| 9460 |
} else { |
| 9461 |
read("LOGGED"); |
| 9462 |
} |
| 9463 |
} |
| 9464 |
if (readIf("TRANSACTIONAL")) { |
| 9465 |
command.setTransactional(true); |
| 9466 |
} |
| 9467 |
} else if (!persistIndexes && readIf(NOT, "PERSISTENT")) { |
| 9468 |
command.setPersistData(false); |
| 9469 |
} |
| 9470 |
if (readIf("HIDDEN")) { |
| 9471 |
command.setHidden(true); |
| 9472 |
} |
| 9473 |
if (readIf(AS)) { |
| 9474 |
readIf("SORTED"); |
| 9475 |
command.setQuery(parseQuery()); |
| 9476 |
if (readIf(WITH)) { |
| 9477 |
command.setWithNoData(readIf("NO")); |
| 9478 |
read("DATA"); |
| 9479 |
} |
| 9480 |
} |
| 9481 |
return command; |
| 9482 |
} |
| 9483 |
|
| 9484 |
private void parseTableColumnDefinition(CommandWithColumns command, Schema schema, String tableName, |
| 9485 |
boolean forCreateTable) { |
| 9486 |
DefineCommand c = parseTableConstraintIf(tableName, schema, false); |
| 9487 |
if (c != null) { |
| 9488 |
command.addConstraintCommand(c); |
| 9489 |
return; |
| 9490 |
} |
| 9491 |
String columnName = readIdentifier(); |
| 9492 |
if (forCreateTable && (currentTokenType == COMMA || currentTokenType == CLOSE_PAREN)) { |
| 9493 |
command.addColumn(new Column(columnName, TypeInfo.TYPE_UNKNOWN)); |
| 9494 |
return; |
| 9495 |
} |
| 9496 |
Column column = parseColumnForTable(columnName, true); |
| 9497 |
if (column.hasIdentityOptions() && column.isPrimaryKey()) { |
| 9498 |
command.addConstraintCommand(newPrimaryKeyConstraintCommand(session, schema, tableName, column)); |
| 9499 |
} |
| 9500 |
command.addColumn(column); |
| 9501 |
readColumnConstraints(command, schema, tableName, column); |
| 9502 |
} |
| 9503 |
|
| 9504 |
/** |
| 9505 |
* Create a new alter table command. |
| 9506 |
* |
| 9507 |
* @param session the session |
| 9508 |
* @param schema the schema |
| 9509 |
* @param tableName the table |
| 9510 |
* @param column the column |
| 9511 |
* @return the command |
| 9512 |
*/ |
| 9513 |
public static AlterTableAddConstraint newPrimaryKeyConstraintCommand(SessionLocal session, Schema schema, |
| 9514 |
String tableName, Column column) { |
| 9515 |
column.setPrimaryKey(false); |
| 9516 |
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, |
| 9517 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY, false); |
| 9518 |
pk.setTableName(tableName); |
| 9519 |
pk.setIndexColumns(new IndexColumn[] { new IndexColumn(column.getName()) }); |
| 9520 |
return pk; |
| 9521 |
} |
| 9522 |
|
| 9523 |
private void readColumnConstraints(CommandWithColumns command, Schema schema, String tableName, Column column) { |
| 9524 |
String comment = column.getComment(); |
| 9525 |
boolean hasPrimaryKey = false, hasNotNull = false; |
| 9526 |
NullConstraintType nullType; |
| 9527 |
Mode mode = database.getMode(); |
| 9528 |
for (;;) { |
| 9529 |
String constraintName; |
| 9530 |
if (readIf(CONSTRAINT)) { |
| 9531 |
constraintName = readIdentifier(); |
| 9532 |
} else if (comment == null && (comment = readCommentIf()) != null) { |
| 9533 |
// Compatibility: COMMENT may be specified appear after some constraint |
| 9534 |
column.setComment(comment); |
| 9535 |
continue; |
| 9536 |
} else { |
| 9537 |
constraintName = null; |
| 9538 |
} |
| 9539 |
if (!hasPrimaryKey && readIf(PRIMARY, KEY)) { |
| 9540 |
hasPrimaryKey = true; |
| 9541 |
boolean hash = readIf("HASH"); |
| 9542 |
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, |
| 9543 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY, false); |
| 9544 |
pk.setConstraintName(constraintName); |
| 9545 |
pk.setPrimaryKeyHash(hash); |
| 9546 |
pk.setTableName(tableName); |
| 9547 |
pk.setIndexColumns(new IndexColumn[] { new IndexColumn(column.getName()) }); |
| 9548 |
command.addConstraintCommand(pk); |
| 9549 |
} else if (readIf(UNIQUE)) { |
| 9550 |
NullsDistinct nullsDistinct = readNullsDistinct(database.getMode().nullsDistinct); |
| 9551 |
AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema, |
| 9552 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE, false); |
| 9553 |
unique.setConstraintName(constraintName); |
| 9554 |
unique.setNullsDistinct(nullsDistinct); |
| 9555 |
unique.setIndexColumns(new IndexColumn[] { new IndexColumn(column.getName()) }); |
| 9556 |
unique.setTableName(tableName); |
| 9557 |
command.addConstraintCommand(unique); |
| 9558 |
} else if (!hasNotNull |
| 9559 |
&& (nullType = parseNotNullConstraint()) != NullConstraintType.NO_NULL_CONSTRAINT_FOUND) { |
| 9560 |
hasNotNull = true; |
| 9561 |
if (nullType == NullConstraintType.NULL_IS_NOT_ALLOWED) { |
| 9562 |
column.setNullable(false); |
| 9563 |
} else if (nullType == NullConstraintType.NULL_IS_ALLOWED) { |
| 9564 |
if (column.isIdentity()) { |
| 9565 |
throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, column.getName()); |
| 9566 |
} |
| 9567 |
column.setNullable(true); |
| 9568 |
} |
| 9569 |
} else if (readIf(CHECK)) { |
| 9570 |
AlterTableAddConstraint check = new AlterTableAddConstraint(session, schema, |
| 9571 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK, false); |
| 9572 |
check.setConstraintName(constraintName); |
| 9573 |
check.setTableName(tableName); |
| 9574 |
check.setCheckExpression(readExpression()); |
| 9575 |
command.addConstraintCommand(check); |
| 9576 |
} else if (readIf("REFERENCES")) { |
| 9577 |
AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema, |
| 9578 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL, false); |
| 9579 |
ref.setConstraintName(constraintName); |
| 9580 |
ref.setIndexColumns(new IndexColumn[] { new IndexColumn(column.getName()) }); |
| 9581 |
ref.setTableName(tableName); |
| 9582 |
parseReferences(ref, schema, tableName); |
| 9583 |
command.addConstraintCommand(ref); |
| 9584 |
} else if (constraintName == null) { |
| 9585 |
if (column.getIdentityOptions() != null || !parseCompatibilityIdentity(column, mode)) { |
| 9586 |
return; |
| 9587 |
} |
| 9588 |
} else { |
| 9589 |
throw getSyntaxError(); |
| 9590 |
} |
| 9591 |
} |
| 9592 |
} |
| 9593 |
|
| 9594 |
private boolean parseCompatibilityIdentity(Column column, Mode mode) { |
| 9595 |
if (mode.autoIncrementClause && readIf("AUTO_INCREMENT")) { |
| 9596 |
parseCompatibilityIdentityOptions(column); |
| 9597 |
return true; |
| 9598 |
} |
| 9599 |
if (mode.identityClause && readIf("IDENTITY")) { |
| 9600 |
parseCompatibilityIdentityOptions(column); |
| 9601 |
return true; |
| 9602 |
} |
| 9603 |
return false; |
| 9604 |
} |
| 9605 |
|
| 9606 |
private void parseCreateTableMySQLTableOptions(CreateTable command) { |
| 9607 |
boolean requireNext = false; |
| 9608 |
for (;;) { |
| 9609 |
if (readIf("AUTO_INCREMENT")) { |
| 9610 |
readIf(EQUAL); |
| 9611 |
Expression value = readExpression(); |
| 9612 |
set: { |
| 9613 |
AlterTableAddConstraint primaryKey = command.getPrimaryKey(); |
| 9614 |
if (primaryKey != null) { |
| 9615 |
for (IndexColumn ic : primaryKey.getIndexColumns()) { |
| 9616 |
String columnName = ic.columnName; |
| 9617 |
for (Column column : command.getColumns()) { |
| 9618 |
if (database.equalsIdentifiers(column.getName(), columnName)) { |
| 9619 |
SequenceOptions options = column.getIdentityOptions(); |
| 9620 |
if (options != null) { |
| 9621 |
options.setStartValue(value); |
| 9622 |
break set; |
| 9623 |
} |
| 9624 |
} |
| 9625 |
} |
| 9626 |
} |
| 9627 |
} |
| 9628 |
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, "AUTO_INCREMENT PRIMARY KEY"); |
| 9629 |
} |
| 9630 |
} else if (readIf(DEFAULT)) { |
| 9631 |
if (!readIf("CHARACTER", SET)) { |
| 9632 |
readIf("CHARSET"); |
| 9633 |
readIf("COLLATE"); |
| 9634 |
} |
| 9635 |
readMySQLCharset(); |
| 9636 |
} else if (readIf("CHARACTER")) { |
| 9637 |
read(SET); |
| 9638 |
readMySQLCharset(); |
| 9639 |
} else if (readIf("COLLATE")) { |
| 9640 |
readMySQLCharset(); |
| 9641 |
} else if (readIf("CHARSET")) { |
| 9642 |
readMySQLCharset(); |
| 9643 |
} else if (readIf("COMMENT")) { |
| 9644 |
readIf(EQUAL); |
| 9645 |
command.setComment(readString()); |
| 9646 |
} else if (readIf("ENGINE")) { |
| 9647 |
readIf(EQUAL); |
| 9648 |
readIdentifier(); |
| 9649 |
} else if (readIf("ROW_FORMAT")) { |
| 9650 |
readIf(EQUAL); |
| 9651 |
readIdentifier(); |
| 9652 |
} else if (requireNext) { |
| 9653 |
throw getSyntaxError(); |
| 9654 |
} else { |
| 9655 |
break; |
| 9656 |
} |
| 9657 |
requireNext = readIf(COMMA); |
| 9658 |
} |
| 9659 |
} |
| 9660 |
|
| 9661 |
private void readMySQLCharset() { |
| 9662 |
readIf(EQUAL); |
| 9663 |
readIdentifier(); |
| 9664 |
} |
| 9665 |
|
| 9666 |
/** |
| 9667 |
* Enumeration describing null constraints |
| 9668 |
*/ |
| 9669 |
private enum NullConstraintType { |
| 9670 |
NULL_IS_ALLOWED, NULL_IS_NOT_ALLOWED, NO_NULL_CONSTRAINT_FOUND |
| 9671 |
} |
| 9672 |
|
| 9673 |
private NullConstraintType parseNotNullConstraint(NullConstraintType nullConstraint) { |
| 9674 |
if (nullConstraint == NullConstraintType.NO_NULL_CONSTRAINT_FOUND) { |
| 9675 |
nullConstraint = parseNotNullConstraint(); |
| 9676 |
} |
| 9677 |
return nullConstraint; |
| 9678 |
} |
| 9679 |
|
| 9680 |
private NullConstraintType parseNotNullConstraint() { |
| 9681 |
NullConstraintType nullConstraint; |
| 9682 |
if (readIf(NOT, NULL)) { |
| 9683 |
nullConstraint = NullConstraintType.NULL_IS_NOT_ALLOWED; |
| 9684 |
} else if (readIf(NULL)) { |
| 9685 |
nullConstraint = NullConstraintType.NULL_IS_ALLOWED; |
| 9686 |
} else { |
| 9687 |
return NullConstraintType.NO_NULL_CONSTRAINT_FOUND; |
| 9688 |
} |
| 9689 |
if (database.getMode().getEnum() == ModeEnum.Oracle) { |
| 9690 |
nullConstraint = parseNotNullCompatibility(nullConstraint); |
| 9691 |
} |
| 9692 |
return nullConstraint; |
| 9693 |
} |
| 9694 |
|
| 9695 |
private NullConstraintType parseNotNullCompatibility(NullConstraintType nullConstraint) { |
| 9696 |
if (readIf("ENABLE")) { |
| 9697 |
if (!readIf("VALIDATE") && readIf("NOVALIDATE")) { |
| 9698 |
// Turn off constraint, allow NULLs |
| 9699 |
nullConstraint = NullConstraintType.NULL_IS_ALLOWED; |
| 9700 |
} |
| 9701 |
} else if (readIf("DISABLE")) { |
| 9702 |
// Turn off constraint, allow NULLs |
| 9703 |
nullConstraint = NullConstraintType.NULL_IS_ALLOWED; |
| 9704 |
if (!readIf("VALIDATE")) { |
| 9705 |
readIf("NOVALIDATE"); |
| 9706 |
} |
| 9707 |
} |
| 9708 |
return nullConstraint; |
| 9709 |
} |
| 9710 |
|
| 9711 |
private CreateSynonym parseCreateSynonym(boolean orReplace) { |
| 9712 |
boolean ifNotExists = readIfNotExists(); |
| 9713 |
String name = readIdentifierWithSchema(); |
| 9714 |
Schema synonymSchema = getSchema(); |
| 9715 |
read(FOR); |
| 9716 |
String tableName = readIdentifierWithSchema(); |
| 9717 |
|
| 9718 |
Schema targetSchema = getSchema(); |
| 9719 |
CreateSynonym command = new CreateSynonym(session, synonymSchema); |
| 9720 |
command.setName(name); |
| 9721 |
command.setSynonymFor(tableName); |
| 9722 |
command.setSynonymForSchema(targetSchema); |
| 9723 |
command.setComment(readCommentIf()); |
| 9724 |
command.setIfNotExists(ifNotExists); |
| 9725 |
command.setOrReplace(orReplace); |
| 9726 |
return command; |
| 9727 |
} |
| 9728 |
|
| 9729 |
private static int getCompareType(int tokenType) { |
| 9730 |
switch (tokenType) { |
| 9731 |
case EQUAL: |
| 9732 |
return Comparison.EQUAL; |
| 9733 |
case BIGGER_EQUAL: |
| 9734 |
return Comparison.BIGGER_EQUAL; |
| 9735 |
case BIGGER: |
| 9736 |
return Comparison.BIGGER; |
| 9737 |
case SMALLER: |
| 9738 |
return Comparison.SMALLER; |
| 9739 |
case SMALLER_EQUAL: |
| 9740 |
return Comparison.SMALLER_EQUAL; |
| 9741 |
case NOT_EQUAL: |
| 9742 |
return Comparison.NOT_EQUAL; |
| 9743 |
case SPATIAL_INTERSECTS: |
| 9744 |
return Comparison.SPATIAL_INTERSECTS; |
| 9745 |
default: |
| 9746 |
return -1; |
| 9747 |
} |
| 9748 |
} |
| 9749 |
|
| 9750 |
/** |
| 9751 |
* Add double quotes around an identifier if required. |
| 9752 |
* |
| 9753 |
* @param s the identifier |
| 9754 |
* @param sqlFlags formatting flags |
| 9755 |
* @return the quoted identifier |
| 9756 |
*/ |
| 9757 |
public static String quoteIdentifier(String s, int sqlFlags) { |
| 9758 |
if (s == null) { |
| 9759 |
return "\"\""; |
| 9760 |
} |
| 9761 |
if ((sqlFlags & HasSQL.QUOTE_ONLY_WHEN_REQUIRED) != 0 && ParserUtil.isSimpleIdentifier(s, false, false)) { |
| 9762 |
return s; |
| 9763 |
} |
| 9764 |
return StringUtils.quoteIdentifier(s); |
| 9765 |
} |
| 9766 |
|
| 9767 |
public void setLiteralsChecked(boolean literalsChecked) { |
| 9768 |
this.literalsChecked = literalsChecked; |
| 9769 |
} |
| 9770 |
|
| 9771 |
public void setRightsChecked(boolean rightsChecked) { |
| 9772 |
this.rightsChecked = rightsChecked; |
| 9773 |
} |
| 9774 |
|
| 9775 |
public void setSuppliedParameters(ArrayList<Parameter> suppliedParameters) { |
| 9776 |
this.parameters = suppliedParameters; |
| 9777 |
} |
| 9778 |
|
| 9779 |
/** |
| 9780 |
* Parse a SQL code snippet that represents an expression. |
| 9781 |
* |
| 9782 |
* @param sql the code snippet |
| 9783 |
* @return the expression object |
| 9784 |
*/ |
| 9785 |
public Expression parseExpression(String sql) { |
| 9786 |
initialize(sql, null, false); |
| 9787 |
read(); |
| 9788 |
return readExpression(); |
| 9789 |
} |
| 9790 |
|
| 9791 |
/** |
| 9792 |
* Parse a SQL code snippet that represents an expression for a domain constraint. |
| 9793 |
* |
| 9794 |
* @param sql the code snippet |
| 9795 |
* @return the expression object |
| 9796 |
*/ |
| 9797 |
public Expression parseDomainConstraintExpression(String sql) { |
| 9798 |
initialize(sql, null, false); |
| 9799 |
read(); |
| 9800 |
try { |
| 9801 |
parseDomainConstraint = true; |
| 9802 |
return readExpression(); |
| 9803 |
} finally { |
| 9804 |
parseDomainConstraint = false; |
| 9805 |
} |
| 9806 |
} |
| 9807 |
|
| 9808 |
/** |
| 9809 |
* Parse a SQL code snippet that represents a table name. |
| 9810 |
* |
| 9811 |
* @param sql the code snippet |
| 9812 |
* @return the table object |
| 9813 |
*/ |
| 9814 |
public Table parseTableName(String sql) { |
| 9815 |
initialize(sql, null, false); |
| 9816 |
read(); |
| 9817 |
return readTableOrView(); |
| 9818 |
} |
| 9819 |
|
| 9820 |
/** |
| 9821 |
* Parses a list of column names or numbers in parentheses. |
| 9822 |
* |
| 9823 |
* @param sql the source SQL |
| 9824 |
* @param offset the initial offset |
| 9825 |
* @return the array of column names ({@code String[]}) or numbers |
| 9826 |
* ({@code int[]}) |
| 9827 |
* @throws DbException on syntax error |
| 9828 |
*/ |
| 9829 |
public Object parseColumnList(String sql, int offset) { |
| 9830 |
initialize(sql, null, true); |
| 9831 |
for (int i = 0, l = tokens.size(); i < l; i++) { |
| 9832 |
if (tokens.get(i).start() >= offset) { |
| 9833 |
setTokenIndex(i); |
| 9834 |
break; |
| 9835 |
} |
| 9836 |
} |
| 9837 |
read(OPEN_PAREN); |
| 9838 |
if (readIf(CLOSE_PAREN)) { |
| 9839 |
return Utils.EMPTY_INT_ARRAY; |
| 9840 |
} |
| 9841 |
if (isIdentifier()) { |
| 9842 |
ArrayList<String> list = Utils.newSmallArrayList(); |
| 9843 |
do { |
| 9844 |
if (!isIdentifier()) { |
| 9845 |
throw getSyntaxError(); |
| 9846 |
} |
| 9847 |
list.add(currentToken); |
| 9848 |
read(); |
| 9849 |
} while (readIfMore()); |
| 9850 |
return list.toArray(new String[0]); |
| 9851 |
} else if (currentTokenType == LITERAL) { |
| 9852 |
ArrayList<Integer> list = Utils.newSmallArrayList(); |
| 9853 |
do { |
| 9854 |
list.add(readInt()); |
| 9855 |
} while (readIfMore()); |
| 9856 |
int count = list.size(); |
| 9857 |
int[] array = new int[count]; |
| 9858 |
for (int i = 0; i < count; i++) { |
| 9859 |
array[i] = list.get(i); |
| 9860 |
} |
| 9861 |
return array; |
| 9862 |
} else { |
| 9863 |
throw getSyntaxError(); |
| 9864 |
} |
| 9865 |
} |
| 9866 |
|
| 9867 |
/** |
| 9868 |
* Returns the last parse index. |
| 9869 |
* |
| 9870 |
* @return the last parse index |
| 9871 |
*/ |
| 9872 |
public int getLastParseIndex() { |
| 9873 |
return token.start(); |
| 9874 |
} |
| 9875 |
|
| 9876 |
@Override |
| 9877 |
public String toString() { |
| 9878 |
return StringUtils.addAsterisk(sqlCommand, token.start()); |
| 9879 |
} |
| 9880 |
} |