Example #1
0
            protected override void AddParameter(DbCommand command, QueryParameter parameter, object value)
            {
                var sqlType = (DbQueryType)parameter.QueryType;

                if (sqlType == null)
                {
                    sqlType = (DbQueryType)Provider.Language.TypeSystem.GetColumnType(parameter.Type);
                }
                int len = sqlType.Length;

                if (len == 0 && DbTypeResolver.IsVariableLength(sqlType.SqlDbType))
                {
                    len = Int32.MaxValue;
                }
                SqlParameter p = ((SqlCommand)command).Parameters.Add("@" + parameter.Name, sqlType.SqlDbType, len);

                if (sqlType.Precision != 0)
                {
                    p.Precision = (byte)sqlType.Precision;
                }
                if (sqlType.Scale != 0)
                {
                    p.Scale = (byte)sqlType.Scale;
                }
                p.Value = value ?? DBNull.Value;
            }
Example #2
0
        public void TryResolve_Enumerable_ReturnsNull()
        {
            var sut = new DbTypeResolver();

            this.AssertNull <IEnumerable>(sut);
            this.AssertNull <IEnumerable <int> >(sut);
        }
Example #3
0
        public void TryResolve_Unsupprted_ReturnsNull()
        {
            var sut = new DbTypeResolver();

            this.AssertNull <DbTypeResolver>(sut);
            this.AssertNull <object>(sut);
        }
Example #4
0
        public void TryResolve_ReferenceTypes()
        {
            var sut = new DbTypeResolver();

            this.AssertEqual <byte[]>(sut, DbType.Binary);
            this.AssertEqual <string>(sut, DbType.String);
        }
Example #5
0
 /// <summary>
 /// Returns the converted name of the <see cref="Field"/> object.
 /// </summary>
 /// <param name="field">The instance of the <see cref="Field"/> to be converted.</param>
 /// <param name="dbSetting">The current in used <see cref="IDbSetting"/> object.</param>
 /// <returns>The converted name of the <see cref="Field"/> object.</returns>
 public virtual string Resolve(Field field,
                               IDbSetting dbSetting)
 {
     if (field != null && field.Type != null)
     {
         var dbType = DbTypeResolver.Resolve(field.Type);
         if (dbType != null)
         {
             var dbTypeName = StringNameResolver.Resolve(dbType.Value).ToUpper();
             return(string.Concat("CAST(", field.Name.AsField(dbSetting), " AS ", dbTypeName.AsQuoted(dbSetting), ")"));
         }
     }
     return(field?.Name?.AsQuoted(true, true, dbSetting));
 }
 /// <summary>
 /// Returns the converted name of the <see cref="Field"/> object for SQL Server.
 /// </summary>
 /// <param name="field">The instance of the <see cref="Field"/> to be converted.</param>
 /// <param name="dbSetting">The current in used <see cref="IDbSetting"/> object.</param>
 /// <returns>The converted name of the <see cref="Field"/> object for SQL Server.</returns>
 public override string Resolve(Field field,
                                IDbSetting dbSetting)
 {
     if (field?.Type != null)
     {
         var dbType = DbTypeResolver.Resolve(field.Type);
         if (dbType != null)
         {
             var dbTypeName = StringNameResolver.Resolve(dbType.Value).ToUpper().AsQuoted(dbSetting);
             return(string.Concat("CONVERT(", dbTypeName, ", ", field.Name.AsField(dbSetting), ")"));
         }
     }
     return(field?.Name?.AsField(dbSetting));
 }
Example #7
0
        public void TryResolve_NullableValueTypes()
        {
            var sut = new DbTypeResolver();

            this.AssertEqual <Nullable <bool> >(sut, DbType.Boolean);
            this.AssertEqual <Nullable <byte> >(sut, DbType.Byte);
            this.AssertEqual <Nullable <DateTime> >(sut, DbType.DateTime2);
            this.AssertEqual <Nullable <DateTimeOffset> >(sut, DbType.DateTimeOffset);
            this.AssertEqual <Nullable <decimal> >(sut, DbType.Decimal);
            this.AssertEqual <Nullable <double> >(sut, DbType.Double);
            this.AssertEqual <Nullable <Guid> >(sut, DbType.Guid);
            this.AssertEqual <Nullable <short> >(sut, DbType.Int16);
            this.AssertEqual <Nullable <int> >(sut, DbType.Int32);
            this.AssertEqual <Nullable <long> >(sut, DbType.Int64);
            this.AssertEqual <Nullable <sbyte> >(sut, DbType.SByte);
            this.AssertEqual <Nullable <float> >(sut, DbType.Single);
            this.AssertEqual <Nullable <ushort> >(sut, DbType.UInt16);
            this.AssertEqual <Nullable <uint> >(sut, DbType.UInt32);
            this.AssertEqual <Nullable <ulong> >(sut, DbType.UInt64);
        }
Example #8
0
        /// <summary>
        /// Gets the list of <see cref="DbField"/> of the table.
        /// </summary>
        /// <param name="connectionString">The connection string to connect to.</param>
        /// <param name="tableName">The name of the target table.</param>
        /// <returns>A list of <see cref="DbField"/> of the target table.</returns>
        public IEnumerable <DbField> GetFields(string connectionString, string tableName)
        {
            // Open a connection
            using (var connection = new SqlConnection(connectionString).EnsureOpen())
            {
                // TODO: https://github.com/mikependon/RepoDb/issues/229
                // Check for the command type
                var commandText = @"
                    SELECT C.COLUMN_NAME AS ColumnName
	                    , CONVERT(BIT, COALESCE(TC.is_primary, 0)) AS IsPrimary
	                    , CONVERT(BIT, COALESCE(TMP.is_identity, 1)) AS IsIdentity
	                    , CONVERT(BIT, COALESCE(TMP.is_nullable, 1)) AS IsNullable
	                    , C.DATA_TYPE AS DataType
	                    , CONVERT(INT, COALESCE(TMP.max_length, 1)) AS Size
	                    , CONVERT(TINYINT, COALESCE(TMP.precision, 1)) AS Precision
	                    , CONVERT(TINYINT, COALESCE(TMP.scale, 1)) AS Scale
                    FROM INFORMATION_SCHEMA.COLUMNS C
                    OUTER APPLY
                    (
	                    SELECT 1 AS is_primary
	                    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
	                    LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
		                    ON TC.TABLE_SCHEMA = C.TABLE_SCHEMA
		                    AND TC.TABLE_NAME = C.TABLE_NAME
		                    AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
	                    WHERE KCU.TABLE_SCHEMA = C.TABLE_SCHEMA
		                    AND KCU.TABLE_NAME = C.TABLE_NAME
		                    AND KCU.COLUMN_NAME = C.COLUMN_NAME
		                    AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                    ) TC 
                    OUTER APPLY
                    (
	                    SELECT SC.name
		                    , SC.is_identity
		                    , SC.is_nullable
		                    , SC.max_length
		                    , SC.scale
		                    , SC.precision
	                    FROM [sys].[columns] SC
	                    INNER JOIN [sys].[tables] ST ON ST.object_id = SC.object_id
	                    WHERE SC.name = C.COLUMN_NAME
		                    AND ST.name = C.TABLE_NAME
                    ) TMP
                    WHERE
	                    C.TABLE_SCHEMA = @Schema
	                    AND C.TABLE_NAME = @TableName;"    ;
                var schema      = "dbo";

                // Get the schema and table name
                if (tableName.IndexOf(".") > 0)
                {
                    var splitted = tableName.Split(".".ToCharArray());
                    schema    = splitted[0].AsUnquoted(true);
                    tableName = splitted[1].AsUnquoted(true);
                }
                else
                {
                    tableName = tableName.AsUnquoted();
                }

                // Open a command
                using (var dbCommand = connection.EnsureOpen().CreateCommand(commandText))
                {
                    // Create parameters
                    dbCommand.CreateParameters(new { Schema = schema, TableName = tableName });

                    // Execute and set the result
                    using (var reader = dbCommand.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            yield return(new DbField(reader.GetString(0),
                                                     reader.IsDBNull(1) ? false : reader.GetBoolean(1),
                                                     reader.IsDBNull(2) ? false : reader.GetBoolean(2),
                                                     reader.IsDBNull(3) ? false : reader.GetBoolean(3),
                                                     reader.IsDBNull(4) ? DbTypeResolver.Resolve("text") : DbTypeResolver.Resolve(reader.GetString(4)),
                                                     reader.IsDBNull(5) ? 0 : reader.GetInt32(5),
                                                     reader.IsDBNull(6) ? (byte?)0 : reader.GetByte(6),
                                                     reader.IsDBNull(7) ? (byte?)0 : reader.GetByte(7),
                                                     reader.IsDBNull(7) ? "text" : reader.GetString(4)));
                        }
                    }
                }
            }
        }
Example #9
0
        public void TryResolve_Null_ThrowsException()
        {
            var sut = new DbTypeResolver();

            sut.TryResolve(null);
        }