コード例 #1
0
        /// <summary>
        /// Executes a raw sql statement. BEWARE OF SQL INJECTION ATTACKS. Use for hardcoded sql only.
        /// </summary>
        /// <param name="sql">The sql statement to run as a scalar</param>
        /// <returns>The scalar result</returns>
        public object ExecuteRawSqlScalar(string sql)
        {
            ArgumentValidationHelper.CheckArgumentNotNull(sql, "sql");
            IDbConnection con = null;

            try
            {
                con = OpenConnection;
                var cmd = CreateCommand(con);
                cmd.CommandText = sql;
                return(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Log.Log
                    ("Error reading database : " + Environment.NewLine
                    + ExceptionUtilities.GetExceptionString(ex, 10, true), LogCategory.Exception);
                Log.Log("Sql: " + sql, LogCategory.Exception);
                Console.WriteLine
                    (@"Error reading database : " + Environment.NewLine
                    + ExceptionUtilities.GetExceptionString(ex, 10, true));
                Console.WriteLine(@"Connect string: " + this.ErrorSafeConnectString());
                throw new DatabaseReadException
                          ("There was an error reading the database. Please contact your system administrator.",
                          "The command ExecuteScalar could not be completed.", ex, sql, ErrorSafeConnectString());
            }
            finally
            {
                if (con != null && con.State != ConnectionState.Closed)
                {
                    con.Close();
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Constructor to create new RelPropDef object
 /// </summary>
 /// <param name="ownerClassPropDef">The property definition of the
 /// owner object</param>
 /// <param name="relatedObjectPropName">The property name of the
 /// related object</param>
 public RelPropDef(IPropDef ownerClassPropDef,
                   string relatedObjectPropName)
 {
     ArgumentValidationHelper.CheckArgumentNotNull(ownerClassPropDef, "ownerClassPropDef");
     OwnerPropDef         = ownerClassPropDef;
     RelatedClassPropName = relatedObjectPropName;
 }
コード例 #3
0
 /// <summary>
 /// Constructor to create a new single relationship definition
 /// </summary>
 /// <param name="relationshipName">A name for the relationship</param>
 /// <param name="relatedObjectAssemblyName">The assembly name of the related object</param>
 /// <param name="relatedObjectClassName">The class name of the related object</param>
 /// <param name="relKeyDef">The related key definition</param>
 /// <param name="keepReferenceToRelatedObject">Whether to keep a
 /// reference to the related object.  Could be false for memory-
 /// intensive applications.</param>
 /// <param name="orderBy">The sql order-by clause</param>
 /// <param name="deleteParentAction">Provides specific instructions
 /// with regards to deleting a parent object.  See the DeleteParentAction
 /// enumeration for more detail.</param>
 /// <param name="insertParentAction"></param>
 /// <param name="relationshipType">Provides specific instructions for adding/removing a child object.</param>
 /// <param name="timeout">The timout in milliseconds. The collection will not be automatically refreshed from the DB if the timeout has nto expired</param>
 public MultipleRelationshipDef(string relationshipName, string relatedObjectAssemblyName, string relatedObjectClassName, IRelKeyDef relKeyDef, bool keepReferenceToRelatedObject, string orderBy, DeleteParentAction deleteParentAction, InsertParentAction insertParentAction, RelationshipType relationshipType, int timeout)
     : base(relationshipName, relatedObjectAssemblyName, relatedObjectClassName, relKeyDef,
            keepReferenceToRelatedObject, deleteParentAction, insertParentAction, relationshipType)
 {
     ArgumentValidationHelper.CheckArgumentNotNull(orderBy, "orderBy");
     TimeOut             = timeout;
     OrderCriteriaString = orderBy;
     OrderCriteria       = new OrderCriteria();
     OrderCriteria       = Base.OrderCriteria.FromString(orderBy);
 }
コード例 #4
0
ファイル: BOKey.cs プロジェクト: SaberZA/habanero
 /// <summary>
 /// Adds a <see cref="IBOProp"/> to the key
 /// </summary>
 /// <param name="boProp">The BOProp to add</param>
 public virtual void Add(IBOProp boProp)
 {
     ArgumentValidationHelper.CheckArgumentNotNull(boProp, "boProp");
     if (_props.ContainsKey(boProp.PropertyName))
     {
         throw new InvalidPropertyException(String.Format(
                                                "The property with the name '{0}' that is being added already " +
                                                "exists in the key collection.", boProp.PropertyName));
     }
     _props.Add(boProp.PropertyName, boProp);
     boProp.Updated += BOPropUpdated_Handler;
 }
コード例 #5
0
        /// <summary>
        /// Constructor to create a new Multiple Relationship Definition
        /// </summary>
        /// <param name="relationshipName">A name for the relationship</param>
        /// <param name="relatedObjectClassType">The class type of the related objects</param>
        /// <param name="relKeyDef">The related key definition</param>
        /// <param name="keepReferenceToRelatedObject">Whether to keep a
        /// reference to the related object.  Could be false for memory-
        /// intensive applications.</param>
        /// <param name="orderBy">The sql order-by clause</param>
        /// <param name="deleteParentAction">Provides specific instructions
        /// with regards to deleting a parent object.  See the DeleteParentAction
        /// enumeration for more detail.</param>
        public MultipleRelationshipDef(string relationshipName,
                                       Type relatedObjectClassType,
                                       IRelKeyDef relKeyDef,
                                       bool keepReferenceToRelatedObject,
                                       string orderBy,
                                       DeleteParentAction deleteParentAction)
            : base(relationshipName, relatedObjectClassType, relKeyDef, keepReferenceToRelatedObject, deleteParentAction)
        {
            ArgumentValidationHelper.CheckArgumentNotNull(orderBy, "orderBy");
            OrderCriteriaString = orderBy;

            this.OrderCriteria = Base.OrderCriteria.FromString(orderBy);
        }
コード例 #6
0
        /// <summary>
        /// Executes a stored proc with the params given using the timeout length given.
        /// </summary>
        /// <param name="procName">The stored proc name</param>
        /// <param name="params">The parameters to pass in</param>
        /// <param name="timeout">The timeout in seconds</param>
        /// <returns>A scalar result</returns>
        public int ExecuteStoredProcNonQuery(string procName, IEnumerable <Param> @params, int timeout)
        {
            ArgumentValidationHelper.CheckArgumentNotNull(procName, "procName");
            ArgumentValidationHelper.CheckArgumentNotNull(@params, "params");
            IDbConnection con = null;

            try
            {
                con = OpenConnection;
                var cmd = CreateCommand(con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procName;
                if (timeout > 0)
                {
                    cmd.CommandTimeout = timeout;
                }

                foreach (var param in @params)
                {
                    var dbParam = cmd.CreateParameter();
                    dbParam.DbType        = param.DbType;
                    dbParam.ParameterName = param.ParamName;
                    dbParam.Value         = param.ParamValue;
                    cmd.Parameters.Add(dbParam);
                }

                return(cmd.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                Log.Log
                    ("Error executing stored procedure : " + Environment.NewLine
                    + ExceptionUtilities.GetExceptionString(ex, 10, true), LogCategory.Exception);
                Log.Log("Stored procedure: " + procName, LogCategory.Exception);
                throw new DatabaseWriteException
                          ("There was an error writing to the database. Please contact your system administrator."
                          + Environment.NewLine + "A stored procedure command could not be completed.",
                          "A stored procedure command could not be completed: " + procName, ex, procName + String.Join(", ", @params), ErrorSafeConnectString());
            }
            finally
            {
                if (con != null && con.State != ConnectionState.Closed)
                {
                    con.Close();
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Executes a collection of sql commands that returns no result set
        /// and takes no parameters, using the provided connection.
        /// This method can be used effectively where the database vendor
        /// supports the execution of several sql statements in one
        /// ExecuteNonQuery.  However, for database vendors like Microsoft
        /// Access and MySql, the sql statements will need to be split up
        /// and executed as separate transactions.
        /// </summary>
        /// <param name="statements">A valid sql statement object (typically "insert",
        /// "update" or "delete"). Note_ that this assumes that the
        /// sqlCommand is not a stored procedure.</param>
        /// <param name="transaction">A valid transaction object in which the
        /// sql must be executed, or null</param>
        /// <returns>Returns the number of rows affected</returns>
        /// <exception cref="DatabaseWriteException">Thrown if there is an
        /// error writing to the database.  Also outputs error messages to the log.
        /// </exception>
        /// <future>
        /// In future override this method with methods that allow you to
        /// pass in stored procedures and parameters.
        /// </future>
        public virtual int ExecuteSql(IEnumerable <ISqlStatement> statements, IDbTransaction transaction)
        {
            var inTransaction = transaction != null;

            ArgumentValidationHelper.CheckArgumentNotNull(statements, "statements");
            IDbConnection con = null;

            try
            {
                con = GetOpenConnection(transaction);
                if (transaction == null)
                {
                    transaction = BeginTransaction(con);
                }
                var totalRowsAffected = ExecuteSqlInternal(statements, con, transaction);
                if (!inTransaction)
                {
                    transaction.Commit();
                }
                return(totalRowsAffected);
            }
            catch (Exception ex)
            {
                Log.Log
                    ("Error writing to database : " + Environment.NewLine
                    + ExceptionUtilities.GetExceptionString(ex, 10, true), LogCategory.Exception);
                Log.Log("Sql: " + statements, LogCategory.Exception);
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw new DatabaseWriteException
                          ("There was an error writing to the database. Please contact your system administrator."
                          + Environment.NewLine + "The command executeNonQuery could not be completed. :" + statements,
                          "The command executeNonQuery could not be completed.", ex, statements.ToString(), ErrorSafeConnectString());
            }
            finally
            {
                if (!inTransaction)
                {
                    if (con != null && con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
        }
コード例 #8
0
ファイル: RelationshipDef.cs プロジェクト: SaberZA/habanero
        // ReSharper disable DoNotCallOverridableMethodsInConstructor
        private RelationshipDef(string relationshipName, Type relatedObjectClassType, string relatedObjectAssemblyName, string relatedObjectClassName, IRelKeyDef relKeyDef, bool keepReferenceToRelatedObject, DeleteParentAction deleteParentAction, InsertParentAction insertParentAction, RelationshipType relationshipType)
        {
            ArgumentValidationHelper.CheckArgumentNotNull(relKeyDef, "relKeyDef");
            ArgumentValidationHelper.CheckStringArgumentNotEmpty(relationshipName, "relationshipName");

            if (relatedObjectClassType != null)
            {
                MyRelatedObjectClassType = relatedObjectClassType;
            }
            else
            {
                _relatedObjectAssemblyName = relatedObjectAssemblyName;
                _relatedObjectClassName    = relatedObjectClassName;
                _relatedObjectClassType    = null;
            }
            RelKeyDef                    = relKeyDef;
            RelationshipName             = relationshipName;
            KeepReferenceToRelatedObject = keepReferenceToRelatedObject;
            this.DeleteParentAction      = deleteParentAction;
            this.InsertParentAction      = insertParentAction;
            RelationshipType             = relationshipType;
        }
コード例 #9
0
ファイル: BOKey.cs プロジェクト: SaberZA/habanero
 /// <summary>
 /// Constructor to initialise a new instance
 /// </summary>
 /// <param name="lKeyDef">The key definition</param>
 internal BOKey(KeyDef lKeyDef)
 {
     ArgumentValidationHelper.CheckArgumentNotNull(lKeyDef, "lKeyDef");
     _keyDef = lKeyDef;
     _props  = new Dictionary <string, IBOProp>();
 }
コード例 #10
0
 /// <summary>
 /// Constructor to create new RelPropDef object
 /// </summary>
 /// <param name="ownerClassPropDefName">The name of the prop on the owner object</param>
 /// <param name="relatedObjectPropName">The property name of the
 /// related object</param>
 public RelPropDef(string ownerClassPropDefName, string relatedObjectPropName)
 {
     ArgumentValidationHelper.CheckArgumentNotNull(ownerClassPropDefName, "ownerClassPropDefName");
     _ownerPropDefName    = ownerClassPropDefName;
     RelatedClassPropName = relatedObjectPropName;
 }