Skip to content

Commit c43bec5

Browse files
committed
Make Generated code more compact
1 parent 7ae777c commit c43bec5

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

nuget/Sqlite_net.SourceGenerator/SQLiteFastColumnSetterGenerator.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,8 @@ static void Execute(
375375
sb.AppendLine($" SQLiteConnection.RegisterFastColumnSetter(");
376376
sb.AppendLine($" typeof({fullTypeName}),");
377377
sb.AppendLine($" \"{property.ColumnName}\",");
378-
sb.AppendLine($" (obj, stmt, index) => ");
379-
sb.AppendLine($" {{");
378+
sb.Append($" (obj, stmt, index) => ");
380379
GeneratePropertySetter ($"(({fullTypeName})obj)", sb, property);
381-
sb.AppendLine($" }});");
382380
}
383381
}
384382

@@ -418,111 +416,117 @@ static void GeneratePropertySetter(string typedObject, StringBuilder sb, Propert
418416
}
419417

420418
if (isNullable) {
421-
sb.AppendLine($" if (SQLite3.ColumnType(stmt, index) != SQLite3.ColType.Null)");
419+
sb.AppendLine("");
420+
sb.AppendLine($" {{");
421+
sb.AppendLine($" if (SQLite3.ColumnType(stmt, index) != SQLite3.ColType.Null)");
422422
sb.AppendLine($" {{");
423-
}
423+
sb.Append($" ");
424+
}
424425

425426
switch (propertyType)
426427
{
427428
case "string":
428429
case "String":
429430
case "System.String":
430-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnString(stmt, index);");
431+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnString(stmt, index)");
431432
break;
432433

433434
case "byte":
434435
case "Byte":
435436
case "System.Byte":
436-
sb.AppendLine($" {typedObject}.{property.PropertyName} = (byte)SQLite3.ColumnInt(stmt, index);");
437+
sb.Append($"{typedObject}.{property.PropertyName} = (byte)SQLite3.ColumnInt(stmt, index)");
437438
break;
438439

439440
case "short":
440441
case "Int16":
441442
case "System.Int16":
442-
sb.AppendLine($" {typedObject}.{property.PropertyName} = (short)SQLite3.ColumnInt(stmt, index);");
443+
sb.Append($"{typedObject}.{property.PropertyName} = (short)SQLite3.ColumnInt(stmt, index)");
443444
break;
444445

445446

446447
case "int":
447448
case "Int32":
448449
case "System.Int32":
449-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnInt(stmt, index);");
450+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnInt(stmt, index)");
450451
break;
451452

452453
case "long":
453454
case "Int64":
454455
case "System.Int64":
455-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnInt64(stmt, index);");
456+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnInt64(stmt, index)");
456457
break;
457458

458459
case "double":
459460
case "Double":
460461
case "System.Double":
461-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnDouble(stmt, index);");
462+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnDouble(stmt, index)");
462463
break;
463464

464465
case "decimal":
465466
case "Decimal":
466467
case "System.Decimal":
467-
sb.AppendLine($" {typedObject}.{property.PropertyName} = System.Convert.ToDecimal(SQLite3.ColumnDouble(stmt, index));");
468+
sb.Append($"{typedObject}.{property.PropertyName} = System.Convert.ToDecimal(SQLite3.ColumnDouble(stmt, index))");
468469
break;
469470

470471
case "float":
471472
case "Single":
472473
case "System.Single":
473-
sb.AppendLine($" {typedObject}.{property.PropertyName} = (float)SQLite3.ColumnDouble(stmt, index);");
474+
sb.Append($"{typedObject}.{property.PropertyName} = (float)SQLite3.ColumnDouble(stmt, index)");
474475
break;
475476

476477
case "bool":
477478
case "Boolean":
478479
case "System.Boolean":
479-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnInt(stmt, index) == 1;");
480+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnInt(stmt, index) == 1");
480481
break;
481482

482483
case "DateTime":
483484
case "System.DateTime":
484-
sb.AppendLine($" {typedObject}.{property.PropertyName} = new DateTime(SQLite3.ColumnInt64(stmt, index));");
485+
sb.Append($"{typedObject}.{property.PropertyName} = new DateTime(SQLite3.ColumnInt64(stmt, index))");
485486
break;
486487

487488
case "TimeSpan":
488489
case "System.TimeSpan":
489-
sb.AppendLine($" {typedObject}.{property.PropertyName} = new TimeSpan(SQLite3.ColumnInt64(stmt, index));");
490+
sb.Append($"{typedObject}.{property.PropertyName} = new TimeSpan(SQLite3.ColumnInt64(stmt, index))");
490491
break;
491492

492493
case "Guid":
493494
case "System.Guid":
494-
sb.AppendLine($" {typedObject}.{property.PropertyName} = new Guid(SQLite3.ColumnString(stmt, index));");
495+
sb.Append($"{typedObject}.{property.PropertyName} = new Guid(SQLite3.ColumnString(stmt, index))");
495496
break;
496497

497498
case "byte[]":
498499
case "Byte[]":
499500
case "System.Byte[]":
500-
sb.AppendLine($" {typedObject}.{property.PropertyName} = SQLite3.ColumnByteArray(stmt, index);");
501+
sb.Append($"{typedObject}.{property.PropertyName} = SQLite3.ColumnByteArray(stmt, index)");
501502
break;
502503

503504
default:
504505
if (property.Enum != null) {
505506
// For other types, try to use a generic approach
506-
sb.AppendLine ($" // Enum setter for {propertyType}");
507507
if (property.Enum.StoreAsText) {
508-
sb.AppendLine($" {typedObject}.{property.PropertyName} = ({propertyType})Enum.Parse(typeof({propertyType}), SQLite3.ColumnString(stmt, index), ignoreCase: true);");
509-
}
508+
sb.Append($"{typedObject}.{property.PropertyName} = ({propertyType})Enum.Parse(typeof({propertyType}), SQLite3.ColumnString(stmt, index), ignoreCase: true)");
509+
}
510510
else {
511-
sb.AppendLine($" {typedObject}.{property.PropertyName} = ({propertyType})SQLite3.ColumnInt(stmt, index);");
511+
sb.Append($"{typedObject}.{property.PropertyName} = ({propertyType})SQLite3.ColumnInt(stmt, index)");
512512
}
513513
}
514514
else {
515515
// For other types, try to use a generic approach
516-
sb.AppendLine($" // Generic setter for {propertyType}");
517-
sb.AppendLine($" {typedObject}.{property.PropertyName} = ({propertyType})Convert.ChangeType(SQLite3.ColumnString(stmt, index), typeof({propertyType}));");
516+
sb.Append($"{typedObject}.{property.PropertyName} = ({propertyType})Convert.ChangeType(SQLite3.ColumnString(stmt, index), typeof({propertyType}))");
518517
}
519518

520-
break;
521-
}
519+
break;
520+
}
522521

523522
if (isNullable) {
524-
sb.AppendLine($" }}");
523+
sb.AppendLine($";");
524+
sb.AppendLine($" }}");
525+
sb.AppendLine($" }});");
525526
}
527+
else {
528+
sb.AppendLine(");");
529+
}
526530
}
527531

528532
record ClassInfo(string ClassName, string Namespace, List<PropertyInfo> Properties);

0 commit comments

Comments
 (0)