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.mysql.ast.statement.MySqlLockTableStatement;
17 
18 import hunt.collection;
19 import hunt.sql.ast.SQLObject;
20 
21 import hunt.sql.ast.SQLCommentHint;
22 import hunt.sql.ast.statement.SQLExprTableSource;
23 import hunt.sql.dialect.mysql.ast.MySqlObjectImpl;
24 import hunt.sql.dialect.mysql.visitor.MySqlASTVisitor;
25 import hunt.sql.dialect.mysql.ast.statement.MySqlStatementImpl;
26 
27 public class MySqlLockTableStatement : MySqlStatementImpl
28 {
29     alias accept0 = MySqlStatementImpl.accept0;
30     
31     private List!(Item) items;
32 
33     this()
34     {
35         items = new ArrayList!(Item)();
36     }
37 
38     override public void accept0(MySqlASTVisitor visitor)
39     {
40         if (visitor.visit(this))
41         {
42             acceptChild(visitor, cast(List!(SQLObject))items);
43         }
44         visitor.endVisit(this);
45     }
46 
47     public static struct LockType
48     {
49         enum LockType READ = LockType("READ");
50         enum LockType READ_LOCAL = LockType("READ LOCAL");
51         enum LockType WRITE = LockType("WRITE");
52         enum LOW_PRIORITY_WRITE = LockType("LOW_PRIORITY WRITE");
53 
54         public string name;
55 
56         this(string name)
57         {
58             this.name = name;
59         }
60 
61         bool opEquals(const LockType h) nothrow
62         {
63             return name == h.name;
64         }
65 
66         bool opEquals(ref const LockType h) nothrow
67         {
68             return name == h.name;
69         }
70     }
71 
72     public List!(Item) getItems()
73     {
74         return items;
75     }
76 
77     public void setItems(List!(Item) items)
78     {
79         this.items = items;
80     }
81 
82     public LockType getLockType()
83     {
84         if (items.size() == 1)
85         {
86             return items.get(0).lockType;
87         }
88         return LockType(string.init);
89     }
90 
91     public SQLExprTableSource getTableSource()
92     {
93         if (items.size() == 1)
94         {
95             return items.get(0).tableSource;
96         }
97         return null;
98     }
99 
100     public static class Item : MySqlObjectImpl
101     {
102 
103         alias accept0 = MySqlObjectImpl.accept0;
104 
105         private SQLExprTableSource tableSource;
106 
107         private LockType lockType;
108 
109         private List!(SQLCommentHint) hints;
110 
111         this()
112         {
113             tableSource = new SQLExprTableSource();
114         }
115     
116         override public void accept0(MySqlASTVisitor visitor)
117         {
118             if (visitor.visit(this))
119             {
120                 acceptChild(visitor, tableSource);
121             }
122             visitor.endVisit(this);
123         }
124 
125         public SQLExprTableSource getTableSource()
126         {
127             return tableSource;
128         }
129 
130         public void setTableSource(SQLExprTableSource tableSource)
131         {
132             if (tableSource !is null)
133             {
134                 tableSource.setParent(this);
135             }
136             this.tableSource = tableSource;
137         }
138 
139         public LockType getLockType()
140         {
141             return lockType;
142         }
143 
144         public void setLockType(LockType lockType)
145         {
146             this.lockType = lockType;
147         }
148 
149         public List!(SQLCommentHint) getHints()
150         {
151             return hints;
152         }
153 
154         public void setHints(List!(SQLCommentHint) hints)
155         {
156             this.hints = hints;
157         }
158     }
159 }