Skip to content

Commit d1dd611

Browse files
fixed examples doc script + bugfixes in codegen
1 parent bb640d2 commit d1dd611

File tree

6 files changed

+83
-115
lines changed

6 files changed

+83
-115
lines changed

CodeGenerator/CodeGenerator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ private static SimpleStatement GenerateDataclass(string name, ClassMember classM
138138
var dataColumnsStr = dataColumns.JoinByCommaAndFormat();
139139
return new SimpleStatement(dataclassName,
140140
new SimpleExpression(options.RubyVersion.ImmutableDataSupported()
141-
? $"Struct.new({dataColumnsStr})"
142-
: $"Data.define({dataColumnsStr})"));
141+
? $"Data.define({dataColumnsStr})"
142+
: $"Struct.new({dataColumnsStr})"));
143143
}
144144

145145
private SimpleStatement? GetQueryColumnsDataclass(Query query)

Extensions/StringExtensions.cs

-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ public static string FirstCharToUpper(this string input)
1414
};
1515
}
1616

17-
public static string FirstCharToLower(this string input)
18-
{
19-
return input switch
20-
{
21-
null => throw new ArgumentNullException(nameof(input)),
22-
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
23-
_ => string.Concat(input[0].ToString().ToLower(), input.AsSpan(1))
24-
};
25-
}
26-
2717
public static string SnakeCase(this string input)
2818
{
2919
return string.IsNullOrEmpty(input)

RubySyntax/Flows.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public string Build()
5757
{
5858
var initParams = initExpressions
5959
.Select(e => e.Build())
60-
.JoinByNewLine()
61-
.Indent();
62-
var baseCommand = $"{objectType}.new(\n{initParams}\n)";
60+
.ToList()
61+
.JoinByCommaAndFormat();
62+
var baseCommand = $"{objectType}.new({initParams})";
6363
if (bodyStatements is null)
6464
return baseCommand;
6565

examples/mysql2/query_sql.rb

+46-54
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,46 @@ module Mysql2Codegen
66
GetAuthorSql = %q(SELECT id, name, bio FROM authors
77
WHERE id = ? LIMIT 1)
88

9-
GetAuthorRow = Struct.new( :id, :name, :bio)
9+
GetAuthorRow = Data.define( :id, :name, :bio)
1010

11-
GetAuthorArgs = Struct.new( :id)
11+
GetAuthorArgs = Data.define( :id)
1212

1313
ListAuthorsSql = %q(SELECT id, name, bio FROM authors
1414
ORDER BY name)
1515

16-
ListAuthorsRow = Struct.new( :id, :name, :bio)
16+
ListAuthorsRow = Data.define( :id, :name, :bio)
1717

1818
CreateAuthorSql = %q(INSERT INTO authors (
1919
name, bio
2020
) VALUES (
2121
?, ?
2222
))
2323

24-
CreateAuthorArgs = Struct.new( :name, :bio)
24+
CreateAuthorArgs = Data.define( :name, :bio)
2525

2626
UpdateAuthorSql = %q(UPDATE authors
2727
SET bio = ?
2828
WHERE id = ?)
2929

30-
UpdateAuthorArgs = Struct.new( :bio, :id)
30+
UpdateAuthorArgs = Data.define( :bio, :id)
3131

3232
CreateAuthorReturnIdSql = %q(INSERT INTO authors (
3333
name, bio
3434
) VALUES (
3535
?, ?
3636
))
3737

38-
CreateAuthorReturnIdArgs = Struct.new( :name, :bio)
38+
CreateAuthorReturnIdArgs = Data.define( :name, :bio)
3939

4040
DeleteAuthorSql = %q(DELETE FROM authors
4141
WHERE id = ?)
4242

43-
DeleteAuthorArgs = Struct.new( :id)
43+
DeleteAuthorArgs = Data.define( :id)
4444

4545
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
4646
LIMIT 1)
4747

48-
TestRow = Struct.new(
48+
TestRow = Data.define(
4949
:c_bit,
5050
:c_tinyint,
5151
:c_bool,
@@ -97,11 +97,7 @@ def get_author(get_author_args)
9797
result = stmt.execute(*query_params)
9898
row = result.first
9999
return nil if row.nil?
100-
entity = GetAuthorRow.new(
101-
row['id']
102-
row['name']
103-
row['bio']
104-
)
100+
entity = GetAuthorRow.new( row['id'], row['name'], row['bio'])
105101
return entity
106102
end
107103
end
@@ -112,11 +108,7 @@ def list_authors
112108
result = stmt.execute
113109
entities = []
114110
result.each do |row|
115-
entities << ListAuthorsRow.new(
116-
row['id']
117-
row['name']
118-
row['bio']
119-
)
111+
entities << ListAuthorsRow.new( row['id'], row['name'], row['bio'])
120112
end
121113
return entities
122114
end
@@ -162,42 +154,42 @@ def test
162154
row = result.first
163155
return nil if row.nil?
164156
entity = TestRow.new(
165-
row['c_bit']
166-
row['c_tinyint']
167-
row['c_bool']
168-
row['c_boolean']
169-
row['c_smallint']
170-
row['c_mediumint']
171-
row['c_int']
172-
row['c_integer']
173-
row['c_bigint']
174-
row['c_serial']
175-
row['c_decimal']
176-
row['c_dec']
177-
row['c_numeric']
178-
row['c_fixed']
179-
row['c_float']
180-
row['c_double']
181-
row['c_double_precision']
182-
row['c_date']
183-
row['c_time']
184-
row['c_datetime']
185-
row['c_timestamp']
186-
row['c_year']
187-
row['c_char']
188-
row['c_nchar']
189-
row['c_national_char']
190-
row['c_varchar']
191-
row['c_binary']
192-
row['c_varbinary']
193-
row['c_tinyblob']
194-
row['c_tinytext']
195-
row['c_blob']
196-
row['c_text']
197-
row['c_mediumblob']
198-
row['c_mediumtext']
199-
row['c_longblob']
200-
row['c_longtext']
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'],
201193
row['c_json']
202194
)
203195
return entity

examples/pg/query_sql.rb

+28-42
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ module PgCodegen
77
GetAuthorSql = %q(SELECT id, name, bio FROM authors
88
WHERE id = $1 LIMIT 1)
99

10-
GetAuthorRow = Struct.new( :id, :name, :bio)
10+
GetAuthorRow = Data.define( :id, :name, :bio)
1111

12-
GetAuthorArgs = Struct.new( :id)
12+
GetAuthorArgs = Data.define( :id)
1313

1414
ListAuthorsSql = %q(SELECT id, name, bio FROM authors
1515
ORDER BY name)
1616

17-
ListAuthorsRow = Struct.new( :id, :name, :bio)
17+
ListAuthorsRow = Data.define( :id, :name, :bio)
1818

1919
CreateAuthorSql = %q(INSERT INTO authors (
2020
name, bio
@@ -23,19 +23,19 @@ module PgCodegen
2323
)
2424
RETURNING id, name, bio)
2525

26-
CreateAuthorRow = Struct.new( :id, :name, :bio)
26+
CreateAuthorRow = Data.define( :id, :name, :bio)
2727

28-
CreateAuthorArgs = Struct.new( :name, :bio)
28+
CreateAuthorArgs = Data.define( :name, :bio)
2929

3030
DeleteAuthorSql = %q(DELETE FROM authors
3131
WHERE id = $1)
3232

33-
DeleteAuthorArgs = Struct.new( :id)
33+
DeleteAuthorArgs = Data.define( :id)
3434

3535
TestSql = %q(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
3636
LIMIT 1)
3737

38-
TestRow = Struct.new(
38+
TestRow = Data.define(
3939
:c_bit,
4040
:c_smallint,
4141
:c_boolean,
@@ -58,9 +58,7 @@ module PgCodegen
5858

5959
class QuerySql
6060
def initialize(connection_pool_params, pg_params)
61-
@pool = ConnectionPool.new(
62-
**connection_pool_params
63-
) {
61+
@pool = ConnectionPool.new( **connection_pool_params) {
6462
client = PG.connect(**pg_params)
6563
client.type_map_for_results = PG::BasicTypeMapForResults.new client
6664
client
@@ -78,11 +76,7 @@ def get_author(get_author_args)
7876
result = client.exec_prepared('get_author', query_params)
7977
row = result.first
8078
return nil if row.nil?
81-
entity = GetAuthorRow.new(
82-
row['id']
83-
row['name']
84-
row['bio']
85-
)
79+
entity = GetAuthorRow.new( row['id'], row['name'], row['bio'])
8680
return entity
8781
end
8882
end
@@ -96,11 +90,7 @@ def list_authors
9690
result = client.exec_prepared('list_authors')
9791
entities = []
9892
result.each do |row|
99-
entities << ListAuthorsRow.new(
100-
row['id']
101-
row['name']
102-
row['bio']
103-
)
93+
entities << ListAuthorsRow.new( row['id'], row['name'], row['bio'])
10494
end
10595
return entities
10696
end
@@ -116,11 +106,7 @@ def create_author(create_author_args)
116106
result = client.exec_prepared('create_author', query_params)
117107
row = result.first
118108
return nil if row.nil?
119-
entity = CreateAuthorRow.new(
120-
row['id']
121-
row['name']
122-
row['bio']
123-
)
109+
entity = CreateAuthorRow.new( row['id'], row['name'], row['bio'])
124110
return entity
125111
end
126112
end
@@ -146,23 +132,23 @@ def test
146132
row = result.first
147133
return nil if row.nil?
148134
entity = TestRow.new(
149-
row['c_bit']
150-
row['c_smallint']
151-
row['c_boolean']
152-
row['c_integer']
153-
row['c_bigint']
154-
row['c_serial']
155-
row['c_decimal']
156-
row['c_numeric']
157-
row['c_real']
158-
row['c_double_precision']
159-
row['c_date']
160-
row['c_time']
161-
row['c_timestamp']
162-
row['c_char']
163-
row['c_varchar']
164-
row['c_bytea']
165-
row['c_text']
135+
row['c_bit'],
136+
row['c_smallint'],
137+
row['c_boolean'],
138+
row['c_integer'],
139+
row['c_bigint'],
140+
row['c_serial'],
141+
row['c_decimal'],
142+
row['c_numeric'],
143+
row['c_real'],
144+
row['c_double_precision'],
145+
row['c_date'],
146+
row['c_time'],
147+
row['c_timestamp'],
148+
row['c_char'],
149+
row['c_varchar'],
150+
row['c_bytea'],
151+
row['c_text'],
166152
row['c_json']
167153
)
168154
return entity

scripts/docs/generate_examples.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ for ((i = 0 ; i < "${examples_cnt}" ; i++ )); do
1111
engine_name=$(yq ".sql[${i}].engine" sqlc.ci.yaml)
1212
schema_file=$(yq ".sql[${i}].schema" sqlc.ci.yaml)
1313
query_files=$(yq ".sql[${i}].queries" sqlc.ci.yaml)
14+
driver=$(yq ".sql[${i}].codegen[0].options.driver" sqlc.ci.yaml)
15+
test_class_name="end2end_${driver}.rb"
1416

15-
project_name=$(yq ".sql[${i}].codegen[0].out" sqlc.ci.yaml)
16-
test_class_name="${project_name/Example/"Tester"}"
1717
examples_doc+="
18-
## Engine \`${engine_name}\`: [${project_name}](../${project_name})
18+
## Engine \`${engine_name}\`: [examples/${driver}](../examples/${driver})
1919
20-
### [Schema](../${schema_file}) | [Queries](../${query_files}) | [End2End Test](../EndToEndTests/${test_class_name}.cs)
20+
### [Schema](../${schema_file}) | [Queries](../${query_files}) | [End2End Test](../tests/${test_class_name}.rb)
2121
2222
### Config
2323
\`\`\`yaml

0 commit comments

Comments
 (0)