public static string BindKey(this Command command, DataSet.Define define, DataSet.Key key, string sql) { KeyReplacement replacement = Pool <KeyReplacement> .Default.Acquire(); replacement.define = define; replacement.key = key; string result = KeyPattern.Replace(sql, replacement.Execute); replacement.define = null; replacement.key = null; Pool <KeyReplacement> .Default.Release(replacement); var keys = define.Keys; for (int i = 0; i < keys.Count; ++i) { var type = keys[i]; if (type == DataSet.Type.String) { string str = null; if (!key.Read(i, ref str)) { throw new DataSet.TypeMismatchException(); } str = str ?? ""; command.AddParam(str); } else if (type == DataSet.Type.Blob) { byte[] bytes = null; if (!key.Read(i, ref bytes)) { throw new DataSet.TypeMismatchException(); } command.AddParam(bytes); } } return(result); }
public KeyReplacement() { Execute = match => { string value = match.Value; 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()); } } throw new DataSet.TypeMismatchException(); }; }