/// <summary> /// CreateUpdateSQL /// </summary> /// <param name="tableName"></param> /// <param name="values"></param> /// <param name="whereSql"></param> /// <param name="whereArgs"></param> /// <returns></returns> public string CreateUpdateSQL(string tableName, DataValues values, string whereSql, DataValues whereArgs) { StringBuilder stringBuilder = new StringBuilder("UPDATE"); stringBuilder.Append(" "); stringBuilder.AppendFormat("[{0}]", tableName); string[] keys = values.Keys; if (keys == null || keys.Length == 0) { throw new ArgumentException("values"); } stringBuilder.Append(" "); stringBuilder.Append("SET"); stringBuilder.Append(" "); for (int i = 0; i < values.Count; i++) { string key = keys[i]; stringBuilder.AppendFormat("[{0}] = {1}", key, values.Get(key)); if ((i + 1) < keys.Length) { stringBuilder.Append(","); } } if (!String.IsNullOrEmpty(whereSql)) { whereSql = CreateWhereSql(whereSql, whereArgs); stringBuilder.Append(" "); stringBuilder.Append(whereSql); } stringBuilder.Append(";"); return(stringBuilder.ToString()); }
/// <summary> /// CreateInsertSQL /// </summary> /// <param name="tableName"></param> /// <param name="contentValues"></param> /// <returns></returns> public string CreateInsertSQL(string tableName, DataValues contentValues) { StringBuilder stringBuilder = new StringBuilder("INSERT INTO"); stringBuilder.Append(" "); if (String.IsNullOrEmpty(tableName)) { throw new ArgumentException("tableName"); } stringBuilder.AppendFormat("[{0}]", tableName); if (contentValues == null || contentValues.Count == 0) { throw new ArgumentException("contentValues"); } string[] keys = contentValues.Keys; StringBuilder kBuilder = new StringBuilder(); StringBuilder vBuilder = new StringBuilder(); for (int i = 0; i < keys.Length; i++) { string key = keys[i]; kBuilder.AppendFormat("[{0}]", key); vBuilder.AppendFormat("{0}", contentValues.Get(key)); if ((i + 1) < keys.Length) { kBuilder.Append(","); vBuilder.Append(","); } } stringBuilder.AppendFormat("({0})", kBuilder); stringBuilder.AppendFormat("VALUES({0})", vBuilder); stringBuilder.Append(";"); return(stringBuilder.ToString()); }
/// <summary> /// CreateWhereSql /// </summary> /// <param name="whereSql"></param> /// <param name="whereArgs"></param> /// <returns></returns> protected virtual string CreateWhereSql(string whereSql, DataValues whereArgs) { if (String.IsNullOrEmpty(whereSql)) { throw new ArgumentException("whereSql"); } StringBuilder stringBuilder = new StringBuilder("WHERE"); string[] args = whereSql.Split(',', ';'); if (args.Length > 0) { stringBuilder.Append(" "); for (int i = 0; i < args.Length; i++) { string str = args[i]; string[] aa = str.Split('=', '<', '>'); string key = aa[0].Trim(); string value = aa[1].Trim(); if (Equals(value, "?")) { if (whereArgs == null || whereArgs.Count == 0) { throw new ArgumentException("contentValues"); } value = whereArgs.Get(key); } stringBuilder.AppendFormat("[{0}] = {1}", key, value); if ((i + 1) < args.Length) { stringBuilder.Append(" "); stringBuilder.Append("and"); stringBuilder.Append(" "); } } } return(stringBuilder.ToString()); }
/// <summary> /// CreateWhereSql /// </summary> /// <param name="whereSql"></param> /// <param name="whereArgs"></param> /// <returns></returns> protected virtual string CreateWhereSql(string whereSql, DataValues whereArgs) { if (String.IsNullOrEmpty(whereSql)) { throw new ArgumentException("whereSql"); } StringBuilder stringBuilder = new StringBuilder("WHERE"); string[] args = whereSql.Split(',', ';'); if (args.Length > 0) { stringBuilder.Append(" "); for (int i = 0; i < args.Length; i++) { string str = args[i]; string[] aa = str.Split('=', '<', '>'); string key = aa[0].Trim(); string value = aa[1].Trim(); if (Equals(value, "?")) { if (whereArgs == null || whereArgs.Count == 0) { throw new ArgumentException("contentValues"); } value = whereArgs.Get(key); } stringBuilder.AppendFormat("[{0}] = {1}", key, value); if ((i + 1) < args.Length) { stringBuilder.Append(" "); stringBuilder.Append("and"); stringBuilder.Append(" "); } } } return stringBuilder.ToString(); }
/// <summary> /// CreateUpdateSQL /// </summary> /// <param name="tableName"></param> /// <param name="values"></param> /// <param name="whereSql"></param> /// <param name="whereArgs"></param> /// <returns></returns> public string CreateUpdateSQL(string tableName, DataValues values, string whereSql, DataValues whereArgs) { StringBuilder stringBuilder = new StringBuilder("UPDATE"); stringBuilder.Append(" "); stringBuilder.AppendFormat("[{0}]", tableName); string[] keys = values.Keys; if (keys == null || keys.Length == 0) { throw new ArgumentException("values"); } stringBuilder.Append(" "); stringBuilder.Append("SET"); stringBuilder.Append(" "); for (int i = 0; i < values.Count; i++) { string key = keys[i]; stringBuilder.AppendFormat("[{0}] = {1}", key, values.Get(key)); if ((i + 1) < keys.Length) { stringBuilder.Append(","); } } if (!String.IsNullOrEmpty(whereSql)) { whereSql = CreateWhereSql(whereSql, whereArgs); stringBuilder.Append(" "); stringBuilder.Append(whereSql); } stringBuilder.Append(";"); return stringBuilder.ToString(); }
/// <summary> /// CreateInsertSQL /// </summary> /// <param name="tableName"></param> /// <param name="contentValues"></param> /// <returns></returns> public string CreateInsertSQL(string tableName, DataValues contentValues) { StringBuilder stringBuilder = new StringBuilder("INSERT INTO"); stringBuilder.Append(" "); if (String.IsNullOrEmpty(tableName)) { throw new ArgumentException("tableName"); } stringBuilder.AppendFormat("[{0}]", tableName); if (contentValues == null || contentValues.Count == 0) { throw new ArgumentException("contentValues"); } string[] keys = contentValues.Keys; StringBuilder kBuilder = new StringBuilder(); StringBuilder vBuilder = new StringBuilder(); for (int i = 0; i < keys.Length; i++) { string key = keys[i]; kBuilder.AppendFormat("[{0}]", key); vBuilder.AppendFormat("{0}", contentValues.Get(key)); if ((i + 1) < keys.Length) { kBuilder.Append(","); vBuilder.Append(","); } } stringBuilder.AppendFormat("({0})", kBuilder); stringBuilder.AppendFormat("VALUES({0})", vBuilder); stringBuilder.Append(";"); return stringBuilder.ToString(); }