Skip to content

Commit

Permalink
feat(presence): do not extract value
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Nov 18, 2024
1 parent 0b5a5de commit d9a81cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
16 changes: 14 additions & 2 deletions internal/infra/commonsql/dialect_db2.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func (db2 Db2Dialect) Select(tableName string, schemaName string, where string,

if names := Names(columns); len(names) > 0 {
for i := range names {
names[i] = db2.Quote(names[i])
if columns[i].OnlyPresence {
names[i] = db2.selectPresence(names[i])
} else {
names[i] = db2.Quote(names[i])
}
}
query.WriteString(strings.Join(names, ", "))
} else {
Expand All @@ -91,7 +95,11 @@ func (db2 Db2Dialect) SelectLimit(tableName string, schemaName string, where str

if names := Names(columns); len(names) > 0 {
for i := range names {
names[i] = db2.Quote(names[i])
if columns[i].OnlyPresence {
names[i] = db2.selectPresence(names[i])
} else {
names[i] = db2.Quote(names[i])
}
}
query.WriteString(strings.Join(names, ", "))
} else {
Expand Down Expand Up @@ -123,3 +131,7 @@ func (db2 Db2Dialect) Quote(id string) string {
func (db2 Db2Dialect) CreateSelect(sel string, where string, limit string, columns string, from string) string {
return fmt.Sprintf("%s %s %s %s %s", sel, columns, from, where, limit)
}

func (db2 Db2Dialect) selectPresence(column string) string {
return fmt.Sprintf("CASE WHEN %s IS NOT NULL THEN 1 ELSE NULL END AS %s", db2.Quote(column), db2.Quote(column))
}
16 changes: 14 additions & 2 deletions internal/infra/commonsql/dialect_mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func (pd MariadbDialect) Select(tableName string, schemaName string, where strin

if names := Names(columns); len(names) > 0 {
for i := range names {
names[i] = pd.Quote(names[i])
if columns[i].OnlyPresence {
names[i] = pd.selectPresence(names[i])
} else {
names[i] = pd.Quote(names[i])
}
}
query.WriteString(strings.Join(names, ", "))
} else {
Expand All @@ -91,7 +95,11 @@ func (pd MariadbDialect) SelectLimit(tableName string, schemaName string, where

if names := Names(columns); len(names) > 0 {
for i := range names {
names[i] = pd.Quote(names[i])
if columns[i].OnlyPresence {
names[i] = pd.selectPresence(names[i])
} else {
names[i] = pd.Quote(names[i])
}
}
query.WriteString(strings.Join(names, ", "))
} else {
Expand Down Expand Up @@ -123,3 +131,7 @@ func (sd MariadbDialect) Quote(id string) string {
func (sd MariadbDialect) CreateSelect(sel string, where string, limit string, columns string, from string) string {
return fmt.Sprintf("%s %s %s %s %s", sel, columns, from, where, limit)
}

func (sd MariadbDialect) selectPresence(column string) string {
return fmt.Sprintf("CASE WHEN %s IS NOT NULL THEN 'TRUE' ELSE NULL END AS %s", sd.Quote(column), sd.Quote(column))
}
2 changes: 1 addition & 1 deletion internal/infra/commonsql/dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ func (pgd PostgresDialect) CreateSelect(sel string, where string, limit string,
}

func (pgd PostgresDialect) selectPresence(column string) string {
return fmt.Sprintf("CASE WHEN (%s IS NULL) THEN NULL WHEN (%s IS NOT NULL) THEN TRUE END AS %s", pgd.Quote(column), pgd.Quote(column), pgd.Quote(column))
return fmt.Sprintf("CASE WHEN (%s IS NOT NULL) THEN TRUE ELSE NULL END AS %s", pgd.Quote(column), pgd.Quote(column))
}

0 comments on commit d9a81cb

Please sign in to comment.