/// <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=&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);
        }
Example #3
0
        internal void DeriveParametersInternal()
        {
            if (CommandType != CommandType.StoredProcedure)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Operation not supported for CommandType: "
                                                        + CommandType.ToString()));
            }

            Prepare();

            HsqlStatement     statement = m_statement;
            ParameterMetaData pmd       = statement.ParameterDescriptor.metaData;

            string[] parameterNames = pmd.colNames;
            int      count          = parameterNames.Length;

            HsqlParameter[] parameters = new HsqlParameter[count];

            for (int i = 0; i < count; i++)
            {
                string        name        = parameterNames[i];
                ParameterMode mode        = (ParameterMode)pmd.paramMode[i];
                int           type        = pmd.colTypes[i];
                int           precision   = pmd.colSizes[i];
                int           scale       = pmd.colScales[i];
                int           nullability = pmd.colNullable[i];

                HsqlProviderType   providerType = (HsqlProviderType)type;
                DbType             dbType       = HsqlConvert.ToDbType(providerType);
                ParameterDirection?direction    = HsqlConvert.ToParameterDirection(mode);
                bool?isNullable  = IsNullable(nullability);
                bool isCharacter = IsCharacterType(type);
                bool isNumber    = (!isCharacter) && IsNumberType(type);
                bool isTemporal  = !(isCharacter || isNumber) && IsTemporalType(type);
                int  size        = ToBufferSize(type, precision);

                if (isCharacter)
                {
                    precision = 0;
                    scale     = 0;
                }
                else if (isNumber || isTemporal)
                {
                    if (precision == 0)
                    {
                        precision = ToDefaultPrecision(type);
                    }
                }

                HsqlParameter parameter = new HsqlParameter();

                parameter.DbType = dbType;

                if (direction != null)
                {
                    parameter.Direction = direction.Value;
                }

                if (isNullable != null)
                {
                    parameter.IsNullable = isNullable.Value;
                }

                parameter.ParameterName = name;
                parameter.Precision     = (byte)Math.Min(byte.MaxValue, precision);
                parameter.ProviderType  = providerType;
                parameter.Scale         = (byte)Math.Min(byte.MaxValue, scale);
                parameter.Size          = size;
                parameter.SourceVersion = DataRowVersion.Default;

                parameters[i] = parameter;
            }

            HsqlParameterCollection pc = Parameters;

            pc.Clear();

            foreach (HsqlParameter parameter in parameters)
            {
                pc.Add(parameter);
            }
        }
        internal static string ResultMetaDataToString(ResultMetaData rmd)
        {
            StringBuilder sb = new StringBuilder();
            int columnCount = rmd.colTypes.Length;

            for (int i = 0; i < columnCount; i++)
            {
                if (i > 0)
                {
                    sb.AppendLine(",");
                }

                sb.AppendFormat("{0}: ", i);

                ColumnMetaData cmd = new ColumnMetaData();
                int type = rmd.colTypes[i];

                cmd.catalogName = (rmd.catalogNames[i] == null) ?
                    "" : rmd.catalogNames[i];
                cmd.schemaName = (rmd.schemaNames[i] == null) ?
                    "" : rmd.schemaNames[i];
                cmd.tableName = rmd.tableNames[i] == null ?
                    "" : rmd.tableNames[i];
                cmd.columnName = rmd.colNames[i] == null ?
                    "" : rmd.colNames[i];
                cmd.columnLabel = rmd.colLabels[i] == null ?
                    "" : rmd.colLabels[i];
                cmd.columnType = type;
                cmd.columnTypeName = HsqlTypes.getTypeString(type);
                cmd.isWritable = rmd.isWritable[i];
                cmd.isReadOnly = !cmd.isWritable;
                cmd.isAutoIncrement = rmd.isIdentity[i];
                cmd.isNullable = rmd.colNullable[i];

                cmd.columnClassName = rmd.classNames[i];

                if (string.IsNullOrEmpty(cmd.columnClassName))
                {
                    cmd.columnClassName = HsqlTypes.getColStClsName(type);
                }

                if (HsqlTypes.acceptsPrecisionCreateParam(type))
                {
                    if (rmd.colSizes[i] == 0)
                    {
                        cmd.columnDisplaySize = HsqlTypes.getMaxDisplaySize(type);
                    }
                    else
                    {
                        cmd.columnDisplaySize = rmd.colSizes[i];

                        if (HsqlTypes.acceptsScaleCreateParam(type))
                        {
                            if (rmd.colScales[i] != 0)
                            {
                                cmd.columnDisplaySize += (1 + rmd.colScales[i]);
                            }
                        }
                    }
                }
                else
                {
                    cmd.columnDisplaySize = HsqlTypes.getMaxDisplaySize(type);
                }

                if (HsqlTypes.isNumberType(type)
                        && HsqlTypes.acceptsPrecisionCreateParam(type))
                {
                    cmd.precision = rmd.colSizes[i];

                    if (cmd.precision == 0)
                    {
                        cmd.precision = HsqlTypes.getPrecision(type);
                    }
                }
                else
                {
                    cmd.precision = HsqlTypes.getPrecision(type);
                }

                if (HsqlTypes.acceptsScaleCreateParam(type))
                {
                    cmd.scale = rmd.colScales[i];
                }

                JavaBoolean iua = HsqlTypes.isUnsignedAttribute(type);

                cmd.isSigned = ((iua != null) && !iua.booleanValue());

                JavaBoolean ics = HsqlTypes.isCaseSensitive(type);

                cmd.isCaseSensitive = ((ics != null) && ics.booleanValue());
                cmd.isSearchable = HsqlTypes.isSearchable(type);

                sb.Append(cmd.toString());
            }

            return sb.ToString();
        }
Example #5
0
        internal static string ResultMetaDataToString(ResultMetaData rmd)
        {
            StringBuilder sb          = new StringBuilder();
            int           columnCount = rmd.colTypes.Length;

            for (int i = 0; i < columnCount; i++)
            {
                if (i > 0)
                {
                    sb.AppendLine(",");
                }

                sb.AppendFormat("{0}: ", i);

                ColumnMetaData cmd  = new ColumnMetaData();
                int            type = rmd.colTypes[i];

                cmd.catalogName = (rmd.catalogNames[i] == null) ?
                                  "" : rmd.catalogNames[i];
                cmd.schemaName = (rmd.schemaNames[i] == null) ?
                                 "" : rmd.schemaNames[i];
                cmd.tableName = rmd.tableNames[i] == null ?
                                "" : rmd.tableNames[i];
                cmd.columnName = rmd.colNames[i] == null ?
                                 "" : rmd.colNames[i];
                cmd.columnLabel = rmd.colLabels[i] == null ?
                                  "" : rmd.colLabels[i];
                cmd.columnType      = type;
                cmd.columnTypeName  = HsqlTypes.getTypeString(type);
                cmd.isWritable      = rmd.isWritable[i];
                cmd.isReadOnly      = !cmd.isWritable;
                cmd.isAutoIncrement = rmd.isIdentity[i];
                cmd.isNullable      = rmd.colNullable[i];

                cmd.columnClassName = rmd.classNames[i];

                if (string.IsNullOrEmpty(cmd.columnClassName))
                {
                    cmd.columnClassName = HsqlTypes.getColStClsName(type);
                }

                if (HsqlTypes.acceptsPrecisionCreateParam(type))
                {
                    if (rmd.colSizes[i] == 0)
                    {
                        cmd.columnDisplaySize = HsqlTypes.getMaxDisplaySize(type);
                    }
                    else
                    {
                        cmd.columnDisplaySize = rmd.colSizes[i];

                        if (HsqlTypes.acceptsScaleCreateParam(type))
                        {
                            if (rmd.colScales[i] != 0)
                            {
                                cmd.columnDisplaySize += (1 + rmd.colScales[i]);
                            }
                        }
                    }
                }
                else
                {
                    cmd.columnDisplaySize = HsqlTypes.getMaxDisplaySize(type);
                }

                if (HsqlTypes.isNumberType(type) &&
                    HsqlTypes.acceptsPrecisionCreateParam(type))
                {
                    cmd.precision = rmd.colSizes[i];

                    if (cmd.precision == 0)
                    {
                        cmd.precision = HsqlTypes.getPrecision(type);
                    }
                }
                else
                {
                    cmd.precision = HsqlTypes.getPrecision(type);
                }

                if (HsqlTypes.acceptsScaleCreateParam(type))
                {
                    cmd.scale = rmd.colScales[i];
                }

                JavaBoolean iua = HsqlTypes.isUnsignedAttribute(type);

                cmd.isSigned = ((iua != null) && !iua.booleanValue());

                JavaBoolean ics = HsqlTypes.isCaseSensitive(type);

                cmd.isCaseSensitive = ((ics != null) && ics.booleanValue());
                cmd.isSearchable    = HsqlTypes.isSearchable(type);

                sb.Append(cmd.toString());
            }

            return(sb.ToString());
        }