1 /*
2  * Entity - Entity is an object-relational mapping tool for the D programming language. Referring to the design idea of JPA.
3  *
4  * Copyright (C) 2015-2018  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs.cn
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.entity.EntityExpression;
13 
14 import hunt.logging;
15 import std.string;
16 
17 
18 class EntityExpression
19 {
20     private string _columnName;
21     private string _columnSpecifier;
22     private string _tableName;
23 
24     private string _fullColumn;
25     private string _selectColumn;
26     private string _columnAs;
27 
28     this(string columnName, string tableName)
29     {
30         _columnName = columnName;
31         _tableName = tableName;
32         _fullColumn = tableName ~ "." ~ columnName;
33         _columnAs = tableName ~ "__as__" ~ columnName;
34         _selectColumn = _fullColumn ~ " AS " ~ _columnAs;
35     }
36 
37     string getFullColumn() {return _fullColumn;}
38     string getColumnName() {return _columnName;}
39     string getTableName() {return _tableName;}
40     string getColumnAs() {return _selectColumn;}
41     string getColumnAsName() {return _columnAs;}
42 
43     string getSelectColumn()
44     {
45         return _fullColumn ~ " AS " ~ _columnAs;
46     }
47 
48     //s: max min avg sum count
49     EntityExpression setColumnSpecifier(string s)
50     {
51         _fullColumn = s ~ "(" ~ _fullColumn ~ ")";
52         if (s == "COUNT" || s == "count") {
53             _columnAs = _tableName ~ "__as__countfor"~_tableName~"_";
54         }
55         return this;
56     }
57 
58     EntityExpression setDistinct(bool b)
59     {
60         if (b)
61             _fullColumn = "DISTINCT "~_fullColumn;
62         else 
63             _fullColumn = _fullColumn.replace("DISTINCT ", "");
64         return this;
65     } 
66 
67 
68     static string getFullColumnName(string columnName, string tableName) {
69         return tableName ~ "." ~ columnName;
70     }
71 
72     static string getColumnAsName(string columnName, string tableName) {
73         return tableName ~ "__as__" ~ columnName;
74     }
75 
76     static string getColumnAs(string columnName, string tableName) {
77         return getFullColumnName(columnName, tableName) ~ " AS " ~ getColumnAsName(columnName, tableName);
78     }    
79 
80     static string getCountAsName(string tableName) {
81         return tableName ~ "__as__countfor" ~ tableName ~ "_";
82     }
83 }