private static long GetTopCount(SqlTopSpec sqlTopSpec) { if (sqlTopSpec == null) { throw new ArgumentNullException(nameof(sqlTopSpec)); } SqlScalarExpression topExpression = sqlTopSpec.TopExpresion; if (!(topExpression is SqlLiteralScalarExpression topLiteralExpression)) { throw new ArgumentException($"Expected number literal scalar expression."); } SqlLiteral sqlLiteral = topLiteralExpression.Literal; if (!(sqlLiteral is SqlNumberLiteral sqlNumberLiteral)) { throw new ArgumentException($"Expected number literal."); } if (!sqlNumberLiteral.Value.IsInteger) { throw new ArgumentException($"Expected integer literal."); } long value = Number64.ToLong(sqlNumberLiteral.Value); return(value); }
public override SqlObject VisitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context) { Contract.Requires(context != null); SqlLiteral sqlLiteral = (SqlLiteral)this.Visit(context.literal()); return(SqlLiteralScalarExpression.Create(sqlLiteral)); }
public void SqlLiteralCloneTest() { SqlLiteral <int> l = SqlDml.Literal(1); SqlLiteral <int> lClone = (SqlLiteral <int>)l.Clone(); Assert.AreNotEqual(l, lClone); Assert.AreEqual(l.Value, lClone.Value); Assert.AreEqual(l.NodeType, lClone.NodeType); }
public void BinaryExpressionTest() { SqlLiteral <int> l = SqlDml.Literal(1); bool passed = false; if (!l.IsNullReference()) { passed = true; } Assert.IsTrue(passed); if (l.IsNullReference()) { passed = false; } Assert.IsTrue(passed); }
protected override SqlLiteral VisitSqlLiteral(SqlLiteral sqlLiteral) { if (sqlLiteral.Parameters != null && sqlLiteral.Parameters.Length > 0) { sqlLiteral.FormattedSql = Regex.Replace(sqlLiteral.FormattedSql, @"\{(?<index>\d+)\}", m => { var index = Convert.ToInt32(m.Groups["index"].Value); var value = sqlLiteral.Parameters[index]; index = _sql.Parameters.Add(value); return("{" + index + "}"); }); } _sql.Append(sqlLiteral.FormattedSql); return(sqlLiteral); }
public void AddTest() { SqlLiteral <int> l1 = SqlDml.Literal(1); SqlLiteral <int> l2 = SqlDml.Literal(2); SqlBinary b = l1 + l2; Assert.AreEqual(b.NodeType, SqlNodeType.Add); b = b - ~l1; Assert.AreEqual(b.NodeType, SqlNodeType.Subtract); Assert.AreEqual(b.Right.NodeType, SqlNodeType.BitNot); SqlSelect s = SqlDml.Select(); s.Columns.Add(1, "id"); b = b / s; Assert.AreEqual(b.NodeType, SqlNodeType.Divide); Assert.AreEqual(b.Right.NodeType, SqlNodeType.SubSelect); SqlCast c = SqlDml.Cast(l1, SqlType.Decimal); Assert.AreEqual(c.NodeType, SqlNodeType.Cast); SqlFunctionCall l = SqlDml.CharLength(SqlDml.Literal("name")); b = c % l; Assert.AreEqual(b.NodeType, SqlNodeType.Modulo); Assert.AreEqual(b.Right.NodeType, SqlNodeType.FunctionCall); b = l1 * (-l2); Assert.AreEqual(b.NodeType, SqlNodeType.Multiply); Assert.AreEqual(b.Right.NodeType, SqlNodeType.Negate); SqlBatch batch = SqlDml.Batch(); SqlVariable v1 = SqlDml.Variable("v1", SqlType.Double); batch.Add(v1.Declare()); batch.Add(SqlDml.Assign(v1, 1.0)); s = SqlDml.Select(); s.Columns.Add(b, "value"); batch.Add(s); }
public void SqlLiteralReplacingTest() { SqlLiteral <int> l = SqlDml.Literal(1); SqlLiteral <int> lReplacing = SqlDml.Literal(2); l.ReplaceWith(lReplacing); bool passed = false; try { l.ReplaceWith("string"); } catch { passed = true; } Assert.IsTrue(passed); Assert.AreNotEqual(l, lReplacing); Assert.AreEqual(l.NodeType, lReplacing.NodeType); Assert.AreEqual(l.Value, lReplacing.Value); }
public override CosmosElement Visit(SqlLiteralScalarExpression scalarExpression, CosmosElement document) { SqlLiteral sqlLiteral = scalarExpression.Literal; return(sqlLiteral.Accept(SqlLiteralToCosmosElement.Singleton)); }
public virtual void Visit(SqlLiteral node) { }
public bool InternalRefresh(Table table, bool clearErrors) { if (clearErrors) { ClearLastErrors(); } EnsureExtractedCatalog(); global::Xtensive.Sql.Model.Table ormTable = this.extractedCatalog.Schemas[table.Owner.Name].Tables[table.Name]; if (ormTable != null) { var tableColumns = new List <Column>(); // load columns foreach (TableColumn column in ormTable.TableColumns) { Column newColumn = new Column(column.Name, table); newColumn.DefaultValue = string.Empty; if (column.DefaultValue != null) { SqlLiteral sqlLiteral = column.DefaultValue as SqlLiteral; if (sqlLiteral != null) { object value = sqlLiteral.GetValue(); if (value != null) { newColumn.DefaultValue = value.ToString(); } } } newColumn.MaxLength = column.DataType.Length.GetValueOrDefault(0); newColumn.IsMaxLength = column.DataType.Type == SqlType.VarCharMax || column.DataType.Type == SqlType.VarBinaryMax; // column.DataType.SqlDataType.In( // SqlDataType.NVarCharMax, SqlDataType.VarBinaryMax, SqlDataType.VarCharMax); newColumn.Precision = column.DataType.Precision.GetValueOrDefault(0); newColumn.Scale = column.DataType.Scale.GetValueOrDefault(0); newColumn.ID = -1;// column.SequenceDescriptor.Index; newColumn.IdentityIncrement = column.SequenceDescriptor != null?column.SequenceDescriptor.Increment.GetValueOrDefault(0) : 0; newColumn.IdentitySeed = column.SequenceDescriptor != null?column.SequenceDescriptor.StartValue.GetValueOrDefault(0) : 0; newColumn.IsIdentity = column.SequenceDescriptor != null; newColumn.IsNullable = column.IsNullable; newColumn.NotForReplication = false; newColumn.RowGuidCol = false; newColumn.SqlDataType = column.DataType.Type.ToString(); Type clrType; if (column.DataType.Type == SqlType.Unknown && column.DataType.TypeName.ToLower() == "timestamp") { clrType = typeof(byte[]); } else { clrType = column.DataType.Type.ToClrType(); } Type nullableItemType = Nullable.GetUnderlyingType(clrType); newColumn.ClrDataType = nullableItemType ?? clrType; tableColumns.Add(newColumn); } table.Columns = new ColumnCollection(tableColumns); // load foreign keys List <ForeignKey> foreignKeys = new List <ForeignKey>(); foreach (var key in ormTable.TableConstraints.Where(constraint => constraint is global::Xtensive.Sql.Model.ForeignKey).Cast <global::Xtensive.Sql.Model.ForeignKey>()) { ForeignKey newForeignKey = new ForeignKey(key.Name, table); //TODO: How to get 'IsEnabled' and 'NotForReplication' values ?? newForeignKey.Enabled = true; newForeignKey.NotForReplication = true; newForeignKey.OnDeleteAction = ConvertForeignKeyAction(key.OnDelete); newForeignKey.OnUpdateAction = ConvertForeignKeyAction(key.OnUpdate); //newForeignKey.SourceTable = table; List <ForeignKeyColumn> newKeyColumns = new List <ForeignKeyColumn>(); for (int i = 0; i < key.ReferencedColumns.Count; i++) { var column = key.Columns[i]; var keyColumnRef = key.ReferencedColumns[i]; //newKeyColumns.Add(new ForeignKeyColumn(column.Name, column.Value.Name, newForeignKey)); newKeyColumns.Add(new ForeignKeyColumn(column.Name, keyColumnRef.Name, newForeignKey)); } newForeignKey.Columns = new ForeignKeyColumnCollection(newKeyColumns); string referencedTableName = key.ReferencedTable.Name; string referencedTableSchema = key.ReferencedTable.Schema.Name; var referencedTable = table.Owner.Owner.GetTable(referencedTableSchema, referencedTableName); //var referencedTable = table.Owner.Tables[referencedTableName, referencedTableSchema]; //, key.ReferencedTableSchema]; if (referencedTable != null) { newForeignKey.ForeignTable = referencedTable; } else { var temporaryReferencedTable = new TemporaryReferencedTable(referencedTableName, table.Owner); newForeignKey.ForeignTable = temporaryReferencedTable; } foreignKeys.Add(newForeignKey); } table.ForeignKeys = new ForeignKeyCollection(foreignKeys); // load indexes List <Index> indices = new List <Index>(); foreach (global::Xtensive.Sql.Model.Index index in ormTable.Indexes) { Index newIndex = new Index(index.Name, table); newIndex.Primary = false; // primaryKeys.Any(item => item.Name == index.Name); newIndex.Clustered = index.IsClustered; newIndex.FillFactor = 0.0; //newIndex.Unique = newIndex.Primary ? false : index.IsUnique; newIndex.Unique = index.IsUnique; List <IndexedColumn> newIndexedColumns = new List <IndexedColumn>(); foreach (var indexedColumn in index.Columns) { //ColumnInfo columnInfo = indexedColumn.Value; newIndexedColumns.Add(new IndexedColumn(indexedColumn.Name, ConvertDirection(indexedColumn.Ascending), false, newIndex)); } foreach (var includedColumn in index.NonkeyColumns) { newIndexedColumns.Add(new IndexedColumn(includedColumn.Name, IndexOrder.Ascending, true, newIndex)); } newIndex.Columns = new IndexedColumnCollection(newIndexedColumns); indices.Add(newIndex); } var primaryKeys = ormTable.TableConstraints.Where(constraint => constraint is PrimaryKey).Cast <PrimaryKey>(); foreach (PrimaryKey primaryKey in primaryKeys) { Index newIndex = new Index(primaryKey.Name, table); newIndex.Primary = true; newIndex.Clustered = true; newIndex.Unique = true; newIndex.FillFactor = 0.0; List <IndexedColumn> newIndexedColumns = new List <IndexedColumn>(); foreach (var indexedColumn in primaryKey.Columns) { newIndexedColumns.Add(new IndexedColumn(indexedColumn.Name, IndexOrder.Ascending, false, newIndex)); } newIndex.Columns = new IndexedColumnCollection(newIndexedColumns); indices.Add(newIndex); } table.Indexes = new IndexCollection(indices); } return(true); }
public void Visit(SqlLiteral node) { }