How to use Insert method of result Package

Best Testkube code snippet using result.Insert

cronFuncFactory.go

Source:cronFuncFactory.go Github

copy

Full Screen

...21 maxCheckDateString string // 检查日期 格式2006-01-02 23:59:5922 schedulerInfo engineType.SchedulerInfo // 所有调度的配置信息23 configTableList []map[string]string24 sourceAndTargetQuerySqlChan chan map[string]string // {s:sSql,t:tSql,owner,tablename,bd_column}25 resultTableInsertSqlChan chan string // result table insert sql26 backendDBOption dbLinkEngine.DataBaseOption27 logger *zap.Logger28}29func NewCronFuncFactory(schedulerInfo engineType.SchedulerInfo, logger *zap.Logger) *CronFuncFactory {30 backendDBOption := dbLinkEngine.DataBaseOption{31 DBType: "mysql",32 DBHost: global.ServerConfig.DatabaseInfo.MysqlInfo.Host,33 DBPort: uint(global.ServerConfig.DatabaseInfo.MysqlInfo.Port),34 DBName: global.ServerConfig.DatabaseInfo.MysqlInfo.DBName,35 DBUsername: global.ServerConfig.DatabaseInfo.MysqlInfo.Username,36 DBPassword: global.ServerConfig.DatabaseInfo.MysqlInfo.Password,37 }38 return &CronFuncFactory{39 schedulerInfo: schedulerInfo,40 backendDBOption: backendDBOption,41 sourceAndTargetQuerySqlChan: make(chan map[string]string, 100),42 resultTableInsertSqlChan: make(chan string, 100),43 logger: logger,44 }45}46//47// queryConfigTable48// @Description: 查询配置表49// @receiver c50// @return error:51//52func (c *CronFuncFactory) queryConfigTable() error {53 c.configTableList = make([]map[string]string, 0)54 configTableQuerySql := c.schedulerInfo["config_table_query_sql"]55 go func() {56 // config query sql 写入数据库57 insertSchedulerConfigTableQuerySql := fmt.Sprintf(58 "update compare_scheduler_list set config_table_query_sql= '%s' where id = %s ",59 strings.ReplaceAll(configTableQuerySql, "'", "''"), c.schedulerInfo["sid"],60 )61 backendDBLinker, err := dbLinkEngine.GetDBLinker(c.backendDBOption)62 if err != nil {63 c.logger.Error(err.Error())64 return65 }66 defer backendDBLinker.Close()67 _, err = backendDBLinker.Exec(insertSchedulerConfigTableQuerySql)68 if err != nil {69 c.logger.Error(err.Error())70 return71 }72 }()73 configTableDBPort, err := strconv.Atoi(c.schedulerInfo["config_db_port"])74 if err != nil {75 c.logger.Error(err.Error())76 return err77 }78 configDBOption := dbLinkEngine.DataBaseOption{79 DBType: c.schedulerInfo["config_db_type"],80 DBHost: c.schedulerInfo["config_db_host"],81 DBPort: uint(configTableDBPort),82 DBName: c.schedulerInfo["config_db_name"],83 DBUsername: c.schedulerInfo["config_db_username"],84 DBPassword: c.schedulerInfo["config_db_password"],85 }86 dbLinker, err := dbLinkEngine.GetDBLinker(configDBOption)87 if err != nil {88 c.logger.Error(err.Error())89 return err90 }91 defer dbLinker.Close()92 queryRes, err := dbLinker.Query(configTableQuerySql)93 if err != nil {94 c.logger.Error(err.Error(), zap.String("configTableQuerySqlErrorMsg", configTableQuerySql))95 return err96 }97 byteSliceRes, err := json.Marshal(queryRes)98 gResult := gjson.ParseBytes(byteSliceRes)99 infoKeys := gResult.Get("columns").Array()100 infoValues := gResult.Get("values").Array()101 for _, values := range infoValues {102 configInfo := make(map[string]string)103 for j, value := range values.Array() {104 configInfo[strings.ToLower(infoKeys[j].String())] = value.String()105 }106 c.configTableList = append(c.configTableList, configInfo)107 }108 return nil109}110//111// initResultTable112// @Description: 初始化结果表113// @receiver c114// @return error:115//116func (c *CronFuncFactory) initResultTable() error {117 // config init sql 写入数据库118 go func() {119 insertSchedulerConfigTableInitSql := fmt.Sprintf(120 "update compare_scheduler_list set result_table_init_sql= '%s' where id = %s ",121 strings.ReplaceAll(c.schedulerInfo["result_table_init_sql"], "'", "''"), c.schedulerInfo["sid"],122 )123 backendDBLinker, err := dbLinkEngine.GetDBLinker(c.backendDBOption)124 if err != nil {125 c.logger.Error(err.Error())126 return127 }128 defer backendDBLinker.Close()129 _, err = backendDBLinker.Exec(insertSchedulerConfigTableInitSql)130 if err != nil {131 c.logger.Error(err.Error())132 return133 }134 }()135 resultTableInitCheckSql := fmt.Sprintf(c.schedulerInfo["result_table_init_check_sql"], c.checkDateString)136 resultTableDBPort, err := strconv.Atoi(c.schedulerInfo["result_db_port"])137 if err != nil {138 return err139 }140 resultBDOption := dbLinkEngine.DataBaseOption{141 DBType: c.schedulerInfo["result_db_type"],142 DBHost: c.schedulerInfo["result_db_host"],143 DBPort: uint(resultTableDBPort),144 DBName: c.schedulerInfo["result_db_name"],145 DBUsername: c.schedulerInfo["result_db_username"],146 DBPassword: c.schedulerInfo["result_db_password"],147 }148 dbLinker, err := dbLinkEngine.GetDBLinker(resultBDOption)149 defer dbLinker.Close()150 if err != nil {151 c.logger.Error(err.Error())152 return err153 }154 queryRes, err := dbLinker.Query(resultTableInitCheckSql)155 if err != nil {156 c.logger.Error(err.Error(), zap.String("resultTableInitCheckSqlErrorMsg", resultTableInitCheckSql))157 c.logger.Error(err.Error())158 return err159 }160 byteSliceRes, err := json.Marshal(queryRes)161 gResult := gjson.ParseBytes(byteSliceRes)162 infoValues := gResult.Get("values").Array()163 resultQueryNum := infoValues[0].Array()[0].Int()164 if resultQueryNum > 0 {165 return nil166 }167 // 初始化结果表168 resultTableInitSqlChan := make(chan string, 10)169 var waitGroup sync.WaitGroup170 waitGroup.Add(len(c.configTableList))171 go func() { // 拼接sql172 for _, item := range c.configTableList {173 resultTableInitSql := fmt.Sprintf(c.schedulerInfo["result_table_init_sql"], item["owner"], item["tablename"], c.checkDateString)174 resultTableInitSqlChan <- resultTableInitSql175 }176 }()177 // 拼接初始化结果表sql (3个协程读取sql 插入到结果表)178 for i := 0; i < 3; i++ {179 go func() {180 for resultTableInitSql := range resultTableInitSqlChan {181 _, err := dbLinker.Exec(resultTableInitSql)182 if err != nil {183 c.logger.Error(err.Error())184 }185 waitGroup.Done()186 }187 }()188 }189 waitGroup.Wait()190 defer close(resultTableInitSqlChan)191 return nil192}193//194// buildOriginTableQuerySql195// @Description: 构造 source 和 target 查询sql196// @receiver c197//198func (c *CronFuncFactory) buildOriginTableQuerySql() {199 // 构造 source 和target 查询sql200 for _, item := range c.configTableList {201 owner := item["owner"]202 tablename := item["tablename"]203 bdColumn := item["bd_column"]204 sourceTableDBType := c.schedulerInfo["source_db_type"]205 targetTableDBType := c.schedulerInfo["target_db_type"]206 var sourceQuerySql string207 var targetQuerySql string208 // 判断是否存在 bd_column 列 拼接 源表和目标表查询sql209 if len(bdColumn) > 0 {210 sourceQuerySql = fmt.Sprintf(taskSql.SourceAndTargetTableHasBdQuerySqlMap[sourceTableDBType], bdColumn, owner, tablename, bdColumn, c.maxCheckDateString)211 targetQuerySql = fmt.Sprintf(taskSql.SourceAndTargetTableHasBdQuerySqlMap[targetTableDBType], bdColumn, owner, tablename, bdColumn, c.maxCheckDateString)212 } else {213 sourceQuerySql = fmt.Sprintf(taskSql.SourceAndTargetTableNoBdQuerySqlMap[sourceTableDBType], owner, tablename)214 targetQuerySql = fmt.Sprintf(taskSql.SourceAndTargetTableNoBdQuerySqlMap[targetTableDBType], owner, tablename)215 }216 // 将sql写入channel217 c.sourceAndTargetQuerySqlChan <- map[string]string{218 "sourceQuerySql": sourceQuerySql,219 "targetQuerySql": targetQuerySql,220 "owner": owner,221 "tablename": tablename,222 "bd_column": bdColumn,223 }224 }225 defer close(c.sourceAndTargetQuerySqlChan)226}227//228// querySourceAndTargetTable229// @Description: 查询源表和结果表230// @receiver c231// @return error:232//233func (c *CronFuncFactory) querySourceAndTargetTable() error {234 // 并发数235 taskConcurrent, err := strconv.Atoi(c.schedulerInfo["task_concurrent"])236 if err != nil {237 c.logger.Error(err.Error())238 return err239 }240 // 获取sql 并查询241 var waitGroup sync.WaitGroup242 waitGroup.Add(taskConcurrent)243 // 结果表的更新sql写入到后台库244 go func() {245 // result insert sql字符串 写入数据库246 insertSchedulerResultTableInsertSql := fmt.Sprintf(247 "update compare_scheduler_list set result_table_insert_sql= '%s' where id = %s ",248 strings.ReplaceAll(c.schedulerInfo["result_table_insert_sql"], "'", "''"), c.schedulerInfo["sid"],249 )250 backendDBLinker, err := dbLinkEngine.GetDBLinker(c.backendDBOption)251 if err != nil {252 c.logger.Error(err.Error())253 return254 }255 defer backendDBLinker.Close()256 _, err = backendDBLinker.Exec(insertSchedulerResultTableInsertSql)257 if err != nil {258 c.logger.Error(err.Error(), zap.String("insertSchedulerResultTableInsertSqlErrorMsg", insertSchedulerResultTableInsertSql))259 c.logger.Error(err.Error())260 return261 }262 }()263 // 构造结果表insert sql264 for i := 0; i < taskConcurrent; i++ {265 go func() {266 defer waitGroup.Done()267 sourceDBPort, err := strconv.Atoi(c.schedulerInfo["source_db_port"])268 if err != nil {269 c.logger.Error(err.Error())270 return271 }272 targetDBPort, err := strconv.Atoi(c.schedulerInfo["target_db_port"])273 if err != nil {274 c.logger.Error(err.Error())275 return276 }277 sourceDBOptions := dbLinkEngine.DataBaseOption{278 DBType: c.schedulerInfo["source_db_type"],279 DBHost: c.schedulerInfo["source_db_host"],280 DBPort: uint(sourceDBPort),281 DBName: c.schedulerInfo["source_db_name"],282 DBUsername: c.schedulerInfo["source_db_username"],283 DBPassword: c.schedulerInfo["source_db_password"],284 }285 targetDBOptions := dbLinkEngine.DataBaseOption{286 DBType: c.schedulerInfo["target_db_type"],287 DBHost: c.schedulerInfo["target_db_host"],288 DBPort: uint(targetDBPort),289 DBName: c.schedulerInfo["target_db_name"],290 DBUsername: c.schedulerInfo["target_db_username"],291 DBPassword: c.schedulerInfo["target_db_password"],292 }293 sourceDBlinker, err := dbLinkEngine.GetDBLinker(sourceDBOptions)294 if err != nil {295 c.logger.Error(err.Error())296 return297 }298 defer sourceDBlinker.Close()299 targetDBlinker, err := dbLinkEngine.GetDBLinker(targetDBOptions)300 if err != nil {301 c.logger.Error(err.Error())302 return303 }304 defer targetDBlinker.Close()305 // for range 会在 sourceAndTargetQuerySqlChan 关闭且读取完毕 之后跳出306 for itemMap := range c.sourceAndTargetQuerySqlChan {307 sourceQuerySql := itemMap["sourceQuerySql"]308 targetQuerySql := itemMap["targetQuerySql"]309 owner := itemMap["owner"]310 tablename := itemMap["tablename"]311 // 查询源端和目标端数据312 sourceQueryRes, err := sourceDBlinker.Query(sourceQuerySql)313 if err != nil {314 c.logger.Error(err.Error(), zap.String("sourceQuerySqlErrorMsg", sourceQuerySql))315 continue316 }317 sourceQueryResBytes, err := json.Marshal(sourceQueryRes)318 if err != nil {319 c.logger.Error(err.Error())320 continue321 }322 sourceValues := gjson.ParseBytes(sourceQueryResBytes).Get("values").Array()[0].Array()323 sourceCount, sourceMax := sourceValues[0].String(), sourceValues[1].String()324 targetQueryRes, err := targetDBlinker.Query(targetQuerySql)325 if err != nil {326 c.logger.Error(err.Error(), zap.String("targetQuerySqlErrorMsg", targetQuerySql))327 c.logger.Error(err.Error())328 continue329 }330 targetQueryResBytes, err := json.Marshal(targetQueryRes)331 if err != nil {332 c.logger.Error(err.Error())333 continue334 }335 targetValues := gjson.ParseBytes(targetQueryResBytes).Get("values").Array()[0].Array()336 targetCount, targetMax := targetValues[0].String(), targetValues[1].String()337 // 拼接 结果表更新sql338 resultInsertSql := fmt.Sprintf(c.schedulerInfo["result_table_insert_sql"], sourceCount, targetCount, sourceMax, targetMax, c.checkDateString, owner, tablename)339 c.resultTableInsertSqlChan <- resultInsertSql340 }341 }()342 }343 waitGroup.Wait()344 // 结果表插入sql 拼接完毕之后关闭 ResultTableInsertSqlChan345 close(c.resultTableInsertSqlChan)346 return nil347}348//349// insertResultTable350// @Description: 读取chan sql 更新结果表数据351// @receiver c352// @return error:353//354func (c *CronFuncFactory) insertResultTable() error {355 taskConcurrent, err := strconv.Atoi(c.schedulerInfo["task_concurrent"])356 if err != nil {357 c.logger.Error(err.Error())358 return err359 }360 var waitGroup sync.WaitGroup361 waitGroup.Add(taskConcurrent)362 for i := 0; i < taskConcurrent; i++ {363 go func() {364 resultTableDBPort, err := strconv.Atoi(c.schedulerInfo["result_db_port"])365 if err != nil {366 c.logger.Error(err.Error())367 return368 }369 resultBDOption := dbLinkEngine.DataBaseOption{370 DBType: c.schedulerInfo["result_db_type"],371 DBHost: c.schedulerInfo["result_db_host"],372 DBPort: uint(resultTableDBPort),373 DBName: c.schedulerInfo["result_db_name"],374 DBUsername: c.schedulerInfo["result_db_username"],375 DBPassword: c.schedulerInfo["result_db_password"],376 }377 resultDBLinker, err := dbLinkEngine.GetDBLinker(resultBDOption)378 if err != nil {379 c.logger.Error(err.Error())380 return381 }382 defer resultDBLinker.Close()383 for insertSql := range c.resultTableInsertSqlChan {384 _, err := resultDBLinker.Exec(insertSql)385 if err != nil {386 c.logger.Error(err.Error(), zap.String("insertSqlErrorMsg", insertSql))387 c.logger.Error(err.Error())388 continue389 }390 }391 // for range 会在 resultTableInsertSqlChan 关闭且读取完毕 之后跳出392 waitGroup.Done()393 }()394 }395 waitGroup.Wait()396 return nil397}...

Full Screen

Full Screen

result_test.go

Source:result_test.go Github

copy

Full Screen

...9 db, mock, _ := New()10 result := NewErrorResult(fmt.Errorf("some error"))11 mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)12 res, _ := db.Exec("INSERT something")13 _, err := res.LastInsertId()14 fmt.Println(err)15 // Output: some error16}17func ExampleNewResult() {18 var lastInsertID, affected int6419 result := NewResult(lastInsertID, affected)20 mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)21 fmt.Println(mock.ExpectationsWereMet())22 // Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec or ExecContext which:23 // - matches sql: '^INSERT (.+)'24 // - is without arguments25 // - should return Result having:26 // LastInsertId: 027 // RowsAffected: 028}29func TestShouldReturnValidSqlDriverResult(t *testing.T) {30 result := NewResult(1, 2)31 id, err := result.LastInsertId()32 if 1 != id {33 t.Errorf("Expected last insert id to be 1, but got: %d", id)34 }35 if err != nil {36 t.Errorf("expected no error, but got: %s", err)37 }38 affected, err := result.RowsAffected()39 if 2 != affected {40 t.Errorf("Expected affected rows to be 2, but got: %d", affected)41 }42 if err != nil {43 t.Errorf("expected no error, but got: %s", err)44 }45}46func TestShouldReturnErroeSqlDriverResult(t *testing.T) {47 result := NewErrorResult(fmt.Errorf("some error"))48 _, err := result.LastInsertId()49 if err == nil {50 t.Error("expected error, but got none")51 }52 _, err = result.RowsAffected()53 if err == nil {54 t.Error("expected error, but got none")55 }56}...

Full Screen

Full Screen

Insert

Using AI Code Generation

copy

Full Screen

1result.Insert(0, 1)2result.Insert(0, 2)3result.Insert(0, 3)4result.Insert(0, 4)5result.Insert(0, 5)6result.Insert(0, 6)7result.Insert(0, 7)8result.Insert(0, 8)9result.Insert(0, 9)10result.Insert(0, 10)11result.Remove(0)12result.Remove(0)13result.Remove(0)

Full Screen

Full Screen

Insert

Using AI Code Generation

copy

Full Screen

1result := NewResult()2result.Insert(1, "one")3result.Insert(2, "two")4result.Insert(3, "three")5result.Insert(4, "four")6result.Insert(5, "five")7result.Insert(6, "six")8result.Insert(7, "seven")9result.Insert(8, "eight")10result.Insert(9, "nine")11result.Insert(10, "ten")12result.Insert(11, "eleven")13result.Insert(12, "twelve")14result.Remove(1)15result.Remove(3)16result.Remove(5)17fmt.Println(result.Get(1))18fmt.Println(result.Get(3))19fmt.Println(result.Get(5))20fmt.Println(result.GetIndex("one"))21fmt.Println(result.GetIndex("three"))22fmt.Println(result.GetIndex("five"))23fmt.Println(result.GetIndex("one"))24fmt.Println(result.GetIndex("three"))25fmt.Println(result.GetIndex("five"))26fmt.Println(result.Count())27result.Clear()28fmt.Println(result.Count())29result.Insert(1, "one")30result.Insert(2, "two")31result.Insert(3, "three")32result.Insert(4, "four")33result.Insert(5, "five")34result.Insert(6, "six")35result.Insert(7, "seven")36result.Insert(8, "eight")37result.Insert(9, "nine")38result.Insert(10, "ten")39result.Insert(11, "eleven")40result.Insert(12, "twelve")41fmt.Println(result.GetIndex("one"))42fmt.Println(result.GetIndex("three"))43fmt.Println(result.GetIndex("five"))44fmt.Println(result.GetIndex("one"))45fmt.Println(result.GetIndex("three"))46fmt.Println(result.GetIndex("five"))47fmt.Println(result.Count())48result.Clear()49fmt.Println(result.Count())

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful