public void TestExprTree() { Expr expr = new ExprParser("a like 'xyz' and $.count > 10 + 1").Parse(); Assert.Equal(Expr.Types.Type.Operator, expr.Type); Assert.Equal("&&", expr.Operator.Name); Assert.Equal(2, expr.Operator.Param.Count); // check left side of AND: (a like 'xyz') Expr andLeft = expr.Operator.Param[0]; Assert.Equal(Expr.Types.Type.Operator, andLeft.Type); Assert.Equal("like", andLeft.Operator.Name); Assert.Equal(2, andLeft.Operator.Param.Count); Expr identA = andLeft.Operator.Param[0]; Assert.Equal(Expr.Types.Type.Ident, identA.Type); Assert.Equal("a", identA.Identifier.Name); Expr literalXyz = andLeft.Operator.Param[1]; Assert.Equal(Expr.Types.Type.Literal, literalXyz.Type); Scalar scalarXyz = literalXyz.Literal; Assert.Equal(Scalar.Types.Type.VString, scalarXyz.Type); Assert.Equal("xyz", scalarXyz.VString.Value.ToStringUtf8()); // check right side of AND: ($.count > 10 + 1) Expr andRight = expr.Operator.Param[1]; Assert.Equal(Expr.Types.Type.Operator, andRight.Type); Assert.Equal(">", andRight.Operator.Name); Assert.Equal(2, andRight.Operator.Param.Count); Expr countDocPath = andRight.Operator.Param[0]; Assert.Equal(Expr.Types.Type.Ident, countDocPath.Type); ColumnIdentifier countId = countDocPath.Identifier; Assert.Equal(string.Empty, countId.Name); Assert.Equal(string.Empty, countId.TableName); Assert.Equal(string.Empty, countId.SchemaName); Assert.Equal(1, countId.DocumentPath.Count); Assert.Equal(DocumentPathItem.Types.Type.Member, countId.DocumentPath[0].Type); Assert.Equal("count", countId.DocumentPath[0].Value); Expr addition = andRight.Operator.Param[1]; Scalar addLeftScalar = addition.Operator.Param[0].Literal; Scalar addRightScalar = addition.Operator.Param[1].Literal; Assert.Equal(Expr.Types.Type.Operator, addition.Type); Assert.Equal("+", addition.Operator.Name); Assert.Equal(2, addition.Operator.Param.Count); Assert.Equal(Expr.Types.Type.Literal, addition.Operator.Param[0].Type); Assert.Equal(Expr.Types.Type.Literal, addition.Operator.Param[1].Type); Assert.Equal(Scalar.Types.Type.VSint, addLeftScalar.Type); Assert.Equal(Scalar.Types.Type.VSint, addRightScalar.Type); Assert.Equal(10, addLeftScalar.VSignedInt); Assert.Equal(1, addRightScalar.VSignedInt); }
public static bool Consume(Parser parser) { if (!parser.Terminal("INSERT")) { return(false); } if (!parser.Terminal("INTO")) { return(false); } if (!TableName.Consume(parser)) { return(false); } if (parser.Peek("(")) { parser.Terminal("("); if (!ColumnIdentifier.Consume(parser)) { return(false); } while (parser.Peek(",")) { parser.Terminal(","); if (!ColumnIdentifier.Consume(parser)) { return(false); } } parser.Terminal(")"); } if (!parser.Terminal("VALUES")) { return(false); } if (!parser.Terminal("(")) { return(false); } if (!InsertValue.Consume(parser)) { return(false); } while (parser.Peek(",")) { parser.Terminal(","); if (!InsertValue.Consume(parser)) { return(false); } } return(parser.Terminal(")")); }
/// <summary> /// Writes a column identifier, optionally with resolved /// names and quoted /// </summary> /// <param name="node"></param> /// <returns></returns> public void WriteColumnIdentifier(ColumnIdentifier node) { if (ResolveNames) { Writer.Write(GetResolvedColumnName(node.ColumnReference)); } else { // Fall back to original behavior base.WriteNode(node); } }
// Get query for altering user defined table type private IEnumerable <string> GetAlterQueryForUserDefinedTableType(ColumnIdentifier columnIdentifier) { var alterQuery = new List <string> { "BEGIN TRAN" }; string typeName = columnIdentifier.Table; string typeSchema = columnIdentifier.Schema; var sprocs = GetSprocsWithUserDefinedTableType(typeName, typeSchema).ToList(); var funcs = GetFuncsWithUserDefinedTableType(typeName, typeSchema).ToList(); var triggers = GetTriggersWithUserDefinedTableType(typeName, typeSchema).ToList(); // Drop functions and sprocs alterQuery.AddRange(sprocs.Select(sproc => sproc.GetDropQuery())); alterQuery.AddRange(funcs.Select(func => func.GetDropQuery())); alterQuery.AddRange(triggers.Select(trigger => trigger.GetDropQuery())); List <UserDefinedTableTypeColumnDetail> existingColumnDetails; var updatedColumnDetails = GetUpdatedColumnDetailsInUserDefinedTableType(typeName, typeSchema, columnIdentifier.Column, out existingColumnDetails); var updatedTableTypeDetails = new UserDefinedTableTypeDetail(typeSchema, typeName, updatedColumnDetails.ToList()); var existingTableTypeDetails = new UserDefinedTableTypeDetail(typeSchema, typeName, existingColumnDetails.ToList()); alterQuery.AddRange(updatedTableTypeDetails.GetAlterQuery()); BeforeSqlText.AddRange(existingTableTypeDetails.GetDefintion()); AfterSqlText.AddRange(updatedTableTypeDetails.GetDefintion()); // Recreate functions and sprocs with original definitions foreach (var sproc in sprocs) { alterQuery.AddRange(GetCreateQuery(sproc.GetDefinition(Connection))); } foreach (var func in funcs) { alterQuery.AddRange(GetCreateQuery(func.GetDefinition(Connection))); } foreach (var trigger in triggers) { alterQuery.AddRange(GetCreateQuery(trigger.GetDefinition(Connection))); } alterQuery.Add("COMMIT TRAN"); return(alterQuery); }
private static bool _1(Parser parser) { // With the table name prefix if (!parser.Terminal("[")) { return(false); } if (!TableName.Consume(parser)) { return(false); } if (!parser.Terminal("}")) { return(false); } return(parser.Terminal(".") && ColumnIdentifier.Consume(parser)); }
/** * Column identifier (or JSON path) to string. */ static string ColumnIdentifierToString(ColumnIdentifier e) { string s = QuoteIdentifier(e.Name); if (!string.IsNullOrEmpty(e.TableName)) { s = QuoteIdentifier(e.TableName) + "." + s; } if (!string.IsNullOrEmpty(e.SchemaName)) { s = QuoteIdentifier(e.SchemaName) + "." + s; } if (e.DocumentPath.Count > 0) { s = s + "$" + DocumentPathToString(e.DocumentPath); } return(s); }
/// <summary> /// Retrieves a <c>DataTable</c> object representing the column /// metadata of the given data reader's current result. /// </summary> /// <param name="reader"> /// A reader object for which to retrieve the column metadata. /// </param> /// <returns> /// A <c>DataTable</c> object representing the column metadata of the /// given data reader's current result. /// </returns> /// <exception cref="HsqlDataSourceException"> /// If a data access error occurs. /// </exception> public static DataTable CreateSchemaTable(HsqlDataReader reader) { Result result = reader.m_result; int columnCount = result.getColumnCount(); ResultMetaData metaData = result.metaData; DataTable table = CreateTable(columnCount); bool includeKeyInfo = reader.HasCommandBehavior(CommandBehavior.KeyInfo); Dictionary <ColumnIdentifier, KeyInfo> keyInfoMap = (includeKeyInfo) ? HsqlResultSetMetaData.GetKeyInfo(reader) : null; string catalogName = reader.OriginatingConnection.Database; for (int i = 0; i < columnCount; i++) { bool isAutoIncrement = metaData.isIdentity[i]; string columnName = metaData.colLabels[i]; int columnOrdinal = i; int columnSize = metaData.colSizes[i]; int numericPrecision = metaData.colSizes[i]; int numericScale = metaData.colScales[i]; bool isUnique = false; // isAutoIncrement; bool isKey = isAutoIncrement; string baseServerName = null; string baseCatalogName = catalogName;//metaData.catalogNames[i]; string baseColumnName = metaData.colNames[i]; string baseSchemaName = metaData.schemaNames[i]; string baseTableName = metaData.tableNames[i]; int providerType = metaData.colTypes[i]; Type dataType = HsqlConvert.ToDataType(providerType); int nullability = metaData.colNullable[i]; bool allowDBNull = isAutoIncrement || (nullability != 0); bool isAliased = (columnName != baseColumnName); bool isExpression = string.IsNullOrEmpty(baseTableName); bool isIdentity = isAutoIncrement; bool isRowVersion = false; bool isHidden = false; bool isLong = HsqlConvert.ToIsLongProviderType(providerType); bool isReadOnly = !metaData.isWritable[i]; if ((columnSize == 0) && HsqlTypes.isCharacterType(providerType)) { columnSize = HsqlTypes.getPrecision(providerType); } if ((numericPrecision == 0) && HsqlTypes.isNumberType(providerType)) { numericPrecision = HsqlTypes.getPrecision(providerType); } if (includeKeyInfo) { if (!(string.IsNullOrEmpty(baseTableName) || string.IsNullOrEmpty(baseColumnName))) { ColumnIdentifier key = new ColumnIdentifier( baseSchemaName, baseTableName, baseColumnName); KeyInfo keyInfo; if (keyInfoMap.TryGetValue(key, out keyInfo)) { isKey = keyInfo.m_isKey; isUnique = keyInfo.m_isUnique; } } } HsqlResultSetMetaData.AddRow(table, columnName, columnOrdinal, columnSize, numericPrecision, numericScale, isUnique, isKey, baseServerName, baseCatalogName, baseColumnName, baseSchemaName, baseTableName, dataType, allowDBNull, providerType, isAliased, isExpression, isIdentity, isAutoIncrement, isRowVersion, isHidden, isLong, isReadOnly); } DataColumnCollection columns = table.Columns; int count = columns.Count; for (int i = 0; i < count; i++) { columns[i].ReadOnly = true; } return(table); }
/// <summary> /// Computes the key info dictionary for the column metadata of the /// given data reader. /// </summary> /// <remarks> /// Depending upon the column metadata already present in the data /// reader, it may be required to perform further access to the /// originating data source using the reader's /// <c>OriginatingConnection</c>. This in turn implies that the /// <c>OriginatingConnection</c> must be open and must still /// represent the originating session on the originating data source; /// otherwise, the reported key info may be incorrect or the attempt /// access the data source may simply fail. /// </remarks> /// <param name="reader"> /// The reader for which to compute the column metadata key info map. /// </param> /// <returns> /// Map {ColumnIdentifier=>KeyInfo} /// </returns> /// <exception cref="HsqlDataSourceException"> /// If a data access error occurs. /// </exception> internal static Dictionary <ColumnIdentifier, KeyInfo> GetKeyInfo( HsqlDataReader reader) { ResultMetaData metaData = reader.m_result.metaData; Dictionary <TableIdentifier, object> tableSet = new Dictionary <TableIdentifier, object>(); object placeholder = new object(); string[] schemaNames = metaData.schemaNames; string[] tableNames = metaData.tableNames; string[] columnNames = metaData.colNames; int count = columnNames.Length; for (int i = 0; i < count; i++) { string tableName = tableNames[i]; string columnName = columnNames[i]; if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(columnName)) { // not a table column continue; } string schemaName = schemaNames[i]; TableIdentifier tableIdentifier = new TableIdentifier( schemaName, tableName); tableSet[tableIdentifier] = placeholder; } Dictionary <ColumnIdentifier, KeyInfo> columnMap = new Dictionary <ColumnIdentifier, KeyInfo>(); if (tableSet.Count == 0) { return(columnMap); } StringBuilder sb = new StringBuilder('('); count = 0; foreach (TableIdentifier tableIdentifier in tableSet.Keys) { if (count > 0) { sb.Append(" OR "); } count++; sb.Append("(bri.table_schem"); string schemaName = tableIdentifier.m_schema; if (string.IsNullOrEmpty(schemaName)) { sb.Append(" IS NULL "); } else { sb.Append(" = ").Append(StringConverter.toQuotedString( schemaName, '\'', /*escape inner quotes*/ true)); } string tableName = tableIdentifier.m_table; sb.Append(" AND bri.table_name = ").Append( StringConverter.toQuotedString(tableName, '\'', /*escape inner quotes*/ true)); sb.Append(')'); } sb.Append(')'); string predicate = sb.ToString(); using (HsqlCommand command = reader.OriginatingConnection.CreateCommand()) { command.CommandText = string.Format(KeyInfoQuery, predicate); command.CommandType = CommandType.Text; using (HsqlDataReader keyInfoReader = command.ExecuteReader()) { while (keyInfoReader.Read()) { bool isKey = keyInfoReader.GetBoolean(3); if (!isKey) { continue; } string schema = keyInfoReader.GetString(0); string table = keyInfoReader.GetString(1); string column = keyInfoReader.GetString(2); ColumnIdentifier key = new ColumnIdentifier(schema, table, column); if (!columnMap.ContainsKey(key)) { KeyInfo keyInfo = new KeyInfo(); keyInfo.m_isKey = true; keyInfo.m_isUnique = false; columnMap.Add(key, keyInfo); } } } } return(columnMap); }
/// <summary> /// Determines whether the specified <c>ColumnIdentifier</c> is /// equal to this one. /// </summary> /// <param name="other"> /// The <c>ColumnIdentifier</c> to which to compare.</param> /// <returns> /// <c>true</c> if the specified <c>ColumnIdentifier</c> is /// equal to this one; otherwise, <c>false</c>. /// </returns> internal bool Equals(ColumnIdentifier other) { return(object.Equals(m_schema, other.m_schema) && object.Equals(m_table, other.m_table) && object.Equals(m_column, other.m_column)); }
/// <summary> /// Determines whether the specified object is equal /// to this one. /// </summary> /// <param name="obj"> /// The object to which to compare.</param> /// <returns> /// <c>true</c> if the specified object is equal to this one; /// otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { ColumnIdentifier other = obj as ColumnIdentifier; return((other != null) && Equals(other)); }
/// <summary> /// Determines whether the specified <c>ColumnIdentifier</c> is /// equal to this one. /// </summary> /// <param name="other"> /// The <c>ColumnIdentifier</c> to which to compare.</param> /// <returns> /// <c>true</c> if the specified <c>ColumnIdentifier</c> is /// equal to this one; otherwise, <c>false</c>. /// </returns> internal bool Equals(ColumnIdentifier other) { return object.Equals(m_schema, other.m_schema) && object.Equals(m_table, other.m_table) && object.Equals(m_column, other.m_column); }
/// <summary> /// Computes the key info dictionary for the column metadata of the /// given data reader. /// </summary> /// <remarks> /// Depending upon the column metadata already present in the data /// reader, it may be required to perform further access to the /// originating data source using the reader's /// <c>OriginatingConnection</c>. This in turn implies that the /// <c>OriginatingConnection</c> must be open and must still /// represent the originating session on the originating data source; /// otherwise, the reported key info may be incorrect or the attempt /// access the data source may simply fail. /// </remarks> /// <param name="reader"> /// The reader for which to compute the column metadata key info map. /// </param> /// <returns> /// Map {ColumnIdentifier=>KeyInfo} /// </returns> /// <exception cref="HsqlDataSourceException"> /// If a data access error occurs. /// </exception> internal static Dictionary<ColumnIdentifier, KeyInfo> GetKeyInfo( HsqlDataReader reader) { ResultMetaData metaData = reader.m_result.metaData; Dictionary<TableIdentifier, object> tableSet = new Dictionary<TableIdentifier, object>(); object placeholder = new object(); string[] schemaNames = metaData.schemaNames; string[] tableNames = metaData.tableNames; string[] columnNames = metaData.colNames; int count = columnNames.Length; for (int i = 0; i < count; i++) { string tableName = tableNames[i]; string columnName = columnNames[i]; if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(columnName)) { // not a table column continue; } string schemaName = schemaNames[i]; TableIdentifier tableIdentifier = new TableIdentifier( schemaName, tableName); tableSet[tableIdentifier] = placeholder; } Dictionary<ColumnIdentifier, KeyInfo> columnMap = new Dictionary<ColumnIdentifier, KeyInfo>(); if (tableSet.Count == 0) { return columnMap; } StringBuilder sb = new StringBuilder('('); count = 0; foreach (TableIdentifier tableIdentifier in tableSet.Keys) { if (count > 0) { sb.Append(" OR "); } count++; sb.Append("(bri.table_schem"); string schemaName = tableIdentifier.m_schema; if (string.IsNullOrEmpty(schemaName)) { sb.Append(" IS NULL "); } else { sb.Append(" = ").Append(StringConverter.toQuotedString( schemaName, '\'', /*escape inner quotes*/ true)); } string tableName = tableIdentifier.m_table; sb.Append(" AND bri.table_name = ").Append( StringConverter.toQuotedString(tableName, '\'', /*escape inner quotes*/ true)); sb.Append(')'); } sb.Append(')'); string predicate = sb.ToString(); using (HsqlCommand command = reader.OriginatingConnection.CreateCommand()) { command.CommandText = string.Format(KeyInfoQuery, predicate); command.CommandType = CommandType.Text; using (HsqlDataReader keyInfoReader = command.ExecuteReader()) { while (keyInfoReader.Read()) { bool isKey = keyInfoReader.GetBoolean(3); if (!isKey) { continue; } string schema = keyInfoReader.GetString(0); string table = keyInfoReader.GetString(1); string column = keyInfoReader.GetString(2); ColumnIdentifier key = new ColumnIdentifier(schema, table, column); if (!columnMap.ContainsKey(key)) { KeyInfo keyInfo = new KeyInfo(); keyInfo.m_isKey = true; keyInfo.m_isUnique = false; columnMap.Add(key, keyInfo); } } } } return columnMap; }
/// <summary> /// Retrieves a <c>DataTable</c> object representing the column /// metadata of the given data reader's current result. /// </summary> /// <param name="reader"> /// A reader object for which to retrieve the column metadata. /// </param> /// <returns> /// A <c>DataTable</c> object representing the column metadata of the /// given data reader's current result. /// </returns> /// <exception cref="HsqlDataSourceException"> /// If a data access error occurs. /// </exception> public static DataTable CreateSchemaTable(HsqlDataReader reader) { Result result = reader.m_result; int columnCount = result.getColumnCount(); ResultMetaData metaData = result.metaData; DataTable table = CreateTable(columnCount); bool includeKeyInfo = reader.HasCommandBehavior(CommandBehavior.KeyInfo); Dictionary<ColumnIdentifier, KeyInfo> keyInfoMap = (includeKeyInfo) ? HsqlResultSetMetaData.GetKeyInfo(reader) : null; string catalogName = reader.OriginatingConnection.Database; for (int i = 0; i < columnCount; i++) { bool isAutoIncrement = metaData.isIdentity[i]; string columnName = metaData.colLabels[i]; int columnOrdinal = i; int columnSize = metaData.colSizes[i]; int numericPrecision = metaData.colSizes[i]; int numericScale = metaData.colScales[i]; bool isUnique = false; // isAutoIncrement; bool isKey = isAutoIncrement; string baseServerName = null; string baseCatalogName = catalogName;//metaData.catalogNames[i]; string baseColumnName = metaData.colNames[i]; string baseSchemaName = metaData.schemaNames[i]; string baseTableName = metaData.tableNames[i]; int providerType = metaData.colTypes[i]; Type dataType = HsqlConvert.ToDataType(providerType); int nullability = metaData.colNullable[i]; bool allowDBNull = isAutoIncrement || (nullability != 0); bool isAliased = (columnName != baseColumnName); bool isExpression = string.IsNullOrEmpty(baseTableName); bool isIdentity = isAutoIncrement; bool isRowVersion = false; bool isHidden = false; bool isLong = HsqlConvert.ToIsLongProviderType(providerType); bool isReadOnly = !metaData.isWritable[i]; if ((columnSize == 0) && HsqlTypes.isCharacterType(providerType)) { columnSize = HsqlTypes.getPrecision(providerType); } if ((numericPrecision == 0) && HsqlTypes.isNumberType(providerType)) { numericPrecision = HsqlTypes.getPrecision(providerType); } if (includeKeyInfo) { if (!(string.IsNullOrEmpty(baseTableName) || string.IsNullOrEmpty(baseColumnName))) { ColumnIdentifier key = new ColumnIdentifier( baseSchemaName, baseTableName, baseColumnName); KeyInfo keyInfo; if (keyInfoMap.TryGetValue(key, out keyInfo)) { isKey = keyInfo.m_isKey; isUnique = keyInfo.m_isUnique; } } } HsqlResultSetMetaData.AddRow(table, columnName, columnOrdinal, columnSize, numericPrecision, numericScale, isUnique, isKey, baseServerName, baseCatalogName, baseColumnName, baseSchemaName, baseTableName, dataType, allowDBNull, providerType, isAliased, isExpression, isIdentity, isAutoIncrement, isRowVersion, isHidden, isLong, isReadOnly); } DataColumnCollection columns = table.Columns; int count = columns.Count; for (int i = 0; i < count; i++) { columns[i].ReadOnly = true; } return table; }
public override bool WriteColumnIdentifier(ColumnIdentifier node) { if (ResolveNames) { Writer.Write(GetResolvedColumnName(node.ColumnReference)); return false; } else { return true; } }
public override bool WriteColumnIdentifier(ColumnIdentifier node) { if (ResolveNames) { string res = String.Empty; if (node.TableReference != null) { res += QuoteTableReferenceName(node.TableReference); } if (res != String.Empty) res += "."; if (node.ColumnReference.IsStar) { res += "*"; } else { res += QuoteIdentifier(node.ColumnReference.ColumnName); } Writer.Write(res); return false; } else { return true; } }