github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/column_test.go (about) 1 // Copyright 2021 ecodeclub 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package eorm 16 17 import "fmt" 18 19 func ExampleC() { 20 db := memoryDB() 21 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").EQ(18)).Build() 22 fmt.Printf(` 23 SQL: %s 24 Args: %v 25 `, query.SQL, query.Args) 26 // Output: 27 // SQL: SELECT `id` FROM `test_model` WHERE `id`=?; 28 // Args: [18] 29 } 30 31 func ExampleColumn_EQ() { 32 db := memoryDB() 33 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").EQ(18)).Build() 34 fmt.Printf(` 35 SQL: %s 36 Args: %v 37 `, query.SQL, query.Args) 38 // Output: 39 // SQL: SELECT `id` FROM `test_model` WHERE `id`=?; 40 // Args: [18] 41 } 42 43 func ExampleColumn_Add() { 44 db := memoryDB() 45 tm := &TestModel{} 46 query, _ := NewUpdater[TestModel](db).Update(tm).Set(Assign("Age", C("Age").Add(1))).Build() 47 fmt.Printf(` 48 SQL: %s 49 Args: %v 50 `, query.SQL, query.Args) 51 // Output: 52 // SQL: UPDATE `test_model` SET `age`=(`age`+?); 53 // Args: [1] 54 } 55 56 func ExampleColumn_As() { 57 db := memoryDB() 58 query, _ := NewSelector[TestModel](db).Select(C("Id").As("my_id")).Build() 59 fmt.Printf(` 60 SQL: %s 61 Args: %v 62 `, query.SQL, query.Args) 63 // Output: 64 // SQL: SELECT `id` AS `my_id` FROM `test_model`; 65 // Args: [] 66 } 67 68 func ExampleColumn_GT() { 69 db := memoryDB() 70 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").GT(18)).Build() 71 fmt.Printf(` 72 SQL: %s 73 Args: %v 74 `, query.SQL, query.Args) 75 // Output: 76 // SQL: SELECT `id` FROM `test_model` WHERE `id`>?; 77 // Args: [18] 78 } 79 80 func ExampleColumn_GTEQ() { 81 db := memoryDB() 82 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").GTEQ(18)).Build() 83 fmt.Printf(` 84 SQL: %s 85 Args: %v 86 `, query.SQL, query.Args) 87 // Output: 88 // SQL: SELECT `id` FROM `test_model` WHERE `id`>=?; 89 // Args: [18] 90 } 91 92 func ExampleColumn_LT() { 93 db := memoryDB() 94 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").LT(18)).Build() 95 fmt.Printf(` 96 SQL: %s 97 Args: %v 98 `, query.SQL, query.Args) 99 // Output: 100 // SQL: SELECT `id` FROM `test_model` WHERE `id`<?; 101 // Args: [18] 102 } 103 104 func ExampleColumn_LTEQ() { 105 db := memoryDB() 106 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").LTEQ(18)).Build() 107 fmt.Printf(` 108 SQL: %s 109 Args: %v 110 `, query.SQL, query.Args) 111 // Output: 112 // SQL: SELECT `id` FROM `test_model` WHERE `id`<=?; 113 // Args: [18] 114 } 115 116 func ExampleColumn_Multi() { 117 db := memoryDB() 118 tm := &TestModel{} 119 query, _ := NewUpdater[TestModel](db).Update(tm).Set(Assign("Age", C("Age").Multi(2))).Build() 120 fmt.Printf(` 121 SQL: %s 122 Args: %v 123 `, query.SQL, query.Args) 124 // Output: 125 // SQL: UPDATE `test_model` SET `age`=(`age`*?); 126 // Args: [2] 127 } 128 129 func ExampleColumn_NEQ() { 130 db := memoryDB() 131 query, _ := NewSelector[TestModel](db).Select(C("Id")).Where(C("Id").NEQ(18)).Build() 132 fmt.Printf(` 133 SQL: %s 134 Args: %v 135 `, query.SQL, query.Args) 136 // Output: 137 // SQL: SELECT `id` FROM `test_model` WHERE `id`!=?; 138 // Args: [18] 139 } 140 141 func ExampleColumns() { 142 db := memoryDB() 143 query, _ := NewSelector[TestModel](db).Select(Columns("Id", "Age")).Build() 144 fmt.Printf(` 145 SQL: %s 146 Args: %v 147 `, query.SQL, query.Args) 148 // Output: 149 // SQL: SELECT `id`,`age` FROM `test_model`; 150 // Args: [] 151 }