1 module hunt.sql.ast.SQLOrderBy;
2 
3 import hunt.sql.ast.SQLObject;
4 import hunt.sql.ast.SQLObjectImpl;
5 import hunt.sql.visitor.SQLASTVisitor;
6 import hunt.sql.ast.SQLExpr;
7 import hunt.sql.ast.SQLOrderingSpecification;
8 import hunt.collection;
9 import hunt.sql.ast.statement.SQLSelectOrderByItem;
10 
11 
12 
13 public  class SQLOrderBy : SQLObjectImpl {
14 
15     protected  List!SQLSelectOrderByItem items;
16     
17     // for postgres
18     private bool                            sibings;
19 
20     public this(){
21         items = new ArrayList!SQLSelectOrderByItem();
22     }
23 
24     public this(SQLExpr expr){
25         this();
26         SQLSelectOrderByItem item = new SQLSelectOrderByItem(expr);
27         addItem(item);
28     }
29 
30     public void addItem(SQLSelectOrderByItem item) {
31         if (item !is null) {
32             item.setParent(this);
33         }
34         this.items.add(item);
35     }
36 
37     public List!SQLSelectOrderByItem getItems() {
38         return this.items;
39     }
40     
41     public bool isSibings() {
42         return this.sibings;
43     }
44 
45     public void setSibings(bool sibings) {
46         this.sibings = sibings;
47     }
48 
49     protected override void accept0(SQLASTVisitor visitor) {
50         if (visitor.visit(this)) {
51             acceptChild!SQLSelectOrderByItem(visitor, this.items);
52         }
53 
54         visitor.endVisit(this);
55     }
56 
57     override public size_t toHash() @trusted nothrow {
58          int prime = 31;
59         size_t result = 1;
60         result = prime * result + ((items is null) ? 0 : (cast(Object)items).toHash());
61         result = prime * result + (sibings ? 1231 : 1237);
62         return result;
63     }
64 
65     override public bool opEquals(Object obj) {
66         if (this is obj) return true;
67         if (obj is null) return false;
68         if (typeid(SQLOrderBy) != typeid(obj)) return false;
69         SQLOrderBy other = cast(SQLOrderBy) obj;
70         if (items is null) {
71             if (other.items !is null) return false;
72         } else if (!(items == other.items)) return false;
73         if (sibings != other.sibings) return false;
74         return true;
75     }
76 
77     public void addItem(SQLExpr expr, SQLOrderingSpecification type) {
78         SQLSelectOrderByItem item = createItem();
79         item.setExpr(expr);
80         item.setType(type);
81         addItem(item);
82     }
83 
84     protected SQLSelectOrderByItem createItem() {
85         return new SQLSelectOrderByItem();
86     }
87 
88     public override SQLOrderBy clone() {
89         SQLOrderBy x = new SQLOrderBy();
90 
91         foreach (SQLSelectOrderByItem item ; items) {
92             SQLSelectOrderByItem item1 = item.clone();
93             item1.setParent(x);
94             x.items.add(item1);
95         }
96 
97         x.sibings = sibings;
98 
99         return x;
100     }
101 }