/// <summary> /// /// </summary> /// <param name="dsid">数据源id或progid</param> public LibDSContext(string dsid) { this._dsid = dsid; CachHelp cach = new CachHelp(); _ds = cach.GetCach(string.Format("{0}_{1}", dsid, SysConstManage.TBSchemasuffix)) as LibDataSource; if (_ds == null) { _ds = SDPCRL.COM.ModelManager.ModelManager.GetDataSource(dsid); if (_ds == null) { LibFormPage form = SDPCRL.COM.ModelManager.ModelManager.GetFormSource(dsid); _ds = SDPCRL.COM.ModelManager.ModelManager.GetDataSource(form.DSID); } //101:数据源:{0} 不存在 if (_ds == null) { throw new LibExceptionBase(101, dsid); } //100:没有表结构 if (_ds.DefTables == null) { throw new LibExceptionBase(100); } cach.AddCachItem(string.Format("{0}_{1}", dsid, SysConstManage.TBSchemasuffix), _ds, DateTimeOffset.Now.AddMinutes(30)); } InitialContext(); }
/// <summary>用于取整个数据源的查询语法</summary> /// <param name="where">指主表的条件</param> /// <returns></returns> public string GetSQL(WhereObject where) { StringBuilder sql = new StringBuilder(); if (string.IsNullOrEmpty(this._id)) { return(string.Empty); } StringBuilder fields = null; LibFormPage form = ModelManager.GetFormSource(this._id); var datasourse = ModelManager.GetDataSource(form.DSID); if (datasourse != null) { foreach (LibDefineTable deftb in datasourse.DefTables) { if (deftb.TableStruct == null) { continue; } foreach (LibDataTableStruct tbstruct in deftb.TableStruct) { if (!tbstruct.Ignore) { continue; } fields = new StringBuilder(); char tbaliasnm = LibSysUtils.ToCharByTableIndex(tbstruct.TableIndex); foreach (LibField f in tbstruct.Fields) { if (fields.Length > 0) { fields.Append(SysConstManage.Comma); } fields.AppendFormat("{0}.{1}", tbaliasnm, string.IsNullOrEmpty(f.AliasName) ? f.Name : f.AliasName); } if (fields.Length > 0) { if (string.IsNullOrEmpty(where.WhereFormat)) { sql.AppendFormat("select {0} from {1} {2}", fields.ToString(), tbstruct.Name, tbaliasnm); } else { sql.AppendFormat("select {0} from {1} {2} where {3}", fields.ToString(), tbstruct.Name, tbaliasnm, where.WhereFormat); } sql.AppendLine(); } } } } return(sql.ToString()); }
public string GetSQL(string tableNm, string[] fields, WhereObject where, bool IsJoinRelateTable = true, bool IsJoinFromSourceField = true) { StringBuilder builder = new StringBuilder(); builder.Append(ResFactory.ResManager.SQLSelect); if (fields != null) { foreach (string field in fields) { if (builder.Length != ResFactory.ResManager.SQLSelect.Length) { builder.Append(SysConstManage.Comma); } builder.AppendFormat(" {0}", field); } } if (string.IsNullOrEmpty(this._id)) { if (builder.Length == ResFactory.ResManager.SQLSelect.Length) { builder.AppendFormat(" {0}", SysConstManage.Asterisk); } builder.AppendFormat(" {0}", ResFactory.ResManager.SQLFrom); builder.AppendFormat(" {0}", tableNm); } else { LibDataSource ds = null; if (this._mark) { ds = ModelManager.GetDataSource(this._id); } else { LibFormPage form = ModelManager.GetFormSource(this._id); ds = ModelManager.GetDataSource(form.DSID); } if (ds != null) { DoGetSQL(builder, tableNm, ds, where, null, false, IsJoinRelateTable, IsJoinFromSourceField); } } if (where != null && !string.IsNullOrEmpty(where.WhereFormat)) { return(string.Format("EXEC sp_executesql N'{0} where {1}',{2}", builder.ToString(), where.WhereFormat, where.ValueTostring)); } return(string.Format("EXEC sp_executesql N'{0}'", builder.ToString())); }
private void InternalGetSQL(StringBuilder builder, string tableNm, string[] fields, string[] sumaryfields, WhereObject where, bool ispage, bool IsJoinRelateTable, bool IsJoinFromSourceField, bool IsRpt) { builder.Append(ResFactory.ResManager.SQLSelect); if (fields != null) { foreach (string field in fields) { if (builder.Length != ResFactory.ResManager.SQLSelect.Length) { builder.Append(SysConstManage.Comma); } builder.AppendFormat(" {0}", field); } } if (string.IsNullOrEmpty(this._id)) { if (builder.Length == ResFactory.ResManager.SQLSelect.Length) { builder.AppendFormat(" {0}", SysConstManage.Asterisk); } //builder.AppendFormat("{0}ROW_NUMBER()OVER(order by A.BillNo) as rownumber"); builder.AppendFormat(" {0}", ResFactory.ResManager.SQLFrom); builder.AppendFormat(" {0}", tableNm); } else { LibDataSource ds = null; if (this._mark) { ds = ModelManager.GetDataSource(this._id); } else { LibFormPage form = ModelManager.GetFormSource(this._id); ds = ModelManager.GetDataSource(form.DSID); } if (ds != null) { DoGetSQL(builder, tableNm, ds, where, sumaryfields, ispage, IsJoinRelateTable, IsJoinFromSourceField, IsRpt); } } }