IEnumerable <ITreeNode> ITreeNode.GetChildren(bool refresh)
    {
        var commandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
        var executor    = _connection.CreateCommandExecutor();
        var dataTable   = executor.ExecuteDataTable(new ExecuteReaderRequest(commandText));
        var nodes       = new List <ITreeNode>();

        foreach (DataRow dataRow in dataTable.Rows)
        {
            var tableName = (string)dataRow["TABLE_NAME"];
            var tableNode = new TableNode(tableName);
            nodes.Add(tableNode);
        }

        return(nodes);
    }
Beispiel #2
0
    void IResultWriter.WriteTableBegin(DataTable schemaTable)
    {
        var createTable = new StringBuilder();

        createTable.AppendFormat("create table [{0}]\r\n(\r\n", _tableName);
        var insertInto = new StringBuilder();

        insertInto.AppendFormat("insert into [{0}](", _tableName);
        var values = new StringBuilder();

        values.Append("values(");
        var stringTable = new StringTable(3);

        _insertCommand = _connection.CreateCommand();
        var last = schemaTable.Rows.Count - 1;

        for (var i = 0; i <= last; i++)
        {
            var       dataRow      = schemaTable.Rows[i];
            var       schemaRow    = FoundationDbColumnFactory.Create(dataRow);
            var       columnName   = schemaRow.ColumnName;
            var       columnSize   = schemaRow.ColumnSize;
            var       allowDbNull  = schemaRow.AllowDbNull;
            var       dataType     = schemaRow.DataType;
            var       dataTypeName = "???";
            var       typeCode     = Type.GetTypeCode(dataType);
            string    typeName;
            SqlDbType sqlDbType;

            switch (typeCode)
            {
            case TypeCode.Boolean:
                typeName  = "bit";
                sqlDbType = SqlDbType.Bit;
                break;

            case TypeCode.DateTime:
                typeName  = "datetime";
                sqlDbType = SqlDbType.DateTime;
                break;

            case TypeCode.Decimal:
                sqlDbType = SqlDbType.Decimal;
                var precision = schemaRow.NumericPrecision.Value;
                var scale     = schemaRow.NumericScale.Value;

                if (precision > 38)
                {
                    precision = 38;
                }

                if (scale > 38)
                {
                    scale = 38;
                }

                if (precision < scale)
                {
                    precision = scale;
                }

                if (scale == 0)
                {
                    typeName = $"decimal({precision})";
                }
                else
                {
                    typeName = $"decimal({precision},{scale})";
                }

                break;

            case TypeCode.Double:
                typeName  = "float";
                sqlDbType = SqlDbType.Float;
                break;

            case TypeCode.Int16:
                typeName  = "smallint";
                sqlDbType = SqlDbType.SmallInt;
                break;

            case TypeCode.Int32:
                typeName  = "int";
                sqlDbType = SqlDbType.Int;
                break;

            case TypeCode.Int64:
                typeName  = "bigint";
                sqlDbType = SqlDbType.BigInt;
                break;

            case TypeCode.Single:
                typeName  = "real";
                sqlDbType = SqlDbType.Real;
                break;

            case TypeCode.String:
                bool isFixedLength;
                var  dataTypeNameUpper = dataTypeName.ToUpper();

                switch (dataTypeName)
                {
                case "CHAR":
                case "NCHAR":
                    isFixedLength = true;
                    break;

                case "VARCHAR":
                case "NVARCHAR":
                case "VARCHAR2":
                default:
                    isFixedLength = false;
                    break;
                }

                if (columnSize <= 4000)
                {
                    if (isFixedLength)
                    {
                        typeName  = $"nchar({columnSize})";
                        sqlDbType = SqlDbType.NChar;
                    }
                    else
                    {
                        typeName  = $"nvarchar({columnSize})";
                        sqlDbType = SqlDbType.NVarChar;
                    }
                }
                else
                {
                    typeName  = "ntext";
                    sqlDbType = SqlDbType.NText;
                }

                break;

            default:
                throw new NotImplementedException();
            }

            var row = stringTable.NewRow();
            row[1] = columnName;
            row[2] = typeName;

            if (allowDbNull != null && !allowDbNull.Value)
            {
                row[2] += " not null";
            }

            insertInto.Append(columnName);
            values.Append('?');

            if (i < last)
            {
                row[2] += ',';
                insertInto.Append(',');
                values.Append(',');
            }

            stringTable.Rows.Add(row);
            var parameter = new SqlCeParameter(null, sqlDbType);
            _insertCommand.Parameters.Add(parameter);
        }

        createTable.AppendLine(stringTable.ToString(4));
        createTable.Append(')');
        var commandText = createTable.ToString();

        _messageWriter.WriteLine(commandText);
        var executor = _connection.CreateCommandExecutor();

        executor.ExecuteNonQuery(new CreateCommandRequest(commandText));
        insertInto.Append(") ");
        values.Append(')');
        insertInto.Append(values);
        var insertCommandText = insertInto.ToString();

        _messageWriter.WriteLine(insertCommandText);
        _insertCommand.CommandText = insertInto.ToString();
    }