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.builder.FunctionBuilder; 17 18 import hunt.sql.ast.SQLExpr; 19 import hunt.sql.ast.expr.SQLMethodInvokeExpr; 20 import hunt.sql.util.DBType; 21 22 /** 23 * Created by wenshao on 09/07/2017. 24 */ 25 public class FunctionBuilder { 26 private string dbType; 27 28 public this(string dbType) { 29 this.dbType = dbType; 30 } 31 32 // for character function 33 public SQLMethodInvokeExpr length(SQLExpr expr) { 34 return new SQLMethodInvokeExpr("length", null, expr); 35 } 36 37 public SQLMethodInvokeExpr lower(SQLExpr expr) { 38 return new SQLMethodInvokeExpr("lower", null, expr); 39 } 40 41 public SQLMethodInvokeExpr upper(SQLExpr expr) { 42 return new SQLMethodInvokeExpr("upper", null, expr); 43 } 44 45 public SQLMethodInvokeExpr substr(SQLExpr expr) { 46 return new SQLMethodInvokeExpr("substr", null, expr); 47 } 48 49 public SQLMethodInvokeExpr ltrim(SQLExpr expr) { 50 return new SQLMethodInvokeExpr("ltrim", null, expr); 51 } 52 53 public SQLMethodInvokeExpr rtrim(SQLExpr expr) { 54 return new SQLMethodInvokeExpr("rtrim", null, expr); 55 } 56 57 public SQLMethodInvokeExpr trim(SQLExpr expr) { 58 return new SQLMethodInvokeExpr("trim", null, expr); 59 } 60 61 public SQLMethodInvokeExpr ifnull(SQLExpr expr1, SQLExpr expr2) { 62 if (DBType.ALIYUN_ADS.name == dbType 63 || DBType.PRESTO.name == dbType 64 || DBType.ODPS.name == dbType) { 65 return new SQLMethodInvokeExpr("coalesce", null, expr1, expr2); 66 } 67 68 if (DBType.ORACLE.name == dbType) { 69 return new SQLMethodInvokeExpr("nvl", null, expr1, expr2); 70 } 71 72 if (DBType.SQL_SERVER.name == dbType) { 73 return new SQLMethodInvokeExpr("isnull", null, expr1, expr2); 74 } 75 76 return new SQLMethodInvokeExpr("ifnull", null, expr1, expr2); 77 } 78 }