3
3
require 'mysql2'
4
4
5
5
module Mysql2Codegen
6
- GetAuthorSql = %q(SELECT id, name, bio FROM authors
7
- WHERE id = ? LIMIT 1)
8
-
9
- GetAuthorRow = Data . define ( :id , :name , :bio )
10
-
11
- GetAuthorArgs = Data . define ( :id )
12
-
13
- ListAuthorsSql = %q(SELECT id, name, bio FROM authors
14
- ORDER BY name)
15
-
16
- ListAuthorsRow = Data . define ( :id , :name , :bio )
17
-
18
- CreateAuthorSql = %q(INSERT INTO authors (
19
- name, bio
20
- ) VALUES (
21
- ?, ?
22
- ))
23
-
24
- CreateAuthorArgs = Data . define ( :name , :bio )
25
-
26
- UpdateAuthorSql = %q(UPDATE authors
27
- SET bio = ?
28
- WHERE id = ?)
29
-
30
- UpdateAuthorArgs = Data . define ( :bio , :id )
6
+ GetAuthorSql = %q(SELECT id, name, bio FROM authors
7
+ WHERE id = ? LIMIT 1)
8
+
9
+ GetAuthorRow = Data . define ( :id , :name , :bio )
10
+
11
+ GetAuthorArgs = Data . define ( :id )
12
+
13
+ ListAuthorsSql = %q(SELECT id, name, bio FROM authors
14
+ ORDER BY name)
15
+
16
+ ListAuthorsRow = Data . define ( :id , :name , :bio )
17
+
18
+ CreateAuthorSql = %q(INSERT INTO authors (
19
+ name, bio
20
+ ) VALUES (
21
+ ?, ?
22
+ ))
23
+
24
+ CreateAuthorArgs = Data . define ( :name , :bio )
25
+
26
+ UpdateAuthorSql = %q(UPDATE authors
27
+ SET bio = ?
28
+ WHERE id = ?)
29
+
30
+ UpdateAuthorArgs = Data . define ( :bio , :id )
31
+
32
+ CreateAuthorReturnIdSql = %q(INSERT INTO authors (
33
+ name, bio
34
+ ) VALUES (
35
+ ?, ?
36
+ ))
37
+
38
+ CreateAuthorReturnIdArgs = Data . define ( :name , :bio )
39
+
40
+ DeleteAuthorSql = %q(DELETE FROM authors
41
+ WHERE id = ?)
42
+
43
+ DeleteAuthorArgs = Data . define ( :id )
44
+
45
+ TestSql = %q(SELECT c_bit, c_tinyint, c_bool, c_boolean, c_smallint, c_mediumint, c_int, c_integer, c_bigint, c_serial, c_decimal, c_dec, c_numeric, c_fixed, c_float, c_double, c_double_precision, c_date, c_time, c_datetime, c_timestamp, c_year, c_char, c_nchar, c_national_char, c_varchar, c_binary, c_varbinary, c_tinyblob, c_tinytext, c_blob, c_text, c_mediumblob, c_mediumtext, c_longblob, c_longtext, c_json FROM node_mysql_types
46
+ LIMIT 1)
47
+
48
+ TestRow = Data . define (
49
+ :c_bit ,
50
+ :c_tinyint ,
51
+ :c_bool ,
52
+ :c_boolean ,
53
+ :c_smallint ,
54
+ :c_mediumint ,
55
+ :c_int ,
56
+ :c_integer ,
57
+ :c_bigint ,
58
+ :c_serial ,
59
+ :c_decimal ,
60
+ :c_dec ,
61
+ :c_numeric ,
62
+ :c_fixed ,
63
+ :c_float ,
64
+ :c_double ,
65
+ :c_double_precision ,
66
+ :c_date ,
67
+ :c_time ,
68
+ :c_datetime ,
69
+ :c_timestamp ,
70
+ :c_year ,
71
+ :c_char ,
72
+ :c_nchar ,
73
+ :c_national_char ,
74
+ :c_varchar ,
75
+ :c_binary ,
76
+ :c_varbinary ,
77
+ :c_tinyblob ,
78
+ :c_tinytext ,
79
+ :c_blob ,
80
+ :c_text ,
81
+ :c_mediumblob ,
82
+ :c_mediumtext ,
83
+ :c_longblob ,
84
+ :c_longtext ,
85
+ :c_json
86
+ )
87
+
88
+ class QuerySql
89
+ def initialize ( connection_pool_params , mysql2_params )
90
+ @pool = ConnectionPool ::new ( **connection_pool_params ) { Mysql2 ::Client . new ( **mysql2_params ) }
91
+ end
31
92
32
- CreateAuthorReturnIdSql = %q(INSERT INTO authors (
33
- name, bio
34
- ) VALUES (
35
- ?, ?
36
- ))
93
+ def get_author ( get_author_args )
94
+ @pool . with do |client |
95
+ query_params = [ get_author_args . id ]
96
+ stmt = client . prepare ( GetAuthorSql )
97
+ result = stmt . execute ( *query_params )
98
+ row = result . first
99
+ return nil if row . nil?
100
+ entity = GetAuthorRow . new ( row [ 'id' ] , row [ 'name' ] , row [ 'bio' ] )
101
+ return entity
102
+ end
103
+ end
37
104
38
- CreateAuthorReturnIdArgs = Data . define ( :name , :bio )
105
+ def list_authors
106
+ @pool . with do |client |
107
+ stmt = client . prepare ( ListAuthorsSql )
108
+ result = stmt . execute
109
+ entities = [ ]
110
+ result . each do |row |
111
+ entities << ListAuthorsRow . new ( row [ 'id' ] , row [ 'name' ] , row [ 'bio' ] )
112
+ end
113
+ return entities
114
+ end
115
+ end
39
116
40
- DeleteAuthorSql = %q(DELETE FROM authors
41
- WHERE id = ?)
117
+ def create_author ( create_author_args )
118
+ @pool . with do |client |
119
+ query_params = [ create_author_args . name , create_author_args . bio ]
120
+ stmt = client . prepare ( CreateAuthorSql )
121
+ stmt . execute ( *query_params )
122
+ end
123
+ end
42
124
43
- DeleteAuthorArgs = Data . define ( :id )
125
+ def update_author ( update_author_args )
126
+ @pool . with do |client |
127
+ query_params = [ update_author_args . bio , update_author_args . id ]
128
+ stmt = client . prepare ( UpdateAuthorSql )
129
+ stmt . execute ( *query_params )
130
+ end
131
+ end
44
132
45
- TestSql = %q(SELECT c_bit, c_tinyint, c_bool, c_boolean, c_smallint, c_mediumint, c_int, c_integer, c_bigint, c_serial, c_decimal, c_dec, c_numeric, c_fixed, c_float, c_double, c_double_precision, c_date, c_time, c_datetime, c_timestamp, c_year, c_char, c_nchar, c_national_char, c_varchar, c_binary, c_varbinary, c_tinyblob, c_tinytext, c_blob, c_text, c_mediumblob, c_mediumtext, c_longblob, c_longtext, c_json FROM node_mysql_types
46
- LIMIT 1)
133
+ def create_author_return_id ( create_author_return_id_args )
134
+ @pool . with do |client |
135
+ query_params = [ create_author_return_id_args . name , create_author_return_id_args . bio ]
136
+ stmt = client . prepare ( CreateAuthorReturnIdSql )
137
+ stmt . execute ( *query_params )
138
+ return client . last_id
139
+ end
140
+ end
47
141
48
- TestRow = Data . define (
49
- :c_bit ,
50
- :c_tinyint ,
51
- :c_bool ,
52
- :c_boolean ,
53
- :c_smallint ,
54
- :c_mediumint ,
55
- :c_int ,
56
- :c_integer ,
57
- :c_bigint ,
58
- :c_serial ,
59
- :c_decimal ,
60
- :c_dec ,
61
- :c_numeric ,
62
- :c_fixed ,
63
- :c_float ,
64
- :c_double ,
65
- :c_double_precision ,
66
- :c_date ,
67
- :c_time ,
68
- :c_datetime ,
69
- :c_timestamp ,
70
- :c_year ,
71
- :c_char ,
72
- :c_nchar ,
73
- :c_national_char ,
74
- :c_varchar ,
75
- :c_binary ,
76
- :c_varbinary ,
77
- :c_tinyblob ,
78
- :c_tinytext ,
79
- :c_blob ,
80
- :c_text ,
81
- :c_mediumblob ,
82
- :c_mediumtext ,
83
- :c_longblob ,
84
- :c_longtext ,
85
- :c_json
86
- )
142
+ def delete_author ( delete_author_args )
143
+ @pool . with do |client |
144
+ query_params = [ delete_author_args . id ]
145
+ stmt = client . prepare ( DeleteAuthorSql )
146
+ stmt . execute ( *query_params )
147
+ end
148
+ end
87
149
88
- class QuerySql
89
- def initialize ( connection_pool_params , mysql2_params )
90
- @pool = ConnectionPool ::new ( **connection_pool_params ) { Mysql2 ::Client . new ( **mysql2_params ) }
91
- end
92
-
93
- def get_author ( get_author_args )
94
- @pool . with do |client |
95
- query_params = [ get_author_args . id ]
96
- stmt = client . prepare ( GetAuthorSql )
97
- result = stmt . execute ( *query_params )
98
- row = result . first
99
- return nil if row . nil?
100
- entity = GetAuthorRow . new ( row [ 'id' ] , row [ 'name' ] , row [ 'bio' ] )
101
- return entity
102
- end
103
- end
104
-
105
- def list_authors
106
- @pool . with do |client |
107
- stmt = client . prepare ( ListAuthorsSql )
108
- result = stmt . execute
109
- entities = [ ]
110
- result . each do |row |
111
- entities << ListAuthorsRow . new ( row [ 'id' ] , row [ 'name' ] , row [ 'bio' ] )
112
- end
113
- return entities
114
- end
115
- end
116
-
117
- def create_author ( create_author_args )
118
- @pool . with do |client |
119
- query_params = [ create_author_args . name , create_author_args . bio ]
120
- stmt = client . prepare ( CreateAuthorSql )
121
- stmt . execute ( *query_params )
122
- end
123
- end
124
-
125
- def update_author ( update_author_args )
126
- @pool . with do |client |
127
- query_params = [ update_author_args . bio , update_author_args . id ]
128
- stmt = client . prepare ( UpdateAuthorSql )
129
- stmt . execute ( *query_params )
130
- end
131
- end
132
-
133
- def create_author_return_id ( create_author_return_id_args )
134
- @pool . with do |client |
135
- query_params = [ create_author_return_id_args . name , create_author_return_id_args . bio ]
136
- stmt = client . prepare ( CreateAuthorReturnIdSql )
137
- stmt . execute ( *query_params )
138
- return client . last_id
139
- end
140
- end
141
-
142
- def delete_author ( delete_author_args )
143
- @pool . with do |client |
144
- query_params = [ delete_author_args . id ]
145
- stmt = client . prepare ( DeleteAuthorSql )
146
- stmt . execute ( *query_params )
147
- end
148
- end
149
-
150
- def test
151
- @pool . with do |client |
152
- stmt = client . prepare ( TestSql )
153
- result = stmt . execute
154
- row = result . first
155
- return nil if row . nil?
156
- entity = TestRow . new (
157
- row [ 'c_bit' ] ,
158
- row [ 'c_tinyint' ] ,
159
- row [ 'c_bool' ] ,
160
- row [ 'c_boolean' ] ,
161
- row [ 'c_smallint' ] ,
162
- row [ 'c_mediumint' ] ,
163
- row [ 'c_int' ] ,
164
- row [ 'c_integer' ] ,
165
- row [ 'c_bigint' ] ,
166
- row [ 'c_serial' ] ,
167
- row [ 'c_decimal' ] ,
168
- row [ 'c_dec' ] ,
169
- row [ 'c_numeric' ] ,
170
- row [ 'c_fixed' ] ,
171
- row [ 'c_float' ] ,
172
- row [ 'c_double' ] ,
173
- row [ 'c_double_precision' ] ,
174
- row [ 'c_date' ] ,
175
- row [ 'c_time' ] ,
176
- row [ 'c_datetime' ] ,
177
- row [ 'c_timestamp' ] ,
178
- row [ 'c_year' ] ,
179
- row [ 'c_char' ] ,
180
- row [ 'c_nchar' ] ,
181
- row [ 'c_national_char' ] ,
182
- row [ 'c_varchar' ] ,
183
- row [ 'c_binary' ] ,
184
- row [ 'c_varbinary' ] ,
185
- row [ 'c_tinyblob' ] ,
186
- row [ 'c_tinytext' ] ,
187
- row [ 'c_blob' ] ,
188
- row [ 'c_text' ] ,
189
- row [ 'c_mediumblob' ] ,
190
- row [ 'c_mediumtext' ] ,
191
- row [ 'c_longblob' ] ,
192
- row [ 'c_longtext' ] ,
193
- row [ 'c_json' ]
194
- )
195
- return entity
196
- end
197
- end
150
+ def test
151
+ @pool . with do |client |
152
+ stmt = client . prepare ( TestSql )
153
+ result = stmt . execute
154
+ row = result . first
155
+ return nil if row . nil?
156
+ entity = TestRow . new (
157
+ row [ 'c_bit' ] ,
158
+ row [ 'c_tinyint' ] ,
159
+ row [ 'c_bool' ] ,
160
+ row [ 'c_boolean' ] ,
161
+ row [ 'c_smallint' ] ,
162
+ row [ 'c_mediumint' ] ,
163
+ row [ 'c_int' ] ,
164
+ row [ 'c_integer' ] ,
165
+ row [ 'c_bigint' ] ,
166
+ row [ 'c_serial' ] ,
167
+ row [ 'c_decimal' ] ,
168
+ row [ 'c_dec' ] ,
169
+ row [ 'c_numeric' ] ,
170
+ row [ 'c_fixed' ] ,
171
+ row [ 'c_float' ] ,
172
+ row [ 'c_double' ] ,
173
+ row [ 'c_double_precision' ] ,
174
+ row [ 'c_date' ] ,
175
+ row [ 'c_time' ] ,
176
+ row [ 'c_datetime' ] ,
177
+ row [ 'c_timestamp' ] ,
178
+ row [ 'c_year' ] ,
179
+ row [ 'c_char' ] ,
180
+ row [ 'c_nchar' ] ,
181
+ row [ 'c_national_char' ] ,
182
+ row [ 'c_varchar' ] ,
183
+ row [ 'c_binary' ] ,
184
+ row [ 'c_varbinary' ] ,
185
+ row [ 'c_tinyblob' ] ,
186
+ row [ 'c_tinytext' ] ,
187
+ row [ 'c_blob' ] ,
188
+ row [ 'c_text' ] ,
189
+ row [ 'c_mediumblob' ] ,
190
+ row [ 'c_mediumtext' ] ,
191
+ row [ 'c_longblob' ] ,
192
+ row [ 'c_longtext' ] ,
193
+ row [ 'c_json' ]
194
+ )
195
+ return entity
196
+ end
198
197
end
198
+ end
199
199
end
0 commit comments