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.repository.SchemaObjectImpl; 17 18 import hunt.sql.ast.SQLStatement; 19 import hunt.sql.ast.statement.SQLColumnDefinition; 20 import hunt.sql.ast.statement.SQLCreateTableStatement; 21 import hunt.sql.ast.statement.SQLTableElement; 22 import hunt.sql.ast.statement.SQLUniqueConstraint; 23 import hunt.sql.util.FnvHash; 24 import hunt.sql.repository.SchemaObjectType; 25 import hunt.sql.repository.SchemaObject; 26 27 /** 28 * Created by wenshao on 08/06/2017. 29 */ 30 public class SchemaObjectImpl : SchemaObject { 31 private string name; 32 private long hashCode64; 33 34 private SchemaObjectType type; 35 private SQLStatement statement; 36 37 public long rowCount = -1; 38 39 public this(string name, SchemaObjectType type) { 40 this(name, type, null); 41 } 42 43 public this(string name, SchemaObjectType type, SQLStatement statement) { 44 this.name = name; 45 this.type = type; 46 this.statement = statement; 47 48 this.hashCode64 = FnvHash.hashCode64(name); 49 } 50 51 public long nameHashCode64() { 52 return hashCode64; 53 } 54 55 public static enum Type { 56 Sequence, Table, View, Index, Function 57 } 58 59 public SQLStatement getStatement() { 60 return statement; 61 } 62 63 public SQLColumnDefinition findColumn(string columName) { 64 long hash = FnvHash.hashCode64(columName); 65 return findColumn(hash); 66 } 67 68 public SQLColumnDefinition findColumn(long columNameHash) { 69 if (statement is null) { 70 return null; 71 } 72 73 if (cast(SQLCreateTableStatement)(statement) !is null) { 74 return (cast(SQLCreateTableStatement) statement).findColumn(columNameHash); 75 } 76 77 return null; 78 } 79 80 public bool matchIndex(string columnName) { 81 if (statement is null) { 82 return false; 83 } 84 85 if (cast(SQLCreateTableStatement)(statement) !is null) { 86 SQLTableElement index = (cast(SQLCreateTableStatement) statement).findIndex(columnName); 87 return index !is null; 88 } 89 90 return false; 91 } 92 93 public bool matchKey(string columnName) { 94 if (statement is null) { 95 return false; 96 } 97 98 if (cast(SQLCreateTableStatement)(statement) !is null) { 99 SQLTableElement index = (cast(SQLCreateTableStatement) statement).findIndex(columnName); 100 return cast(SQLUniqueConstraint)(index) !is null; 101 } 102 103 return false; 104 } 105 106 override 107 public string getName() { 108 return name; 109 } 110 111 override 112 public SchemaObjectType getType() { 113 return type; 114 } 115 116 override 117 public long getRowCount() { 118 return rowCount; 119 } 120 }