internal
class
DbOper<T> :IDbPhysiceOper<T>, IDisposable where T :
new
()
{
internal IHelper db;
internal StringBuilder where;
internal StringBuilder select;
internal StringBuilder orderby;
internal List<IDataParameter> ps;
internal StringBuilder sqlinfo;
internal int index = 0;
internal int size = OrmGlobal.PageSize;
private
DbOper(IHelper h, StringBuilder w, StringBuilder s, StringBuilder
or
, List<IDataParameter> p,StringBuilder sql)
{
db = h;
where = w;
select = s;
orderby =
or
;
sqlinfo = sql;
ps = p;
}
internal DbOper(DbInfo info)
{
{
db =
new
Helper.Mssql(info.DbConntion);
}
else
if
(info.DbType.Equals(
"msmars"
))
{
db =
new
Helper.MsMars(info.DbConntion);
}
else
if
(info.DbType.Equals(
"mysql"
))
{
db =
new
Helper.Mysql(info.DbConntion);
}
where =
new
StringBuilder();
select =
new
StringBuilder();
orderby =
new
StringBuilder();
sqlinfo =
new
StringBuilder();
ps =
new
List<IDataParameter>();
}
public
object Insert(T m)
{
try
{
StringBuilder fields =
new
StringBuilder();
StringBuilder values =
new
StringBuilder();
List<IDataParameter> lt =
new
List<IDataParameter>();
string tp = string.
Empty
; object o = null;
foreach
(
var
n in m.
GetType
().GetProperties())
{
if
(n.GetCustomAttributes(typeof(ExcludeColumn), false).Length > 0) {
continue
; }
if
(n.GetCustomAttributes(typeof(Key), false).Length > 0) {
continue
; }
o = n.GetValue(m,null);
if
(o == null) {
continue
; }
fields.Append(n.Name +
","
);
tp = db.ParStr(n.Name);
values.Append(tp +
","
);
lt.Add(db.Cp(tp, o));
}
if
(fields.Length > 0) { fields.Length--; }
if
(values.Length > 0) { values.Length--; }
tp =
"INSERT INTO "
+ typeof(T).Name +
"("
+ fields.ToString() +
")VALUES("
+ values.ToString() +
") "
+ db.GetIdStr;
if
(OrmGlobal.isrecord) { Record(tp); }
object a = db.ExectueScalar(tp, lt, false);
Clear();
return
a;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
int Update(string str)
{
try
{
string tp =
"UPDATE "
+ typeof(T).Name +
" SET "
+ str + (where.Length > 0 ?
" WHERE "
+ where : string.
Empty
);
if
(OrmGlobal.isrecord) { Record(tp); }
int i = db.ExecuteQuery(tp, ps, false);
Clear();
return
i;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
int Update(T m)
{
try
{
StringBuilder sb =
new
StringBuilder();
sb.Append(
"UPDATE "
+ typeof(T).Name +
" SET "
);
List<IDataParameter> lt =
new
List<IDataParameter>();
object o = null;
foreach
(
var
n in m.
GetType
().GetProperties())
{
if
(o == null) {
continue
; }
if
(n.GetCustomAttributes(typeof(Key), false).Length > 0)
{
where.Append((where.Length > 0 ?
" AND "
: string.
Empty
) + n.Name +
"="
+ db.ParStr(n.Name));
lt.Add(db.Cp(db.ParStr(n.Name), o));
continue
;
}
sb.Append(n.Name +
"="
+ db.ParStr(n.Name) +
","
);
lt.Add(db.Cp(db.ParStr(n.Name), o));
}
if
(sb.Length > 0) { sb.Length--; }
if
(where.Length > 0) { sb.Append(
" WHERE "
+ where); }
var
sql = sb.ToString();
if
(OrmGlobal.isrecord) { Record(sql); }
int i = db.ExecuteQuery(sql, lt, false);
Clear();
return
i;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
int
Delete
()
{
try
{
string sql =
"DELETE FROM "
+ typeof(T).Name + (where.Length > 0 ?
" WHERE "
+ where : string.
Empty
);
if
(OrmGlobal.isrecord) { Record(sql); }
int i = db.ExecuteQuery(sql, ps, false);
Clear();
return
i;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
IDbOper<T> Select(string sl)
{
if
(string.IsNullOrEmpty(sl)) {
return
this; }
select.Append((select.Length > 0 ?
","
: string.
Empty
) + sl);
return
this;
}
public
IDbOper<T> Select(Expression<Func<T, object>> sl)
{
string tp=null;
using (
var
tp1 =
new
LinqVisitor())
{
tp=tp1.VisitNew(sl.Body
as
NewExpression);
}
return
Select(tp);
}
public
IDbOper<T> Where(Dictionary<string, object> dic)
{
if
(dic == null || dic.
Count
== 0) {
return
this; }
var
sb =
new
StringBuilder(); string tp;
foreach
(
var
n in dic)
{
if
(sb.Length > 0) { sb.Append(
" AND "
); }
sb.Append(n.Key);
if
(n.Value is string)
{
tp = n.Value.ToString();
if
(tp.Substring(tp.Length - 1, 1) ==
"*"
)
{
sb.Append(
" LIKE "
);
tp = tp.Substring(0, tp.Length - 1) +
"%"
;
}
else
{ sb.Append(
"="
); }
ps.Add(db.Cp(db.ParStr(n.Key), tp));
}
else
{
sb.Append(
"="
);
ps.Add(db.Cp(db.ParStr(n.Key), n.Value));
}
sb.Append(db.ParStr(n.Key));
}
Where(sb.ToString());
return
this;
}
public
IDbOper<T> Where(string sl)
{
if
(string.IsNullOrEmpty(sl)) {
return
this; }
where.Append((where.Length > 0 ?
" AND "
: string.
Empty
) + sl);
return
this;
}
public
IDbOper<T> Where(Expression<Func<T, bool>> sl)
{
List<object> tp=null;
{
tp = tp1.Visit(sl)
as
List<object>;
StringBuilder sb =
new
StringBuilder(); string s = string.
Empty
;
for
(int i = 0; i < tp.
Count
; i += 4)
{
s = db.ParStr(tp[i].ToString());
sb.Append(tp[i].ToString() + tp[i + 1].ToString() + s);
if
(i + 4 < tp.
Count
) { sb.Append(tp[i + 3]); }
ps.Add(db.Cp(s, tp[i + 2]));
}
Where(sb.ToString());
}
return
this;
}
public
IDbOper<T> Orderby(string orby)
{
if
(string.IsNullOrEmpty(orby)) {
return
this; }
orderby.Append((orderby.Length > 0 ?
","
: string.
Empty
) + orby);
return
this;
}
public
IDbOper<T> Orderby(Dictionary<string, string> dic)
{
if
(dic.
Count
== 0) {
return
this; }
StringBuilder sb =
new
StringBuilder();
foreach
(
var
n in dic.Keys)
{
if
(string.Compare(
"DESC"
,dic[n],true)!=0 && string.Compare(
"ASC"
,dic[n],true)!=0){
continue
;}
sb.Append(n +
" "
+ dic[n] +
","
);
}
if
(sb.Length > 0) { sb.Length--; }
Orderby(sb.ToString());
return
this;
}
public
IDbOper<T> Index(int i) {
if
(i > 0) { index = i; }
return
this; }
public
IDbOper<T> Size(int i) {
if
(i > 0) { size = i; }
return
this; }
public
void BegTran() { db.BegTran(); }
public
void RollBack() { db.RollBack(); }
public
void Commit() { db.Commit(); }
public
void Clear()
{
where.Length = 0; select.Length = 0; orderby.Length = 0; ps.Clear(); index = 0; size = OrmGlobal.size;
}
public
M ToObj<M>(Func<IDataReader, M> func, string sql)
{
try
{
if
(OrmGlobal.isrecord) { Record(sql); }
var
rd = db.ExectueReader(sql, ps, false);
M t = func(rd);
rd.Close(); Clear();
return
t;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
List<T> ToList()
{
string sql = GetSql();
return
ToObj<List<T>>(rd => ToList(rd),sql);
}
{
var
lt =
new
List<T>();
var
set = DelegateExpr.SetMethod(typeof(T));
while
(rd.Read())
{
var
m =
new
T();
for
(
var
i = 0; i < rd.FieldCount; i++)
{
if
(rd[i] == DBNull.Value || rd[i] == null) {
continue
; }
set(m, rd.GetName(i).ToLower(), rd[i]);
}
lt.Add(m);
}
return
lt;
}
public
string GetSql()
{
return
db.CreateSql(select.ToString(), typeof(T).Name, where.ToString(), orderby.ToString(), size, index);
}
public
IDbOper<M> ToOper<M>() where M:
new
()
{
Clear();
return
new
DbOper<M>(db,where,select,orderby,ps,sqlinfo);
}
public
int
Count
()
{
try
{
string sql =
"SELECT COUNT(*) FROM "
+ typeof(T).Name + (where.Length > 0 ?
" WHERE "
+ where : string.
Empty
);
if
(OrmGlobal.RecordLog) { Record(sql); }
int i= (int)db.ExectueScalar(sql, ps, false);
Clear();
return
i;
}
catch
{
OrmGlobal.DoErr(sqlinfo.ToString());
throw
;
}
}
public
int DoCommand(string sql,bool issp)
{
int i=db.ExecuteQuery(sql,ps,issp);
Clear();
return
i;
}
public
void Dispose()
{
where = null; select = null; orderby = null; db.Dispose(); ps = null; sqlinfo = null;
GC.SuppressFinalize(this);
}
public
T First()
{
var
lt=Size(1).Index(1).ToList();
if
(lt.
Count
> 0) {
return
lt[0]; }
return
default
(T);
}
~DbOper()
{
Dispose();
}
}