private static void BuildFields(XTableSchema schema) { string connectionName = schema.ConnectionName; string selectSql = schema.SelectCommand; XDataBase xdb = XDatabaseFactory.Instance.GetDataBase(connectionName); XSql xSql = new XSql(selectSql); string tableName = xSql.GetFrom(); // schema.Fields.Clear(); SchemaList <FieldSchema> tmpfs = new SchemaList <FieldSchema>(); DbConnection con = xdb.GetConnection(); DataTable netTable = new DataTable(); DbDataAdapter da = xdb.GetAdapter(); DbCommand cmd = con.CreateCommand(); cmd.CommandText = selectSql; string[] paramNames = xSql.GetParamNames(); for (int i = 0; i < paramNames.Count(); i++) { string pName = paramNames[i]; if (!string.IsNullOrEmpty(pName)) { pName = pName.Trim(); } SqlParameter p = new SqlParameter(); p.ParameterName = "@" + pName; //p.SourceColumn = pName; p.Value = ""; cmd.Parameters.Add(p); } da.SelectCommand = cmd; da.Fill(netTable); //da.FillSchema(netTable, SchemaType.Source); foreach (DataColumn col in netTable.Columns) { FieldSchema field = new FieldSchema(); field.Id = col.ColumnName; field.DisplayName = col.Caption; // schema.Fields.Add(field); if (schema.Fields.FindItem(field.Id) == null) { schema.Fields.Add(field); } if (tmpfs.FindItem(field.Id) == null) { tmpfs.Add(field); } } //从原是表中获取,字段标题等信息; if (xSql.IsFromSourceTable(tableName)) { string keyfield = xdb.GetPrimaryKey(tableName); if (!string.IsNullOrEmpty(keyfield)) { schema.KeyField = keyfield; } List <FieldDef> fieldDefs = xdb.GetFieldInfos(tableName); for (int i = 0; i < fieldDefs.Count; i++) { FieldDef fieldDef = fieldDefs[i]; FieldSchema field = schema.Fields.FindItem(fieldDef.Name); // field.Id =; if (field != null) { field.DisplayName = fieldDef.Alias;//未用 if (string.IsNullOrEmpty(field.Title)) { field.Title = fieldDef.Alias; } if (fieldDef.IsIdentity) { field.IsAutoInc = true; } } // if (field == null) // schema.Fields.Add(field); // if (tmpfs.FindItem(field.Id) == null) // tmpfs.Add(field); } } //删除不存在的字段 for (int i = schema.Fields.Count - 1; i >= 0; i--) { FieldSchema fld = schema.Fields[i]; FieldSchema newFld = tmpfs.FindItem(fld.Id); if (newFld == null) { schema.Fields.Remove(fld); } } xdb.Close(); }
private static void BuildFields1(XTableSchema schema) { string connectionName = schema.ConnectionName; string selectSql = schema.SelectCommand; // XDataBase xdb = XDatabaseFactory.Instance.GetDataBase(connectionName); DatabaseAdmin XSql xSql = new XSql(selectSql); string tableName = xSql.GetFrom(); // schema.Fields.Clear(); SchemaList <FieldSchema> tmpfs = new SchemaList <FieldSchema>(); if (xSql.IsFromSourceTable(tableName)) { string keyfield = xdb.GetPrimaryKey(tableName); if (!string.IsNullOrEmpty(keyfield)) { schema.KeyField = keyfield; } List <FieldDef> fieldDefs = xdb.GetFieldInfos(tableName); for (int i = 0; i < fieldDefs.Count; i++) { FieldSchema field = new FieldSchema(); FieldDef fieldDef = fieldDefs[i]; field.Id = fieldDef.Name; field.DisplayName = fieldDef.Alias;//未用 if (string.IsNullOrEmpty(field.Title)) { field.Title = fieldDef.Alias; } if (schema.Fields.FindItem(field.Id) == null) { schema.Fields.Add(field); } if (tmpfs.FindItem(field.Id) == null) { tmpfs.Add(field); } } } else { DbConnection con = xdb.GetConnection(); DataTable netTable = new DataTable(); DbDataAdapter da = xdb.GetAdapter(); DbCommand cmd = con.CreateCommand(); xSql.ParamGetter = delegate(string name) { if (!name.StartsWith("@")) { name = "@" + name; } if (schema.QueryParams.FindItem(name) != null) { XQueryParameterSchema paramSch = schema.QueryParams.GetItem(name); return(paramSch.DefaultValue); } else { return(null); } }; cmd.CommandText = xSql.RelaceParamValue(); da.SelectCommand = cmd; da.Fill(netTable); foreach (DataColumn col in netTable.Columns) { FieldSchema field = new FieldSchema(); field.Id = col.ColumnName; field.DisplayName = col.Caption; // schema.Fields.Add(field); if (schema.Fields.FindItem(field.Id) == null) { schema.Fields.Add(field); } if (tmpfs.FindItem(field.Id) == null) { tmpfs.Add(field); } } } for (int i = schema.Fields.Count - 1; i >= 0; i--) { FieldSchema fld = schema.Fields[i]; FieldSchema newFld = tmpfs.FindItem(fld.Id); if (newFld == null) { schema.Fields.Remove(fld); } } xdb.Close(); }
private static void BuildFields(DataSourceSchema schema) { string connectionName = schema.ConnectionName; string selectSql = schema.SelectCommand.CommandText; DatabaseAdmin dbAdmin = DatabaseAdmin.getInstance(connectionName); XSql xSql = new XSql(selectSql); string tableName = xSql.GetFrom(); // schema.Fields.Clear(); SchemaList <FieldSchema> tmpfs = new SchemaList <FieldSchema>(); DbConnection con = dbAdmin.Database.CreateConnection(); try { DataTable netTable = new DataTable(); DbDataAdapter da = dbAdmin.Database.GetDataAdapter(); DbCommand cmd = con.CreateCommand(); cmd.CommandText = selectSql; string[] paramNames = xSql.GetParamNames(); for (int i = 0; i < paramNames.Count(); i++) { string pName = paramNames[i]; if (!string.IsNullOrEmpty(pName)) { pName = pName.Trim(); } SqlParameter p = new SqlParameter(); p.ParameterName = "@" + pName; //p.SourceColumn = pName; p.Value = ""; cmd.Parameters.Add(p); } da.SelectCommand = cmd; da.Fill(netTable); //da.FillSchema(netTable, SchemaType.Source); foreach (DataColumn col in netTable.Columns) { FieldSchema field = new FieldSchema(); field.Id = col.ColumnName; field.DisplayName = col.Caption; // schema.Fields.Add(field); if (schema.Fields.FindItem(field.Id) == null) { schema.Fields.Add(field); } if (tmpfs.FindItem(field.Id) == null) { tmpfs.Add(field); } } //从原是表中获取,字段标题等信息; if (xSql.IsFromSourceTable(tableName)) { List <string> pks = dbAdmin.GetPrimaryKeys(tableName); schema.KeyField = JsonConvert.SerializeObject(pks); //if (pks.Count > 0) // keyfield = pks[0]; //if (!string.IsNullOrEmpty(keyfield)) // schema.KeyField = keyfield; var tbName = tableName.Trim('[', ']'); List <FieldDef> fieldDefs = dbAdmin.getTableDef(tbName).FieldDefs; for (int i = 0; i < fieldDefs.Count; i++) { FieldDef fieldDef = fieldDefs[i]; FieldSchema field = schema.Fields.FindItem(fieldDef.Name); // field.Id =; if (field != null) { field.IsKey = pks.Contains(field.Id); field.DisplayName = fieldDef.Alias;//未用 if (string.IsNullOrEmpty(field.Title)) { field.Title = fieldDef.Alias; } if (fieldDef.IsIdentity) { field.IsAutoInc = true; } } // if (field == null) // schema.Fields.Add(field); // if (tmpfs.FindItem(field.Id) == null) // tmpfs.Add(field); } } //删除不存在的字段 for (int i = schema.Fields.Count - 1; i >= 0; i--) { FieldSchema fld = schema.Fields[i]; FieldSchema newFld = tmpfs.FindItem(fld.Id); if (newFld == null) { schema.Fields.Remove(fld); } } } finally { con.Close(); } }