Example #1
0
        public override Fields ListAllFieldsFromLivingOf(string tablename)
        {
            var    fields = new Fields();
            string sel    =
                @"SELECT
    COLUMN_NAME,
    IS_NULLABLE,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH,
    NUMERIC_PRECISION,
    NUMERIC_SCALE,
    COLUMN_KEY 
 FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE TABLE_SCHEMA='{0}' 
 AND TABLE_NAME='{1}'
 ORDER BY ORDINAL_POSITION";
            Field  fld;
            DbType ttype;
            int    isize;
            int    iscale;
            bool   has_size;

            /* 1. list the fields */
            using (DbCommand cmdX = new MySqlCommand(String.Format(sel, conn.Database, tablename.ToLower()), (MySqlConnection)conn, (MySqlTransaction)trans))
            {
                using (DbDataReader rcsX = cmdX.ExecuteReader(CommandBehavior.SingleResult))
                {
                    while (rcsX.HasRows && rcsX.Read())
                    {
                        if (rcsX["NUMERIC_SCALE"].Equals(DBNull.Value))
                        {
                            iscale = 0;
                        }
                        else
                        {
                            iscale = rcsX.GetInt32(rcsX.GetOrdinal("NUMERIC_SCALE"));
                        }
                        /* determine data type */
                        has_size = false;
                        isize    = 0;
                        switch (rcsX["DATA_TYPE"].ToString().ToLowerInvariant())
                        {
                        case "longblob":
                            ttype = DbType.Object;
                            break;

                        case "longtext":
                            ttype = DbType.String;
                            break;

                        case "datetime":
                            ttype = DbType.DateTime;
                            break;

                        case "decimal":
                            if (iscale == 4)
                            {
                                ttype = DbType.Currency;
                            }
                            else
                            {
                                ttype = DbType.Decimal;
                            }
                            break;

                        case "int":
                            ttype = DbType.Int32;
                            break;

                        case "varchar":
                            ttype    = DbType.StringFixedLength;
                            has_size = true;
                            break;

                        case "binary":
                            ttype    = DbType.Guid;
                            has_size = true;
                            break;

                        default:
                            ttype = DbType.Object;
                            break;
                        }
                        if (has_size)
                        {
                            if (rcsX["CHARACTER_MAXIMUM_LENGTH"].Equals(DBNull.Value))
                            {
                                isize = 0;
                            }
                            else
                            {
                                isize = rcsX.GetInt32(rcsX.GetOrdinal("CHARACTER_MAXIMUM_LENGTH"));
                            }
                        }
                        fld = new Field
                        {
                            Name        = rcsX["COLUMN_NAME"].ToString(),
                            Description = String.Empty,
                            NonuniqueIndexConstituent = 0, /* -!- */
                            Nullability           = rcsX["IS_NULLABLE"].ToString() == "YES",
                            PrimaryKeyConstituent = rcsX["COLUMN_KEY"].ToString() == "PRI",
                            RelatedField          = String.Empty,
                            RelatedTable          = String.Empty,
                            Size = isize,
                            Type = ttype,
                            UniqueIndexConstituent = 0 /* -!- */
                        };
                        fields.Add(fld);
                    }
                }
            }
            /* deliver */
            return(fields);
        }