yjp il y a 4 mois
Parent
commit
cfc5e46e70

+ 13 - 9
framework/core/infrastructure/database/clause/condition.go

@@ -26,7 +26,7 @@ func (conditions *Conditions) Equal(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" = ?")
+	conditions.appendQuery(columnName, " = ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -36,7 +36,7 @@ func (conditions *Conditions) Like(columnName string, arg string) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" LIKE ?")
+	conditions.appendQuery(columnName, " LIKE ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -46,7 +46,7 @@ func (conditions *Conditions) In(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" IN ?")
+	conditions.appendQuery(columnName, " IN ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -56,7 +56,7 @@ func (conditions *Conditions) NotIn(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" NOT IN ?")
+	conditions.appendQuery(columnName, " NOT IN ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -66,7 +66,7 @@ func (conditions *Conditions) Not(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" != ?")
+	conditions.appendQuery(columnName, " != ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -76,7 +76,7 @@ func (conditions *Conditions) LessThan(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" < ?")
+	conditions.appendQuery(columnName, " < ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -86,7 +86,7 @@ func (conditions *Conditions) LessThanAndEqual(columnName string, arg any) *Cond
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" <= ?")
+	conditions.appendQuery(columnName, " <= ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -96,7 +96,7 @@ func (conditions *Conditions) GreaterThan(columnName string, arg any) *Condition
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" > ?")
+	conditions.appendQuery(columnName, " > ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -106,7 +106,7 @@ func (conditions *Conditions) GreaterThanAndEqual(columnName string, arg any) *C
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" >= ?")
+	conditions.appendQuery(columnName, " >= ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -119,6 +119,10 @@ func (conditions *Conditions) Or() *Or {
 	return &Or{conditions: conditions}
 }
 
+func (conditions *Conditions) appendQuery(columnName string, expr string) {
+	conditions.queries = append(conditions.queries, `"`+columnName+`"`+expr)
+}
+
 func (conditions *Conditions) formClause(conditionOperator string) (string, error) {
 	if conditions.queries == nil || len(conditions.queries) == 0 {
 		return "", nil

+ 13 - 9
framework/core/infrastructure/database/sql/conditions.go

@@ -40,7 +40,7 @@ func (conditions *Conditions) Equal(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" = ?")
+	conditions.appendQuery(columnName, " = ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -56,7 +56,7 @@ func (conditions *Conditions) Like(columnName string, arg string) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" LIKE ?")
+	conditions.appendQuery(columnName, " LIKE ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -72,7 +72,7 @@ func (conditions *Conditions) In(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" IN ?")
+	conditions.appendQuery(columnName, " IN ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -88,7 +88,7 @@ func (conditions *Conditions) NotIn(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" NOT IN ?")
+	conditions.appendQuery(columnName, " NOT IN ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -104,7 +104,7 @@ func (conditions *Conditions) Not(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" != ?")
+	conditions.appendQuery(columnName, " != ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -120,7 +120,7 @@ func (conditions *Conditions) LessThan(columnName string, arg any) *Conditions {
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" < ?")
+	conditions.appendQuery(columnName, " < ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -136,7 +136,7 @@ func (conditions *Conditions) LessThanAndEqual(columnName string, arg any) *Cond
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" <= ?")
+	conditions.appendQuery(columnName, " <= ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -152,7 +152,7 @@ func (conditions *Conditions) GreaterThan(columnName string, arg any) *Condition
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" > ?")
+	conditions.appendQuery(columnName, " > ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -168,7 +168,7 @@ func (conditions *Conditions) GreaterThanAndEqual(columnName string, arg any) *C
 		return conditions
 	}
 
-	conditions.queries = append(conditions.queries, columnName+" >= ?")
+	conditions.appendQuery(columnName, " >= ?")
 	conditions.args = append(conditions.args, []any{arg})
 	return conditions
 }
@@ -218,6 +218,10 @@ func (conditions *Conditions) OR() string {
 	return conditions.formClause("OR")
 }
 
+func (conditions *Conditions) appendQuery(columnName string, expr string) {
+	conditions.queries = append(conditions.queries, `"`+columnName+`"`+expr)
+}
+
 func (conditions *Conditions) formClause(conditionOperator string) string {
 	if conditions.queries == nil || len(conditions.queries) == 0 {
 		return ""

+ 1 - 1
test/database_clause_test.go

@@ -16,7 +16,7 @@ FROM
   test.students
   JOIN test.classes ON classes.id = test.students.class_id
 WHERE
-  (name = ? AND hobby IN ?) OR (age = ? OR gender = ?)
+  ("name" = ? AND "hobby" IN ?) OR ("age" = ? OR "gender" = ?)
 ORDER BY name DESC
 LIMIT ? OFFSET ?`
 

+ 10 - 10
test/database_sql_test.go

@@ -25,15 +25,15 @@ func TestDatabaseSqlConditions(t *testing.T) {
 
 	exceptQueries := []string{
 		`name1 = ?`,
-		`age = ?`,
-		`name2 LIKE ?`,
-		`id1 IN ?`,
-		`id2 NOT IN ?`,
-		`id3 != ?`,
-		`num1 < ?`,
-		`num2 <= ?`,
-		`num3 > ?`,
-		`num4 >= ?`,
+		`"age" = ?`,
+		`"name2" LIKE ?`,
+		`"id1" IN ?`,
+		`"id2" NOT IN ?`,
+		`"id3" != ?`,
+		`"num1" < ?`,
+		`"num2" <= ?`,
+		`"num3" > ?`,
+		`"num4" >= ?`,
 	}
 
 	exceptArgs := []any{
@@ -49,7 +49,7 @@ func TestDatabaseSqlConditions(t *testing.T) {
 		40,
 	}
 
-	exceptAnd := `name1 = ? AND age = ? AND name2 LIKE ? AND id1 IN ? AND id2 NOT IN ? AND id3 != ? AND num1 < ? AND num2 <= ? AND num3 > ? AND num4 >= ?`
+	exceptAnd := `name1 = ? AND "age" = ? AND "name2" LIKE ? AND "id1" IN ? AND "id2" NOT IN ? AND "id3" != ? AND "num1" < ? AND "num2" <= ? AND "num3" > ? AND "num4" >= ?`
 
 	for i, query := range conditions.Queries() {
 		if exceptQueries[i] != query {