26 Aug 2011

Stored Procedure for creating Properties in BLL

CREATE PROCEDURE usp_makepropertiesvb @TableName VARCHAR(max)
AS
  BEGIN
      DECLARE @Temp VARCHAR(max)
      DECLARE @Temp1 VARCHAR(max)
      DECLARE @Variables VARCHAR(max)
      DECLARE @Variables1 VARCHAR(max)

      SET @Variables = ''
      SET @Variables1 = ''

      DECLARE @Properties VARCHAR(max)
      DECLARE @Properties1 VARCHAR(max)
      DECLARE @Properties2 VARCHAR(max)

      SET @Properties = ''
      SET @Properties1 = ''
      SET @Properties2 = ''

      DECLARE CUR1 CURSOR forward_only FOR
        SELECT i_s.COLUMN_NAME,
               CASE i_s.DATA_TYPE
                 WHEN 'text' THEN 'String'
                 WHEN 'datetime' THEN 'DateTime'
                 WHEN 'image' THEN 'Image'
                 WHEN 'bit' THEN 'Boolean'
                 WHEN 'numeric' THEN 'Integer'
                 WHEN 'smallmoney' THEN 'Integer'
                 WHEN 'sql_variant' THEN 'String'
                 WHEN 'ntext' THEN 'String'
                 WHEN 'timestamp' THEN 'Integer'
                 WHEN 'bigint' THEN 'Double'
                 WHEN 'tinyint' THEN 'Integer'
                 WHEN 'smallint' THEN 'Integer'
                 WHEN 'decimal' THEN 'Double'
                 WHEN 'date' THEN 'Date'
                 WHEN 'nvarchar' THEN 'String'
                 WHEN 'real' THEN 'Double'
                 WHEN 'nchar' THEN 'String'
                 WHEN 'smalldatetime' THEN 'DateTime'
                 WHEN 'int' THEN 'Integer'
                 WHEN 'varbinary' THEN 'Binary'
                 WHEN 'uniqueidentifier' THEN 'uniqueidentifier'
                 WHEN 'float' THEN 'Double'
                 WHEN 'money' THEN 'Double'
                 WHEN 'datetime2' THEN 'DateTime'
                 WHEN 'binary' THEN 'Binary'
                 WHEN 'varchar' THEN 'String'
                 WHEN 'char' THEN 'String'
               END AS DATA_TYPE
        FROM   INFORMATION_SCHEMA.COLUMNS AS i_s
               LEFT OUTER JOIN sys.extended_properties AS s
                 ON s.major_id = Object_id(i_s.TABLE_SCHEMA + '.' + i_s.TABLE_NAME)
                    AND s.minor_id = i_s.ORDINAL_POSITION
                    AND s.name = 'MS_Description'
        WHERE  ( Objectproperty(Object_id(i_s.TABLE_SCHEMA + '.' + i_s.TABLE_NAME), 'IsMsShipped') = 0 )
               AND ( i_s.TABLE_NAME = @TableName )
        ORDER  BY i_s.ORDINAL_POSITION
        FOR READ ONLY

      OPEN CUR1

      FETCH NEXT FROM CUR1 INTO @Temp, @Temp1

      WHILE @@FETCH_STATUS = 0
        BEGIN
            IF Len(@Variables) > 7500
              SET @Variables1 += 'Private _' + @Temp + ' As ' + @Temp1 + Char(10)
            ELSE
              SET @Variables += 'Private _' + @Temp + ' As ' + @Temp1 + Char(10)

            IF Len(@Properties1) > 7500
              BEGIN
                  SET @Properties2 += 'Public Property ' + @Temp + '() As ' + @Temp1 + Char(10)
                  SET @Properties2 += 'Get' + Char(10)
                  SET @Properties2 += 'Return _' + @Temp + Char(10)
                  SET @Properties2 += 'End Get' + Char(10)
                  SET @Properties2 += 'Set(ByVal value As ' + @Temp1 + ')' + Char(10)
                  SET @Properties2 += '_' + @Temp + ' = value' + Char(10)
                  SET @Properties2 += 'End Set' + Char(10)
                  SET @Properties2 += 'End Property' + Char(10)
              END
            ELSE
              IF Len(@Properties) > 7500
                BEGIN
                    SET @Properties1 += 'Public Property ' + @Temp + '() As ' + @Temp1 + Char(10)
                    SET @Properties1 += 'Get' + Char(10)
                    SET @Properties1 += 'Return _' + @Temp + Char(10)
                    SET @Properties1 += 'End Get' + Char(10)
                    SET @Properties1 += 'Set(ByVal value As ' + @Temp1 + ')' + Char(10)
                    SET @Properties1 += '_' + @Temp + ' = value' + Char(10)
                    SET @Properties1 += 'End Set' + Char(10)
                    SET @Properties1 += 'End Property' + Char(10)
                END
              ELSE
                BEGIN
                    SET @Properties += 'Public Property ' + @Temp + '() As ' + @Temp1 + Char(10)
                    SET @Properties += 'Get' + Char(10)
                    SET @Properties += 'Return _' + @Temp + Char(10)
                    SET @Properties += 'End Get' + Char(10)
                    SET @Properties += 'Set(ByVal value As ' + @Temp1 + ')' + Char(10)
                    SET @Properties += '_' + @Temp + ' = value' + Char(10)
                    SET @Properties += 'End Set' + Char(10)
                    SET @Properties += 'End Property' + Char(10)
                END

            FETCH NEXT FROM CUR1 INTO @Temp, @Temp1
        END

      CLOSE CUR1

      DEALLOCATE CUR1

      PRINT '#Region " Variables "'

      PRINT @Variables

      PRINT @Variables1

      PRINT '#End Region'

      PRINT '#Region " Properties "'

      PRINT @Properties

      PRINT @Properties1

      PRINT @Properties2

      PRINT '#End Region'
  END

No comments:

Post a Comment