|
| 1 | +package drivers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/rayakame/sqlc-gen-better-python/internal/codegen/builders" |
| 6 | + "github.com/rayakame/sqlc-gen-better-python/internal/core" |
| 7 | + "github.com/sqlc-dev/plugin-sdk-go/metadata" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const AsyncpgConn = "asyncpg.Connection" |
| 13 | + |
| 14 | +func AsyncpgBuildPyQueryFunc(query *core.Query, body *builders.IndentStringBuilder, args []string, retType string, isClass bool) error { |
| 15 | + indentLevel := 0 |
| 16 | + params := fmt.Sprintf("conn: %s", AsyncpgConn) |
| 17 | + conn := "conn" |
| 18 | + if isClass { |
| 19 | + params = "self" |
| 20 | + conn = "self._conn" |
| 21 | + indentLevel = 1 |
| 22 | + } |
| 23 | + body.WriteIndentedString(indentLevel, fmt.Sprintf("async def %s(%s", query.FuncName, params)) |
| 24 | + for i, arg := range args { |
| 25 | + if i == 0 { |
| 26 | + body.WriteString(", *") |
| 27 | + } |
| 28 | + body.WriteString(fmt.Sprintf(", %s", arg)) |
| 29 | + } |
| 30 | + if query.Cmd == metadata.CmdExec { |
| 31 | + body.WriteLine(fmt.Sprintf(") -> %s:", retType)) |
| 32 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("await %s.execute(%s", conn, query.ConstantName)) |
| 33 | + asyncpgWriteParams(query, body) |
| 34 | + body.WriteLine(")") |
| 35 | + } else if query.Cmd == metadata.CmdOne { |
| 36 | + body.WriteLine(fmt.Sprintf(") -> typing.Optional[%s]:", retType)) |
| 37 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("row = await %s.fetchrow(%s", conn, query.ConstantName)) |
| 38 | + asyncpgWriteParams(query, body) |
| 39 | + body.WriteLine(")") |
| 40 | + body.WriteIndentedLine(indentLevel+1, "if row is None:") |
| 41 | + body.WriteIndentedLine(indentLevel+2, "return None") |
| 42 | + if query.Ret.IsStruct() { |
| 43 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("return %s(", retType)) |
| 44 | + i := 0 |
| 45 | + for _, col := range query.Ret.Table.Columns { |
| 46 | + if i != 0 { |
| 47 | + body.WriteString(", ") |
| 48 | + } |
| 49 | + if len(col.EmbedFields) != 0 { |
| 50 | + var inner []string |
| 51 | + body.WriteString(fmt.Sprintf("%s=%s(", col.Name, col.Type.Type)) |
| 52 | + for _, embedCol := range col.EmbedFields { |
| 53 | + inner = append(inner, fmt.Sprintf("%s=row[%s]", embedCol.Name, strconv.Itoa(i))) |
| 54 | + i++ |
| 55 | + } |
| 56 | + body.WriteString(strings.Join(inner, ", ") + ")") |
| 57 | + } else { |
| 58 | + body.WriteString(fmt.Sprintf("%s=row[%s]", col.Name, strconv.Itoa(i))) |
| 59 | + i++ |
| 60 | + } |
| 61 | + } |
| 62 | + body.WriteLine(")") |
| 63 | + } else { |
| 64 | + body.WriteIndentedLine(indentLevel+1, fmt.Sprintf("return %s(row[0])", retType)) |
| 65 | + } |
| 66 | + } else if query.Cmd == metadata.CmdMany { |
| 67 | + body.WriteLine(fmt.Sprintf(") -> typing.Sequence[%s]:", retType)) |
| 68 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("rows = await %s.fetch(%s", conn, query.ConstantName)) |
| 69 | + asyncpgWriteParams(query, body) |
| 70 | + body.WriteLine(")") |
| 71 | + body.WriteIndentedLine(indentLevel+1, fmt.Sprintf("return_rows: typing.List[%s] = []", retType)) |
| 72 | + body.WriteIndentedLine(indentLevel+1, "for row in rows:") |
| 73 | + if query.Ret.IsStruct() { |
| 74 | + body.WriteIndentedString(indentLevel+2, fmt.Sprintf("return_rows.append(%s(", retType)) |
| 75 | + i := 0 |
| 76 | + for _, col := range query.Ret.Table.Columns { |
| 77 | + if i != 0 { |
| 78 | + body.WriteString(", ") |
| 79 | + } |
| 80 | + if len(col.EmbedFields) != 0 { |
| 81 | + var inner []string |
| 82 | + body.WriteString(fmt.Sprintf("%s=%s(", col.Name, col.Type.Type)) |
| 83 | + for _, embedCol := range col.EmbedFields { |
| 84 | + inner = append(inner, fmt.Sprintf("%s=row[%s]", embedCol.Name, strconv.Itoa(i))) |
| 85 | + i++ |
| 86 | + } |
| 87 | + body.WriteString(strings.Join(inner, ", ") + ")") |
| 88 | + } else { |
| 89 | + body.WriteString(fmt.Sprintf("%s=row[%s]", col.Name, strconv.Itoa(i))) |
| 90 | + i++ |
| 91 | + } |
| 92 | + } |
| 93 | + body.WriteLine("))") |
| 94 | + body.WriteIndentedString(indentLevel+1, "return return_rows") |
| 95 | + } else { |
| 96 | + body.WriteIndentedLine(indentLevel+2, fmt.Sprintf("return_rows.append(%s(row[0]))", retType)) |
| 97 | + body.WriteIndentedString(indentLevel+1, "return return_rows") |
| 98 | + } |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func AsyncpgAcceptedDriverCMDs() []string { |
| 104 | + return []string{ |
| 105 | + metadata.CmdExec, |
| 106 | + metadata.CmdOne, |
| 107 | + metadata.CmdMany, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func asyncpgWriteParams(query *core.Query, body *builders.IndentStringBuilder) { |
| 112 | + if len(query.Args) == 0 { |
| 113 | + return |
| 114 | + } |
| 115 | + params := "" |
| 116 | + for i, arg := range query.Args { |
| 117 | + if !arg.IsEmpty() { |
| 118 | + if i == len(query.Args)-1 { |
| 119 | + params += fmt.Sprintf(" %s", arg.Name) |
| 120 | + } else { |
| 121 | + params += fmt.Sprintf(" %s,", arg.Name) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + body.WriteString("," + params) |
| 126 | +} |
0 commit comments