Esempio n. 1
0
        /// <summary>
        /// Write the necessary Sql statements into the specified SqlStringBuilder
        /// to delete all the records represented by the current collection.
        /// </summary>
        /// <param name="sql"></param>
        public virtual void WriteDelete(SqlStringBuilder sql)
        {
            if (this._values.Count > 0)
            {
                bool deleteIndividually = Parent == null;

                if (!deleteIndividually)
                {
                    if (string.IsNullOrEmpty(ReferencingColumn))
                    {
                        throw new ArgumentNullException("{0}.ReferencingColumn not set", this.GetType().Name);
                    }

                    sql.Delete(Dao.TableName(typeof(T)))
                    .Where(new AssignValue(ReferencingColumn, Parent.IdValue))
                    .Go();
                }

                foreach (Dao d in this)
                {
                    if (d.AutoDeleteChildren)
                    {
                        d.AutoHydrateChildrenOnDelete = AutoHydrateChildrenOnDelete;
                        d.WriteChildDeletes(sql);
                        sql.Go();
                    }

                    if (deleteIndividually)
                    {
                        d.WriteDelete(sql);
                        sql.Go();
                    }
                }
            }
        }
Esempio n. 2
0
        protected virtual void WriteCreateTable(Type daoType)
        {
            ColumnAttribute[] columns           = GetColumns(daoType);
            string            columnDefinitions = GetColumnDefinitions(columns);

            WriteCreateTable(Dao.TableName(daoType), columnDefinitions);
        }
        protected override void WriteCreateTable(Type daoType)
        {
            ColumnAttribute[] columns           = GetColumns(daoType);
            string            tableName         = Dao.TableName(daoType);
            string            columnDefinitions = GetColumnDefinitions(columns);

            WriteCreateTable(tableName, columnDefinitions, GetForeignKeys(daoType));
        }
Esempio n. 4
0
        private SqlStringBuilder GetBaseIdQuery()
        {
            // get the ids for the specified query
            SqlStringBuilder sql = Database.GetService <SqlStringBuilder>();

            sql.Select(Dao.TableName(typeof(T)), sql.ColumnNameFormatter(Dao.GetKeyColumnName(typeof(T))));
            SetQuery(sql);
            return(sql);
        }
Esempio n. 5
0
        public virtual SqlStringBuilder Select <T>(params string[] columns)
        {
            List <string> goodColumns = ColumnAttribute.GetColumns(typeof(T)).Select(c => c.Name).ToList();

            foreach (string column in columns)
            {
                if (!SelectStar && !goodColumns.Contains(column))
                {
                    throw new InvalidOperationException(string.Format("Invalid column specified {0}", ColumnNameFormatter(column)));
                }
            }

            return(Select(Dao.TableName(typeof(T)),
                          columns.ToDelimited(c => ColumnNameFormatter(c))));
        }
        protected override void WriteCreateTable(Type daoType)
        {
            ColumnAttribute[] columns = GetColumns(daoType);

            Builder.AppendFormat(CreateTableFormat,
                                 TableNameFormatter(Dao.TableName(daoType)),
                                 columns.ToDelimited(c =>
            {
                if (c is KeyColumnAttribute)
                {
                    return(GetKeyColumnDefinition((KeyColumnAttribute)c));
                }
                else
                {
                    return(GetColumnDefinition(c));
                }
            }));
        }
Esempio n. 7
0
        /// <summary>
        /// Creates an in memory dynamic type representing
        /// the current Dao's Columns only.
        /// </summary>
        /// <param name="includeExtras">Include anything added through the Value method</param>
        /// <returns></returns>
        public object ToJsonSafe(bool includeExtras = false)
        {
            object jsonSafe = ToJsonSafe();
            object result   = jsonSafe;

            if (includeExtras)
            {
                object extras     = ToDynamic();
                Type   mergedType = new List <object>()
                {
                    jsonSafe, extras
                }.MergeToDynamicType(Dao.TableName(this), 0);
                result = mergedType.Construct();
                result.CopyProperties(extras);
                result.CopyProperties(jsonSafe);
            }
            return(result);
        }
Esempio n. 8
0
        private void AssociateToParent(T instance)
        {
            Type childType = instance.GetType();

            ValidateParent();

            // from the parent get the ReferencedBy Attribute that matches the referencingClass name
            PropertyInfo[] properties = childType.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.HasCustomAttributeOfType(out ForeignKeyAttribute fk))
                {
                    if (fk.ReferencedTable.Equals(Dao.TableName(_parent)) && fk.Name.Equals(ReferencingColumn))
                    {
                        Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                        property.SetValue(instance, System.Convert.ChangeType(_parent.IdValue.Value, propertyType), null);
                    }
                }
            }
        }
Esempio n. 9
0
        public static SqlStringBuilder From <T>(QueryFilter filter, Database db = null) where T : Dao, new()
        {
            SqlStringBuilder sql = GetSqlStringBuilder <T>(db);

            return(sql.Delete(Dao.TableName(typeof(T))).Where(filter));
        }
Esempio n. 10
0
 public static SqlStringBuilder From <T>(Database db = null) where T : Dao, new()
 {
     return(GetSqlStringBuilder <T>(db).Delete(Dao.TableName(typeof(T))));
 }
Esempio n. 11
0
 /// <summary>
 /// Select count from the table for the specified type T
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public virtual SqlStringBuilder SelectCount <T>() where T : Dao, new()
 {
     return(SelectCount(Dao.TableName(typeof(T))));
 }
Esempio n. 12
0
 /// <summary>
 /// Select Top [topCount].  Same as Top
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="topCount"></param>
 /// <returns></returns>
 public virtual SqlStringBuilder SelectTop <T>(int topCount) where T : Dao, new()
 {
     return(SelectTop(topCount, Dao.TableName(typeof(T)), SelectStar ? "*" : ColumnAttribute.GetColumns(typeof(T)).ToDelimited(c => ColumnNameFormatter(c.Name))));
 }
Esempio n. 13
0
        internal static void GetJsCtorParamsAndBody(Type type, StringBuilder fkProto, out StringBuilder paramList, out StringBuilder body)
        {
            paramList = new StringBuilder();
            body      = new StringBuilder();
            PropertyInfo[] properties = (from prop in type.GetPropertiesWithAttributeOfType <ColumnAttribute>()
                                         where !prop.HasCustomAttributeOfType <KeyColumnAttribute>()
                                         select prop).ToArray();

            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];

                string propertyName = property.Name.CamelCase();
                paramList.Append(propertyName);
                if (i != properties.Length - 1)
                {
                    paramList.Append(", ");
                }

                ForeignKeyAttribute fk;
                if (property.HasCustomAttributeOfType <ForeignKeyAttribute>(out fk))
                {
                    string refProperty = string.Format("{0}Of{1}", fk.ReferencedTable, fk.Name).CamelCase();
                    body.AppendFormat("\tthis.{0} = new dao.entity('{1}', {2});\r\n", refProperty, fk.ReferencedTable, fk.Name);

                    fkProto.AppendFormat("b.ctor.{0}.prototype.{1}Collection = function(){{\r\n", fk.ReferencedTable, fk.Table.CamelCase());
                    fkProto.AppendFormat("\treturn new dao.collection(this, '{0}', '{1}', '{2}', '{3}');\r\n", fk.ReferencedTable, fk.ReferencedKey, fk.Table, fk.Name);
                    fkProto.Append("};\r\n");
                }
                else
                {
                    body.AppendFormat("\tthis.{0} = {0};\r\n", propertyName);
                }
            }

            string varName = GetVarName(type);

            body.AppendFormat("\tthis.update = function(opts){{ bam.{0}.update(this, opts); }};\r\n", varName);
            body.AppendFormat("\tthis.save = this.update;\r\n");
            body.AppendFormat("\tthis.delete = function(opts){{ bam.{0}.delete(this, opts); }};\r\n", varName);
            body.AppendFormat("\tthis.fks = function(){{ return dao.getFks('{0}');}};\r\n", Dao.TableName(type));
            body.AppendFormat("\tthis.pk = function(){{ return '{0}'; }};\r\n", Dao.GetKeyColumnName(type).ToLowerInvariant());
        }
Esempio n. 14
0
 public virtual SqlStringBuilder Insert(Dao instance)
 {
     return(Insert(Dao.TableName(instance.GetType()), instance.GetNewAssignValues()));
 }
Esempio n. 15
0
 public virtual SqlStringBuilder Insert <T>(T instance) where T : Dao, new()
 {
     return(Insert(Dao.TableName(instance), instance.GetNewAssignValues()));
 }
Esempio n. 16
0
 public virtual SqlStringBuilder Update <T>(T instance) where T : Dao
 {
     return(Update(Dao.TableName(instance), instance.GetNewAssignValues()));
 }
Esempio n. 17
0
 public virtual SqlStringBuilder Update(Dao instance)
 {
     return(Update(instance.TableName(), instance.GetNewAssignValues()));
 }
Esempio n. 18
0
        private void GetCtorParamsAndBody(Type type, StringBuilder meta, string connectionName, out StringBuilder paramList, out StringBuilder body)
        {
            string ctorName = type.Name;//GetVarName(type);

            paramList = new StringBuilder();
            body      = new StringBuilder();
            body.AppendFormat("\t\t\tthis.tableName = '{0}';\r\n", ctorName);
            body.AppendFormat("\t\t\tthis.ctx = this.schemaName = this.cxName = this.connectionName = '{0}';\r\n", connectionName);
            body.AppendLine("\t\t\tthis.Dao = {};");
            body.AppendLine("\t\t\tthis.collections = {};");
            body.AppendFormat("\t\t\tthis.Dao.{0} = undefined;\r\n", Dao.GetKeyColumnName(type));

            PropertyInfo[] properties = (from prop in type.GetPropertiesWithAttributeOfType <ColumnAttribute>()
                                         where !prop.HasCustomAttributeOfType <KeyColumnAttribute>() && !metaProperties.Contains(prop.Name)
                                         select prop).ToArray();

            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];

                string propertyName = property.Name;
                paramList.Append(propertyName);
                if (i != properties.Length - 1)
                {
                    paramList.Append(", ");
                }

                ForeignKeyAttribute fk;
                if (property.HasCustomAttributeOfType <ForeignKeyAttribute>(out fk))
                {
                    string refProperty = string.Format("{0}Of{1}", fk.ReferencedTable, fk.Name);
                    body.AppendFormat("\t\t\tthis.{0} = new dao.wrapper('{1}', {2});\r\n", refProperty, fk.ReferencedTable, fk.Name);

                    meta.AppendFormat("\t\td.ctors.{0}.prototype.{1}Collection = function(){{\r\n", fk.ReferencedTable, fk.Table);

                    meta.AppendFormat("\t\t\tif(_.isUndefined(this.collections.{0})){{\r\n", fk.Table);
                    meta.AppendFormat("\t\t\t\tthis.collections.{2} =  new dao.collection(this, '{0}', '{1}', '{2}', '{3}');\r\n", fk.ReferencedTable, fk.ReferencedKey, fk.Table, fk.Name);
                    meta.Append("\t\t\t}\r\n");
                    meta.AppendFormat("\t\t\treturn this.collections.{0};\r\n", fk.Table);
                    meta.Append("\t\t};\r\n");
                }
                else
                {
                    body.AppendFormat("\t\t\tthis.Dao.{0} = {0};\r\n", propertyName);
                }
            }

            meta.AppendFormat("\t\tfor(var f in dao.wrapper.prototype){{ d.ctors.{0}.prototype[f] = dao.wrapper.prototype[f];}}\r\n", ctorName);

            body.AppendFormat("\t\t\tthis.fks = function(){{ return dao.getFks('{0}');}};\r\n", Dao.TableName(type));
        }
 public override SqlStringBuilder Select <T>()
 {
     return(Select(TableNameFormatter(Dao.TableName(typeof(T))), SelectStar ? "*" : ColumnAttribute.GetColumns(typeof(T)).ToDelimited(c => ColumnNameFormatter(c.Name))));
 }
Esempio n. 20
0
 public override SqlStringBuilder Insert(Dao instance)
 {
     ResultDataTables.Add(new InsertResult(instance, "Id"));
     return(Insert(Dao.TableName(instance.GetType()), instance.GetNewAssignValues()).Id().Go());
 }
Esempio n. 21
0
 public SelectColumn(string column)
 {
     this.Select(Dao.TableName(typeof(T)), column);
 }