|
| 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 | +) |
| 10 | + |
| 11 | +const SQLite3Conn = "sqlite3.Connection" |
| 12 | + |
| 13 | +func SQLite3BuildPyQueryFunc(query *core.Query, body *builders.IndentStringBuilder, argType string, retType string, isClass bool) error { |
| 14 | + indentLevel := 0 |
| 15 | + params := fmt.Sprintf("conn: %s", SQLite3Conn) |
| 16 | + conn := "conn" |
| 17 | + if isClass { |
| 18 | + params = "self" |
| 19 | + conn = "self._conn" |
| 20 | + indentLevel = 1 |
| 21 | + } |
| 22 | + body.WriteIndentedString(indentLevel, fmt.Sprintf("def %s(%s", query.FuncName, params)) |
| 23 | + if argType != "" { |
| 24 | + body.WriteString(fmt.Sprintf(", %s: %s", query.Arg.Name, argType)) |
| 25 | + } |
| 26 | + if query.Cmd == metadata.CmdExec { |
| 27 | + body.WriteLine(fmt.Sprintf(") -> %s:", retType)) |
| 28 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("%s.execute(%s", conn, query.ConstantName)) |
| 29 | + writeParams(query, body, argType) |
| 30 | + body.WriteLine(")") |
| 31 | + } else if query.Cmd == metadata.CmdExecResult { |
| 32 | + body.WriteLine(fmt.Sprintf(") -> %s:", "sqlite3.Cursor")) |
| 33 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("%s.execute(%s", conn, query.ConstantName)) |
| 34 | + writeParams(query, body, argType) |
| 35 | + body.WriteLine(")") |
| 36 | + } else if query.Cmd == metadata.CmdExecRows { |
| 37 | + body.WriteLine(fmt.Sprintf(") -> %s:", retType)) |
| 38 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("%s.execute(%s", conn, query.ConstantName)) |
| 39 | + writeParams(query, body, argType) |
| 40 | + body.WriteLine(").rowcount") |
| 41 | + } else if query.Cmd == metadata.CmdExecLastId { |
| 42 | + body.WriteLine(fmt.Sprintf(") -> %s:", retType)) |
| 43 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("%s.execute(%s", conn, query.ConstantName)) |
| 44 | + writeParams(query, body, argType) |
| 45 | + body.WriteLine(").lastrowid") |
| 46 | + } else if query.Cmd == metadata.CmdOne { |
| 47 | + body.WriteLine(fmt.Sprintf(") -> typing.Optional[%s]:", retType)) |
| 48 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("row = %s.execute(%s", conn, query.ConstantName)) |
| 49 | + writeParams(query, body, argType) |
| 50 | + body.WriteLine(").fetchone()") |
| 51 | + body.WriteIndentedLine(indentLevel+1, "if row is None:") |
| 52 | + body.WriteIndentedLine(indentLevel+2, "return None") |
| 53 | + if query.Ret.IsStruct() { |
| 54 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("return %s(", retType)) |
| 55 | + for i, col := range query.Ret.Table.Columns { |
| 56 | + if i != 0 { |
| 57 | + body.WriteString(", ") |
| 58 | + } |
| 59 | + body.WriteString(fmt.Sprintf("%s=row[%s]", col.Name, strconv.Itoa(i))) |
| 60 | + } |
| 61 | + body.WriteLine(")") |
| 62 | + } else { |
| 63 | + body.WriteIndentedLine(indentLevel+1, fmt.Sprintf("return %s(row[0])", retType)) |
| 64 | + } |
| 65 | + } else if query.Cmd == metadata.CmdMany { |
| 66 | + body.WriteLine(fmt.Sprintf(") -> typing.List[%s]:", retType)) |
| 67 | + body.WriteIndentedLine(indentLevel+1, fmt.Sprintf("rows: typing.List[%s] = []", retType)) |
| 68 | + body.WriteIndentedString(indentLevel+1, fmt.Sprintf("for row in %s.execute(%s", conn, query.ConstantName)) |
| 69 | + writeParams(query, body, argType) |
| 70 | + body.WriteLine(").fetchall():") |
| 71 | + if query.Ret.IsStruct() { |
| 72 | + body.WriteIndentedString(indentLevel+2, fmt.Sprintf("rows.append(%s(", retType)) |
| 73 | + for i, col := range query.Ret.Table.Columns { |
| 74 | + if i != 0 { |
| 75 | + body.WriteString(", ") |
| 76 | + } |
| 77 | + body.WriteString(fmt.Sprintf("%s=row[%s]", col.Name, strconv.Itoa(i))) |
| 78 | + } |
| 79 | + body.WriteLine("))") |
| 80 | + } else { |
| 81 | + body.WriteIndentedLine(indentLevel+2, fmt.Sprintf("rows.append(%s(row[0]))", retType)) |
| 82 | + } |
| 83 | + body.WriteIndentedLine(indentLevel+1, "return rows") |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func SQLite3AcceptedDriverCMDs() []string { |
| 89 | + return []string{ |
| 90 | + metadata.CmdExec, |
| 91 | + metadata.CmdExecResult, |
| 92 | + metadata.CmdExecLastId, |
| 93 | + metadata.CmdExecRows, |
| 94 | + metadata.CmdOne, |
| 95 | + metadata.CmdMany, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func writeParams(query *core.Query, body *builders.IndentStringBuilder, argType string) { |
| 100 | + if argType != "" { |
| 101 | + params := "(" |
| 102 | + if query.Arg.IsStruct() { |
| 103 | + for _, col := range query.Arg.Table.Columns { |
| 104 | + params += fmt.Sprintf("%s.%s, ", query.Arg.Name, col.Name) |
| 105 | + } |
| 106 | + } else { |
| 107 | + params += fmt.Sprintf("%s, ", query.Arg.Name) |
| 108 | + } |
| 109 | + body.WriteString("," + params + ")") |
| 110 | + } |
| 111 | +} |
0 commit comments