Browse Source

added C# templates

develop
Robin Thoni 7 years ago
parent
commit
540a167633
3 changed files with 320 additions and 0 deletions
  1. 127
    0
      Templates/C_Sharp/DataSource.twig
  2. 24
    0
      Templates/C_Sharp/Models.twig
  3. 169
    0
      Templates/C_Sharp/types-cast.json

+ 127
- 0
Templates/C_Sharp/DataSource.twig View File

@@ -0,0 +1,127 @@
1
+{#- This file can be edited -#}
2
+/**
3
+ * This is a generated file. Do not edit, changes will be overwritten.
4
+ */
5
+using System;
6
+using System.Collections.Generic;
7
+using System.ComponentModel.DataAnnotations.Schema;
8
+using System.Data;
9
+using Microsoft.EntityFrameworkCore;
10
+using Npgsql.EntityFrameworkCore.PostgreSQL;
11
+using WebApi.DataAccess.Models;
12
+
13
+namespace WebApi.DataAccess
14
+{
15
+    public partial class DatabaseContext : DbContext
16
+    {
17
+        private readonly string _connectionString;
18
+
19
+        public DatabaseContext(string connectionString)
20
+        {
21
+            _connectionString = connectionString;
22
+        }
23
+
24
+        public DatabaseContext(DbContextOptions options) : base(options)
25
+        {
26
+        }
27
+
28
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
29
+        {
30
+            if (_connectionString != null) {
31
+                optionsBuilder.UseNpgsql(_connectionString);
32
+            }
33
+        }
34
+
35
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
36
+        {
37
+            modelBuilder.HasPostgresExtension("uuid-ossp");
38
+        {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
39
+            modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>().ToTable("{{ table.getName() | escapeString }}");
40
+
41
+            {% for column in table.getColumns() %}{% if (column.isSelected()) %}
42
+            modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
43
+            .Property(e => e.{{ column.getName() | escapeReservedWords }})
44
+            .HasColumnName("{{ column.getName() | escapeString }}");
45
+            {% endif %}{% endfor %}
46
+
47
+            modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
48
+            .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
49
+
50
+            {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
51
+            modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
52
+            .Property(e => e.{{ column.getName() | escapeReservedWords }})
53
+            .HasDefaultValueSql("{{ column.getDefaultValue() | escapeString }}");
54
+            {% endif %}{% endfor %}
55
+
56
+            {% for fk in table.getSourceForeignKeys() %}{% if (fk.isSelected()) %}
57
+            modelBuilder.Entity<{{ fk.getSourceTable().getName() | escapeReservedWords }}>()
58
+            .HasOne(e => e.{{ fk.getSourceForeignKeyName() | escapeReservedWords }})
59
+            .WithMany(e => e.{{ fk.getTargetForeignKeyName() | escapeReservedWords }})
60
+            .HasForeignKey(e => new { {% for column in fk.getSourceColumns() %}e.{{ column.getName | escapeReservedWords | escapeString  }}{% if not (loop.last) %}, {% endif %}{% endfor %} })
61
+            .HasConstraintName("{{ fk.getName() | escapeString  }}");
62
+            {% endif %}{% endfor %}
63
+
64
+        {% endif %}{% endfor %}
65
+        }
66
+    {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
67
+        public virtual DbSet<{{ table.getName() | escapeReservedWords }}> {{ table.getName() | escapeReservedWords }} { get; set; }
68
+    {% endif %}{% endfor %}
69
+
70
+    {% for sp in dataSource.getStoredProcedures() %}{% if (sp.isSelected()) %}
71
+        public static {% if (sp.getType().getLanguageTypeName() == null) %}void{% else %}{{ sp.getType().getLanguageTypeName() }}{% endif %} {{ sp.getName() | escapeReservedWords }}({% for arg in sp.getArguments() %}{{ arg.getType().getLanguageTypeName() }} {{ arg.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endfor %})
72
+        {
73
+            throw new NotImplementedException();
74
+        }
75
+
76
+        public {% if (sp.getType().getLanguageTypeName() == null) %}void{% else %}{{ sp.getType().getLanguageTypeName() }}{% endif %} sp_{{ sp.getName() | escapeReservedWords }}({% for arg in sp.getArguments() %}{{ arg.getType().getLanguageTypeName() }} {{ arg.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endfor %})
77
+        {
78
+            using (var connection = Database.GetDbConnection()) {
79
+                connection.Open();
80
+
81
+                using (var command = connection.CreateCommand()) {
82
+                    command.CommandType = CommandType.StoredProcedure;
83
+                    command.CommandText = "{{ sp.getName() | escapeString }}";
84
+                    {% for arg in sp.getArguments() %}
85
+                    var arg_{{ arg.getName() }} = command.CreateParameter();
86
+                    arg_{{ arg.getName() }}.ParameterName = "{{ arg.getName() | escapeString }}";
87
+                    arg_{{ arg.getName() }}.Direction = ParameterDirection.Input;
88
+                    arg_{{ arg.getName() }}.Value = {{ arg.getName() | escapeReservedWords }};
89
+                    command.Parameters.Add(arg_{{ arg.getName() }});
90
+                    {% endfor %}
91
+
92
+                    using (var reader = command.ExecuteReader()) {
93
+                        {% if (sp.getType().isSetOf()) %}
94
+                        var value = new List<{{ sp.getType().getLanguageBaseTypeName() }}>();
95
+                        {% else %}
96
+                        var value = default({{ sp.getType().getLanguageTypeName() }});
97
+                        {% endif %}
98
+
99
+                        while (reader.Read()) {
100
+                            {% if ((sp.getType().getLanguageType() != null)) %}
101
+                            var item = new {{ sp.getType().getLanguageBaseTypeName() }}
102
+                            {
103
+                                {% for col in sp.getType().getLanguageType().getColumns() %}{{ col.getName() | escapeReservedWords }} = ({{ col.getType().getLanguageTypeName() }}) reader.GetValue({{ loop.index0 }}){% if not (loop.last) %},{% endif %}
104
+                                {% endfor %}
105
+                            };
106
+                            {% else %}
107
+                            var item = ({{ sp.getType().getLanguageBaseTypeName() }}) reader.GetValue(0);
108
+                            {% endif %}
109
+                            {% if (sp.getType().isSetOf()) %}
110
+                            value.Add(item);
111
+                            {% else %}
112
+                            value = item;
113
+                            {% endif %}
114
+                        }
115
+
116
+                        connection.Close();
117
+
118
+                        {% if (sp.getType().getLanguageTypeName() != null) %}
119
+                        return value;
120
+                        {% endif %}
121
+                    }
122
+                }
123
+            }
124
+        }
125
+    {% endif %}{% endfor %}
126
+    }
127
+}

+ 24
- 0
Templates/C_Sharp/Models.twig View File

@@ -0,0 +1,24 @@
1
+{#- This file can be edited -#}
2
+/**
3
+ * This is a generated file. Do not edit, changes will be overwritten.
4
+ */
5
+using System;
6
+using System.Collections.Generic;
7
+using System.ComponentModel.DataAnnotations;
8
+using System.ComponentModel.DataAnnotations.Schema;
9
+
10
+namespace WebApi.DataAccess.Models
11
+{
12
+    public partial class {{ table.getName() | escapeReservedWords }}
13
+    {
14
+        {% for column in table.getColumns() %}{% if (column.isSelected()) %}
15
+        public {{ column.getType().getLanguageTypeName() }}{% if (not (column.isNotNull()) and (column.isTypeNotNull())) %}?{% endif %} {{ column.getName() | escapeReservedWords }} { get; set; }
16
+        {% endif %}{% endfor %}
17
+        {% for fk in table.getSourceForeignKeys() %}{% if (fk.isSelected()) %}
18
+        public {{ fk.getTargetTable().getName() | escapeReservedWords }} {{ fk.getSourceForeignKeyName() | escapeReservedWords }} { get; set; }
19
+        {% endif %}{% endfor %}
20
+        {% for fk in table.getTargetForeignKeys() %}{% if (fk.isSelected()) %}
21
+        public IList<{{ fk.getSourceTable().getName() | escapeReservedWords }}> {{ fk.getTargetForeignKeyName() | escapeReservedWords }} { get; set; }
22
+        {% endif %}{% endfor %}
23
+    }
24
+}

+ 169
- 0
Templates/C_Sharp/types-cast.json View File

@@ -0,0 +1,169 @@
1
+{
2
+  "arrayTemplate": "%t[]",
3
+  "setOfTemplate": "IEnumerable<%t>",
4
+  "non-nullable-types": [
5
+    "bool",
6
+    "char",
7
+    "short",
8
+    "ushort",
9
+    "int",
10
+    "uint",
11
+    "long",
12
+    "ulong",
13
+    "float",
14
+    "double",
15
+    "decimal",
16
+    "NpgsqlTypes.NpgsqlPoint",
17
+    "NpgsqlTypes.NpgsqlLSeg",
18
+    "NpgsqlTypes.NpgsqlPath",
19
+    "NpgsqlTypes.NpgsqlPolygon",
20
+    "NpgsqlTypes.NpgsqlLine",
21
+    "NpgsqlTypes.NpgsqlCircle",
22
+    "NpgsqlTypes.NpgsqlBox",
23
+    "NpgsqlTypes.NpgsqlInet",
24
+    "Guid",
25
+    "DateTime",
26
+    "TimeSpan",
27
+    "DateTimeOffset"
28
+  ],
29
+  "types": {
30
+    "boolean": "bool",
31
+    "smallint": "short",
32
+    "integer": "int",
33
+    "bigint": "long",
34
+    "real": "float",
35
+    "double precision": "double",
36
+    "numeric": "decimal",
37
+    "money": "decimal",
38
+    "text": "string",
39
+    "varchar": "string",
40
+    "json": "string",
41
+    "jsonb": "string",
42
+    "xml": "string",
43
+    "point": "NpgsqlTypes.NpgsqlPoint",
44
+    "lseg": "NpgsqlTypes.NpgsqlLSeg",
45
+    "path": "NpgsqlTypes.NpgsqlPath",
46
+    "polygon": "NpgsqlTypes.NpgsqlPolygon",
47
+    "line": "NpgsqlTypes.NpgsqlLine",
48
+    "circle": "NpgsqlTypes.NpgsqlCircle",
49
+    "box": "NpgsqlTypes.NpgsqlBox",
50
+    "bit": {
51
+      "-1": "bool",
52
+      "1": "bool",
53
+      "*": "System.Collections.BitArray"
54
+    },
55
+    "bit varying": "System.Collections.BitArray",
56
+    "uuid": "Guid",
57
+    "cidr": "NpgsqlTypes.NpgsqlInet",
58
+    "inet": "System.Net.IPAddress",
59
+    "macaddr": "System.Net.NetworkInformation.PhysicalAddress",
60
+    "tsquery": "NpgsqlTypes.NpgsqlTsQuery",
61
+    "tsvector": "NpgsqlTypes.NpgsqlTsVector",
62
+    "date": "DateTime",
63
+    "interval": "TimeSpan",
64
+    "timestamp": "DateTime",
65
+    "timestamp with time zone": "DateTime",
66
+    "time": "TimeSpan",
67
+    "time with time zone": "DateTimeOffset",
68
+    "bytea": "byte[]",
69
+    "name": "string",
70
+    "\"char\"": "char",
71
+    "char": "char",
72
+    "oid": "uint",
73
+    "xid": "uint",
74
+    "cid": "uint",
75
+    "oidvector": "uint[]",
76
+    "record": "dynamic",
77
+    "*": "object"
78
+  },
79
+  "reserved-words": [
80
+    "abstract",
81
+    "as",
82
+    "base",
83
+    "bool",
84
+    "break",
85
+    "byte",
86
+    "case",
87
+    "catch",
88
+    "char",
89
+    "checked",
90
+    "class",
91
+    "const",
92
+    "continue",
93
+    "decimal",
94
+    "default",
95
+    "delegate",
96
+    "do",
97
+    "double",
98
+    "else",
99
+    "enum",
100
+    "event",
101
+    "explicit",
102
+    "extern",
103
+    "false",
104
+    "finally",
105
+    "fixed",
106
+    "float",
107
+    "for",
108
+    "foreach",
109
+    "goto",
110
+    "if",
111
+    "implicit",
112
+    "in",
113
+    "int",
114
+    "interface",
115
+    "internal",
116
+    "is",
117
+    "lock",
118
+    "long",
119
+    "namespace",
120
+    "new",
121
+    "null",
122
+    "object",
123
+    "operator",
124
+    "out",
125
+    "override",
126
+    "params",
127
+    "private",
128
+    "protected",
129
+    "public",
130
+    "readonly",
131
+    "ref",
132
+    "return",
133
+    "sbyte",
134
+    "sealed",
135
+    "short",
136
+    "sizeof",
137
+    "stackalloc",
138
+    "static",
139
+    "string",
140
+    "struct",
141
+    "switch",
142
+    "this",
143
+    "throw",
144
+    "true",
145
+    "try",
146
+    "typeof",
147
+    "uint",
148
+    "ulong",
149
+    "unchecked",
150
+    "unsafe",
151
+    "ushort",
152
+    "using",
153
+    "var",
154
+    "virtual",
155
+    "void",
156
+    "volatile",
157
+    "while",
158
+    "add",
159
+    "alias",
160
+    "get",
161
+    "global",
162
+    "partial",
163
+    "remove",
164
+    "set",
165
+    "value",
166
+    "where",
167
+    "yield"
168
+  ]
169
+}

Loading…
Cancel
Save