简体   繁体   中英

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

I am trying to run three different queries: 1. To get details of few employees when some condition is true. 2. To get all details of all employees. 3. To get all details of an employee when matches the Empid.

When i run my code, it compiles successfully

But when i call the API for (2 and 3) it says failed to fetch. API for 1 runs successfully.

On browsing for solution and trying to find where the error happens, it converges at rows.Scan() function.

When i comment rows.Scan() line, it runs without giving data but when runned after un-commenting

Shows error: Failed to fetch.

So the error is with rows.Scan()

Maybe we cannot have more than one rows.Scan() in a project. Thinking of this i created a function that takes a querystatement and returns an interface.

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.

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.

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.

This thing worked and solved all the errors.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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