Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates entity in table "T", checks if the entity is modified if the entity is tracked by the Get() extension.
        /// </summary>
        /// <typeparam name="T">Type to be updated</typeparam>
        /// <param name="connection">Open SqlConnection</param>
        /// <param name="entityToUpdate">Entity to be updated</param>
        /// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
        public static bool Update <T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int?commandTimeout = null) where T : class
        {
            // Argument Validation
            if (connection == null)
            {
                throw new ArgumentNullException("connection", "connection cannot be null");
            }
            if (entityToUpdate == null)
            {
                throw new ArgumentNullException("entityToUpdate", "entityToUpdate cannot be null");
            }

            var proxy = entityToUpdate as IProxy;

            if (proxy != null && !proxy.IsDirty)
            {
                return(false);                                 // skip unchanged proxied entities
            }
            InitializeCacheSweeper();

            var type = typeof(T);

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

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

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

            // Get type details
            var allProperties        = TypePropertiesCache(type);
            var computedProperties   = ComputedPropertiesCache(type);
            var updateableProperties = allProperties.Except(keyProperties.Union(computedProperties));

            // Execute
            ISqlAdapter adapter = GetFormatter(connection);

            return(adapter.Update(connection, transaction, commandTimeout, name, keyProperties, updateableProperties, entityToUpdate));
        }