@@ -2885,6 +2885,164 @@ func TestQuery(t *testing.T) {
28852885 }
28862886 })
28872887
2888+ t .Run ("query with case when then" , func (t * testing.T ) {
2889+ _ , _ , err := engine .Exec (
2890+ context .Background (),
2891+ nil ,
2892+ `CREATE TABLE employees (
2893+ employee_id INTEGER AUTO_INCREMENT,
2894+ first_name VARCHAR[50],
2895+ last_name VARCHAR[50],
2896+ department VARCHAR[50],
2897+ salary INTEGER,
2898+ hire_date TIMESTAMP,
2899+ job_title VARCHAR[50],
2900+
2901+ PRIMARY KEY employee_id
2902+ );` ,
2903+ nil ,
2904+ )
2905+ require .NoError (t , err )
2906+
2907+ n := 100
2908+ for i := 0 ; i < n ; i ++ {
2909+ _ , _ , err := engine .Exec (
2910+ context .Background (),
2911+ nil ,
2912+ `INSERT INTO employees(first_name, last_name, department, salary, job_title)
2913+ VALUES (@first_name, @last_name, @department, @salary, @job_title)
2914+ ` ,
2915+ map [string ]interface {}{
2916+ "first_name" : fmt .Sprintf ("name%d" , i ),
2917+ "last_name" : fmt .Sprintf ("surname%d" , i ),
2918+ "department" : []string {"sales" , "manager" , "engineering" }[rand .Intn (3 )],
2919+ "salary" : []int64 {20 , 40 , 50 , 80 , 100 }[rand .Intn (5 )] * 1000 ,
2920+ "job_title" : []string {"manager" , "senior engineer" , "executive" }[rand .Intn (3 )],
2921+ },
2922+ )
2923+ require .NoError (t , err )
2924+ }
2925+
2926+ _ , err = engine .queryAll (
2927+ context .Background (),
2928+ nil ,
2929+ "SELECT CASE WHEN salary THEN 0 END FROM employees" ,
2930+ nil ,
2931+ )
2932+ require .ErrorIs (t , err , ErrInvalidTypes )
2933+
2934+ rows , err := engine .queryAll (
2935+ context .Background (),
2936+ nil ,
2937+ `SELECT
2938+ employee_id,
2939+ first_name,
2940+ last_name,
2941+ salary,
2942+ CASE
2943+ WHEN salary < 50000 THEN @low
2944+ WHEN salary >= 50000 AND salary <= 100000 THEN @medium
2945+ ELSE @high
2946+ END AS salary_category
2947+ FROM employees;` ,
2948+ map [string ]interface {}{
2949+ "low" : "Low" ,
2950+ "medium" : "Medium" ,
2951+ "high" : "High" ,
2952+ },
2953+ )
2954+ require .NoError (t , err )
2955+ require .Len (t , rows , n )
2956+
2957+ for _ , row := range rows {
2958+ salary := row .ValuesByPosition [3 ].RawValue ().(int64 )
2959+ category , _ := row .ValuesByPosition [4 ].RawValue ().(string )
2960+
2961+ expectedCategory := "High"
2962+ if salary < 50000 {
2963+ expectedCategory = "Low"
2964+ } else if salary >= 50000 && salary <= 100000 {
2965+ expectedCategory = "Medium"
2966+ }
2967+ require .Equal (t , expectedCategory , category )
2968+ }
2969+
2970+ rows , err = engine .queryAll (
2971+ context .Background (),
2972+ nil ,
2973+ `SELECT
2974+ department,
2975+ job_title,
2976+ CASE
2977+ WHEN department = 'sales' THEN
2978+ CASE
2979+ WHEN job_title = 'manager' THEN '20% Bonus'
2980+ ELSE '10% Bonus'
2981+ END
2982+ WHEN department = 'engineering' THEN
2983+ CASE
2984+ WHEN job_title = 'senior engineer' THEN '15% Bonus'
2985+ ELSE '5% Bonus'
2986+ END
2987+ ELSE
2988+ CASE
2989+ WHEN job_title = 'executive' THEN '12% Bonus'
2990+ ELSE 'No Bonus'
2991+ END
2992+ END AS bonus
2993+ FROM employees;` ,
2994+ nil ,
2995+ )
2996+ require .NoError (t , err )
2997+ require .Len (t , rows , n )
2998+
2999+ for _ , row := range rows {
3000+ department := row .ValuesByPosition [0 ].RawValue ().(string )
3001+ job , _ := row .ValuesByPosition [1 ].RawValue ().(string )
3002+ bonus , _ := row .ValuesByPosition [2 ].RawValue ().(string )
3003+
3004+ var expectedBonus string
3005+ switch department {
3006+ case "sales" :
3007+ if job == "manager" {
3008+ expectedBonus = "20% Bonus"
3009+ } else {
3010+ expectedBonus = "10% Bonus"
3011+ }
3012+ case "engineering" :
3013+ if job == "senior engineer" {
3014+ expectedBonus = "15% Bonus"
3015+ } else {
3016+ expectedBonus = "5% Bonus"
3017+ }
3018+ default :
3019+ if job == "executive" {
3020+ expectedBonus = "12% Bonus"
3021+ } else {
3022+ expectedBonus = "No Bonus"
3023+ }
3024+ }
3025+ require .Equal (t , expectedBonus , bonus )
3026+ }
3027+
3028+ rows , err = engine .queryAll (
3029+ context .Background (),
3030+ nil ,
3031+ `SELECT
3032+ CASE
3033+ WHEN department = 'sales' THEN 'Sales Team'
3034+ END AS department
3035+ FROM employees
3036+ WHERE department != 'sales'
3037+ LIMIT 1
3038+ ;` ,
3039+ nil ,
3040+ )
3041+ require .NoError (t , err )
3042+ require .Len (t , rows , 1 )
3043+ require .Nil (t , rows [0 ].ValuesByPosition [0 ].RawValue ())
3044+ })
3045+
28883046 t .Run ("invalid queries" , func (t * testing.T ) {
28893047 r , err = engine .Query (context .Background (), nil , "INVALID QUERY" , nil )
28903048 require .ErrorIs (t , err , ErrParsingError )
0 commit comments