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); }
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)); }
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()); }
public void Load(Database db) { if (!_loaded) { lock (_loadLock) { if (!_loaded) { XrefsByListId = new Dictionary <long, X>(); QuerySet q = Dao.GetQuerySet(db); q.Select <X>().Where(new AssignValue(ParentColumnName, Parent.IdValue, q.ColumnNameFormatter)); q.Execute(db); // should have all the ids of L that should be retrieved if (q.Results[0].DataTable.Rows.Count > 0) { List <long> ids = new List <long>(); foreach (DataRow row in q.Results[0].DataTable.Rows) { long id = Convert.ToInt64(row[ListColumnName]); ids.Add(id); X xref = new X(); xref.DataRow = row; XrefsByListId.Add(id, xref); } QuerySet q2 = Dao.GetQuerySet(db); QueryFilter filter = new QueryFilter(Dao.GetKeyColumnName <L>()); filter.In(ids.ToArray(), db.ParameterPrefix); q2.Select <L>().Where(filter); q2.Execute(db); Initialize(q2.Results[0].DataTable, db); } _loaded = true; } } } }
public virtual long?GetIdValue <T>(DataRow row) where T : Dao, new() { return(this.GetLongValue(Dao.GetKeyColumnName(typeof(T)), row)); }
private StringBuilder GetBodyAndMeta(Incubator incubator) { StringBuilder script = new StringBuilder(); StringBuilder meta = new StringBuilder(); foreach (string className in incubator.ClassNames) { Type modelType = incubator[className]; if (modelType.IsSubclassOf(typeof(Dao))) { StringBuilder parameters; StringBuilder body; string connectionName = Dao.ConnectionName(modelType); meta.AppendLine("\t\td.tables = d.tables || {};"); meta.AppendFormat("\t\td.tables.{0} = {{}};\r\n", className); meta.AppendFormat("\t\td.tables.{0}.keyColumn = '{1}';\r\n", className, Dao.GetKeyColumnName(modelType)); meta.AppendFormat("\t\td.tables.{0}.cols = [];\r\n", className); meta.AppendFormat("\t\td.tables.{0}.ctx = '{1}';\r\n\r\n", className, connectionName); GetCtorParamsAndBody(modelType, meta, connectionName, out parameters, out body); script.AppendFormat("\t\td.ctors.{0} = function {0}(", className); // -- params script.Append(parameters.ToString()); // -- end params script.AppendLine("){"); // writing meta data PropertyInfo[] modelProps = modelType.GetProperties(); foreach (PropertyInfo prop in modelProps) { ColumnAttribute col; if (prop.HasCustomAttributeOfType <ColumnAttribute>(out col)) { string typeName = prop.PropertyType.Name; if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeName = prop.PropertyType.GetGenericArguments()[0].Name; } meta.AppendFormat("\t\td.tables.{0}.cols.push({{name: '{1}', type: '{2}', nullable: {3} }});\r\n", className, col.Name, typeName, col.AllowNull ? "true" : "false"); } ForeignKeyAttribute fk; if (prop.HasCustomAttributeOfType <ForeignKeyAttribute>(out fk)) { meta.AppendFormat("\t\td.fks.push({{ pk: '{0}', pt: '{1}', fk: '{2}', ft: '{3}', nullable: {4} }});\r\n", fk.ReferencedKey, fk.ReferencedTable, fk.Name, fk.Table, fk.AllowNull ? "true" : "false"); } } // -- body script.Append(body.ToString()); // -- end body script.AppendLine("\t\t}"); // -- end writing meta data } } script.AppendLine(meta.ToString()); return(script); }