| 23 | } |
| 24 | |
| 25 | export async function insertDatabase(table: string | ICharacter, options: IInsertDatabaseOptions, context: Context) { |
| 26 | const columns: string[] = []; |
| 27 | const values: string[] = []; |
| 28 | |
| 29 | if (options.values === undefined && options.table === undefined) { |
| 30 | throw new Error("insertDatabase, wrong input"); |
| 31 | } |
| 32 | |
| 33 | if (options.table !== undefined) { |
| 34 | let subrc = 0; |
| 35 | let dbcnt = 0; |
| 36 | for (const row of options.table.array()) { |
| 37 | await insertDatabase(table, {values: row, connection: options.connection}, context); |
| 38 | subrc = Math.max(subrc, abap.builtin.sy.get().subrc.get()); |
| 39 | dbcnt += abap.builtin.sy.get().dbcnt.get(); |
| 40 | } |
| 41 | abap.builtin.sy.get().subrc.set(subrc); |
| 42 | abap.builtin.sy.get().dbcnt.set(dbcnt); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | const structure = options.values!.get(); |
| 47 | for (const k of Object.keys(structure)) { |
| 48 | const field = structure[k]; |
| 49 | if (field instanceof Structure) { |
| 50 | // then its a group, ignore |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | columns.push(k); |
| 55 | values.push(toValue(field.get())); |
| 56 | } |
| 57 | |
| 58 | if (typeof table !== "string") { |
| 59 | table = table.get().trimEnd().toLowerCase(); |
| 60 | } |
| 61 | |
| 62 | let db = context.defaultDB(); |
| 63 | if (options.connection) { |
| 64 | db = context.databaseConnections[options.connection]; |
| 65 | } |
| 66 | const {subrc, dbcnt} = await db.insert({ |
| 67 | table: buildDbTableName(table), |
| 68 | columns, |
| 69 | values, |
| 70 | }); |
| 71 | |
| 72 | abap.builtin.sy.get().subrc.set(subrc); |
| 73 | abap.builtin.sy.get().dbcnt.set(dbcnt); |
| 74 | return subrc; |
| 75 | } |