2
2
// ReSharper disable NotAccessedPositionalProperty.Global
3
3
// ReSharper disable UnusedAutoPropertyAccessor.Global
4
4
// ReSharper disable InconsistentNaming
5
- namespace NpgsqlExample
6
- {
7
- using System . Collections . Generic ;
8
- using System . Threading . Tasks ;
9
- using Npgsql ;
5
+ using System . Collections . Generic ;
6
+ using System . Threading . Tasks ;
7
+ using Npgsql ;
10
8
11
- public class QuerySql ( string connectionString )
9
+ namespace NpgsqlExample ;
10
+ public class QuerySql ( string connectionString )
11
+ {
12
+ private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 " ;
13
+ public readonly record struct GetAuthorRow ( long Id , string Name , string ? Bio ) ;
14
+ public readonly record struct GetAuthorArgs ( long Id ) ;
15
+ public async Task < GetAuthorRow ? > GetAuthor ( GetAuthorArgs args )
12
16
{
13
- private const string GetAuthorSql = "SELECT id, name, bio FROM authors WHERE id = @id LIMIT 1 " ;
14
- public readonly record struct GetAuthorRow ( long Id , string Name , string ? Bio ) ;
15
- public readonly record struct GetAuthorArgs ( long Id ) ;
16
- public async Task < GetAuthorRow ? > GetAuthor ( GetAuthorArgs args )
17
17
{
18
+ await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
19
+ ;
20
+ await using var command = connection . CreateCommand ( GetAuthorSql ) ;
21
+ command . Parameters . AddWithValue ( "@id" , args . Id ) ;
22
+ var reader = await command . ExecuteReaderAsync ( ) ;
23
+ if ( await reader . ReadAsync ( ) )
18
24
{
19
- await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
20
- ;
21
- await using var command = connection . CreateCommand ( GetAuthorSql ) ;
22
- command . Parameters . AddWithValue ( "@id" , args . Id ) ;
23
- var reader = await command . ExecuteReaderAsync ( ) ;
24
- if ( await reader . ReadAsync ( ) )
25
+ return new GetAuthorRow
25
26
{
26
- return new GetAuthorRow
27
- {
28
- Id = reader . GetInt64 ( 0 ) ,
29
- Name = reader . GetString ( 1 ) ,
30
- Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 )
31
- } ;
32
- }
33
-
34
- return null ;
27
+ Id = reader . GetInt64 ( 0 ) ,
28
+ Name = reader . GetString ( 1 ) ,
29
+ Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 )
30
+ } ;
35
31
}
32
+
33
+ return null ;
36
34
}
35
+ }
37
36
38
- private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name " ;
39
- public readonly record struct ListAuthorsRow ( long Id , string Name , string ? Bio ) ;
40
- public async Task < List < ListAuthorsRow > > ListAuthors ( )
37
+ private const string ListAuthorsSql = "SELECT id, name, bio FROM authors ORDER BY name " ;
38
+ public readonly record struct ListAuthorsRow ( long Id , string Name , string ? Bio ) ;
39
+ public async Task < List < ListAuthorsRow > > ListAuthors ( )
40
+ {
41
41
{
42
+ await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
43
+ ;
44
+ await using var command = connection . CreateCommand ( ListAuthorsSql ) ;
45
+ var reader = await command . ExecuteReaderAsync ( ) ;
46
+ var result = new List < ListAuthorsRow > ( ) ;
47
+ while ( await reader . ReadAsync ( ) )
42
48
{
43
- await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
44
- ;
45
- await using var command = connection . CreateCommand ( ListAuthorsSql ) ;
46
- var reader = await command . ExecuteReaderAsync ( ) ;
47
- var result = new List < ListAuthorsRow > ( ) ;
48
- while ( await reader . ReadAsync ( ) )
49
- {
50
- result . Add ( new ListAuthorsRow { Id = reader . GetInt64 ( 0 ) , Name = reader . GetString ( 1 ) , Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 ) } ) ;
51
- }
52
-
53
- return result ;
49
+ result . Add ( new ListAuthorsRow { Id = reader . GetInt64 ( 0 ) , Name = reader . GetString ( 1 ) , Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 ) } ) ;
54
50
}
51
+
52
+ return result ;
55
53
}
54
+ }
56
55
57
- private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio " ;
58
- public readonly record struct CreateAuthorRow ( long Id , string Name , string ? Bio ) ;
59
- public readonly record struct CreateAuthorArgs ( string Name , string ? Bio ) ;
60
- public async Task < CreateAuthorRow ? > CreateAuthor ( CreateAuthorArgs args )
56
+ private const string CreateAuthorSql = "INSERT INTO authors ( name , bio ) VALUES ( @name, @bio ) RETURNING id, name, bio " ;
57
+ public readonly record struct CreateAuthorRow ( long Id , string Name , string ? Bio ) ;
58
+ public readonly record struct CreateAuthorArgs ( string Name , string ? Bio ) ;
59
+ public async Task < CreateAuthorRow ? > CreateAuthor ( CreateAuthorArgs args )
60
+ {
61
61
{
62
+ await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
63
+ ;
64
+ await using var command = connection . CreateCommand ( CreateAuthorSql ) ;
65
+ command . Parameters . AddWithValue ( "@name" , args . Name ) ;
66
+ command . Parameters . AddWithValue ( "@bio" , args . Bio ) ;
67
+ var reader = await command . ExecuteReaderAsync ( ) ;
68
+ if ( await reader . ReadAsync ( ) )
62
69
{
63
- await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
64
- ;
65
- await using var command = connection . CreateCommand ( CreateAuthorSql ) ;
66
- command . Parameters . AddWithValue ( "@name" , args . Name ) ;
67
- command . Parameters . AddWithValue ( "@bio" , args . Bio ) ;
68
- var reader = await command . ExecuteReaderAsync ( ) ;
69
- if ( await reader . ReadAsync ( ) )
70
+ return new CreateAuthorRow
70
71
{
71
- return new CreateAuthorRow
72
- {
73
- Id = reader . GetInt64 ( 0 ) ,
74
- Name = reader . GetString ( 1 ) ,
75
- Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 )
76
- } ;
77
- }
78
-
79
- return null ;
72
+ Id = reader . GetInt64 ( 0 ) ,
73
+ Name = reader . GetString ( 1 ) ,
74
+ Bio = reader . IsDBNull ( 2 ) ? null : reader . GetString ( 2 )
75
+ } ;
80
76
}
77
+
78
+ return null ;
81
79
}
80
+ }
82
81
83
- private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id " ;
84
- public readonly record struct DeleteAuthorArgs ( long Id ) ;
85
- public async Task DeleteAuthor ( DeleteAuthorArgs args )
82
+ private const string DeleteAuthorSql = "DELETE FROM authors WHERE id = @id " ;
83
+ public readonly record struct DeleteAuthorArgs ( long Id ) ;
84
+ public async Task DeleteAuthor ( DeleteAuthorArgs args )
85
+ {
86
86
{
87
- {
88
- await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
89
- ;
90
- await using var command = connection . CreateCommand ( DeleteAuthorSql ) ;
91
- command . Parameters . AddWithValue ( "@id" , args . Id ) ;
92
- await command . ExecuteScalarAsync ( ) ;
93
- }
87
+ await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
88
+ ;
89
+ await using var command = connection . CreateCommand ( DeleteAuthorSql ) ;
90
+ command . Parameters . AddWithValue ( "@id" , args . Id ) ;
91
+ await command . ExecuteScalarAsync ( ) ;
94
92
}
93
+ }
95
94
96
- private const string TestSql = "SELECT c_bit, c_smallint, c_boolean, c_integer, c_bigint, c_serial, c_decimal, c_numeric, c_real, c_double_precision, c_date, c_time, c_timestamp, c_char, c_varchar, c_bytea, c_text, c_json FROM node_postgres_types LIMIT 1 " ;
97
- public readonly record struct TestRow ( byte [ ] ? C_bit , int ? C_smallint , bool ? C_boolean , int ? C_integer , int ? C_bigint , long ? C_serial , float ? C_decimal , float ? C_numeric , float ? C_real , float ? C_double_precision , string ? C_date , string ? C_time , string ? C_timestamp , string ? C_char , string ? C_varchar , byte [ ] ? C_bytea , string ? C_text , object ? C_json ) ;
98
- public async Task < TestRow ? > Test ( )
95
+ private const string TestSql = "SELECT c_bit, c_smallint, c_boolean, c_integer, c_bigint, c_serial, c_decimal, c_numeric, c_real, c_double_precision, c_date, c_time, c_timestamp, c_char, c_varchar, c_bytea, c_text, c_json FROM node_postgres_types LIMIT 1 " ;
96
+ public readonly record struct TestRow ( byte [ ] ? C_bit , int ? C_smallint , bool ? C_boolean , int ? C_integer , int ? C_bigint , long ? C_serial , float ? C_decimal , float ? C_numeric , float ? C_real , float ? C_double_precision , string ? C_date , string ? C_time , string ? C_timestamp , string ? C_char , string ? C_varchar , byte [ ] ? C_bytea , string ? C_text , object ? C_json ) ;
97
+ public async Task < TestRow ? > Test ( )
98
+ {
99
99
{
100
+ await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
101
+ ;
102
+ await using var command = connection . CreateCommand ( TestSql ) ;
103
+ var reader = await command . ExecuteReaderAsync ( ) ;
104
+ if ( await reader . ReadAsync ( ) )
100
105
{
101
- await using var connection = NpgsqlDataSource . Create ( connectionString ) ;
102
- ;
103
- await using var command = connection . CreateCommand ( TestSql ) ;
104
- var reader = await command . ExecuteReaderAsync ( ) ;
105
- if ( await reader . ReadAsync ( ) )
106
+ return new TestRow
106
107
{
107
- return new TestRow
108
- {
109
- C_bit = reader . IsDBNull ( 0 ) ? null : Utils . GetBytes ( reader , 0 ) ,
110
- C_smallint = reader . IsDBNull ( 1 ) ? null : reader . GetInt32 ( 1 ) ,
111
- C_boolean = reader . IsDBNull ( 2 ) ? null : reader . GetBoolean ( 2 ) ,
112
- C_integer = reader . IsDBNull ( 3 ) ? null : reader . GetInt32 ( 3 ) ,
113
- C_bigint = reader . IsDBNull ( 4 ) ? null : reader . GetInt32 ( 4 ) ,
114
- C_serial = reader . IsDBNull ( 5 ) ? null : reader . GetInt64 ( 5 ) ,
115
- C_decimal = reader . IsDBNull ( 6 ) ? null : reader . GetFloat ( 6 ) ,
116
- C_numeric = reader . IsDBNull ( 7 ) ? null : reader . GetFloat ( 7 ) ,
117
- C_real = reader . IsDBNull ( 8 ) ? null : reader . GetFloat ( 8 ) ,
118
- C_double_precision = reader . IsDBNull ( 9 ) ? null : reader . GetFloat ( 9 ) ,
119
- C_date = reader . IsDBNull ( 10 ) ? null : reader . GetString ( 10 ) ,
120
- C_time = reader . IsDBNull ( 11 ) ? null : reader . GetString ( 11 ) ,
121
- C_timestamp = reader . IsDBNull ( 12 ) ? null : reader . GetString ( 12 ) ,
122
- C_char = reader . IsDBNull ( 13 ) ? null : reader . GetString ( 13 ) ,
123
- C_varchar = reader . IsDBNull ( 14 ) ? null : reader . GetString ( 14 ) ,
124
- C_bytea = reader . IsDBNull ( 15 ) ? null : Utils . GetBytes ( reader , 15 ) ,
125
- C_text = reader . IsDBNull ( 16 ) ? null : reader . GetString ( 16 ) ,
126
- C_json = reader . IsDBNull ( 17 ) ? null : reader . GetString ( 17 )
127
- } ;
128
- }
129
-
130
- return null ;
108
+ C_bit = reader . IsDBNull ( 0 ) ? null : Utils . GetBytes ( reader , 0 ) ,
109
+ C_smallint = reader . IsDBNull ( 1 ) ? null : reader . GetInt32 ( 1 ) ,
110
+ C_boolean = reader . IsDBNull ( 2 ) ? null : reader . GetBoolean ( 2 ) ,
111
+ C_integer = reader . IsDBNull ( 3 ) ? null : reader . GetInt32 ( 3 ) ,
112
+ C_bigint = reader . IsDBNull ( 4 ) ? null : reader . GetInt32 ( 4 ) ,
113
+ C_serial = reader . IsDBNull ( 5 ) ? null : reader . GetInt64 ( 5 ) ,
114
+ C_decimal = reader . IsDBNull ( 6 ) ? null : reader . GetFloat ( 6 ) ,
115
+ C_numeric = reader . IsDBNull ( 7 ) ? null : reader . GetFloat ( 7 ) ,
116
+ C_real = reader . IsDBNull ( 8 ) ? null : reader . GetFloat ( 8 ) ,
117
+ C_double_precision = reader . IsDBNull ( 9 ) ? null : reader . GetFloat ( 9 ) ,
118
+ C_date = reader . IsDBNull ( 10 ) ? null : reader . GetString ( 10 ) ,
119
+ C_time = reader . IsDBNull ( 11 ) ? null : reader . GetString ( 11 ) ,
120
+ C_timestamp = reader . IsDBNull ( 12 ) ? null : reader . GetString ( 12 ) ,
121
+ C_char = reader . IsDBNull ( 13 ) ? null : reader . GetString ( 13 ) ,
122
+ C_varchar = reader . IsDBNull ( 14 ) ? null : reader . GetString ( 14 ) ,
123
+ C_bytea = reader . IsDBNull ( 15 ) ? null : Utils . GetBytes ( reader , 15 ) ,
124
+ C_text = reader . IsDBNull ( 16 ) ? null : reader . GetString ( 16 ) ,
125
+ C_json = reader . IsDBNull ( 17 ) ? null : reader . GetString ( 17 )
126
+ } ;
131
127
}
128
+
129
+ return null ;
132
130
}
133
131
}
134
132
}
0 commit comments