 |
|
|
 |
|
 |
complicated way of generate simple sql querys
|
|
|
|
 |
|
 |
Two reasons for my vote:
1. It's certainly not any easier to use than ADO.NET and SQL. It confuses the language and makes maintainability a nightmare. New and/or junior developers would have to be brought up to speed on the library before they could maintain anything that used it.
2. The article is terrible. There are no implementation details given. An NDOC (or whatever) listing is hardly a useful article. How are we to praise or critique the code if you don't provide us anything in the article?
|
|
|
|
 |
|
 |
BooGhost wrote: New and/or junior developers would have to be brought up to speed on the library before they could maintain anything that used it.
This is true for any new library.
BooGhost wrote: How are we to praise or critique the code if you don't provide us anything in the article?
Download it.
|
|
|
|
 |
|
 |
This class is a great conceptual idea. I have re-written it to include GetAll and GetByID. Will try and upload that later with example.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair
|
|
|
|
 |
|
 |
Very Interesting! I hadn't thought of those cases as I typically write out my SELECT queries to only get what I need. Looking forward to seeing your modifications.
|
|
|
|
 |
|
 |
Translated to VB.NET and C#. Thanks for your contribution. I think this class will work great into an ORM Framework.
Imports System
Imports System.Collections.Generic
Imports System.Text
Friend Class Program1
Shared Sub Main(ByVal args() As String)
Dim builder As New Common.QueryBuilder
builder.Table = "Horses"
builder.IDSettings("HorseID", "3", False)
builder.Build("Name", "John Doe", True)
builder.Build("Age", "15", False)
builder.GetIdentity = True
Console.WriteLine(builder.GetAll)
Console.WriteLine(builder.Insert)
Console.WriteLine(builder.Update)
Console.WriteLine(builder.Delete)
Console.WriteLine(builder.GetByID)
End Sub
End Class
using System;
using System.Collections.Generic;
using System.Text;
namespace Common
{
public class Program1
{
public static void Main()
{
Common.QueryBuilder builder = new QueryBuilder();
builder.Table = "Horses";
builder.IDSettings("HorseID", "3", false);
builder.Build("Name", "John Doe", true);
builder.Build("Age", "15", false);
builder.GetIdentity = true;
Console.WriteLine(builder.GetAll() );
Console.WriteLine(builder.Insert());
Console.WriteLine(builder.Update());
Console.WriteLine(builder.Delete());
Console.WriteLine(builder.GetByID());
}
}
}
To stay fairly consistent with Gof design Builder Pattern[^] the class was renamed to QueryBuilder.
using System;
using System.Text;
using System.Collections.Generic;
[assembly: CLSCompliant(true)]
namespace Common
{
public class QueryBuilder
{
private int numFieldsCount;
private int dicpos;
private Dictionary<string, string> dicFields = new Dictionary<string, string>();
private Dictionary<string, string> dicData = new Dictionary<string, string>();
private Dictionary<string, bool> dicQuotes = new Dictionary<string, bool>();
private List<string> listIDFields = new List<string>();
private List<string> listIDValues = new List<string>();
private List<bool> listIDQuotes = new List<bool>();
private List<string> listIDOperators = new List<string>();
private bool IdentityCheck;
private string strTable;
public string Table {
get { return strTable; }
set { strTable = value; }
}
public bool GetIdentity {
get { return IdentityCheck; }
set { IdentityCheck = value; }
}
public QueryBuilder()
{
}
public void FlushAllData()
{
numFieldsCount = 0;
dicpos = 0;
dicFields.Clear();
dicData.Clear();
dicQuotes.Clear();
listIDFields.Clear();
listIDValues.Clear();
listIDQuotes.Clear();
listIDOperators.Clear();
}
public void Build(string InputField, string InputData, bool InputQuotes)
{
if (!(dicFields.ContainsKey(InputField))) {
dicFields.Add(InputField, InputField);
dicData.Add(InputField, InputData);
dicQuotes.Add(InputField, InputQuotes);
} else {
dicData[InputField] = InputData;
dicQuotes[InputField] = InputQuotes;
}
}
public void IDSettings(string IDField, string IDValue, bool IDQuotes)
{
listIDFields.Add(IDField);
listIDValues.Add(IDValue);
listIDQuotes.Add(IDQuotes);
listIDOperators.Add("=");
}
public void IDSettings(string IDField, string IDValue, bool IDQuotes, string IDOperator)
{
listIDFields.Add(IDField);
listIDValues.Add(IDValue);
listIDQuotes.Add(IDQuotes);
listIDOperators.Add(IDOperator);
}
public string Insert()
{
StringBuilder InsertString = new StringBuilder();
dicpos = 0;
numFieldsCount = dicData.Count;
InsertString.AppendFormat("INSERT INTO {0} (", strTable);
foreach (KeyValuePair<string, string> I in dicFields) {
InsertString.Append(I.Value);
dicpos += 1;
if (dicpos + 1 <= numFieldsCount) {
InsertString.Append(", ");
}
}
dicpos = 0;
InsertString.Append(") VALUES (");
foreach (KeyValuePair<string, string> K in dicData) {
if (dicQuotes[K.Key]) {
InsertString.Append("'");
}
InsertString.Append(dicData[K.Key]);
if (dicQuotes[K.Key]) {
InsertString.Append("'");
}
dicpos += 1;
if (dicpos + 1 <= numFieldsCount) {
InsertString.Append(", ");
}
}
InsertString.Append(")");
if (IdentityCheck) {
string InsString = InsertString.ToString();
InsertString = new StringBuilder();
InsertString.Append(string.Format("SET NOCOUNT ON;{0};SELECT @@Identity As LastID", InsString.ToString()));
}
return InsertString.ToString();
}
public string Update()
{
StringBuilder UpdateString = new StringBuilder();
dicpos = 0;
numFieldsCount = dicData.Count;
UpdateString.AppendFormat("UPDATE {0} SET ", strTable);
foreach (KeyValuePair<string, string> I in dicFields) {
UpdateString.AppendFormat("{0} = ", I.Value);
if (dicQuotes[I.Key]) {
UpdateString.Append("'");
}
UpdateString.Append(dicData[I.Key]);
if (dicQuotes[I.Key]) {
UpdateString.Append("'");
}
dicpos += 1;
if (dicpos + 1 <= numFieldsCount) {
UpdateString.Append(", ");
}
}
UpdateString.Append(" WHERE ");
int Conditions = 0;
for (int IDCount = 0; IDCount <= listIDFields.Count - 1; IDCount++) {
if (Conditions > 0) {
UpdateString.Append(" AND ");
}
UpdateString.AppendFormat("{0} {1}", listIDFields[IDCount], listIDOperators[IDCount]);
if (listIDQuotes[IDCount]) {
UpdateString.Append("'");
}
UpdateString.Append(listIDValues[IDCount]);
if (listIDQuotes[IDCount]) {
UpdateString.Append("'");
}
Conditions += 1;
}
return UpdateString.ToString();
}
public string Delete()
{
StringBuilder DeleteString = new StringBuilder();
DeleteString.AppendFormat("DELETE FROM {0} WHERE ", strTable);
int Conditions = 0;
for (int IDCount = 0; IDCount <= listIDFields.Count - 1; IDCount++) {
if (Conditions > 0) {
DeleteString.Append(" AND ");
}
DeleteString.AppendFormat("{0} {1}", listIDFields[IDCount], listIDOperators[IDCount]);
if (listIDQuotes[IDCount]) {
DeleteString.Append("'");
}
DeleteString.Append(listIDValues[IDCount]);
if (listIDQuotes[IDCount]) {
DeleteString.Append("'");
}
Conditions += 1;
}
return DeleteString.ToString();
}
public string GetAll()
{
StringBuilder GetAllString = new StringBuilder();
GetAllString.AppendFormat("SELECT * FROM {0} ", strTable);
return GetAllString.ToString();
}
public string GetByID()
{
StringBuilder SpecificString = new StringBuilder();
SpecificString.AppendFormat("SELECT * FROM {0} WHERE ", strTable);
int Conditions = 0;
for (int IDCount = 0; IDCount <= listIDFields.Count - 1; IDCount++) {
if (Conditions > 0) {
SpecificString.Append(" AND ");
}
SpecificString.AppendFormat("{0} {1}", listIDFields[IDCount], listIDOperators[IDCount]);
if (listIDQuotes[IDCount]) {
SpecificString.Append("'");
}
SpecificString.Append(listIDValues[IDCount]);
if (listIDQuotes[IDCount]) {
SpecificString.Append("'");
}
Conditions += 1;
}
return SpecificString.ToString();
}
}
}
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair
modified on Tuesday, December 29, 2009 3:15 PM
|
|
|
|
 |
|
|
 |
|
 |
TL Wallace wrote: I think this class will work great into an ORM Framework.
You mean like the multitude that are already available, better written and don't use VB?
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
cant see a use for it, if i wanted to run a bespoke inline query i would just write it into the code, otherwise i would make use of a built in existing feature called "Stored procedures". generally poor understanding of these two concepts
|
|
|
|
 |
|
 |
Although the article itself is ok, I would question the use case for it
"used to make the creation of large or complex SQL INSERT or UPDATE queries faster and easier in ASP.Net"
There is already a method for that, it's called a stored procedure. IMO, there is no case to support the creation of inline or dynamic sql in your ASP.NET application.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
The stored procedure still has to be written, and it still involves the same parts of the query. Also, putting a simple INSERT or UPDATE query (like this generates) into a stored procedure is really a waste. It's not what they were designed for. They were designed for running complex tasks quickly on the database server and reducing the load on your web server, thereby reducing the amount of time it takes to serve up the page. Now, if the query needs to trigger a whole series of events that are all database-related, and if that series of events would be more of a load on the web server than on the database server, then yes, it makes sense to put it into a stored procedure. Otherwise, you are not getting any benefit from using a stored procedure, and it becomes using one just for the sake of doing it. This is what I was taught, what I stick to, and it has yet to do me wrong.
|
|
|
|
 |
|
 |
mbielski wrote: They were designed for running complex tasks quickly on the database server...
No a stored procedure is subroutine that runs in a RDBMS. They weren't designed simply for speed. You are ignoring other usages for stored procs, such as maintainability, reuse, security and access control.
mbielski wrote: ... and reducing the load on your web server thereby reducing the amount of time it takes to serve up the page
Hardly accurate. A poorly written stored proc can slow down an application and increase the load on the DB and slow other applications as well.
Besides, there are many other factors involved in rendering a page.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
Mark Nischalke wrote: You are ignoring other usages for stored procs, such as maintainability, reuse, security and access control.
I'm not ignoring them, I'm focusing on creating a simple query. There are certainly times when stored procedures are needed. I just don't think this is one of them.
Mark Nischalke wrote: A poorly written stored proc can slow down an application and increase the load on the DB and slow other applications as well.
Yes, but that's not the point of this tool.
|
|
|
|
 |
|
 |
mbielski wrote: I just don't think this is one of them.
And when would you focus on maintainability, reuse and security?
mbielski wrote: Yes, but that's not the point of this tool.
It's not about your tool. It's about your argument that sotred procs speed up your application, which is not true in all cases.
I thought the article was good enough to publish, though not very practical or useful IMO. I can ask to remove it if you'd like to continue to argue the case for it.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
You raise some valid points, and I'm not arguing them. Obviously we have different views on the uses of stored procedures, and that's fine. I don't have a problem with agreeing to disagree. I'm not sweating your comments on the article, if that is what you are seeking to remove.
|
|
|
|
 |
|
 |
You want to remove the article because the author is not agreeing with you? Well then go on and delete all my articles as well, because I'm also not sharing your opinion. That's really poor for a gold member.
I think you very well know that what to put in a stored procedure and what not is a very fundamental question. You are obviously belonging to the group that think that everything has to be put in stored procedures. Personally I don't think this is good. It adds complexity where its not required. Like mbielski I would use stored procedures mainly in more complex situations.
Robert
|
|
|
|
 |
|
 |
You have missed the point of the discussion, but your childish 1 votes prove that are not capable of adult reasoning.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
Mark Nischalke wrote: I can ask to remove it if you'd like to continue to argue the case for it.
Than clarify please. My interpretation of this sentence is: "Don't disagree with me or your article will be deleted."
That's childish!
Btw: I haven't one voted your posts, but I think about doing so .
|
|
|
|
 |
|
 |
Robert Rohde wrote: "Don't disagree with me or your article will be deleted."
I don't see that quote anywhere. You understand when you quote someone it should be accurate? What you have is your interruption, not a quote as indicated by the quote marks. It could be construed as defamation.
Robert Rohde wrote: I haven't one voted your posts, but I think about doing so
Grow up or just go away.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
I didn't say it was a quote. I said that this was my interpretation of what you said.
If that's not true, then please tell me what you meant with this sentence:
Mark Nischalke wrote: I can ask to remove it if you'd like to continue to argue the case for it.
If I really misintepreted it then sorry but you should at least clarify on it. If you are not willing to do so and instead start a flamewar then you are childish.
Some more things to the article:
It was never my intention to say that what the author did should be the way to do it. To be honest I would more likely go another way (Linq2Sql, nHibernate or simply hand-coded statements). But going for stored procedures wouldn't be on my favourites list either.
My whole point is that theres no correct or wrong here. No black or white. It has a lot to do with personal preferences and circumstances. I already worked in projects where modifying the (already existing) database was out of discussion and thus there was no possibility to add stored procedures there. Some databases (SqlLite) don't have stored procedures - so what now? If the author introduces another possibility than thats always good even when you don't like his approach in general.
Just as a side note: This[^] article might be more for your taste.
Robert
|
|
|
|
 |
|
 |
Robert Rohde wrote: instead start a flamewar
Who was the one that first reponded with "That's really poor for a gold member." or "That's childish!" or the misquote.
"That's really poor for a gold member.", could be thrown back at you.
I know the language. I've read a book. - _Madmatt
|
|
|
|
 |
|
 |
I ask you a question - you don't answer. I try to get back to the topic - you ignore it.
You are obviously far away from wanting a real discussion.
I wish you good luck. You might need it with such an attitude.
|
|
|
|
 |