<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using DbShell.Driver.Common.AbstractDb;
using DbShell.Driver.Common.CommonTypeSystem;
using DbShell.Driver.Common.Structure;
using DbShell.Driver.Common.Utility;

namespace DbShell.Driver.SqlServer
{
    public class SqlServerDatabaseAnalyser : DatabaseAnalyser
    {
        private Dictionary<NameWithSchema, TableInfo> _tables = new Dictionary<NameWithSchema, TableInfo>();

        private SqlConnection Connection
        {
            get { return (SqlConnection) _conn; }
        }

        protected override void DoRun()
        {
            var dialect = SqlServerDatabaseFactory.Instance.CreateDialect();
            var tables = Connection.GetSchema("Tables");
            var columns = new List<DataRow>();
            foreach (DataRow row in Connection.GetSchema("Columns").Rows) columns.Add(row);
            columns = new List<DataRow>(columns.SortedByKey<DataRow, int>(c => c.SafeInt("ORDINAL_POSITION")));

            foreach (DataRow row in tables.Rows)
            {
                if (row.SafeString("TABLE_TYPE") != "BASE TABLE") continue;
                var table = new TableInfo(Result);
                table.FullName = new NameWithSchema(row.SafeString("TABLE_SCHEMA"), row.SafeString("TABLE_NAME"));
                Result.Tables.Add(table);
                _tables[table.FullName] = table;
            }

            foreach (DataRow row in columns)
            {
                var tname = new NameWithSchema(row.SafeString("TABLE_SCHEMA"), row.SafeString("TABLE_NAME"));
                if (!_tables.ContainsKey(tname)) continue;
                var table = _tables[tname];
                var col = new ColumnInfo(table);
                col.Name = row.SafeString("COLUMN_NAME");
                col.NotNull = row.SafeString("IS_NULLABLE") == "NO";
                col.DataType = row.SafeString("DATA_TYPE");
                col.Length = row.SafeInt("CHARACTER_MAXIMUM_LENGTH");
                col.Precision = row.SafeInt("NUMERIC_PRECISION");
                col.Scale = row.SafeInt("NUMERIC_SCALE");
                col.DefaultValue = row.SafeString("COLUMN_DEFAULT");
                col.CommonType = AnalyseType(col.DataType, col.Length, col.Precision, col.Scale);
                table.Columns.Add(col);
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("identity_columns.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string table = reader.SafeString("TableName");
                        string schema = reader.SafeString("SchemaName");
                        string column = reader.SafeString("ColumnName");
                        var t = _tables[new NameWithSchema(schema, table)];
                        t.Columns[column].AutoIncrement = true;
                    }
                }
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("primary_keys.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string table = reader.SafeString("TableName");
                        string schema = reader.SafeString("SchemaName");
                        string column = reader.SafeString("ColumnName");
                        var t = _tables[new NameWithSchema(schema, table)];
                        t.Columns[column].PrimaryKey = true;
                    }
                }
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("foreign_keys.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string fktable = reader.SafeString("FK_Table");
                        string fkcolumn = reader.SafeString("FK_Column");
                        string fkschema = reader.SafeString("FK_Schema");

                        string pktable = reader.SafeString("PK_Table");
                        if (String.IsNullOrEmpty(pktable)) pktable = reader.SafeString("IX_Table");
                        string pkcolumn = reader.SafeString("PK_Column");
                        if (String.IsNullOrEmpty(pkcolumn)) pkcolumn = reader.SafeString("IX_Column");
                        string pkschema = reader.SafeString("PK_Schema");
                        if (String.IsNullOrEmpty(pkschema)) pkschema = reader.SafeString("IX_Schema");

                        string deleteAction = reader.SafeString("Delete_Action");
                        string updateAction = reader.SafeString("Update_Action");
<commit_msg>fixed foreign key analysis<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using DbShell.Driver.Common.AbstractDb;
using DbShell.Driver.Common.CommonTypeSystem;
using DbShell.Driver.Common.Structure;
using DbShell.Driver.Common.Utility;

namespace DbShell.Driver.SqlServer
{
    public class SqlServerDatabaseAnalyser : DatabaseAnalyser
    {
        private Dictionary<NameWithSchema, TableInfo> _tables = new Dictionary<NameWithSchema, TableInfo>();

        private SqlConnection Connection
        {
            get { return (SqlConnection) _conn; }
        }

        protected override void DoRun()
        {
            var dialect = SqlServerDatabaseFactory.Instance.CreateDialect();
            var tables = Connection.GetSchema("Tables");
            var columns = new List<DataRow>();
            foreach (DataRow row in Connection.GetSchema("Columns").Rows) columns.Add(row);
            columns = new List<DataRow>(columns.SortedByKey<DataRow, int>(c => c.SafeInt("ORDINAL_POSITION")));

            foreach (DataRow row in tables.Rows)
            {
                if (row.SafeString("TABLE_TYPE") != "BASE TABLE") continue;
                var table = new TableInfo(Result);
                table.FullName = new NameWithSchema(row.SafeString("TABLE_SCHEMA"), row.SafeString("TABLE_NAME"));
                Result.Tables.Add(table);
                _tables[table.FullName] = table;
            }

            foreach (DataRow row in columns)
            {
                var tname = new NameWithSchema(row.SafeString("TABLE_SCHEMA"), row.SafeString("TABLE_NAME"));
                if (!_tables.ContainsKey(tname)) continue;
                var table = _tables[tname];
                var col = new ColumnInfo(table);
                col.Name = row.SafeString("COLUMN_NAME");
                col.NotNull = row.SafeString("IS_NULLABLE") == "NO";
                col.DataType = row.SafeString("DATA_TYPE");
                col.Length = row.SafeInt("CHARACTER_MAXIMUM_LENGTH");
                col.Precision = row.SafeInt("NUMERIC_PRECISION");
                col.Scale = row.SafeInt("NUMERIC_SCALE");
                col.DefaultValue = row.SafeString("COLUMN_DEFAULT");
                col.CommonType = AnalyseType(col.DataType, col.Length, col.Precision, col.Scale);
                table.Columns.Add(col);
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("identity_columns.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string table = reader.SafeString("TableName");
                        string schema = reader.SafeString("SchemaName");
                        string column = reader.SafeString("ColumnName");
                        var t = _tables[new NameWithSchema(schema, table)];
                        t.Columns[column].AutoIncrement = true;
                    }
                }
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("primary_keys.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string table = reader.SafeString("TableName");
                        string schema = reader.SafeString("SchemaName");
                        string column = reader.SafeString("ColumnName");
                        var t = _tables[new NameWithSchema(schema, table)];
                        t.Columns[column].PrimaryKey = true;
                    }
                }
            }

            using (var cmd = Connection.CreateCommand())
            {
                cmd.CommandText = SqlServerDatabaseFactory.LoadEmbeddedResource("foreign_keys.sql");
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string fktable = reader.SafeString("FK_Table");
                        string fkcolumn = reader.SafeString("FK_Column");
                        string fkschema = reader.SafeString("FK_Schema");

                        string pktable = reader.SafeString("IX_Table");
                        if (String.IsNullOrEmpty(pktable)) pktable = reader.SafeString("PK_Table");
                        string pkcolumn = reader.SafeString("IX_Column");
                        if (String.IsNullOrEmpty(pkcolumn)) pkcolumn = reader.SafeString("PK_Column");
                        string pkschema = reader.SafeString("IX_Schema");
                        if (String.IsNullOrEmpty(pkschema)) pkschema = reader.SafeString("PK_Schema");

                        string deleteAction = reader.SafeString("Delete_Action");
                        string updateAction = reader.SafeString("Update_Action");

{'fraction_non_alphanumeric': 0.10174849352958609, 'fraction_numerical': 0.0, 'mean_word_length': 1.746608790016278, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '131409', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2389, 'n_words': 457}