Пример #1
0
        private Object ExtractCurrentVersionValue(IReadOnlyEntity entity, IColumn versionColumn, Type type
                                                  , ITransaction tx)
        {
            Object versionValue = null;

            ITypeFieldValueList keyFieldValueList = OperationUtils.ExtractEntityTypeKeyValues(entity, type);
            IDbCommand          cmd    = null;
            IDataReader         reader = null;

            try
            {
                cmd    = CreateRetrievalPreparedStatement(keyFieldValueList, tx);
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    versionValue = DbLayer.DataManipulate().ReadFromResultSet(reader, versionColumn);
                }
            }
            catch (Exception ex)
            {
                String message = String.Format("SQL Exception while trying retrieve version information from {0}"
                                               , entity.GetType().FullName);
                throw new StatementExecutionException(message, ex);
            }
            finally
            {
                DbMgtUtility.Close(reader);
                DbMgtUtility.Close(cmd);
            }
            return(versionValue);
        }
Пример #2
0
        public ICollection <Object> Select(ISelectionQuery query, ITransaction tx)
        {
            IDataReader rs = null;

            try
            {
                var logSb     = new StringBuilder();
                var showQuery = Config.ShowQueries;
                var buildInfo = DbLayer.DataManipulate().ProcessQuery(null, query.Structure);
                var execInfo  = buildInfo.ExecInfo;

                if (showQuery)
                {
                    logSb.Append(execInfo.Sql);
                    foreach (var param in execInfo.Params)
                    {
                        logSb.Append(" ,").Append("Param").Append(param.Index).Append("=").Append(param.Value);
                    }
                    Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString());
                }

                rs = DbLayer.DataManipulate().CreateResultSet(tx, execInfo);

                IList <Object> retList = new List <Object> ();
                ICollection <IQuerySelection> selections = query.Structure.SelectList;
                var selectionCount = selections.Count;

                while (rs.Read())
                {
                    int count = 0;

                    object rowObject = selectionCount > 1 ? new object[selectionCount] : null;
                    foreach (IQuerySelection selection in selections)
                    {
                        Object loaded = ((IAbstractSelection)selection).Retrieve(rs, tx, buildInfo);
                        if (selectionCount > 1)
                        {
                            ((object[])rowObject)[count++] = loaded;
                        }
                        else
                        {
                            rowObject = loaded;
                        }
                    }
                    retList.Add(rowObject);
                }

                return(retList);
            }
            catch (Exception e)
            {
                Logger.GetLogger(Config.LoggerName).Error(e.Message, e);
                throw new RetrievalException(e.Message, e);
            }
            finally
            {
                DbMgtUtility.Close(rs);
            }
        }
Пример #3
0
        private void Insert(IEntity entity, ITypeFieldValueList valueTypeList, Type entityType, ITransaction tx)
        {
            EntityInfo entityInfo = CacheManager.GetEntityInfo(entityType);

            StringBuilder logSb = new StringBuilder();
            string        query = entityInfo.GetInsertQuery(DbLayer);
            IDbCommand    cmd   = tx.CreateCommand();

            cmd.CommandText = query;

            bool showQuery = Config.ShowQueries;

            if (showQuery)
            {
                logSb.Append(query);
            }
            int i = 0;

            foreach (EntityFieldValue fieldValue in valueTypeList.FieldValues)
            {
                IColumn column = fieldValue.Column;
                Object  columnValue;

                if (column.ReadFromSequence &&
                    column.SequenceGenerator != null)
                {
                    columnValue = column.SequenceGenerator.GetNextSequenceValue(tx);
                    PropertyInfo setter = entityType.GetProperty(column.AttributeName);
                    ReflectionUtils.SetValue(setter, entity, columnValue);
                }
                else
                {
                    columnValue = fieldValue.Value;
                }
                if (showQuery)
                {
                    logSb.Append(" ,").Append(column.ColumnName).Append("=").Append(columnValue);
                }
                DbLayer.DataManipulate().SetToPreparedStatement(cmd, columnValue, i + 1, column);
                i++;
            }
            if (showQuery)
            {
                Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString());
            }
            if (Config.EnableStatistics)
            {
                Statistics.RegisterInsert(entityType);
            }
            cmd.ExecuteNonQuery();
            DbMgtUtility.Close(cmd);
        }
Пример #4
0
        private void Delete(ITypeFieldValueList valueTypeList, Type type, ITransaction tx)
        {
            EntityInfo               entityInfo = CacheManager.GetEntityInfo(type);
            StringBuilder            logSb      = new StringBuilder();
            string                   query      = entityInfo.GetDeleteQuery(DbLayer);
            IList <EntityFieldValue> keys       = new List <EntityFieldValue>();

            foreach (EntityFieldValue fieldValue in valueTypeList.FieldValues)
            {
                if (fieldValue.Column.Key)
                {
                    keys.Add(fieldValue);
                }
            }

            bool showQuery = Config.ShowQueries;

            if (showQuery)
            {
                logSb.Append(query);
            }
            IDbCommand ps = tx.CreateCommand();

            ps.CommandText = query;
            for (int i = 0; i < keys.Count; i++)
            {
                EntityFieldValue fieldValue = keys[i];

                if (showQuery)
                {
                    logSb.Append(" ,").Append(fieldValue.Column.ColumnName).Append("=").Append(fieldValue.Value);
                }
                DbLayer.DataManipulate().SetToPreparedStatement(ps, fieldValue.Value, i + 1, fieldValue.Column);
            }
            if (showQuery)
            {
                Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString());
            }
            if (Config.EnableStatistics)
            {
                Statistics.RegisterDelete(type);
            }
            ps.ExecuteNonQuery();
            DbMgtUtility.Close(ps);
        }
Пример #5
0
        private void Update(IEntity entity, ITypeFieldValueList valueTypeList, Type type, ITransaction tx)
        {
            EntityInfo entityInfo = CacheManager.GetEntityInfo(type);
            ICollection <EntityFieldValue> keys   = new List <EntityFieldValue>();
            ICollection <EntityFieldValue> values = new List <EntityFieldValue>();
            StringBuilder logSb = new StringBuilder();
            string        query;


            if (entityInfo.TableInfo.UpdateStrategy == UpdateStrategy.ChangedColumns)
            {
                values = GetModifiedFieldValues(entity, type);
                if (values.Count == 0)
                {
                    return;
                }

                keys = OperationUtils.ExtractEntityKeyValues(entity).FieldValues;
                ICollection <IColumn> keysAndModified = new List <IColumn>();
                foreach (EntityFieldValue fieldValue in values)
                {
                    keysAndModified.Add(fieldValue.Column);
                }
                foreach (EntityFieldValue fieldValue in keys)
                {
                    keysAndModified.Add(fieldValue.Column);
                }
                query = DbLayer.DataManipulate().CreateUpdateQuery(entityInfo.TableInfo.TableName, keysAndModified);
            }
            else
            {
                query = entityInfo.GetUpdateQuery(DbLayer);
                foreach (EntityFieldValue fieldValue in valueTypeList.FieldValues)
                {
                    if (fieldValue.Column.Key)
                    {
                        keys.Add(fieldValue);
                    }
                    else
                    {
                        values.Add(fieldValue);
                    }
                }
            }

            bool showQuery = Config.ShowQueries;

            if (showQuery)
            {
                logSb.Append(query);
            }

            IDbCommand cmd = tx.CreateCommand();

            cmd.CommandText = query;
            int count = 0;

            foreach (EntityFieldValue fieldValue in values)
            {
                if (showQuery)
                {
                    logSb.Append(" ,").Append(fieldValue.Column.ColumnName).Append("=").Append(fieldValue.Value);
                }
                DbLayer.DataManipulate().SetToPreparedStatement(cmd, fieldValue.Value, ++count, fieldValue.Column);
            }

            foreach (EntityFieldValue fieldValue in keys)
            {
                if (showQuery)
                {
                    logSb.Append(" ,").Append(fieldValue.Column.ColumnName).Append("=").Append(fieldValue.Value);
                }
                DbLayer.DataManipulate().SetToPreparedStatement(cmd, fieldValue.Value, ++count, fieldValue.Column);
            }
            if (showQuery)
            {
                Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString());
            }
            if (Config.EnableStatistics)
            {
                Statistics.RegisterUpdate(type);
            }
            cmd.ExecuteNonQuery();
            DbMgtUtility.Close(cmd);
        }