Ejemplo n.º 1
0
        /// <summary>
        /// Given an AbstractRecord object, returns a INSERT QueryCommand object used to generate SQL.
        /// </summary>
        /// <param name="item">The AbstractRecord object</param>
        /// <param name="userName">An optional username to be used if audit fields are present</param>
        /// <returns></returns>
        public static QueryCommand GetInsertCommand(RecordBase <T> item, string userName)
        {
            TableSchema.Table BaseSchema = item.GetSchema();
            Query             q          = new Query(BaseSchema)
            {
                QueryType = QueryType.Insert
            };
            QueryCommand cmd = new QueryCommand(DataService.GetSql(q), BaseSchema.Provider.Name)
            {
                ProviderName = BaseSchema.Provider.Name
            };

            // loop the Columns and addin the params
            foreach (TableSchema.TableColumn column in BaseSchema.Columns)
            {
                if (!column.AutoIncrement && !column.IsReadOnly)
                {
                    object oVal;
                    bool   insertValue = true;
                    if (Utility.MatchesOne(column.ColumnName, ReservedColumnName.CREATED_BY, ReservedColumnName.MODIFIED_BY))
                    {
                        oVal = userName;
                    }
                    else if (Utility.MatchesOne(column.ColumnName, ReservedColumnName.CREATED_ON, ReservedColumnName.MODIFIED_ON))
                    {
                        oVal = BaseSchema.Provider.Now;
                    }
                    else if (column.DataType == DbType.Guid)
                    {
                        if (!Utility.IsMatch(column.DefaultSetting, SqlSchemaVariable.DEFAULT))
                        {
                            oVal = item.GetColumnValue <Guid>(column.ColumnName);
                            bool isEmptyGuid = Utility.IsMatch(oVal.ToString(), Guid.Empty.ToString());

                            if (column.IsNullable && isEmptyGuid)
                            {
                                oVal = null;
                            }
                            else if (column.IsPrimaryKey && isEmptyGuid)
                            {
                                oVal = Guid.NewGuid();
                            }
                        }
                        else
                        {
                            oVal        = null;
                            insertValue = false;
                        }
                    }
                    else
                    {
                        oVal = item.GetColumnValue <object>(column.ColumnName);

                        // if the value is a boolean, it can be read improperly so reset it to 0 or 1
                        if (oVal != null && column.DataType == DbType.Boolean)
                        {
                            if (Utility.IsMatch(oVal.ToString(), Boolean.FalseString))
                            {
                                oVal = 0;
                            }
                            else if (Utility.IsMatch(oVal.ToString(), Boolean.TrueString))
                            {
                                oVal = 1;
                            }
                        }
                    }

                    if (oVal == null)
                    {
                        oVal = DBNull.Value;
                    }
                    if (insertValue)
                    {
                        cmd.Parameters.Add(column.ParameterName, oVal, column.DataType);
                    }
                }
            }

            return(cmd);
        }