public override IEnumerable <IRow> Extract(IUnstructuredReader input, IUpdatableRow output) { using (var streamReader = new StreamReader(input.BaseStream)) { // assumes each line is an independent json object. var recordLine = streamReader.ReadLine(); while (!string.IsNullOrEmpty(recordLine)) { Tweet tweet = Newtonsoft.Json.JsonConvert.DeserializeObject <Tweet> (recordLine); output.Set <string> ("tweetText", tweet.text); output.Set <string> ("tweetId", tweet.id_str); output.Set <string> ("timestampMs", tweet.timestamp_ms); output.Set <string> ("language", tweet.id_str); SqlArray <string> hashtags = new SqlArray <string> (tweet.entities.hashtags.Select(t => t.text)); SqlArray <string> usermentions = new SqlArray <string> (tweet.entities.user_mentions.Select(t => t.screen_name)); output.Set <SqlArray <string> > ("hashTags", hashtags); output.Set <SqlArray <string> > ("userMentions", usermentions); yield return(output.AsReadOnly()); recordLine = streamReader.ReadLine(); } } yield break; }
// void OutputValueAtCol_I(string c, int i, IUpdatableRow outputrow) // // Helper function that takes the string value c and puts it into the column at position i in the output row. // The value will be cast to the expected type of the column. private void OutputValueAtCol_I(string c, int i, IUpdatableRow outputrow) { ISchema schema = outputrow.Schema; if (schema[i].Type == typeof(SqlMap <string, string>)) { c = DriverFunctions.RemoveOptionalQuotes(c); SqlMap <string, string> scopeMap = String.IsNullOrEmpty(c) ? null : DriverFunctions.ReadStringMap(c, this._map_item_delim, this._map_kv_delim); outputrow.Set <SqlMap <string, string> >(i, scopeMap); } else if (schema[i].Type == typeof(SqlArray <int>)) { c = DriverFunctions.RemoveOptionalQuotes(c); SqlArray <int> scopeArray = String.IsNullOrEmpty(c) ? null : DriverFunctions.ReadIntArray(c, this._array_item_delim); outputrow.Set <SqlArray <int> >(i, scopeArray); } else if (schema[i].Type == typeof(int)) { int num = Convert.ToInt32(c); outputrow.Set <int>(i, num); } else if (schema[i].Type == typeof(int?)) { int?num2 = (c == "") ? null : new int?(Convert.ToInt32(c)); outputrow.Set <int?>(i, num2); } else if (schema[i].Type == typeof(long)) { long num3 = Convert.ToInt64(c); outputrow.Set <long>(i, num3); } else if (schema[i].Type == typeof(long?)) { long?num4 = (c == "") ? null : new long?(Convert.ToInt64(c)); outputrow.Set <long?>(i, num4); } else if (schema[i].Type == typeof(DateTime)) { DateTime dateTime = Convert.ToDateTime(c); outputrow.Set <DateTime>(i, dateTime); } else if (schema[i].Type == typeof(DateTime?)) { DateTime?dateTime2 = (c == "") ? null : new DateTime?(Convert.ToDateTime(c)); outputrow.Set <DateTime?>(i, dateTime2); } else if (schema[i].Type == typeof(string)) { string text = DriverFunctions.RemoveOptionalQuotes(c); outputrow.Set <string>(i, text); } else { outputrow.Set <string>(i, c); } }
static public int TestMovieInArr(Int32 Movie, SqlArray <int> MovieList) { foreach (int x in MovieList) { if (x == Movie) { return(1); } } return(0); }
protected override SqlArray VisitSqlArray(SqlArray sqlArray) { for (int i = 0, c = sqlArray.Items.Count; i < c; i++) { var item = sqlArray.Items[i] as SqlNode; if (i > 0) { _sql.Append(", "); } this.Visit(item); } return(sqlArray); }
public void SqlArrayCloneTest() { SqlArray <int> a = SqlDml.Array(new int[] { 1, 2, 4 }); SqlArray <int> aClone = (SqlArray <int>)a.Clone(); Assert.AreNotEqual(a, aClone); Assert.IsTrue(a.Values != aClone.Values); Assert.AreEqual(a.Values.Length, aClone.Values.Length); for (int i = 0, l = a.Values.Length; i < l; i++) { Assert.AreEqual(a.Values[i], aClone.Values[i]); } Assert.AreEqual(a.NodeType, aClone.NodeType); }
public SqlQuantifyExpressionTests() { var mock = new Mock <IContext>(); mock.SetupGet(x => x.Scope) .Returns(new ServiceContainer()); context = mock.Object; array = new SqlArray(new SqlExpression[] { SqlExpression.Constant(SqlObject.New((SqlNumber)2203.112)), SqlExpression.Constant(SqlObject.New((SqlNumber)32)), SqlExpression.Constant(SqlObject.New((SqlNumber)0.9923)) }); }
public override string Translate(SqlCompilerContext context, SqlArray node, ArraySection section) { switch (section) { case ArraySection.Entry: return("ARRAY["); case ArraySection.Exit: return("]"); case ArraySection.EmptyArray: return(string.Format("'{{}}'::{0}[]", TranslateClrType(node.ItemType))); default: throw new ArgumentOutOfRangeException("section"); } }
public void ArrayTest()//TODO: Find reason why this pattern is structured like this.(Malisa) { SqlArray <int> i = SqlDml.Array(new int[] { 1, 2 }); i.Values[0] = 10; SqlSelect select = SqlDml.Select(); select.Where = SqlDml.In(1, i); MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, select); ms.Seek(0, SeekOrigin.Begin); select = (SqlSelect)bf.Deserialize(ms); Console.WriteLine(SqlDriver.Compile(select).GetCommandText()); }
public void SqlArrayReplacingTest() { SqlArray <int> a = SqlDml.Array(new int[] { 1, 2, 4 }); SqlArray <int> aReplacing = SqlDml.Array(new int[] { 1, 2, 4, 8 }); a.ReplaceWith(aReplacing); bool passed = false; try { a.ReplaceWith(1); } catch { passed = true; } Assert.IsTrue(passed); Assert.AreNotEqual(a, aReplacing); Assert.AreEqual(a.NodeType, aReplacing.NodeType); Assert.AreEqual(a.Values, aReplacing.Values); }
/// <summary> /// 为指定的原始查询生成指定分页效果的新查询。 /// </summary> /// <param name="raw">原始查询</param> /// <param name="pagingInfo">分页信息。</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">pagingInfo</exception> /// <exception cref="System.InvalidProgramException">必须排序后才能使用分页功能。</exception> protected virtual ISqlSelect ModifyToPagingTree(SqlSelect raw, PagingInfo pagingInfo) { if (PagingInfo.IsNullOrEmpty(pagingInfo)) { throw new ArgumentNullException("pagingInfo"); } if (!raw.HasOrdered()) { throw new InvalidProgramException("必须排序后才能使用分页功能。"); } /*********************** 代码块解释 ********************************* * * 使用 ROW_NUMBER() 函数,此函数 SqlServer、Oracle 都可使用。 * 注意,这个方法只支持不太复杂 SQL 的转换。 * * 源格式: * select ...... from ...... order by xxxx asc, yyyy desc * 不限于以上格式,只要满足没有复杂的嵌套查询,最外层是一个 Select 和 From 语句即可。 * * 目标格式: * select * from (select ......, row_number() over(order by xxxx asc, yyyy desc) _rowNumber from ......) x where x._rowNumber<10 and x._rowNumber>5; **********************************************************************/ var startRow = pagingInfo.PageSize * (pagingInfo.PageNumber - 1) + 1; var endRow = startRow + pagingInfo.PageSize - 1; var innerSelect = new SqlSelect(); var selection = new SqlArray(); if (raw.Selection != null) { selection.Items.Add(raw.Selection); } selection.Items.Add(new SqlNodeList { new SqlLiteral { FormattedSql = "row_number() over (" }, raw.OrderBy, new SqlLiteral { FormattedSql = ") _rowNumber" } }); innerSelect.Selection = selection; var subSelect = new SqlSubSelect { Select = innerSelect, Alias = "x" }; var rowNumberColumn = new SqlTree.SqlColumn { Table = subSelect, ColumnName = "_rowNumber" }; var pagingSelect = new SqlSelect(); pagingSelect.From = subSelect; pagingSelect.Where = new SqlTree.SqlBinaryConstraint { Left = new SqlTree.SqlColumnConstraint { Column = rowNumberColumn, Operator = SqlColumnConstraintOperator.GreaterEqual, Value = startRow }, Opeartor = SqlBinaryConstraintType.And, Right = new SqlTree.SqlColumnConstraint { Column = rowNumberColumn, Operator = SqlColumnConstraintOperator.LessEqual, Value = endRow } }; return(pagingSelect); }
public void Visit(SqlArray node) { }
private async Task <SqlExpression> IsArrayAny(SqlExpressionType opType, SqlObject value, SqlArray array, IContext context) { foreach (var item in array) { var itemValue = await ItemValue(item, context); var result = Relational(opType, value, itemValue); if (result.IsUnknown) { return(Constant(SqlObject.Unknown)); } if (result.IsTrue) { return(Constant(SqlObject.Boolean(true))); } } return(Constant(SqlObject.Boolean(false))); }
// string WriteQuotedIntArray(SqlArray<int> a, string array_item_delim = ",") // // transforms a SQL.ARRAY<int> into a quoted string using the provided array item delimiter. public static string WriteQuotedIntArray(SqlArray <int> a, string array_item_delim = ",") { return("\"" + string.Join <int>(array_item_delim, a) + "\""); }
// IRow Process(IRow input, IUpdatableRow output) // // Actual implementatoin of the user-defined processor. Overwrites the Process method of IProcessor. public override IRow Process(IRow input, IUpdatableRow output) { List <string> list = new List <string>(); foreach (var current in input.Schema) { if (current.Type.IsGenericType && current.Type.GetGenericTypeDefinition() == typeof(SqlMap) && current.Type.GetGenericArguments()[0] == typeof(string)) { list.Add(current.Name); } } Dictionary <string, ArrayList> maps_to_be_changed = new Dictionary <string, ArrayList>(); foreach (var current2 in output.Schema) { bool flag = list.Contains(current2.Name); if (-1 < input.Schema.IndexOf(current2.Name) && !flag) { output.Set <object>(current2.Name, input.Get <object>(current2.Name)); } else if (!flag) { foreach (string current3 in list) { SqlMap <string, string> sqlMap = input.Get <SqlMap <string, string> >(current3); SqlArray <string> sqlArray = null; List <string> list2 = null; if (sqlMap != null) { sqlArray = sqlMap.Keys; if (sqlMap.Values != null) { list2 = sqlMap.Values.ToList <string>(); } } int num = (sqlArray == null) ? -1 : sqlArray.ToList <string>().IndexOf(current2.Name); if (num != -1) { output.Set <string>(current2.Name, list2[num]); if (maps_to_be_changed.Keys.Contains(current3)) { maps_to_be_changed[current3].Add(current2.Name); } else { maps_to_be_changed.Add(current3, new ArrayList { current2.Name }); } break; } output.Set <object>(current2.Name, current2.Type.IsValueType ? Activator.CreateInstance(current2.Type) : null); } } } using (IEnumerator <IColumn> enumerator = output.Schema.GetEnumerator()) { while (enumerator.MoveNext()) { IColumn out_col = enumerator.Current; bool flag = list.Contains(out_col.Name); if (flag) { SqlMap <string, string> sqlMap = input.Get <SqlMap <string, string> >(out_col.Name); if (maps_to_be_changed != null && maps_to_be_changed.Keys.Contains(out_col.Name)) { sqlMap = new SqlMap <string, string>( from kvp in sqlMap where !maps_to_be_changed[out_col.Name].Contains(kvp.Key) select kvp); } output.Set <SqlMap <string, string> >(out_col.Name, sqlMap); } } } return(output.AsReadOnly()); }
public virtual void Visit(SqlArray node) { }
public static string GetName(SqlArray <string> str) { return(str.FirstOrDefault() ?? "Hii"); }
// string WriteQuotedIntArray(SqlArray<int> a, string array_item_delim = ",") // // transforms a SQL.ARRAY<int> into a quoted string using the provided array item delimiter. public static string WriteQuotedIntArray(SqlArray<int> a, string array_item_delim = ",") { return "\"" + string.Join<int>(array_item_delim, a) + "\""; }