|
| 1 | +#nullable enable |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Data; |
| 6 | +using System.Reflection; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Npgsql; |
| 10 | +using NpgsqlTypes; |
| 11 | + |
| 12 | +namespace demo |
| 13 | +{ |
| 14 | + |
| 15 | + public class AllTypesRecord |
| 16 | + { |
| 17 | + [JsonProperty("null_integer", NullValueHandling = NullValueHandling.Ignore)] |
| 18 | + public int? NullInteger { get; set; } |
| 19 | + [JsonProperty("integer")] |
| 20 | + public int? Integer { get; set; } |
| 21 | + [JsonProperty("bigint")] |
| 22 | + public long? Bigint { get; set; } |
| 23 | + [JsonProperty("float")] |
| 24 | + public float? Float { get; set; } |
| 25 | + [JsonProperty("double")] |
| 26 | + public double? Double { get; set; } |
| 27 | + [JsonProperty("decimal")] |
| 28 | + public decimal? Decimal { get; set; } |
| 29 | + //[JsonProperty("bit")] |
| 30 | + //public BitArray Bit { get; set; } |
| 31 | + [JsonProperty("bool")] |
| 32 | + public bool? Bool { get; set; } |
| 33 | + [JsonProperty("text")] |
| 34 | + public string? Text { get; set; } |
| 35 | + [JsonProperty("char")] |
| 36 | + public string? Char { get; set; } |
| 37 | + [JsonProperty("timestamp_tz")] |
| 38 | + public string? Timestamp { get; set; } |
| 39 | + [JsonProperty("timestamp_notz")] |
| 40 | + public string? TimestampNoTz { get; set; } |
| 41 | + [JsonProperty("ip")] |
| 42 | + public string? Ip { get; set; } |
| 43 | + |
| 44 | + [JsonProperty("array")] |
| 45 | + public IList<string>? Array { get; set; } |
| 46 | + [JsonProperty("object")] |
| 47 | + public Dictionary<string, string>? Object { get; set; } |
| 48 | + [JsonProperty("geopoint")] |
| 49 | + public Dictionary<string, double>? Geopoint { get; set; } |
| 50 | + [JsonProperty("geoshape")] |
| 51 | + public Dictionary<string, string>? Geoshape { get; set; } |
| 52 | + [JsonProperty("float_vector")] |
| 53 | + public IList<string>? FloatVector { get; set; } |
| 54 | + |
| 55 | + //public string[]? SummaryWords { get; set; } |
| 56 | + } |
| 57 | + |
| 58 | + public class DatabaseWorkloadsMore |
| 59 | + { |
| 60 | + |
| 61 | + public static async Task<DataTable> AllTypesExample(NpgsqlConnection conn) |
| 62 | + { |
| 63 | + Console.WriteLine("Running AllTypesExample"); |
| 64 | + |
| 65 | + // Submit DDL, create database schema. |
| 66 | + await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.example", conn)) |
| 67 | + { |
| 68 | + cmd.ExecuteNonQuery(); |
| 69 | + } |
| 70 | + |
| 71 | + await using (var cmd = new NpgsqlCommand(@" |
| 72 | + CREATE TABLE testdrive.example ( |
| 73 | + -- Numeric types |
| 74 | + null_integer INT, |
| 75 | + integer INT, |
| 76 | + bigint BIGINT, |
| 77 | + float FLOAT, |
| 78 | + double DOUBLE, |
| 79 | + decimal DECIMAL(8, 2), |
| 80 | + -- Other scalar types |
| 81 | + bit BIT(8), |
| 82 | + bool BOOLEAN, |
| 83 | + text TEXT, |
| 84 | + char CHARACTER(5), |
| 85 | + timestamp_tz TIMESTAMP WITH TIME ZONE, |
| 86 | + timestamp_notz TIMESTAMP WITHOUT TIME ZONE, |
| 87 | + ip IP, |
| 88 | + -- Container types |
| 89 | + ""array"" ARRAY(STRING), |
| 90 | + ""object"" OBJECT(DYNAMIC), |
| 91 | + -- Geospatial types |
| 92 | + geopoint GEO_POINT, |
| 93 | + geoshape GEO_SHAPE, |
| 94 | + -- Vector type |
| 95 | + ""float_vector"" FLOAT_VECTOR(3) |
| 96 | + ); |
| 97 | + ", conn)) |
| 98 | + { |
| 99 | + cmd.ExecuteNonQuery(); |
| 100 | + } |
| 101 | + |
| 102 | + // Insert single data point. |
| 103 | + await using (var cmd = new NpgsqlCommand(@" |
| 104 | + INSERT INTO testdrive.example ( |
| 105 | + null_integer, |
| 106 | + integer, |
| 107 | + bigint, |
| 108 | + float, |
| 109 | + double, |
| 110 | + decimal, |
| 111 | + bit, |
| 112 | + bool, |
| 113 | + text, |
| 114 | + char, |
| 115 | + timestamp_tz, |
| 116 | + timestamp_notz, |
| 117 | + ip, |
| 118 | + ""array"", |
| 119 | + -- ""object"", |
| 120 | + geopoint, |
| 121 | + -- geoshape, |
| 122 | + float_vector |
| 123 | + ) VALUES ( |
| 124 | + @null_integer, |
| 125 | + @integer, |
| 126 | + @bigint, |
| 127 | + @float, |
| 128 | + @double, |
| 129 | + @decimal, |
| 130 | + @bit, |
| 131 | + @bool, |
| 132 | + @text, |
| 133 | + @char, |
| 134 | + @timestamp_tz, |
| 135 | + @timestamp_notz, |
| 136 | + @ip, |
| 137 | + @array, |
| 138 | + -- @object, |
| 139 | + @geopoint, |
| 140 | + -- @egoshape, |
| 141 | + @float_vector |
| 142 | + ); |
| 143 | +", conn)) |
| 144 | + { |
| 145 | + Console.WriteLine(cmd); |
| 146 | + cmd.Parameters.AddWithValue("null_integer", DBNull.Value); |
| 147 | + cmd.Parameters.AddWithValue("integer", 42); |
| 148 | + cmd.Parameters.AddWithValue("bigint", 42); |
| 149 | + cmd.Parameters.AddWithValue("float", 42.42); |
| 150 | + cmd.Parameters.AddWithValue("double", 42.42); |
| 151 | + cmd.Parameters.AddWithValue("decimal", 42.42); |
| 152 | + cmd.Parameters.AddWithValue("bit", "01010101"); |
| 153 | + cmd.Parameters.AddWithValue("bool", true); |
| 154 | + cmd.Parameters.AddWithValue("text", "foobar"); |
| 155 | + cmd.Parameters.AddWithValue("char", "foo"); |
| 156 | + cmd.Parameters.AddWithValue("timestamp_tz", "1970-01-02T00:00:00+01:00"); |
| 157 | + cmd.Parameters.AddWithValue("timestamp_notz", "1970-01-02T00:00:00"); |
| 158 | + cmd.Parameters.AddWithValue("ip", "127.0.0.1"); |
| 159 | + cmd.Parameters.AddWithValue("array", new List<string>{"foo", "bar"}); |
| 160 | + // FIXME: System.NotSupportedException: Cannot resolve 'hstore' to a fully qualified datatype name. The datatype was not found in the current database info. |
| 161 | + // cmd.Parameters.AddWithValue("object", new Dictionary<string, string>(){{"foo", "bar"}}); |
| 162 | + cmd.Parameters.AddWithValue("geopoint", new List<double>{85.43, 66.23}); |
| 163 | + // FIXME: Npgsql.PostgresException : XX000: line 38:9: no viable alternative at input 'VALUES |
| 164 | + // cmd.Parameters.AddWithValue("geoshape", "POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))"); |
| 165 | + cmd.Parameters.AddWithValue("float_vector", new List<double> {1.1, 2.2, 3.3}); |
| 166 | + cmd.ExecuteNonQuery(); |
| 167 | + } |
| 168 | + |
| 169 | + // Flush data. |
| 170 | + await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.example", conn)) |
| 171 | + { |
| 172 | + cmd.ExecuteNonQuery(); |
| 173 | + } |
| 174 | + |
| 175 | + // Query back data. |
| 176 | + await using (var cmd = new NpgsqlCommand("SELECT * FROM testdrive.example", conn)) |
| 177 | + await using (var reader = cmd.ExecuteReader()) |
| 178 | + { |
| 179 | + var dataTable = new DataTable(); |
| 180 | + dataTable.Load(reader); |
| 181 | + var payload = JsonConvert.SerializeObject(dataTable); |
| 182 | + Console.WriteLine(payload); |
| 183 | + return (DataTable) dataTable; |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments