Esempio n. 1
0
        /// <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=&gt;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);
        }
Esempio n. 2
0
        public void GetNextAsLiteralValue()
        {
            // Create Constructor Parameters

            Tokenizer testSubject = new Tokenizer();

            List <NextAsLiteralValueTestParameters> testParameters =
                new List <NextAsLiteralValueTestParameters>();

            // Array Literal

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   "foo",
                                   HsqlProviderType.Array,
                                   null,
                                   org.hsqldb.Trace.UNEXPECTED_TOKEN,
                                   "SQL ARRAY literal tokens are not supposed to be supported"));

            // BigInt Literal

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   "-1",
                                   HsqlProviderType.BigInt,
                                   null,
                                   org.hsqldb.Trace.UNEXPECTED_TOKEN,
                                   "Atomic retrieval of a negative BIGINT literal is not supposed to be supported."));

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   "0",
                                   HsqlProviderType.BigInt,
                                   new java.lang.Long(0),
                                   null,
                                   null));

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   "1",
                                   HsqlProviderType.BigInt,
                                   new java.lang.Long(1),
                                   null,
                                   null));

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   long.MaxValue.ToString(),
                                   HsqlProviderType.BigInt,
                                   new java.lang.Long(long.MaxValue),
                                   null,
                                   null));

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   long.MinValue.ToString(),
                                   HsqlProviderType.BigInt,
                                   null,
                                   org.hsqldb.Trace.UNEXPECTED_TOKEN,
                                   "Atomic retrieval of a negative BIGINT literal is not supposed to be supported."));

            // Binary Literal

            testParameters.Add(new NextAsLiteralValueTestParameters(
                                   "/* a binary literal value */ 'AFD14E7B9F82' ",
                                   HsqlProviderType.Binary,
                                   new org.hsqldb.types.Binary(HsqlStringConverter.hexToByte("AFD14E7B9F82"), false),
                                   null,
                                   null));


            foreach (NextAsLiteralValueTestParameters parameters in
                     testParameters)
            {
                Console.WriteLine(parameters);

                testSubject.Reset(parameters.Chars);

                try
                {
                    object value = testSubject.GetNextAsLiteralValue(
                        parameters.ProviderType);

                    if (parameters.ErrorMessage != null)
                    {
                        Assert.Fail(parameters.ErrorMessage);
                    }

                    System.Type expectedValueType =
                        HsqlConvert.ToProviderSpecificDataType(
                            parameters.ProviderType);

                    Assert.IsAssignableFrom(expectedValueType,
                                            parameters.Value);
                    Assert.IsAssignableFrom(expectedValueType,
                                            value);

                    Assert.AreEqual(parameters.Value, value);
                }
                catch (AssertionException)
                {
                    throw;
                }
                catch (HsqlDataSourceException hdse)
                {
                    Assert.AreEqual(parameters.ErrorCode, -hdse.ErrorCode);
                }
            }

            //testSubject.Reset("'AFD14E7B9F82'");

            //object bytes = testSubject.GetNextAsLiteralValue(HsqlProviderType.Binary);

            //Assert.IsInstanceOfType(typeof(org.hsqldb.types.Binary), bytes);

            //testSubject.Reset("'CAFEBABE'");

            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Blob);

            //    Assert.Fail("SQL BLOB literal tokens are not supposed to be supported at this time");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}

            //testSubject.Reset("TRUE");

            //testSubject.GetNextAsLiteralValue(HsqlProviderType.Boolean);

            //testSubject.Reset("FALSE");

            //testSubject.GetNextAsLiteralValue(HsqlProviderType.Boolean);

            //testSubject.Reset("NULL");

            //testSubject.GetNextAsLiteralValue(HsqlProviderType.Boolean);

            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Char);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Clob);

            //    Assert.Fail("SQL CLOB literal tokens are not supposed to be supported at this time");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.DataLink);

            //    Assert.Fail("SQL DATALINK literal tokens are not supposed to be supported at this time");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Date);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Decimal);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Distinct);

            //    Assert.Fail("SQL DISTINCT literal tokens are not supposed to be supported");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Double);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Float);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Integer);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.JavaObject);

            //    Assert.Fail("SQL JAVA_OBJECT literal tokens are not supposed to be supported at this time");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.LongVarBinary);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.LongVarChar);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Null);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Numeric);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Object);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Real);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Ref);

            //    Assert.Fail("SQL REF literal tokens are not supposed to be supported");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.SmallInt);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Struct);

            //    Assert.Fail("SQL STRUCT literal tokens are not supposed to be supported");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.Time);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.TimeStamp);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.TinyInt);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.VarBinary);
            ////testSubject.GetNextAsLiteralValue(HsqlProviderType.VarChar);
            //try
            //{
            //    testSubject.GetNextAsLiteralValue(HsqlProviderType.Xml);

            //    Assert.Fail("SQL XML literal tokens are not supposed to be supported at this time");
            //}
            //catch (HsqlDataSourceException)
            //{
            //}
        }