1 module dbal.driver.sqlite.syntax; 2 3 import dbal; 4 5 class SqliteSyntax : SqlSyntax 6 { 7 SqlBuilder _builder; 8 9 this(SqliteBuilder builder) 10 { 11 this._builder = builder; 12 } 13 14 string selectExpr() 15 { 16 string select; 17 if(!_builder.selectKeys.length) 18 select = " * "; 19 else { 20 foreach(v;_builder.selectKeys){ 21 select ~= v ~ ","; 22 } 23 select = select[0 .. $-1]; 24 } 25 return select; 26 } 27 string whereExpr() 28 { 29 string where; 30 if(_builder.whereKeys.length){ 31 int i = 0; 32 where = " WHERE "; 33 foreach(v;_builder.whereKeys){ 34 i++; 35 where ~= v.toString; 36 if(i<_builder.whereKeys.length) where ~= " AND "; 37 } 38 } 39 if (_builder.multiWhereStr.length){ 40 where ~= where.length ? (" AND " ~ _builder.multiWhereStr) : (" WHERE" ~ _builder.multiWhereStr); 41 } 42 return where; 43 } 44 string joinExpr() 45 { 46 string joinstr; 47 if(_builder.joins.length) 48 foreach(join;_builder.joins){joinstr ~= join.toString;} 49 return joinstr; 50 } 51 string groupByExpr() 52 { 53 if(_builder.groupBy.length) 54 return " GROUP BY " ~ _builder.groupBy; 55 return null; 56 } 57 string havingExpr() 58 { 59 if(_builder.having.length) 60 return " HAVING " ~ _builder.having; 61 return null; 62 } 63 string orderExpr() 64 { 65 if(_builder.orderBy.length && _builder.order.length) 66 return " ORDER BY " ~ _builder.orderBy ~ " " ~ _builder.order; 67 return null; 68 } 69 string limitExpr() 70 { 71 if(_builder.limit) 72 return " LIMIT " ~ (_builder.limit).to!string; 73 return null; 74 } 75 string offsetExpr() 76 { 77 if(_builder.offset) 78 return " OFFSET " ~ (_builder.offset).to!string; 79 return null; 80 } 81 82 string setExpr() 83 { 84 string set; 85 if(_builder.values.length){ 86 set ~= " SET "; 87 int i = 0; 88 foreach(k,v;_builder.values){ 89 i++; 90 set ~= v.toString; 91 if(i<_builder.values.length) set ~= " , "; 92 } 93 }else{ 94 throw new DbalException("query builder update method have not set values"); 95 } 96 return set; 97 } 98 99 string insertExpr() 100 { 101 if(!_builder.values.length) throw new DbalException("query build insert have not values"); 102 string keys; 103 string values; 104 foreach(k,v;_builder.values){ 105 keys ~= k~","; 106 values ~= v.value~","; 107 } 108 return "(" ~ keys[0.. $-1] ~ ") VALUES("~ values[0..$-1] ~")"; 109 } 110 111 string autoIncreaseExpr() 112 { 113 return ""; 114 } 115 override string toString() 116 { 117 if(!_builder.tableName.length) 118 throw new DbalException("query build table name not exists"); 119 string str; 120 switch(_builder.method){ 121 case Method.Select: 122 str ~= Method.Select; 123 str ~= selectExpr(); 124 str ~= " FROM " ~ _builder.tableName ~ " " ~ _builder.tableNameAlias ~ " "; 125 str ~= joinExpr(); 126 str ~= whereExpr(); 127 str ~= groupByExpr(); 128 str ~= havingExpr(); 129 str ~= orderExpr(); 130 str ~= limitExpr(); 131 str ~= offsetExpr(); 132 break; 133 case Method.Update: 134 str ~= Method.Update ~ " " ~ _builder.tableName; 135 str ~= setExpr(); 136 str ~= whereExpr(); 137 str ~= orderExpr(); 138 str ~= limitExpr(); 139 break; 140 case Method.Delete: 141 str ~= Method.Delete ~ " " ~ _builder.tableName; 142 str ~= whereExpr(); 143 str ~= orderExpr(); 144 str ~= limitExpr(); 145 break; 146 case Method.Insert: 147 str ~= Method.Insert ~ " " ~ _builder.tableName; 148 str ~= insertExpr(); 149 break; 150 default: 151 throw new DbalException("query build method not found"); 152 } 153 return str; 154 } 155 }