public string SqlString()
        {
            string    sql    = string.Empty;
            SqlEntity entity = GetSqlEntity();

            switch (this._type)
            {
            case SqlType.Query:
                sql = _adapter.Query(entity);
                break;

            case SqlType.Insert:
                sql = _adapter.Insert(_userKey, entity);
                break;

            case SqlType.Update:
                sql = _adapter.Update(entity);
                break;

            case SqlType.Delete:
                sql = _adapter.Delete(entity);
                break;

            default:
                break;
            }
            return(sql);
        }
示例#2
0
        /// <summary>
        /// Delete entity in table "T".
        /// </summary>
        /// <typeparam name="T">Type of entity</typeparam>
        /// <param name="connection">Open SqlConnection</param>
        /// <param name="entityToDelete">Entity to delete</param>
        /// <returns>true if deleted, false if not found</returns>
        public static bool Delete <T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int?commandTimeout = null) where T : class
        {
            // Argument Validation
            if (connection == null)
            {
                throw new ArgumentNullException("connection", "connection cannot be null");
            }
            if (entityToDelete == null)
            {
                throw new ArgumentNullException("entityToDelete", "entityToDelete cannot be null");
            }

            InitializeCacheSweeper();

            var type = typeof(T);

            // Get Keys
            var keyProperties = KeyPropertiesCache(type);

            if (keyProperties.Count() == 0)
            {
                throw new ArgumentException("Entity must have at least one [Key] property");
            }

            // Get TableName
            var name = GetTableName(type);

            // Build base sql
            var sb = new StringBuilder();

            sb.AppendFormat("delete from {0} where ", name);

            // Build parameters
            for (var i = 0; i < keyProperties.Count(); i++)
            {
                var property = keyProperties.ElementAt(i);
                sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
                if (i < keyProperties.Count() - 1)
                {
                    sb.AppendFormat(" and ");
                }
            }

            // Execute
            ISqlAdapter adapter = GetFormatter(connection);

            return(adapter.Delete(connection, transaction, commandTimeout, name, keyProperties, entityToDelete));
        }