/// <summary>
        /// Processes the columns.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        protected virtual Dictionary <string, Property> ProcessColumns(EntityMap table)
        {
            var columnList = new Dictionary <string, Property>();

            //var references = new Dictionary<string, Property>();

            using (var reader = db.ExecuteReader(Connection, SELECT_COLUMNS, new QueryParam("tableName", table.TableName, DbType.String)))
            {
                var columnMap  = new List <string>();
                int countOrder = 0;
                while (reader.Read())
                {
                    string colName   = reader.GetValueAsString("column_name");
                    string dataType  = reader.GetValueAsString("udt_name");
                    int?   length    = reader.GetValueAsInt("character_maximum_length");
                    int?   precision = reader.GetValueAsInt("numeric_precision");
                    int?   scale     = reader.GetValueAsInt("numeric_scale");
                    countOrder++;

                    logger.Log(LogLevel.Info, string.Format("\t column: {0} {1}({2})", colName, dataType, length ?? 0));
                    columnMap.Add(colName);

                    Property col = null;
                    if (length.HasValue)
                    {
                        if (length < 0)
                        {
                            length = 2000;
                        }
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType))
                        {
                            Length = length.Value
                        };
                    }
                    else
                    {
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType));
                    }

                    if (precision.HasValue)
                    {
                        col.Precision = precision.Value;
                    }
                    else
                    {
                        col.Precision = -1;
                    }
                    if (scale.HasValue)
                    {
                        col.Scale = scale.Value;
                    }
                    else
                    {
                        col.Scale = -1;
                    }

                    col.SqlType = dataType;
                    col.Order   = countOrder;

                    bool   isNullable    = reader.GetValueAsString("is_nullable").Equals("YES");
                    string columnDefault = reader.GetValueAsString("column_default");
                    if (!string.IsNullOrWhiteSpace(columnDefault) && columnDefault.Contains("nextval(") && columnDefault.Contains("seq'::regclass)"))
                    {
                        col.IsIdentity      = true;
                        col.IsAutoGenerated = true;
                    }

                    col.IsNullable = isNullable;
                    //TODO: implement ProcessDefaultValue
                    //TODO: implement metadata
                    //col.DefaultValue = ProcessDefaultValue(reader.GetValueAsString("COLUMN_DEFAULT"));
                    col.ClrType = Dialect.GetClrType(col.DbType, isNullable);
                    //OnTableAddProperty(table, col);

                    //get constraints
                    string constraintType = reader.GetValueAsString("constraint_type");
                    if (!string.IsNullOrWhiteSpace(constraintType))
                    {
                        string constrainName = reader.GetValueAsString("constraint_name");

                        if (constraintType.ToUpper().Equals("UNIQUE"))
                        {
                            col.IsUnique = true;
                        }
                        else if (constraintType.ToUpper().Equals("PRIMARY KEY"))
                        {
                            col.IsPrimaryKey = true;
                            col.IsUnique     = true;
                        }
                        //else if (constraintType.ToUpper().Equals("FOREIGN KEY"))
                        //{
                        //    references.Add(constrainName, col);
                        //    continue;
                        //    //col = ReadForeignKeyReference(col, constrainName);
                        //}

                        col.ConstraintName = constrainName;
                    }

                    columnList.Add(colName, col);
                }

                tableColumnMap.Add(table.TableName, columnMap.ToArray());

                //foreach(var rl in references)
                //{
                //    var refCol = ReadForeignKeyReference(rl.Value, rl.Key);
                //    refCol.ConstraintName = rl.Key;
                //    if (columnList.ContainsKey(refCol.ColumnName))
                //        columnList.Remove(refCol.ColumnName);
                //    columnList.Add(refCol.ColumnName, refCol);
                //}
            }

            return(columnList);
        }
Example #2
0
        Dictionary <string, Property> ProcessColumns(EntityMap table)
        {
            Dictionary <string, Property> columnList = new Dictionary <string, Property>();

            using (DbDataReader reader = db.ExecuteReader(Connection, string.Format(SELECT_COLUMN, table.TableName)))
            {
                int countOrder = 0;
                while (reader.Read())
                {
                    countOrder++;
                    string colName   = reader.GetValueAsString("name");
                    string dataType  = reader.GetValueAsString("type");
                    var    typeSplit = dataType.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                    int?   length    = null;
                    if (typeSplit.Length > 1)
                    {
                        dataType = typeSplit[0];
                        int lgth;
                        if (int.TryParse(typeSplit[1], out lgth))
                        {
                            length = lgth;
                        }
                    }


                    Property col = null;
                    if (length.HasValue)
                    {
                        //col = new Property(table, colName, mapper.SqlStringToDbType(dataType), length.Value);
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType))
                        {
                            Length = length.Value
                        };
                    }
                    else
                    {
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType));
                    }

                    col.SqlType = dataType;

                    bool isNullable = reader.GetValueAsInt("notnull") != 1;
                    col.IsPrimaryKey = reader.GetValueAsInt("pk") == 1;
                    if (col.IsPrimaryKey)
                    {
                        isNullable   = false;
                        col.IsUnique = true;
                    }

                    col.IsIdentity = false;
                    col.Order      = countOrder;
                    //if (isIdentity)
                    //   col.IsAutoGenerated = true;

                    col.IsNullable   = isNullable;
                    col.DefaultValue = reader.GetValueAsString("dflt_value");
                    col.ClrType      = Dialect.GetClrType(col.DbType, isNullable);

                    columnList.Add(colName, col);
                }
            }
            return(columnList);
        }
Example #3
0
        /// <summary>
        /// Processes the columns.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        protected virtual Dictionary <string, Property> ProcessColumns(EntityMap table)
        {
            Dictionary <string, Property> columnList = new Dictionary <string, Property>();

            using (DbDataReader reader = db.ExecuteReader(Connection, SelectColumns,
                                                          new QueryParam("tableName", table.TableName, DbType.String),
                                                          new QueryParam("schema", table.SchemaName, DbType.String)))
            {
                int countOrder = 0;
                while (reader.Read())
                {
                    countOrder++;
                    string colName   = reader.GetValueAsString("COLUMN_NAME");
                    string dataType  = reader.GetValueAsString("DATA_TYPE").ToLower();
                    int?   length    = reader.GetValueAsInt("CHARACTER_MAXIMUM_LENGTH");
                    int?   precision = reader.GetValueAsInt("NUMERIC_PRECISION");
                    int?   scale     = reader.GetValueAsInt("NUMERIC_SCALE");

                    logger.Log(LogLevel.Info, $"\t column: {colName} {dataType}({length ?? 0})");
                    Property col = null;
                    if (length.HasValue)
                    {
                        //col = new Property(table, colName, mapper.SqlStringToDbType(dataType), length.Value);
                        if (length < 0)
                        {
                            if (dataType.Equals("nvarchar") || dataType.Equals("nchar"))
                            {
                                length = 4000;
                            }
                            else if (dataType.Equals("varchar") || dataType.Equals("char"))
                            {
                                length = 8000;
                            }

                            else
                            {
                                length = 8000;
                            }
                        }
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType))
                        {
                            Length = length.Value
                        };
                    }
                    else
                    {
                        col = new Property(colName, colName, Dialect.SqlStringToDbType(dataType));
                    }

                    if (precision.HasValue)
                    {
                        col.Precision = precision.Value;
                    }
                    else
                    {
                        col.Precision = -1;
                    }
                    if (scale.HasValue)
                    {
                        col.Scale = scale.Value;
                    }
                    else
                    {
                        col.Scale = -1;
                    }

                    col.SqlType = dataType;
                    col.Order   = countOrder;

                    bool isNullable = reader.GetValueAsString("IS_NULLABLE").Equals("YES");
                    bool isIdentity = reader.GetValueAsInt("IsIdentity") == 1;

                    col.IsIdentity = isIdentity;
                    if (isIdentity)
                    {
                        col.IsAutoGenerated = true;
                    }

                    col.IsNullable   = isNullable;
                    col.DefaultValue = ProcessDefaultValue(reader.GetValueAsString("COLUMN_DEFAULT"));
                    col.ClrType      = Dialect.GetClrType(col.DbType, isNullable);
                    OnTableAddProperty(table, col);
                    columnList.Add(colName, col);
                }
            }
            return(columnList);
        }