//public List<DMSource> LoadAll() //{ // LoadQuery query = QueryBuilder.Load(TABLE); // foreach (var entry in DATA_MAP.Columns) // { // query.AddProperty(entry.Key.ColumnName); // } // List<DMSource> items = new List<DMSource>(); // var cmd = db.GetCommand(query.Build()); // using (var reader = cmd.ExecuteReader()) // { // while (reader.Read()) // { // DMSource item = CreateNewItem(); // for (var i = 0; i < DATA_MAP.Columns.Count(); i++) // { // SetItem(reader.GetName(i), reader.GetString(i), DATA_MAP, item); // } // items.Add(item); // } // } // return items; //} public List <DMSource> LoadAll(Expression <Func <DMSource, bool> > exp = null) { LoadQuery query = QueryBuilder.Load(TABLE); foreach (var entry in DATA_MAP.Columns) { query.AddProperty(entry.Key.ColumnName); } if (exp != null) { BinaryExpression operation = (BinaryExpression)exp.Body; query.AddWhereFragment(operation); } List <DMSource> items = new List <DMSource>(); var cmd = db.GetCommand(query.Build()); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { DMSource item = CreateNewItem(); for (var i = 0; i < DATA_MAP.Columns.Count(); i++) { if (!reader.IsDBNull(i)) { SetItem(reader.GetName(i), reader.GetString(i), DATA_MAP, item); } } items.Add(item); } } return(items); }
public DMSource LoadObject(int targetId) { LoadQuery query = QueryBuilder.Load(GetItem <TableItemAttribute>(typeof(DMSource))); ((LoadQuery)query).AddWhereFragment(targetId); var targetMap = new TableDataMap(typeof(DMSource)); foreach (var entry in targetMap.DataColumns) { query.AddProperty(entry.Key.ColumnName); } DMSource item = CreateNewItem(); var cmd = db.GetCommand(query.Build()); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { for (var i = 0; i < targetMap.DataColumns.Count(); i++) { SetItem(reader.GetName(i), reader.GetString(i), targetMap, item); } } } return(item); }
private void RunLoadQuery(LoadQuery lq) { Option <TermError> te = lq.Check(); if (te.HasValue()) { throw new ScrapeQLRunnerException(te.Value().ErrorMessage); } else { try { var x = from StringLiteralToken source in lq.From from IdentifierToken ident in lq.As select Tuple.Create(source, ident); var web = new HtmlWeb(); foreach (Tuple <StringLiteralToken, IdentifierToken> pair in x) { var uri = new Uri(pair.Item1.Value.AsString()); var doc = web.Load(uri.AbsoluteUri); Scope[pair.Item2.Value.AsString()] = doc.DocumentNode; } } catch (Exception e) { throw new ScrapeQLRunnerException("Error while running Load Query", e); } } }
public List <ConnectionDM> LoadConnections(Expression <Func <ConnectionDM, bool> > exp, DMSource model, ConnectionDataMap connection) { LoadQuery query = new LoadQuery(connection.SchemaName, connection.TableName); BinaryExpression operation = (BinaryExpression)exp.Body; query.AddWhereFragment(operation, DataObject: connection); //query.AddProperty("ID"); //query.AddProperty(connection.ParentID); //query.AddProperty(connection.ChildID); var targetMap = new TableDataMap(typeof(ConnectionDM)); Dictionary <string, string> currentPropertyMap = new Dictionary <string, string>(); foreach (var entry in targetMap.IDColumns) { var prop = entry.Value; if (Attribute.IsDefined(prop, typeof(LookupPropertyAttribute))) { var idData = entry.Value.GetCustomAttribute(typeof(IDColumnAttribute)) as IDColumnAttribute; var propertyName = idData.ColumnName; var value = (string)connection.GetType().GetProperty(propertyName).GetValue(connection); query.AddProperty(value); currentPropertyMap[value] = propertyName; } else { query.AddProperty(entry.Key.ColumnName); } } List <ConnectionDM> items = new List <ConnectionDM>(); var cmd = db.GetCommand(query.Build()); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { ConnectionDM item = CreateNewItem <ConnectionDM>(); for (var i = 0; i < DATA_MAP.Columns.Count(); i++) { var itemName = reader.GetName(i); if (currentPropertyMap.ContainsKey(itemName)) { itemName = currentPropertyMap[itemName]; } SetItem(itemName, reader.GetString(i), targetMap, item); } items.Add(item); } } return(items); }
public void TestGenerateLoadWithID() { var schemeName = TestingTools.RandomString; var tableName = TestingTools.RandomString; var propertyName = TestingTools.RandomString; var propertyValue = TestingTools.RandomString; var id = TestingTools.RandomInt; LoadQuery query = new LoadQuery(schemeName, tableName); query.AddProperty(propertyName); query.AddWhereFragment(id); var result = query.Build(); var expected = String.Format("SELECT \"{0}\" FROM \"{1}\".\"{2}\" WHERE \"ID\"={3};", propertyName, schemeName, tableName, id); Assert.IsNotNull(result); Assert.AreEqual(expected, result); }
public void TestGenerateLoadWithWhere() { var schemeName = TestingTools.RandomString; var tableName = TestingTools.RandomString; var propertyName = TestingTools.RandomString; var propertyValue = TestingTools.RandomString; var id = TestingTools.RandomInt; LoadQuery query = new LoadQuery(schemeName, tableName); query.AddProperty(propertyName); Expression <Func <ConnectionDM, bool> > exp = (item) => item.ChildID == id; query.AddWhereFragment((BinaryExpression)exp.Body); var result = query.Build(); var expected = String.Format("SELECT \"{0}\" FROM \"{1}\".\"{2}\" WHERE \"ChildID\" = '{3}';", propertyName, schemeName, tableName, id); Assert.IsNotNull(result); Assert.AreEqual(expected, result); }