Exemplo n.º 1
0
        /// <summary>
        /// Initializes the object using the default values for the underlying column data types.
        /// </summary>
        protected virtual void SetDefaults()
        {
            // initialize to default settings
            bool connectionClosed = true;
            bool setDbDefaults    = DataService.GetInstance(ProviderName).SetPropertyDefaultsFromDatabase;

            if (DataService.GetInstance(ProviderName).CurrentSharedConnection != null)
            {
                connectionClosed = DataService.GetInstance(ProviderName).CurrentSharedConnection.State == ConnectionState.Closed;
            }

            foreach (TableSchema.TableColumn col in BaseSchema.Columns)
            {
                if (setDbDefaults && !String.IsNullOrEmpty(col.DefaultSetting) && connectionClosed)
                {
                    if (!Utility.IsMatch(col.DefaultSetting, SqlSchemaVariable.DEFAULT))
                    {
                        QueryCommand cmdDefault = new QueryCommand(String.Concat(SqlFragment.SELECT, col.DefaultSetting), col.Table.Provider.Name);
                        SetColumnValue(col.ColumnName, DataService.ExecuteScalar(cmdDefault));
                    }
                }
                else
                {
                    SetColumnValue(col.ColumnName, Utility.GetDefaultSetting(col));
                }
            }

            Initialize();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the scalar.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="sql">The SQL.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public TResult ExecuteScalar <TResult>(string sql, params object[] values)
        {
            object  oResult = DataService.ExecuteScalar(GetCommand(sql, values));
            TResult result  = (TResult)Utility.ChangeType(oResult, typeof(TResult));

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves this object's state to the selected Database.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="userName">Name of the user.</param>
        public static void Save(ItemType item, string userName)
        {
            TableSchema.Table schema = item.GetSchema();
            QueryCommand      cmd    = item.IsNew ? GetInsertCommand(item, userName) : GetUpdateCommand(item, userName);

            if (cmd == null)
            {
                return;
            }

            // reset the Primary Key with the id passed back by the operation
            object pkVal = DataService.ExecuteScalar(cmd);

            if (pkVal != null)
            {
                if (pkVal.GetType() == typeof(decimal))
                {
                    pkVal = Convert.ToInt32(pkVal);
                }

                // set the primaryKey, only if an auto-increment
                if (schema.PrimaryKey.AutoIncrement || schema.PrimaryKey.DataType == DbType.Guid)
                {
                    try
                    {
                        item.SetColumnValue(schema.PrimaryKey.ColumnName, pkVal);
                    }
                    catch
                    {
                        // this will happen if there is no PK defined on a table. Catch this and notify
                        throw new Exception("No Primary Key is defined for this table. A primary key is required to use SubSonic");
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the scalar.
        /// </summary>
        /// <returns></returns>
        public object ExecuteScalar()
        {
            object result = DataService.ExecuteScalar(Command);

            OutputValues = Command.OutputValues;
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the scalar.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public object ExecuteScalar <T>()
        {
            object result = DataService.ExecuteScalar(Command);

            OutputValues = Command.OutputValues;
            T converted = (T)Utility.ChangeType(result, typeof(T));

            return(converted);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the name of the foreign key table.
        /// </summary>
        /// <param name="fkColumnName">Name of the fk column.</param>
        /// <returns></returns>
        public override string GetForeignKeyTableName(string fkColumnName)
        {
            QueryCommand cmd = new QueryCommand(GET_TABLE_SQL, Name);

            cmd.AddParameter(COLUMN_NAME_PARAMETER, fkColumnName, DbType.AnsiString);

            object result = DataService.ExecuteScalar(cmd);

            if (result == null)
            {
                return(null);
            }

            return(result.ToString());
        }
Exemplo n.º 7
0
 /// <summary>
 /// Forces object properties to be initialized using the defaults specified in the database schema.
 /// This method is called only if the provider level setting "useDatabaseDefaults" is set to <c>true</c>
 /// </summary>
 protected void ForceDefaults()
 {
     foreach (TableSchema.TableColumn col in BaseSchema.Columns)
     {
         if (!String.IsNullOrEmpty(col.DefaultSetting))
         {
             if (!Utility.IsMatch(col.DefaultSetting, SqlSchemaVariable.DEFAULT))
             {
                 QueryCommand cmdDefault = new QueryCommand(SqlFragment.SELECT + col.DefaultSetting, col.Table.Provider.Name);
                 SetColumnValue(col.ColumnName, DataService.ExecuteScalar(cmdDefault));
             }
         }
         else
         {
             SetColumnValue(col.ColumnName, Utility.GetDefaultSetting(col));
         }
     }
 }
Exemplo n.º 8
0
        public int Insert <T>(RepositoryRecord <T> item, string userName) where T : RepositoryRecord <T>, new()
        {
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }

            int          result = 0;
            QueryCommand cmd    = ActiveHelper <T> .GetInsertCommand(item, userName);

            TableSchema.Table schema = item.GetSchema();
            if (schema.PrimaryKey != null)
            {
                if (schema.PrimaryKey.AutoIncrement || schema.PrimaryKey.DataType == DbType.Guid)
                {
                    object qResult = DataService.ExecuteScalar(cmd);
                    item.SetColumnValue(schema.PrimaryKey.ColumnName, qResult);
                    if (qResult != null)
                    {
                        int.TryParse(qResult.ToString(), out result);
                    }
                }
                else
                {
                    result = DataService.ExecuteQuery(cmd);
                }
            }
            else
            {
                result = DataService.ExecuteQuery(cmd);
            }

            item.DirtyColumns.Clear();
            item.MarkOld();
            item.MarkClean();
            return(result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Saves this object's state to the selected Database.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        public void Save(string userName)
        {
            bool isValid = true;

            if (ValidateWhenSaving)
            {
                BeforeValidate();

                isValid = Validate();
            }

            if (isValid)
            {
                if (IsNew)
                {
                    BeforeInsert();
                }
                else if (IsDirty)
                {
                    BeforeUpdate();
                }

                QueryCommand cmd = GetSaveCommand(userName);
                if (cmd == null)
                {
                    return;
                }

                // reset the Primary Key with the id passed back by the operation
                object pkVal = DataService.ExecuteScalar(cmd);

                // clear out the DirtyColumns
                DirtyColumns.Clear();

                if (pkVal != null)
                {
                    if (pkVal.GetType() == typeof(decimal))
                    {
                        pkVal = Convert.ToInt32(pkVal);
                    }

                    // set the primaryKey, only if an auto-increment
                    if (BaseSchema.PrimaryKey.AutoIncrement || BaseSchema.PrimaryKey.DataType == DbType.Guid)
                    {
                        try
                        {
                            SetPrimaryKey(pkVal);
                        }
                        catch
                        {
                            // this will happen if there is no PK defined on a table. Catch this and notify
                            throw new Exception("No Primary Key is defined for this table. A primary key is required to use SubSonic");
                        }
                    }
                }

                // set this object as old
                bool isNew = IsNew;
                MarkOld();
                MarkClean();
                AfterCommit(isNew);
            }
            else
            {
                // throw an Exception
                string notification = String.Empty;
                foreach (string message in Errors)
                {
                    notification += message + Environment.NewLine;
                }

                throw new Exception("Can't save: " + notification);
            }
        }