private MyCatSchemaCollection GetTable(string sql)
        {
            MyCatSchemaCollection c      = new MyCatSchemaCollection();
            MyCatCommand          cmd    = new MyCatCommand(sql, connection);
            MyCatDataReader       reader = cmd.ExecuteReader();

            // add columns
            for (int i = 0; i < reader.FieldCount; i++)
            {
                c.AddColumn(reader.GetName(i), reader.GetFieldType(i));
            }

            using (reader)
            {
                while (reader.Read())
                {
                    MyCatSchemaRow row = c.AddRow();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        row[i] = reader.GetValue(i);
                    }
                }
            }
            return(c);
        }
Exemple #2
0
        internal void ProcessOutputParameters(MyCatDataReader reader)
        {
            // We apparently need to always adjust our output types since the server
            // provided data types are not always right
            AdjustOutputTypes(reader);

            if ((reader.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
            {
                return;
            }

            // now read the output parameters data row
            reader.Read();

            string prefix = "@" + StoredProcedure.ParameterPrefix;

            for (int i = 0; i < reader.FieldCount; i++)
            {
                string fieldName = reader.GetName(i);
                if (fieldName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                {
                    fieldName = fieldName.Remove(0, prefix.Length);
                }
                MyCatParameter parameter = command.Parameters.GetParameterFlexible(fieldName, true);
                parameter.Value = reader.GetValue(i);
            }
        }
        private void LoadTableColumns(MyCatSchemaCollection schemaCollection, string schema,
                                      string tableName, string columnRestriction)
        {
            string sql = String.Format("SHOW FULL COLUMNS FROM `{0}`.`{1}`",
                                       schema, tableName);
            MyCatCommand cmd = new MyCatCommand(sql, connection);

            int pos = 1;

            using (MyCatDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    string colName = reader.GetString(0);
                    if (columnRestriction != null && colName != columnRestriction)
                    {
                        continue;
                    }
                    MyCatSchemaRow row = schemaCollection.AddRow();
                    row["TABLE_CATALOG"]            = DBNull.Value;
                    row["TABLE_SCHEMA"]             = schema;
                    row["TABLE_NAME"]               = tableName;
                    row["COLUMN_NAME"]              = colName;
                    row["ORDINAL_POSITION"]         = pos++;
                    row["COLUMN_DEFAULT"]           = reader.GetValue(5);
                    row["IS_NULLABLE"]              = reader.GetString(3);
                    row["DATA_TYPE"]                = reader.GetString(1);
                    row["CHARACTER_MAXIMUM_LENGTH"] = DBNull.Value;
                    row["CHARACTER_OCTET_LENGTH"]   = DBNull.Value;
                    row["NUMERIC_PRECISION"]        = DBNull.Value;
                    row["NUMERIC_SCALE"]            = DBNull.Value;
                    row["CHARACTER_SET_NAME"]       = reader.GetValue(2);
                    row["COLLATION_NAME"]           = row["CHARACTER_SET_NAME"];
                    row["COLUMN_TYPE"]              = reader.GetString(1);
                    row["COLUMN_KEY"]               = reader.GetString(4);
                    row["EXTRA"]          = reader.GetString(6);
                    row["PRIVILEGES"]     = reader.GetString(7);
                    row["COLUMN_COMMENT"] = reader.GetString(8);
                    ParseColumnRow(row);
                }
            }
        }
        internal static void InitCollections(MyCatConnection connection)
        {
            defaultCollations = new Dictionary <string, string>();
            maxLengths        = new Dictionary <string, int>();

            MyCatCommand cmd = new MyCatCommand("SHOW CHARSET", connection);

            using (MyCatDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    defaultCollations.Add(reader.GetString(0), reader.GetString(2));
                    maxLengths.Add(reader.GetString(0), Convert.ToInt32(reader.GetValue(3)));
                }
            }
        }
        /// <include file='docs/mysqlcommand.xml' path='docs/ExecuteScalar/*'/>
        public override object ExecuteScalar()
        {
            lastInsertedId = -1;
            object val = null;

#if !NETSTANDARD1_3
            // give our interceptors a shot at it first
            if (connection != null &&
                connection.commandInterceptor.ExecuteScalar(CommandText, ref val))
            {
                return(val);
            }
#endif

            using (MyCatDataReader reader = ExecuteReader())
            {
                if (reader.Read())
                {
                    val = reader.GetValue(0);
                }
            }

            return(val);
        }
        private void FindTables(MyCatSchemaCollection schema, string[] restrictions)
        {
            StringBuilder sql = new StringBuilder();

            StringBuilder where = new StringBuilder();
            sql.AppendFormat(CultureInfo.InvariantCulture,
                             "SHOW TABLE STATUS FROM `{0}`", restrictions[1]);
            if (restrictions != null && restrictions.Length >= 3 &&
                restrictions[2] != null)
            {
                where.AppendFormat(CultureInfo.InvariantCulture,
                                   " LIKE '{0}'", restrictions[2]);
            }
            sql.Append(where.ToString());

            string table_type = restrictions[1].ToLower() == "information_schema"
                        ?
                                "SYSTEM VIEW"
                        : "BASE TABLE";

            MyCatCommand cmd = new MyCatCommand(sql.ToString(), connection);

            using (MyCatDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    MyCatSchemaRow row = schema.AddRow();
                    row["TABLE_CATALOG"]   = null;
                    row["TABLE_SCHEMA"]    = restrictions[1];
                    row["TABLE_NAME"]      = reader.GetString(0);
                    row["TABLE_TYPE"]      = table_type;
                    row["ENGINE"]          = GetString(reader, 1);
                    row["VERSION"]         = reader.GetValue(2);
                    row["ROW_FORMAT"]      = GetString(reader, 3);
                    row["TABLE_ROWS"]      = reader.GetValue(4);
                    row["AVG_ROW_LENGTH"]  = reader.GetValue(5);
                    row["DATA_LENGTH"]     = reader.GetValue(6);
                    row["MAX_DATA_LENGTH"] = reader.GetValue(7);
                    row["INDEX_LENGTH"]    = reader.GetValue(8);
                    row["DATA_FREE"]       = reader.GetValue(9);
                    row["AUTO_INCREMENT"]  = reader.GetValue(10);
                    row["CREATE_TIME"]     = reader.GetValue(11);
                    row["UPDATE_TIME"]     = reader.GetValue(12);
                    row["CHECK_TIME"]      = reader.GetValue(13);
                    row["TABLE_COLLATION"] = GetString(reader, 14);
                    row["CHECKSUM"]        = reader.GetValue(15);
                    row["CREATE_OPTIONS"]  = GetString(reader, 16);
                    row["TABLE_COMMENT"]   = GetString(reader, 17);
                }
            }
        }
        public virtual MyCatSchemaCollection GetIndexColumns(string[] restrictions)
        {
            MyCatSchemaCollection dt = new MyCatSchemaCollection("IndexColumns");

            dt.AddColumn("INDEX_CATALOG", typeof(string));
            dt.AddColumn("INDEX_SCHEMA", typeof(string));
            dt.AddColumn("INDEX_NAME", typeof(string));
            dt.AddColumn("TABLE_NAME", typeof(string));
            dt.AddColumn("COLUMN_NAME", typeof(string));
            dt.AddColumn("ORDINAL_POSITION", typeof(int));
            dt.AddColumn("SORT_ORDER", typeof(string));

            int max = restrictions == null ? 4 : restrictions.Length;

            string[] tableRestrictions = new string[Math.Max(max, 4)];
            if (restrictions != null)
            {
                restrictions.CopyTo(tableRestrictions, 0);
            }
            tableRestrictions[3] = "BASE TABLE";
            MyCatSchemaCollection tables = GetTables(tableRestrictions);

            foreach (MyCatSchemaRow table in tables.Rows)
            {
                string sql = String.Format("SHOW INDEX FROM `{0}`.`{1}`",
                                           table["TABLE_SCHEMA"], table["TABLE_NAME"]);
                MyCatCommand cmd = new MyCatCommand(sql, connection);
                using (MyCatDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string key_name = GetString(reader, reader.GetOrdinal("KEY_NAME"));
                        string col_name = GetString(reader, reader.GetOrdinal("COLUMN_NAME"));

                        if (restrictions != null)
                        {
                            if (restrictions.Length >= 4 && restrictions[3] != null &&
                                key_name != restrictions[3])
                            {
                                continue;
                            }
                            if (restrictions.Length >= 5 && restrictions[4] != null &&
                                col_name != restrictions[4])
                            {
                                continue;
                            }
                        }
                        MyCatSchemaRow row = dt.AddRow();
                        row["INDEX_CATALOG"]    = null;
                        row["INDEX_SCHEMA"]     = table["TABLE_SCHEMA"];
                        row["INDEX_NAME"]       = key_name;
                        row["TABLE_NAME"]       = GetString(reader, reader.GetOrdinal("TABLE"));
                        row["COLUMN_NAME"]      = col_name;
                        row["ORDINAL_POSITION"] = reader.GetValue(reader.GetOrdinal("SEQ_IN_INDEX"));
                        row["SORT_ORDER"]       = reader.GetString("COLLATION");
                    }
                }
            }

            return(dt);
        }