|
1 | 1 | using Microsoft.Data.Sqlite; |
2 | 2 | using SqlServer2SqLite.Core.Models; |
3 | 3 | using SQLServer2SQLite.Core.Helpers; |
4 | | -using System; |
5 | 4 | using System.Collections.Generic; |
6 | 5 | using System.Data; |
7 | 6 | using System.Text; |
8 | 7 |
|
9 | | -namespace SQLServer2SQLite.Core.Builders |
| 8 | +namespace SQLServer2SQLite.Core.Builders; |
| 9 | + |
| 10 | +public class InsertBuilder |
10 | 11 | { |
11 | | - public class InsertBuilder |
| 12 | + /// <summary> |
| 13 | + /// Creates a command object needed to insert values into a specific SQLite table. |
| 14 | + /// </summary> |
| 15 | + /// <param name="tableSchema">The table schema object for the table.</param> |
| 16 | + /// <returns>A command object with the required functionality.</returns> |
| 17 | + // TODO test! |
| 18 | + public static SqliteCommand BuildSQLiteInsert(TableSchema tableSchema) |
12 | 19 | { |
13 | | - /// <summary> |
14 | | - /// Creates a command object needed to insert values into a specific SQLite table. |
15 | | - /// </summary> |
16 | | - /// <param name="tableSchema">The table schema object for the table.</param> |
17 | | - /// <returns>A command object with the required functionality.</returns> |
18 | | - // TODO test! |
19 | | - public static SqliteCommand BuildSQLiteInsert(TableSchema tableSchema) |
20 | | - { |
21 | | - SqliteCommand res = new SqliteCommand(); |
| 20 | + SqliteCommand res = new SqliteCommand(); |
22 | 21 |
|
23 | | - StringBuilder sb = new StringBuilder(); |
24 | | - sb.Append("INSERT INTO [" + tableSchema.TableName + "] ("); |
25 | | - for (int i = 0; i < tableSchema.Columns.Count; i++) |
26 | | - { |
27 | | - sb.Append("[" + tableSchema.Columns[i].ColumnName + "]"); |
28 | | - if (i < tableSchema.Columns.Count - 1) |
29 | | - sb.Append(", "); |
30 | | - } |
31 | | - sb.Append(") VALUES ("); |
| 22 | + StringBuilder sb = new StringBuilder(); |
| 23 | + sb.Append("INSERT INTO [" + tableSchema.TableName + "] ("); |
| 24 | + for (int i = 0; i < tableSchema.Columns.Count; i++) |
| 25 | + { |
| 26 | + sb.Append("[" + tableSchema.Columns[i].ColumnName + "]"); |
| 27 | + if (i < tableSchema.Columns.Count - 1) |
| 28 | + sb.Append(", "); |
| 29 | + } |
| 30 | + sb.Append(") VALUES ("); |
32 | 31 |
|
33 | | - List<string> pnames = new List<string>(); |
34 | | - for (int i = 0; i < tableSchema.Columns.Count; i++) |
35 | | - { |
36 | | - string pname = |
37 | | - "@" + TextHelper.GetNormalizedName(tableSchema.Columns[i].ColumnName, pnames); |
38 | | - sb.Append(pname); |
39 | | - if (i < tableSchema.Columns.Count - 1) |
40 | | - sb.Append(", "); |
| 32 | + List<string> pnames = new List<string>(); |
| 33 | + for (int i = 0; i < tableSchema.Columns.Count; i++) |
| 34 | + { |
| 35 | + string pname = |
| 36 | + "@" + TextHelper.GetNormalizedName(tableSchema.Columns[i].ColumnName, pnames); |
| 37 | + sb.Append(pname); |
| 38 | + if (i < tableSchema.Columns.Count - 1) |
| 39 | + sb.Append(", "); |
41 | 40 |
|
42 | | - SqliteType dbType = ColumnSchema.GetSqLiteDbTypeOfColumn( |
43 | | - tableSchema.Columns[i].ColumnType |
44 | | - ); |
45 | | - SqliteParameter prm = new SqliteParameter( |
46 | | - pname, |
47 | | - dbType, |
48 | | - 1, |
49 | | - tableSchema.Columns[i].ColumnName |
50 | | - ); |
51 | | - res.Parameters.Add(prm); |
| 41 | + SqliteType dbType = ColumnSchema.GetSqLiteDbTypeOfColumn( |
| 42 | + tableSchema.Columns[i].ColumnType |
| 43 | + ); |
| 44 | + SqliteParameter prm = new SqliteParameter( |
| 45 | + pname, |
| 46 | + dbType, |
| 47 | + 1, |
| 48 | + tableSchema.Columns[i].ColumnName |
| 49 | + ); |
| 50 | + res.Parameters.Add(prm); |
52 | 51 |
|
53 | | - // Remember the parameter name in order to avoid duplicates |
54 | | - pnames.Add(pname); |
55 | | - } |
56 | | - sb.Append(")"); |
57 | | - res.CommandText = sb.ToString(); |
58 | | - res.CommandType = CommandType.Text; |
59 | | - return res; |
| 52 | + // Remember the parameter name in order to avoid duplicates |
| 53 | + pnames.Add(pname); |
60 | 54 | } |
| 55 | + sb.Append(")"); |
| 56 | + res.CommandText = sb.ToString(); |
| 57 | + res.CommandType = CommandType.Text; |
| 58 | + return res; |
61 | 59 | } |
62 | 60 | } |
0 commit comments