Exemplo n.º 1
0
        public static DataTable ReadListTable(Type entityType, string[] fields, Condition[] parameters, string[] orders, int limitNumberOfEntities)
        {
            PersistenceStrategy strategy;
            string tableName, sql, paramPrefix, paramSuffix;

            string[]  filterFields, filterParams;
            object[]  parameterValues;
            DataTable table;

            Operator[] operators;

            strategy        = PersistenceStrategyProvider.FindStrategyFor(entityType);
            tableName       = strategy.GetTableNameOf(entityType);
            filterFields    = StrHelper.GetPropertyValuesOf(parameters, "Field");
            filterParams    = StrHelper.GetNumbers(0, filterFields.Length);
            parameterValues = ArrayHelper.GetPropertyValuesOf(parameters, "Value");
            operators       = ArrayHelper.GetPropertyValuesOf <Operator>(parameters, "Operator");

            paramPrefix  = Transaction.SqlHelper().ParameterPrefix();
            paramSuffix  = Transaction.SqlHelper().ParameterSuffix();
            filterParams = StrHelper.Concat(paramPrefix, filterParams, paramSuffix);

            sql = Transaction.SqlHelper().BuildSelectSqlFor(tableName, fields, filterFields, operators, filterParams, orders, limitNumberOfEntities);

            table = Transaction.Instance.ExecuteSql(sql, parameterValues);

            return(table);
        }
Exemplo n.º 2
0
        public static int Update(object entity)
        {
            Type type;
            PersistenceStrategy strategy;
            string tableName, sql, keyField, keyParameter, paramPrefix, paramSuffix,
                   optimisticLockField;

            string[] fieldNames, parameterNames;
            object[] fieldValues;
            object   keyValue;
            byte     optimisticLockValue;
            int      i;

            type     = entity.GetType();
            strategy = PersistenceStrategyProvider.FindStrategyFor(type);

            tableName      = strategy.GetTableNameOf(type);
            fieldNames     = strategy.GetUpdateFieldNamesOf(type);
            parameterNames = StrHelper.GetNumbers(0, fieldNames.Length);

            paramPrefix    = Transaction.SqlHelper().ParameterPrefix();
            paramSuffix    = Transaction.SqlHelper().ParameterSuffix();
            parameterNames = StrHelper.Concat(paramPrefix, parameterNames, paramSuffix);

            keyField     = strategy.GetKeyColumnOf(type);
            keyParameter = paramPrefix + fieldNames.Length;
            keyValue     = strategy.GetKeyValueOf(entity);

            optimisticLockField = strategy.GetOptimisticLockField(type);
            optimisticLockValue = 0;
            if (!string.IsNullOrEmpty(optimisticLockField))
            {
                optimisticLockValue = (byte)strategy.GetOptimisticLockValue(entity);
            }

            fieldValues = strategy.GetFieldValuesOf(entity, fieldNames);
            ArrayHelper.Merge <object>(ref fieldValues, keyValue);

            sql = Transaction.SqlHelper().BuildUpdateSqlFor(tableName, keyField, keyParameter,
                                                            optimisticLockField, optimisticLockValue,
                                                            fieldNames, parameterNames);

            i = Transaction.Instance.ExecuteNonQuery(sql, fieldValues);
            return(i);
        }
Exemplo n.º 3
0
        public static object Insert(object entity)
        {
            Type type;
            PersistenceStrategy strategy;
            string tableName, sql, idSql, paramPrefix, paramSuffix;

            string[]         fieldNames, parameterNames;
            object[]         fieldValues;
            int              i;
            IParameterHandle idParam = null;
            IdMethod         idMethod;
            object           idValue = 0;

            //!!! bu kodun -entity içindeki deðerler alýnmadan- önce çalýþmasý lazým.
            IInsertInfo iInfo = entity as IInsertInfo;

            if (iInfo != null && GetValue != null)
            {
                iInfo.InsertUser = (int)(GetValue("Kullanici_Id") ?? 0);
                iInfo.InsertDate = DateTime.Now;
            }

            type        = entity.GetType();
            strategy    = PersistenceStrategyProvider.FindStrategyFor(type);
            paramPrefix = Transaction.SqlHelper().ParameterPrefix();
            paramSuffix = Transaction.SqlHelper().ParameterSuffix();

            tableName      = strategy.GetTableNameOf(type);
            fieldNames     = strategy.GetInsertFieldNamesOf(type);
            parameterNames = StrHelper.GetNumbers(0, fieldNames.Length);
            parameterNames = StrHelper.Concat(paramPrefix, parameterNames, paramSuffix);
            fieldValues    = strategy.GetFieldValuesOf(entity, fieldNames);


            sql = Transaction.SqlHelper().BuildInsertSqlFor(tableName, fieldNames, parameterNames);

            idMethod = strategy.GetIdMethodFor(type);
            switch (idMethod)
            {
            case IdMethod.Identity:
                idValue = 0;
                idParam = Transaction.Instance.NewParameter("NewId", idValue, ParameterDirection.Output);
                sql     = Transaction.SqlHelper().
                          BuildInsertSqlWithIdentity(tableName, fieldNames, parameterNames, "NewId");
                break;

            case IdMethod.BySql:
                idSql   = strategy.GetIdSqlFor(type);
                idValue = Transaction.Instance.ExecuteScalar(idSql);
                break;

            case IdMethod.Custom:
                idValue = strategy.GetIdFor(entity, Transaction.Instance);
                break;

            case IdMethod.UserSubmitted:
                idValue = strategy.GetIdFor(entity, Transaction.Instance);
                ((ActiveRecord.ActiveRecordBase)entity).Id = (long)idValue;
                fieldValues = strategy.GetFieldValuesOf(entity, fieldNames);
                break;
            }

            if (Transaction.SqlHelper().GetType() == typeof(MySqlHelper))
            {
                //MySql için output parametreler ile ilgili sorun var!!!
                idParam.Value = Transaction.Instance.ExecuteScalar(sql, fieldValues);
                i             = 1; //***
            }
            else
            {
                i = Transaction.Instance.ExecuteNonQuery(sql, fieldValues, idParam);
            }

            if (idParam != null)
            {
                idValue = idParam.Value; //this works when 'idMethod' is '..Identity'
            }
            return(idValue);
        }