public T FirstOrDefault <T>(dynamic parameters = null, params string[] fields) where T : DynamicSword, new() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("SELECT "); sb.Append((fields != null && fields.Length > 0 ? string.Join(", ", fields) + " " : "* ")); sb.Append("FROM " + this.Schema + "." + this.Name + " "); string[] parameterNames = null; parameters = Data.ToDynamicSword(parameters, this.Fields()); if (parameters != null) { parameterNames = parameters.PropertyNames(); if (parameterNames != null && parameterNames.Length > 0) { sb.Append("WHERE "); sb.Append(string.Join(" AND ", parameterNames.Select(s => s + " = @" + s).ToArray())); } } var cmd = new SqlCommand(sb.ToString(), new SqlConnection(Data.ConnectionStringTimeout(this.ConnectionString()))); if (parameterNames != null) { cmd.Parameters.AddRange(parameterNames.Select(s => new SqlParameter { ParameterName = "@" + s, SourceColumn = s }).ToArray()); } DynamicSword swordParameter = null; if (parameters is DynamicSword) { swordParameter = (DynamicSword)parameters; } return(cmd.FirstOrDefault <T>(swordParameter)); }
public List <DynamicSword> Top(dynamic parameters, int top) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (top > 0) { sb.Append("SELECT TOP(" + top.ToString() + ") * "); sb.Append("FROM " + this.Schema + "." + this.Name + " "); string[] parameterNames = null; parameters = Data.ToDynamicSword(parameters, this.Fields()); if (parameters != null) { parameterNames = parameters.PropertyNames(); if (parameterNames != null && parameterNames.Length > 0) { sb.Append("WHERE "); sb.Append(string.Join(" AND ", parameterNames.Select(s => s + " = @" + s).ToArray())); } } var cmd = new SqlCommand(sb.ToString(), new SqlConnection(Data.ConnectionStringTimeout(this.ConnectionString()))); if (parameterNames != null) { cmd.Parameters.AddRange(parameterNames.Select(s => new SqlParameter { ParameterName = "@" + s, SourceColumn = s }).ToArray()); } DynamicSword swordParameter = null; if (parameters is DynamicSword) { swordParameter = (DynamicSword)parameters; } return(cmd.ToList(swordParameter)); } return(new List <DynamicSword>()); }
public DynamicSword Set(string fieldToSet, DynamicSword source, Func <DynamicSword, bool> where) { if (where (source)) { this[fieldToSet] = source[fieldToSet]; } return(this); }
public void AddRow(string[] fields, params object[] objects) { var obj = new DynamicSword(); for (int i = 0; i < objects.Length; i++) { if (i < fields.Length) { obj[fields[i]] = objects[i]; } } this.Rows.Add(obj); }
public void AddRows(string[] fields, List <object[]> rows) { foreach (var objects in rows) { var obj = new DynamicSword(); for (int i = 0; i < objects.Length; i++) { if (i < fields.Length) { obj[fields[i]] = objects[i]; } } this.Rows.Add(obj); } }
//ready? public List <DynamicSword> ToList(dynamic parameters) { var ret = new List <DynamicSword>(); try { if (_command.Connection.State == System.Data.ConnectionState.Closed) { _command.CommandTimeout = Data.Timeout; _command.Connection.Open(); } if (_command.Connection.State == System.Data.ConnectionState.Open) { if (_command.Parameters.Count > 0) { Data.setParameter(parameters, _command); } using (var reader = _command.ExecuteReader()) { Dictionary <string, MapPoint> staticallyTypedMap; Dictionary <string, MapPoint> dynamicallyTypedMap; Data.setMaps(new DynamicSword(), reader, out staticallyTypedMap, out dynamicallyTypedMap); while (reader.Read()) { var newObj = new DynamicSword(); newObj.SetValues(reader, staticallyTypedMap, dynamicallyTypedMap); ret.Add(newObj); } } Data.setOutputParameters(parameters, _command); } } catch (System.Data.SqlClient.SqlException sqlException) { throw sqlException; } catch (Exception) { throw; } finally { _command.Connection.Close(); } return(ret); }
public List <DynamicSword> FieldDefinitions() { List <DynamicSword> fields = new List <DynamicSword>(); if (lastFieldsGrab == DateTime.MinValue || DateTime.Now.Subtract(lastFieldsGrab).TotalHours > 12) { if (!string.IsNullOrEmpty(this.ConnectionString()) && !string.IsNullOrEmpty(this.Name)) { using (var conn = new SqlConnection(Data.ConnectionStringTimeout(this.ConnectionString()))) { var cmdText = "SELECT COLUMN_NAME AS TableName, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + this.Name + "'"; using (var cmd = new SqlCommand(cmdText, conn)) { try { List <string> temp = new List <string>(); conn.Open(); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { var obj = new DynamicSword(); obj["TableName"] = reader.GetString(0); obj["SqlDbType"] = getSqlDbType(reader.GetString(1)); fields.Add(obj); } } _fields = temp.ToArray(); } catch (Exception) { throw; } } } } lastFieldsGrab = DateTime.Now; } return(fields); }
void hydrate(JObject jObject, DynamicSword obj) { if (obj != null) { string[] serializable; string[] notSerializable; obj.SetSerializableAndNotSerializable(out serializable, out notSerializable); foreach (KeyValuePair <string, JToken> item in jObject) { try { if (serializable.Contains(item.Key)) { var type = obj.GetPropertyType(item.Key); if (type != null) { if (type.IsValueType || type == typeof(string)) { obj[item.Key] = item.Value; } else if (type.BaseType == typeof(DynamicSword) && item.Value is JObject) { var childJObject = (JObject)item.Value; if (obj[item.Key] == null) { obj[item.Key] = constructObject(type); } hydrate(childJObject, (DynamicSword)obj[item.Key]); } else if (item.Value is JObject) { JsonSerializer serializer = new JsonSerializer(); obj[item.Key] = serializer.Deserialize(new JTokenReader(item.Value), type); } else { obj[item.Key] = constructObject(type); if (obj[item.Key] != null) { if (obj[item.Key] is IList && item.Value is JArray && type.IsGenericType) { var jArray = ((JArray)item.Value).Children(); var list = (IList)obj[item.Key]; Type subType = list.GetType().GetGenericArguments()[0]; var isDynamicSword = subType.BaseType == typeof(DynamicSword); if (isDynamicSword) { foreach (var subItem in jArray) { if (subItem is JObject) { var newSubObject = (DynamicSword)constructObject(subType); hydrate((JObject)subItem, newSubObject); list.Add(newSubObject); } } } else { JsonSerializer serializer = new JsonSerializer(); obj[item.Key] = serializer.Deserialize(new JTokenReader(item.Value), type); } } } } } } else if (!notSerializable.Contains(item.Key)) { obj[item.Key] = item.Value; } } catch (Exception) { throw; } } } }