Skip to content

Instantly share code, notes, and snippets.

@Jtorrecilla
Created February 14, 2016 19:49
Show Gist options
  • Select an option

  • Save Jtorrecilla/c79bc4f309760a1e1dc0 to your computer and use it in GitHub Desktop.

Select an option

Save Jtorrecilla/c79bc4f309760a1e1dc0 to your computer and use it in GitHub Desktop.
public class SqlServerQueryGeneration : ISqlQueryGeneration
{
public string GetInsertQuery(IEnumerable<string> columns, string table, bool returnId = false)
{
if (!columns.Any())
{
throw new ArgumentNullException("Columns are required");
}
if (string.IsNullOrWhiteSpace(table))
{
throw new ArgumentNullException("Table is required");
}
string scopeIdentity = string.Empty;
if (returnId)
scopeIdentity = " SELECT SCOPE_IDENTITY()";
return string.Format("INSERT INTO [{0}] ({1}) VALUES({2}){3}", table, string.Join(",", columns), string.Join(",", columns.Select(c => string.Format("@{0}", c))), scopeIdentity).ToUpper();
}
public string GetSelectQuery(IEnumerable<string> columns, string table)
{
if (!columns.Any())
{
throw new ArgumentNullException("Columns are required");
}
if (string.IsNullOrWhiteSpace(table))
{
throw new ArgumentNullException("Table is required");
}
return string.Format("SELECT {0} FROM [{1}]", string.Join(",", columns),table).ToUpper();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment