简体   繁体   English

为 postgres 运行多个查询(SELECT * from emp; Select * from adm;)并在两个不同的结构中获得结果 IN GO(GOLANG)

[英]Run multiple queries(SELECT * from emp; Select * from adm;) for postgres and get result in a two different struct IN GO(GOLANG)

I am using Go 1.13 Postgres 11 and GraphQL我正在使用 Go 1.13 Postgres 11 和 GraphQL

I am trying to run three different queries: 1. To get details of few employees when some condition is true.我正在尝试运行三个不同的查询: 1. 在某些条件为真时获取少数员工的详细信息。 2. To get all details of all employees. 2. 获取所有员工的所有详细信息。 3. To get all details of an employee when matches the Empid. 3. 在匹配 Empid 时获取员工的所有详细信息。

When i run my code, it compiles successfully当我运行我的代码时,它编译成功

But when i call the API for (2 and 3) it says failed to fetch.但是当我为(2和3)调用API时,它说无法获取。 API for 1 runs successfully. API for 1 运行成功。

On browsing for solution and trying to find where the error happens, it converges at rows.Scan() function.在浏览解决方案并尝试查找错误发生的位置时,它会在 rows.Scan() function 处收敛。

When i comment rows.Scan() line, it runs without giving data but when runned after un-commenting当我评论 rows.Scan() 行时,它在不提供数据的情况下运行,但在取消注释后运行时

Shows error: Failed to fetch.显示错误:获取失败。

So the error is with rows.Scan()所以错误在于 rows.Scan()

Maybe we cannot have more than one rows.Scan() in a project.也许我们在一个项目中不能有多个 rows.Scan()。 Thinking of this i created a function that takes a querystatement and returns an interface.考虑到这一点,我创建了一个 function,它接受一个查询语句并返回一个接口。

But this also didnot worked.但这也没有奏效。

    connStr := fmt.Sprintf("port=%d host=%s user=%s password=%s dbname=%s sslmode=disable", hostport, hostname, username, password, databaseName)
    db, err := sql.Open("postgres", connStr)

    if err != nil {
        fmt.Println(`Could not connect to db`)
        panic(err)
    }
    defer db.Close()

    rows, err := db.Query(query)
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    for rows.Next() {
        row := models.Universal{}
        if num == 1 {
            if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.PFNo, &row.BloodGroup, &row.PAN, &row.PersonalEmail, &row.FatherName, &row.EmergencyNo, &row.BankAcNo, &row.EmpBank, &row.IFSC, &row.Designation, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        } else if num == 2 {
            if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        } else {
            if err := rows.Scan(&row.AdhaarNo, &row.Gender, &row.Branch, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        }
    }

    return models.AllUniversal
}

I was having that "Failed to Fetch" error because of the query i was using to fetch records from DB.由于我用来从数据库中获取记录的查询,我遇到了“无法获取”错误。

Since there were few columns which didnot have any value stored in them, they were causing error.由于很少有列中没有存储任何值,因此它们会导致错误。 Then i used the 'case' technique to solve those error.然后我使用“案例”技术来解决这些错误。

Although the same query was working in pgadmin query console.虽然相同的查询在 pgadmin 查询控制台中工作。

The case was required because i was using graphql in the middle and graphql was not able to handle those empty field, so i returned 'N/A' if the field value was null.该案例是必需的,因为我在中间使用 graphql 而 graphql 无法处理那些空字段,所以如果字段值为 Z37A6259CC0C1DAE29BDA7866489DFF0.

And before all these things, I was passing multiple queries to a single user defined function, but later i made different function for queries that return single row and multiple row.在所有这些事情之前,我将多个查询传递给单个用户定义的 function,但后来我为返回单行和多行的查询做了不同的 function。

This thing worked and solved all the errors.这件事有效并解决了所有错误。

Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM