448 lines
13 KiB
C#
448 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Matomat.Database.Query
|
|
{
|
|
class DBQuery
|
|
{
|
|
private TDatabase db;
|
|
public enum QTypes
|
|
{
|
|
None,
|
|
Select,
|
|
Update,
|
|
Insert,
|
|
Union,
|
|
Delete,
|
|
From,
|
|
Join,
|
|
Set,
|
|
Where,
|
|
Group,
|
|
Having,
|
|
Order,
|
|
Columns,
|
|
Values,
|
|
Limit,
|
|
}
|
|
private QTypes _type;
|
|
private bool _autoIncrementField;
|
|
private List<DatabaseQueryElement> _join;
|
|
private DatabaseQueryElement _select;
|
|
private DatabaseQueryElement _from;
|
|
private DatabaseQueryElement _where;
|
|
private DatabaseQueryElement _update;
|
|
private DatabaseQueryElement _set;
|
|
private DatabaseQueryElement _insert;
|
|
private DatabaseQueryElement _columns;
|
|
private DatabaseQueryElement _values;
|
|
private DatabaseQueryElement _group;
|
|
private DatabaseQueryElement _having;
|
|
private DatabaseQueryElement _order;
|
|
private DatabaseQueryElement _union;
|
|
private DatabaseQueryElement _delete;
|
|
private DatabaseQueryElement _limit;
|
|
public DBQuery(TDatabase db)
|
|
{
|
|
this.db = db;
|
|
}
|
|
|
|
public DBQuery select(string columns)
|
|
{
|
|
this._type = QTypes.Select;
|
|
if (this._select == null)
|
|
{
|
|
this._select = new DatabaseQueryElement("SELECT", columns);
|
|
}
|
|
else
|
|
{
|
|
this._select.append(columns);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public DBQuery from(string tables)
|
|
{
|
|
if (this._from == null)
|
|
{
|
|
this._from = new DatabaseQueryElement("FROM", tables);
|
|
}
|
|
else
|
|
{
|
|
this._from.append(tables);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public DBQuery where(string conditions, string glue="AND")
|
|
{
|
|
if (this._where == null)
|
|
{
|
|
glue = glue.ToUpper();
|
|
this._where = new DatabaseQueryElement("WHERE", conditions, " " + glue + " ");
|
|
}
|
|
else
|
|
{
|
|
this._where.append(conditions);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string query = "";
|
|
|
|
switch (this._type)
|
|
{
|
|
case QTypes.Select:
|
|
query += this._select;
|
|
query += this._from;
|
|
if (this._join != null)
|
|
{
|
|
// special case for joins
|
|
foreach (DatabaseQueryElement join in this._join)
|
|
{
|
|
query += join;
|
|
}
|
|
}
|
|
|
|
if (this._where != null)
|
|
{
|
|
query += this._where;
|
|
}
|
|
|
|
if (this._group != null)
|
|
{
|
|
query += this._group;
|
|
}
|
|
|
|
if (this._having != null)
|
|
{
|
|
query += this._having;
|
|
}
|
|
|
|
if (this._order != null)
|
|
{
|
|
query += this._order;
|
|
}
|
|
if (this._limit != null)
|
|
{
|
|
query += this._limit;
|
|
}
|
|
|
|
break;
|
|
case QTypes.Insert:
|
|
query += this._insert;
|
|
|
|
if (this._set != null)
|
|
{
|
|
query += this._set;
|
|
}
|
|
else if (this._values != null)
|
|
{
|
|
if (this._columns != null)
|
|
{
|
|
query += this._columns;
|
|
}
|
|
|
|
query += " VALUES ";
|
|
query += this._values;
|
|
}
|
|
break;
|
|
case QTypes.Update:
|
|
query += this._update;
|
|
|
|
if (this._join != null)
|
|
{
|
|
// special case for joins
|
|
foreach (DatabaseQueryElement join in this._join)
|
|
{
|
|
query += join;
|
|
}
|
|
}
|
|
|
|
query += this._set;
|
|
|
|
if (this._where != null)
|
|
{
|
|
query += this._where;
|
|
}
|
|
break;
|
|
case QTypes.Union:
|
|
query += this._union;
|
|
break;
|
|
case QTypes.Delete:
|
|
query += this._delete;
|
|
query += this._from;
|
|
|
|
if (this._join != null)
|
|
{
|
|
// special case for joins
|
|
foreach (DatabaseQueryElement join in this._join)
|
|
{
|
|
query += join;
|
|
}
|
|
}
|
|
if (this._where != null)
|
|
{
|
|
query += this._where;
|
|
}
|
|
break;
|
|
}
|
|
return query.Trim();
|
|
}
|
|
|
|
public DBQuery update(string table)
|
|
{
|
|
this._type = QTypes.Update;
|
|
this._update = new DatabaseQueryElement("UPDATE", table);
|
|
return this;
|
|
}
|
|
|
|
public DBQuery set(string conditions, string glue=",")
|
|
{
|
|
if (this._set == null)
|
|
{
|
|
glue = glue.ToUpper();
|
|
this._set = new DatabaseQueryElement("SET", conditions, "\n\t" + glue + " ");
|
|
}
|
|
else
|
|
{
|
|
this._set.append(conditions);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public DBQuery insert(string table, bool incrementField = false)
|
|
{
|
|
this._type = QTypes.Insert;
|
|
this._insert = new DatabaseQueryElement("INSERT INTO", table);
|
|
this._autoIncrementField = incrementField;
|
|
return this;
|
|
}
|
|
|
|
public DBQuery columns(string columns)
|
|
{
|
|
if (this._columns == null)
|
|
{
|
|
this._columns = new DatabaseQueryElement("()", columns);
|
|
}
|
|
else
|
|
{
|
|
this._columns.append(columns);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public DBQuery values(string values)
|
|
{
|
|
if (this._values == null)
|
|
{
|
|
this._values = new DatabaseQueryElement("()", values, "),(");
|
|
}
|
|
else
|
|
{
|
|
this._values.append(values);
|
|
}
|
|
return this;
|
|
}
|
|
public DBQuery join(string type, string conditions)
|
|
{
|
|
if (this._join == null)
|
|
{
|
|
this._join = new List<DatabaseQueryElement>();
|
|
}
|
|
this._join.Add(new DatabaseQueryElement(type.ToUpper() + " JOIN", conditions));
|
|
return this;
|
|
}
|
|
public DBQuery outerJoin(string condition)
|
|
{
|
|
this.join("OUTER", condition);
|
|
return this;
|
|
}
|
|
public DBQuery rightJoin(string condition)
|
|
{
|
|
this.join("RIGHT", condition);
|
|
return this;
|
|
}
|
|
public DBQuery innerJoin(string condition)
|
|
{
|
|
this.join("INNER", condition);
|
|
return this;
|
|
}
|
|
public DBQuery leftJoin(string condition)
|
|
{
|
|
this.join("LEFT", condition);
|
|
return this;
|
|
}
|
|
public DBQuery group(string columns)
|
|
{
|
|
if (this._group == null)
|
|
{
|
|
this._group = new DatabaseQueryElement("GROUP BY", columns);
|
|
}
|
|
else
|
|
{
|
|
this._group.append(columns);
|
|
}
|
|
return this;
|
|
}
|
|
public DBQuery having(string conditions, string glue = "AND")
|
|
{
|
|
if (this._having == null)
|
|
{
|
|
glue = glue.ToUpper();
|
|
this._having = new DatabaseQueryElement("HAVING", conditions, " " + glue + " ");
|
|
}
|
|
else
|
|
{
|
|
this._having.append(conditions);
|
|
}
|
|
return this;
|
|
}
|
|
public DBQuery order(string columns)
|
|
{
|
|
if (this._order == null)
|
|
{
|
|
this._order = new DatabaseQueryElement("ORDER BY", columns);
|
|
}
|
|
else
|
|
{
|
|
this._order.append(columns);
|
|
}
|
|
return this;
|
|
}
|
|
public DBQuery union(string query, bool distinct = false, string glue = "")
|
|
{
|
|
|
|
if (this._order != null)
|
|
{
|
|
this.clear(QTypes.Order);
|
|
}
|
|
string name;
|
|
if (distinct)
|
|
{
|
|
name = "UNION DISTINCT ()";
|
|
glue = ")\nUNION DISTINCT (";
|
|
}
|
|
else
|
|
{
|
|
glue = ")\nUNION (";
|
|
name = "UNION ()";
|
|
}
|
|
if (this._union == null)
|
|
{
|
|
this._union = new DatabaseQueryElement(name, query, glue);
|
|
}
|
|
else
|
|
{
|
|
glue = "";
|
|
this._union.append(query);
|
|
}
|
|
return this;
|
|
}
|
|
public DBQuery delete(string table)
|
|
{
|
|
this._type = QTypes.Delete;
|
|
this._delete = new DatabaseQueryElement("DELETE", null);
|
|
this.from(table);
|
|
return this;
|
|
}
|
|
public DBQuery clear(QTypes clause = QTypes.None)
|
|
{
|
|
switch (clause)
|
|
{
|
|
case QTypes.Select:
|
|
this._select = null;
|
|
this._type = QTypes.None;
|
|
break;
|
|
|
|
case QTypes.Delete:
|
|
this._delete = null;
|
|
this._type = QTypes.None;
|
|
break;
|
|
|
|
case QTypes.Update:
|
|
this._update = null;
|
|
this._type = QTypes.None;
|
|
break;
|
|
|
|
case QTypes.Insert:
|
|
this._insert = null;
|
|
this._type = QTypes.None;
|
|
this._autoIncrementField = false;
|
|
break;
|
|
|
|
case QTypes.From:
|
|
this._from = null;
|
|
break;
|
|
|
|
case QTypes.Join:
|
|
this._join = null;
|
|
break;
|
|
|
|
case QTypes.Set:
|
|
this._set = null;
|
|
break;
|
|
|
|
case QTypes.Where:
|
|
this._where = null;
|
|
break;
|
|
|
|
case QTypes.Group:
|
|
this._group = null;
|
|
break;
|
|
|
|
case QTypes.Having:
|
|
this._having = null;
|
|
break;
|
|
|
|
case QTypes.Order:
|
|
this._order = null;
|
|
break;
|
|
|
|
case QTypes.Columns:
|
|
this._columns = null;
|
|
break;
|
|
|
|
case QTypes.Values:
|
|
this._values = null;
|
|
break;
|
|
|
|
case QTypes.Limit:
|
|
this._limit = null;
|
|
break;
|
|
|
|
default:
|
|
this._type = QTypes.None;
|
|
this._select = null;
|
|
this._delete = null;
|
|
this._update = null;
|
|
this._insert = null;
|
|
this._from = null;
|
|
this._join = null;
|
|
this._set = null;
|
|
this._where = null;
|
|
this._group = null;
|
|
this._having = null;
|
|
this._order = null;
|
|
this._columns = null;
|
|
this._values = null;
|
|
this._autoIncrementField = false;
|
|
this._limit = null;
|
|
break;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public DBQuery limit(int start, int limit)
|
|
{
|
|
this._limit = new DatabaseQueryElement("LIMIT", start + "," + limit);
|
|
return this;
|
|
}
|
|
}
|
|
}
|