SQL Tablo Kolonlarından C# class Property Nasıl üretilir?

28 Ağu

Entity Framwork gibi ORM araçları ile veritabanı işlemleri yaparken bir tablonun her kolonunu C# sınıfına bir property olarak atamak durumunda kalırız. Hele ki veritabanında çok fazla kolon varsa, tek tek property oluşturmak biraz sıkıcı olabilir. Bu drumda kolon isimleri adım adım C# property tipine dönüştürülebilir.

Postgresql’de bir tablodaki kolon isimlerini almak için gerekli sorgu şu şekildedir:

SELECT column_name
  FROM information_schema.columns
 WHERE table_schema = 'cms'
   AND table_name   = 'consumptions';

Result >

 id
 subscription_id
 created_at
 meter_number
 meter_value
 meter_reading_type

Kolon isimlerini elde ettikten sonra bunları değişik formatlarda çıktılar haline dönüştürebiliriz. SQL kodu ile c# class property üretmek de mümkündür.

SELECT 'public string ' || 
        replace(initcap(column_name),'_','') || 
        ' { get; set; }'
 FROM information_schema.columns
 WHERE table_schema = 'cms'
    AND table_name   = 'consumptions';

Result >
 public string Id { get; set; }
 public string SubscriptionId { get; set; }
 public string CreatedAt { get; set; }
 public string MeterNumber { get; set; }
 public string MeterValue { get; set; }
 public string MeterReadingType { get; set; }

Ancak bütün propert tipleri string oldu ve pek kullanışlı olmadı. Bir adım daha ilerleterek tipleri de data_type kolonundan elde edebiliriz.

SELECT 'public' ||
            (CASE
                WHEN data_type = 'uuid' THEN ' string '
                WHEN data_type = 'integer' THEN ' int '
                WHEN data_type = 'numeric' THEN ' double '
                WHEN data_type = 'timestamp without time zone' THEN ' DateTime '
                ELSE ' string '
            END) ||
       replace(initcap(column_name),'_','') ||
       ' { get; set; }'
  FROM information_schema.columns
 WHERE table_schema = 'cms'
   AND table_name   = 'consumptions';

Result >
 public string Id { get; set; }
 public string SubscriptionId { get; set; }
 public DateTime CreatedAt { get; set; }
 public int MeterNumber { get; set; }
 public double MeterValue { get; set; }
 public int MeterReadingType { get; set; }

Property’ler Column attribute ile biraz daha süslenerek Entity Framework’ün kolay mapping yapmasına imkan sağlanabilir.

SELECT '[Column("' || column_name || '")] '
       'public' ||
            (CASE
                WHEN data_type = 'uuid' THEN ' string '
                WHEN data_type = 'integer' THEN ' int '
                WHEN data_type = 'numeric' THEN ' double '
                WHEN data_type = 'timestamp without time zone' THEN ' DateTime '
                ELSE ' string '
            END) ||
       replace(initcap(column_name),'_','') ||
       ' { get; set; }'
  FROM information_schema.columns
 WHERE table_schema = 'cms'
   AND table_name   = 'consumptions';

Result >
 [Column("id")] public string Id { get; set; }
 [Column("subscription_id")] public string SubscriptionId { get; set; }
 [Column("created_at")] public DateTime CreatedAt { get; set; }
 [Column("meter_number")] public int MeterNumber { get; set; }
 [Column("meter_value")] public double MeterValue { get; set; }
 [Column("meter_reading_type")] public int MeterReadingType { get; set; }