private async Task <object> BuildObjectAsync(IQueryRow dt, DatabaseTypeInfo ti) { object toReturn = await Task.Run(() => ObjectBuilder.BuildObject(_dstore, dt, ti)); FireObjectLoaded(toReturn); return(toReturn); }
/// <summary> /// Adds the column. /// </summary> /// <param name="column">The column</param> /// <param name="t">The data object</param> /// <param name="dstore">The datastore to look in</param> private static void AddColumn(IDataStore dstore, IQueryRow column, DBObject t) { Column toAdd = new Column(); ObjectBuilder.SetFieldData(dstore, column, toAdd); t.Columns.Add(toAdd); }
private object BuildObject(IQueryRow dt, DatabaseTypeInfo ti) { object toReturn = ObjectBuilder.BuildObject(this, dt, ti); FireObjectLoaded(toReturn); return(toReturn); }
private void SetFieldData(DatabaseTypeInfo DatabaseTypeInfo, IQueryRow dt, object p) { if (dt != null) { ObjectBuilder.SetFieldData(this, DatabaseTypeInfo, dt, p); FireObjectLoaded(p); } }
/// <summary> /// Sets the fields on an object /// </summary> /// <param name="dstore">The datastore.</param> /// <param name="row">The row to pull from</param> /// <param name="dataItem">The object to set the data on</param> public static void SetFieldData(this IDataStore dstore, IQueryRow row, object dataItem) { if (row != null) { DatabaseTypeInfo ti = dstore.Connection.CommandGenerator.TypeParser.GetTypeInfo(dataItem.GetType()); SetFieldData(dstore, ti, row, dataItem); } }
private async Task SetFieldDataAsync(DatabaseTypeInfo typeInfo, IQueryRow dt, object p) { if (dt != null) { await Task.Run(() => ObjectBuilder.SetFieldData(_dstore, typeInfo, dt, p)); FireObjectLoaded(p); } }
private void AddColumn(IDataStore dstore, IQueryRow columns, DBObject t) { Column toAdd = new Column(); toAdd.ColumnLength = ((string)CLRConverter.ConvertToType(columns.GetDataForRowField("COLUMN_SIZE"), typeof(string))).Trim(); toAdd.DataType = ((string)CLRConverter.ConvertToType(columns.GetDataForRowField("TYPE_NAME"), typeof(string))).Trim(); toAdd.DefaultValue = null; toAdd.IsPrimaryKey = false; toAdd.Name = ((string)CLRConverter.ConvertToType(columns.GetDataForRowField("COLUMN_NAME"), typeof(string))).Trim(); t.Columns.Add(toAdd); }
/// <summary> /// Loads the object info. /// </summary> /// <param name="columns">The columns.</param> /// <param name="table">The table.</param> /// <param name="dstore">The datastore to look in</param> /// <returns></returns> public static DBObject LoadObjectInfo(IDataStore dstore, IQueryRow table, List <IQueryRow> columns) { DBObject t = new DBObject(); ObjectBuilder.SetFieldData(dstore, table, t); t.Columns = new List <Column>(); foreach (IQueryRow row in columns) //all of the columns for all of the tables were returned, so we need to only get the one I'm working on... { row.ResetUsed(); if (row.FieldHasMapping("TableName")) //make sure the table name is present { string tablename = row.GetDataForRowField("TableName") as string; string schema = row.GetDataForRowField("Schema") as string; if (tablename.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase)) { if (schema != null && t.Schema != null) { if (schema.Equals(t.Schema, StringComparison.InvariantCultureIgnoreCase)) { if (row.FieldHasMapping("ColumnName")) { AddColumn(dstore, row, t); } } } else if (schema == null && t.Schema == null) { if (row.FieldHasMapping("ColumnName")) { AddColumn(dstore, row, t); } } } } } return(t); }
/// <summary> /// Builds a return object /// </summary> /// <param name="dstore">The datastore.</param> /// <param name="dt">The query data to build with</param> /// <param name="ti">The parsed type info for the object</param> /// <returns></returns> public static object BuildObject(this IDataStore dstore, IQueryRow row, DatabaseTypeInfo ti) { object toAdd; ConstructorInfo ci = ti.DataType.GetConstructor(new Type[] { }); object[] parms; if (ci == null) { ci = ti.DataType.GetConstructors().First(); ParameterInfo[] parminfo = ci.GetParameters(); toAdd = ci.Invoke(SetConstructorArguments(dstore, parminfo, ti, row)); } else { parms = new object[0]; toAdd = ci.Invoke(parms); SetFieldData(dstore, ti, row, toAdd); } return(toAdd); }
/// <summary> /// Gets a array of constructor arguments to build an object with /// </summary> /// <param name="dstore">The datastore.</param> /// <param name="parminfo">The parms for the constructor.</param> /// <param name="ti">The type info</param> /// <param name="row">The row in the result set to use</param> /// <param name="dt">The query result set</param> /// <returns></returns> public static object[] SetConstructorArguments(this IDataStore dstore, ParameterInfo[] parminfo, DatabaseTypeInfo ti, IQueryRow dt) { object[] toReturn = new object[parminfo.Length]; for (int i = 0; i < parminfo.Length; i++) { ParameterInfo curr = parminfo[i]; if (ti.IsCompilerGenerated) { if (curr.ParameterType.IsSystemType()) { toReturn[i] = dstore.Connection.CLRConverter.ConvertToType(dt.GetDataForRowField(curr.Name), parminfo[i].ParameterType); } else { toReturn[i] = BuildObject(dstore, dt, dstore.Connection.CommandGenerator.TypeParser.GetTypeInfo(curr.ParameterType)); } } else { //most system classes are handled just nicely by the type converter, only interested in user defined classes if (dt.FieldHasMapping(ti.DataFields[i].FieldName) && curr.ParameterType.IsSystemType()) { toReturn[i] = dstore.Connection.CLRConverter.ConvertToType(dt.GetDataForRowField(parminfo[i].Name), parminfo[i].ParameterType); } else { toReturn[i] = BuildObject(dstore, dt, dstore.Connection.CommandGenerator.TypeParser.GetTypeInfo(curr.ParameterType)); } } } return(toReturn); }
/// <summary> /// Sets the fields on an object /// </summary> /// <param name="dstore">The datastore.</param> /// <param name="info">The information for the type</param> /// <param name="row">The row to pull from</param> /// <param name="dataItem">The object to set the data on</param> public static void SetFieldData(this IDataStore dstore, DatabaseTypeInfo info, IQueryRow row, object dataItem) { foreach (DataFieldInfo dfi in info.DataFields) { object item = row.GetDataForRowField(dfi.FieldName); if (item != null) { if (dfi.PropertyType.IsAssignableFrom(item.GetType())) { dfi.Setter(dataItem, item); } else { try { if (!dfi.PropertyType.IsSystemType() && !dfi.PropertyType.IsEnum) { dfi.Setter(dataItem, BuildObject(dstore, row, dstore.Connection.CommandGenerator.TypeParser.GetTypeInfo(dfi.PropertyType))); } else { object fieldValue = dstore.Connection.CLRConverter.ConvertToType(item, dfi.PropertyType); if (fieldValue == null) {//if the value comes back null, lets use the default for the property type (null, zero, whatever) SetDefaultValue(dataItem, dfi); } else { dfi.Setter(dataItem, fieldValue); } } } catch {//attempt to set to default SetDefaultValue(dataItem, dfi); } } } else { int i = 1; i++; } } if (info.AdditionalInit != null) { info.AdditionalInit.ForEach(R => R.Invoke(dstore, dataItem)); } }