1 /* 2 * Copyright 2015-2018 HuntLabs.cn 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 module hunt.sql.dialect.postgresql.visitor.PGSchemaStatVisitor; 17 18 19 20 import hunt.sql.ast.SQLName; 21 import hunt.sql.ast.statement.SQLSelectQueryBlock; 22 import hunt.sql.ast.statement.SQLSelectStatement; 23 import hunt.sql.ast.statement.SQLTableSource; 24 import hunt.sql.dialect.postgresql.ast.expr.PGBoxExpr; 25 import hunt.sql.dialect.postgresql.ast.expr.PGCidrExpr; 26 import hunt.sql.dialect.postgresql.ast.expr.PGCircleExpr; 27 import hunt.sql.dialect.postgresql.ast.expr.PGExtractExpr; 28 import hunt.sql.dialect.postgresql.ast.expr.PGInetExpr; 29 import hunt.sql.dialect.postgresql.ast.expr.PGLineSegmentsExpr; 30 import hunt.sql.dialect.postgresql.ast.expr.PGMacAddrExpr; 31 import hunt.sql.dialect.postgresql.ast.expr.PGPointExpr; 32 import hunt.sql.dialect.postgresql.ast.expr.PGPolygonExpr; 33 import hunt.sql.dialect.postgresql.ast.expr.PGTypeCastExpr; 34 import hunt.sql.dialect.postgresql.ast.stmt; 35 import hunt.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock; 36 // import hunt.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.FetchClause; 37 // import hunt.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.ForClause; 38 // import hunt.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.WindowClause; 39 import hunt.sql.visitor.SchemaStatVisitor; 40 import hunt.sql.ast.SQLExpr; 41 import hunt.sql.stat.TableStat; 42 import hunt.sql.ast.statement.SQLUpdateSetItem; 43 // import hunt.sql.stat.TableStat.Mode; 44 import hunt.sql.util.DBType; 45 import hunt.sql.util.PGUtils; 46 import hunt.sql.dialect.postgresql.visitor.PGASTVisitor; 47 import hunt.collection; 48 import hunt.sql.ast.SQLObject; 49 50 public class PGSchemaStatVisitor : SchemaStatVisitor , PGASTVisitor { 51 52 alias visit = SchemaStatVisitor.visit; 53 alias endVisit = SchemaStatVisitor.endVisit; 54 55 56 public this() { 57 super(DBType.POSTGRESQL.name); 58 } 59 60 override 61 public string getDbType() { 62 return DBType.POSTGRESQL.name; 63 } 64 65 override 66 public void endVisit(PGSelectQueryBlock.WindowClause x) { 67 68 } 69 70 override 71 public bool visit(PGSelectQueryBlock.WindowClause x) { 72 return true; 73 } 74 75 override 76 public void endVisit(PGSelectQueryBlock.FetchClause x) { 77 78 } 79 80 override 81 public bool visit(PGSelectQueryBlock.FetchClause x) { 82 return true; 83 } 84 85 override 86 public void endVisit(PGSelectQueryBlock.ForClause x) { 87 88 } 89 90 override 91 public bool visit(PGSelectQueryBlock.ForClause x) { 92 93 return true; 94 } 95 96 override 97 public void endVisit(PGDeleteStatement x) { 98 99 } 100 101 override 102 public bool visit(PGDeleteStatement x) { 103 if (repository !is null 104 && x.getParent() is null) { 105 repository.resolve(x); 106 } 107 108 if (x.getWith() !is null) { 109 x.getWith().accept(this); 110 } 111 112 SQLTableSource using = x.getUsing(); 113 if (using !is null) { 114 using.accept(this); 115 } 116 117 x.putAttribute("_original_use_mode", cast(Object)(getMode())); 118 setMode(x, TableStat.Mode.Delete); 119 120 TableStat stat = getTableStat(x.getTableName()); 121 stat.incrementDeleteCount(); 122 123 accept(x.getWhere()); 124 125 return false; 126 } 127 128 override 129 public void endVisit(PGInsertStatement x) { 130 131 } 132 133 override 134 public bool visit(PGInsertStatement x) { 135 if (repository !is null 136 && x.getParent() is null) { 137 repository.resolve(x); 138 } 139 140 if (x.getWith() !is null) { 141 x.getWith().accept(this); 142 } 143 144 x.putAttribute("_original_use_mode", cast(Object)(getMode())); 145 setMode(x, TableStat.Mode.Insert); 146 147 148 SQLName tableName = x.getTableName(); 149 { 150 TableStat stat = getTableStat(tableName); 151 stat.incrementInsertCount(); 152 } 153 154 accept!SQLExpr((x.getColumns())); 155 accept(x.getQuery()); 156 157 return false; 158 } 159 160 override 161 public void endVisit(PGSelectStatement x) { 162 163 } 164 165 override 166 public bool visit(PGSelectStatement x) { 167 return visit(cast(SQLSelectStatement) x); 168 } 169 170 override 171 public void endVisit(PGUpdateStatement x) { 172 173 } 174 175 override public bool isPseudoColumn(long hash) { 176 return PGUtils.isPseudoColumn(hash); 177 } 178 179 override 180 public bool visit(PGUpdateStatement x) { 181 if (repository !is null 182 && x.getParent() is null) { 183 repository.resolve(x); 184 } 185 186 if (x.getWith() !is null) { 187 x.getWith().accept(this); 188 } 189 190 TableStat stat = getTableStat(x.getTableName()); 191 stat.incrementUpdateCount(); 192 193 accept(x.getFrom()); 194 195 accept!SQLUpdateSetItem((x.getItems())); 196 accept(x.getWhere()); 197 198 return false; 199 } 200 201 override 202 public void endVisit(PGSelectQueryBlock x) { 203 super.endVisit(cast(SQLSelectQueryBlock) x); 204 } 205 206 override 207 public bool visit(PGSelectQueryBlock x) { 208 return this.visit(cast(SQLSelectQueryBlock) x); 209 } 210 211 override 212 public void endVisit(PGFunctionTableSource x) { 213 214 } 215 216 override 217 public bool visit(PGFunctionTableSource x) { 218 return true; 219 } 220 221 override 222 public bool visit(PGTypeCastExpr x) { 223 x.getExpr().accept(this); 224 return false; 225 } 226 227 override 228 public void endVisit(PGTypeCastExpr x) { 229 230 } 231 232 override 233 public void endVisit(PGValuesQuery x) { 234 235 } 236 237 override 238 public bool visit(PGValuesQuery x) { 239 return true; 240 } 241 242 override 243 public void endVisit(PGExtractExpr x) { 244 245 } 246 247 override 248 public bool visit(PGExtractExpr x) { 249 return true; 250 } 251 252 override 253 public void endVisit(PGBoxExpr x) { 254 255 } 256 257 override 258 public bool visit(PGBoxExpr x) { 259 return true; 260 } 261 262 override 263 public void endVisit(PGPointExpr x) { 264 265 } 266 267 override 268 public bool visit(PGMacAddrExpr x) { 269 return true; 270 } 271 272 override 273 public void endVisit(PGMacAddrExpr x) { 274 275 } 276 277 override 278 public bool visit(PGInetExpr x) { 279 return true; 280 } 281 282 override 283 public void endVisit(PGInetExpr x) { 284 285 } 286 287 override 288 public bool visit(PGCidrExpr x) { 289 return true; 290 } 291 292 override 293 public void endVisit(PGCidrExpr x) { 294 295 } 296 297 override 298 public bool visit(PGPolygonExpr x) { 299 return true; 300 } 301 302 override 303 public void endVisit(PGPolygonExpr x) { 304 305 } 306 307 override 308 public bool visit(PGCircleExpr x) { 309 return true; 310 } 311 312 override 313 public void endVisit(PGCircleExpr x) { 314 315 } 316 317 override 318 public bool visit(PGLineSegmentsExpr x) { 319 return true; 320 } 321 322 override 323 public void endVisit(PGLineSegmentsExpr x) { 324 325 } 326 327 override 328 public bool visit(PGPointExpr x) { 329 return true; 330 } 331 332 override 333 public void endVisit(PGShowStatement x) { 334 335 } 336 337 override 338 public bool visit(PGShowStatement x) { 339 return false; 340 } 341 342 override 343 public void endVisit(PGStartTransactionStatement x) { 344 345 } 346 347 override 348 public bool visit(PGStartTransactionStatement x) { 349 return false; 350 } 351 352 override 353 public void endVisit(PGConnectToStatement x) { 354 355 } 356 357 override 358 public bool visit(PGConnectToStatement x) { 359 return false; 360 } 361 362 }