Пример #1
0
        public static T[] ReadList <T>(string sql, params object[] parameterValues)
        {
            Type type;

            T[] entities;
            PersistenceStrategy strategy;
            DataTable           table;

            type     = typeof(T);
            strategy = PersistenceStrategyProvider.FindStrategyFor(type);
            if (string.IsNullOrEmpty(sql))
            {
                sql = string.Format("select * from {0}", strategy.GetTableNameOf(type));
            }

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

            if (table.Rows.Count > 0)
            {
                entities = new T[table.Rows.Count];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    entities[i] = Activator.CreateInstance <T>();
                    strategy.Fill(entities[i], table.Rows[i]);
                }
            }
            else
            {
                entities = new T[0];
            }

            return(entities);
        }
Пример #2
0
        public static T[] ReadList <T>(string[] fields, Condition[] parameters, string[] orders, int limitNumberOfEntities)
        {
            Type type;

            T[] entities;
            PersistenceStrategy strategy;
            DataTable           table;

            type     = typeof(T);
            strategy = PersistenceStrategyProvider.FindStrategyFor(type);
            table    = ReadListTable(type, fields, parameters, orders, limitNumberOfEntities);

            if (table.Rows.Count > 0)
            {
                entities = new T[table.Rows.Count];
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    entities[i] = Activator.CreateInstance <T>();
                    strategy.Fill(entities[i], table.Rows[i]);
                }
            }
            else
            {
                entities = new T[0];
            }

            return(entities);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
0
        public static void DeleteByKey(Type entityType, object key, bool throwException)
        {
            PersistenceStrategy strategy;
            string           tableName, keyField, sql, keyParamName, keyParamSql;
            IParameterHandle idParameter;
            int numberOfRows;

            strategy  = PersistenceStrategyProvider.FindStrategyFor(entityType);
            tableName = strategy.GetTableNameOf(entityType);
            keyField  = strategy.GetKeyColumnOf(entityType);

            keyParamName = "prmId";
            keyParamSql  = Transaction.SqlHelper().GenerateParamName(keyParamName);

            sql         = Transaction.SqlHelper().BuildDeleteSqlFor(tableName, keyField, keyParamSql);
            idParameter = Transaction.Instance.NewParameter(keyParamName, key, ParameterDirection.Input);

            numberOfRows = Transaction.Instance.ExecuteNonQuery(sql, ArrayHelper.EmptyArray, idParameter);
        }
Пример #6
0
        public static object Read(object entity, object primaryKey)
        {
            Type                type = entity.GetType();
            DataTable           table;
            PersistenceStrategy strategy;
            string              tableName, keyColumn, keyParamName, sql;

            string[] fieldNames;

            strategy  = PersistenceStrategyProvider.FindStrategyFor(type);
            tableName = strategy.GetTableNameOf(type);
            keyColumn = strategy.GetKeyColumnOf(type);

            sql = strategy.GetSelectStatementFor(type, new string[] { keyColumn }, new string[] { "@prm0" });

            //For types which are not views, sql is null or empty.
            if (string.IsNullOrEmpty(sql))
            {
                keyParamName = Transaction.SqlHelper().GenerateParamName(0);
                fieldNames   = strategy.GetSelectFieldNamesOf(type);

                sql = Transaction.SqlHelper().BuildSelectSqlFor(tableName, fieldNames,
                                                                new string[] { keyColumn },
                                                                new string[] { keyParamName }, null, 0);
            }

            table = Transaction.Instance.ExecuteSql(sql, primaryKey);
            if (table.Rows.Count > 0)
            {
                strategy.Fill(entity, table.Rows[0]);
            }
            else
            {
                entity = null;
            }

            return(entity);
        }
Пример #7
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);
        }