public static string BindColumn(this Command command, DataSet.Define define, DataSet.Column column, string sql) { ColumnReplacement replacement = Pool <ColumnReplacement> .Default.Acquire(); replacement.define = define; replacement.column = column; string result = ColumnPattern.Replace(sql, replacement.Execute); replacement.define = null; replacement.column = null; Pool <ColumnReplacement> .Default.Release(replacement); var columns = define.Columns; for (int i = 0; i < columns.Count; ++i) { var c = columns[i]; if (c.Value == DataSet.Type.String) { string str = null; if (!column.Read(c.Key, ref str)) { throw new DataSet.TypeMismatchException(); } str = str ?? ""; command.AddParam(str); } else if (c.Value == DataSet.Type.Blob) { byte[] bytes = null; if (!column.Read(c.Key, ref bytes)) { throw new DataSet.TypeMismatchException(); } command.AddParam(bytes); } } return(result); }
public KeyColumnReplacement() { Execute = match => { string value = match.Value; if (value[0] == '@') { int index = int.Parse(value.Substring(1)); switch (define.Keys[index]) { case DataSet.Type.Bool: { bool b = false; if (!key.Read(index, ref b)) { throw new DataSet.TypeMismatchException(); } return(b ? "1" : "0"); } case DataSet.Type.Integer: { int i = 0; if (!key.Read(index, ref i)) { throw new DataSet.TypeMismatchException(); } return(i.ToString()); } case DataSet.Type.Long: { long l = 0; if (!key.Read(index, ref l)) { throw new DataSet.TypeMismatchException(); } return(l.ToString()); } case DataSet.Type.Double: { double d = 0; if (!key.Read(index, ref d)) { throw new DataSet.TypeMismatchException(); } return(d.ToString(CultureInfo.InvariantCulture)); } case DataSet.Type.String: { string str = null; if (!key.Read(index, ref str)) { throw new DataSet.TypeMismatchException(); } str = str ?? ""; uint hash = xxHash.Compute(Encoding.UTF8.GetBytes(str)); return(hash.ToString()); } case DataSet.Type.Blob: { byte[] bytes = null; if (!key.Read(index, ref bytes)) { throw new DataSet.TypeMismatchException(); } uint hash = xxHash.Compute(bytes); return(hash.ToString()); } } } else { string name = value.Substring(1, value.Length - 2); switch (define[name]) { case DataSet.Type.Bool: { bool b = false; if (!column.Read(name, ref b)) { throw new DataSet.TypeMismatchException(); } return(b ? "1" : "0"); } case DataSet.Type.Integer: { int i = 0; if (!column.Read(name, ref i)) { throw new DataSet.TypeMismatchException(); } return(i.ToString()); } case DataSet.Type.Long: { long l = 0; if (!column.Read(name, ref l)) { throw new DataSet.TypeMismatchException(); } return(l.ToString()); } case DataSet.Type.Double: { double d = 0; if (!column.Read(name, ref d)) { throw new DataSet.TypeMismatchException(); } return(d.ToString(CultureInfo.InvariantCulture)); } case DataSet.Type.String: { return("error"); } case DataSet.Type.Blob: { return("error"); } } } throw new DataSet.TypeMismatchException(); }; }
public ColumnReplacement() { Execute = match => { string value = match.Value; string name = value.Substring(1, value.Length - 2); switch (define[name]) { case DataSet.Type.Bool: { bool b = false; if (!column.Read(name, ref b)) { throw new DataSet.TypeMismatchException(); } return(b ? "1" : "0"); } case DataSet.Type.Integer: { int i = 0; if (!column.Read(name, ref i)) { throw new DataSet.TypeMismatchException(); } return(i.ToString()); } case DataSet.Type.Long: { long l = 0; if (!column.Read(name, ref l)) { throw new DataSet.TypeMismatchException(); } return(l.ToString()); } case DataSet.Type.Double: { double d = 0; if (!column.Read(name, ref d)) { throw new DataSet.TypeMismatchException(); } return(d.ToString(CultureInfo.InvariantCulture)); } case DataSet.Type.String: { return("error"); } case DataSet.Type.Blob: { return("error"); } } throw new DataSet.TypeMismatchException(); }; }