github.com/RevenueMonster/sqlike@v1.0.6/examples/extra.go (about) 1 package examples 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "cloud.google.com/go/civil" 9 "github.com/RevenueMonster/sqlike/sql" 10 "github.com/RevenueMonster/sqlike/sql/expr" 11 "github.com/RevenueMonster/sqlike/sqlike" 12 "github.com/RevenueMonster/sqlike/sqlike/actions" 13 "github.com/RevenueMonster/sqlike/sqlike/options" 14 "github.com/google/uuid" 15 "github.com/stretchr/testify/require" 16 "go.mongodb.org/mongo-driver/mongo" 17 ) 18 19 // ExtraExamples : 20 func ExtraExamples(ctx context.Context, t *testing.T, db *sqlike.Database, mg *mongo.Database) { 21 var ( 22 err error 23 ) 24 25 table := db.Table("A") 26 27 // replace into 28 { 29 table.MustMigrate(ctx, normalStruct{}) 30 err = table.Truncate(ctx) 31 require.NoError(t, err) 32 33 err = table.Replace( 34 ctx, 35 []string{ 36 "$Key", "SID", "Date", "Emoji", "LongStr", 37 "TinyInt", "Float64", "EmptyStruct", "Struct", 38 }, 39 sql.Select( 40 "$Key", "SID", "Date", "Emoji", "LongStr", 41 "TinyInt", "Float32", "EmptyStruct", "Struct", 42 ).From("sqlike", "NormalStruct"), 43 ) 44 require.NoError(t, err) 45 } 46 47 // set custom primary key 48 { 49 tbl := db.Table("PK") 50 var a struct { 51 Key string `sqlike:"$Key"` 52 No int64 53 } 54 55 err = tbl.DropIfExists(ctx) 56 require.NoError(t, err) 57 tbl.MustMigrate(ctx, a) 58 59 var b struct { 60 Key string `sqlike:"$Key"` 61 No int64 `sqlike:",primary_key"` 62 } 63 64 tbl.MustMigrate(ctx, b) 65 } 66 67 table = db.Table("B") 68 69 // Alter table should add primary key if it's not exists 70 { 71 err = table.DropIfExists(ctx) 72 require.NoError(t, err) 73 table.MustMigrate(ctx, struct { 74 ID string 75 Name string 76 }{}) 77 78 table.MustMigrate(ctx, struct { 79 ID string `sqlike:",primary_key"` 80 Name string 81 }{}) 82 83 idxs, err := table.Indexes().List(ctx) 84 require.NoError(t, err) 85 require.Contains(t, idxs, sqlike.Index{ 86 Name: "PRIMARY", 87 Type: "BTREE", 88 IsUnique: true, 89 }) 90 } 91 92 // on update for datetime, time & date 93 { 94 table := db.Table("dateTime") 95 if err := table.DropIfExists(ctx); err != nil { 96 panic(err) 97 } 98 99 type dateTime struct { 100 ID uuid.UUID `sqlike:",primary_key"` 101 Name string 102 Date civil.Date `sqlike:",on_update"` 103 Time time.Time `sqlike:",on_update"` 104 } 105 106 utcNow := time.Now().UTC() 107 dt := dateTime{} 108 dt.ID = uuid.New() 109 dt.Name = "yuki" 110 dt.Date = civil.DateOf(time.Now()) 111 dt.Time = utcNow 112 113 table.MustUnsafeMigrate(ctx, dt) 114 115 if _, err := table.InsertOne( 116 ctx, 117 &dt, 118 options.InsertOne().SetDebug(true), 119 ); err != nil { 120 panic(err) 121 } 122 123 duration := time.Second * 5 124 time.Sleep(duration) 125 126 if _, err := table.UpdateOne( 127 ctx, 128 actions.UpdateOne(). 129 Where( 130 expr.Equal("ID", dt.ID), 131 ). 132 Set( 133 expr.ColumnValue("Name", "eyoki"), 134 ), 135 ); err != nil { 136 panic(err) 137 } 138 139 var o dateTime 140 if err := table.FindOne( 141 ctx, 142 actions.FindOne(). 143 Where( 144 expr.Equal("ID", dt.ID), 145 ), 146 ).Decode(&o); err != nil { 147 panic(err) 148 } 149 150 require.True(t, o.Time.After(utcNow)) 151 require.Equal(t, o.Name, "eyoki") 152 require.True(t, o.Time.Sub(utcNow) >= duration) 153 154 type dateTimeNoUpdate struct { 155 ID uuid.UUID `sqlike:",primary_key"` 156 Name string 157 Date time.Time 158 Time time.Time 159 } 160 table.MustUnsafeMigrate(ctx, dateTimeNoUpdate{}) 161 162 dtu := dateTimeNoUpdate{} 163 dtu.ID = uuid.New() 164 dtu.Name = "sianloong" 165 dtu.Date = utcNow 166 dtu.Time = utcNow 167 if _, err := table.InsertOne( 168 ctx, 169 &dtu, 170 options.InsertOne().SetDebug(true), 171 ); err != nil { 172 panic(err) 173 } 174 } 175 176 // MongoDB : 177 // { 178 // ctx := context.Background() 179 // coll := mg.Collection("MongoStruct") 180 // coll.Drop(ctx) 181 182 // pk := types.NewNameKey("MongoStruct", types.NewIDKey("ID", nil)) 183 // msg := "hello world!!!" 184 185 // ms := mongoStruct{} 186 // ms.Key = pk 187 // ms.Name = msg 188 // _, err = coll.InsertOne(ctx, ms) 189 190 // result := mongoStruct{} 191 // err = coll.FindOne(ctx, bson.M{"key": pk}). 192 // Decode(&result) 193 // require.NoError(t, err) 194 // require.Equal(t, pk, result.Key) 195 // require.Equal(t, msg, result.Name) 196 // } 197 }