Ejemplo n.º 1
0
        private string BuildCreateTableColumnSql(ColumnInfo column)
        {
            var sb = new StringBuilder();
            sb.Append(_escaper(column.Name));
            var isPrimaryKey = column.IsPrimaryKey ? " PRIMARY KEY NOT NULL" : "";
            switch (column.DbType)
            {
                case OleDbType.Char:
                    if (column.CharacterMaxLength >= int.MaxValue)
                    {
                        sb.AppendFormat("TEXT");
                    }
                    else
                    {
                        sb.AppendFormat("VARCHAR({0})", column.CharacterMaxLength);
                    }
                    break;
                case OleDbType.Integer:
                    if (column.NumericPrecision != 4)
                        throw new NotSupportedException(string.Format("Only int(4) is supported at this time..."));

                    sb.AppendFormat("{0}{1}", _integerType, isPrimaryKey);
                    break;
                case OleDbType.Numeric:
                    const string numericVarType = "NUMERIC";
                    if (column.NumericPrecision != null)
                    {
                        if (column.NumericScale != null && column.NumericScale > 0)
                        {
                            sb.AppendFormat("{0}({1},{2}){3}", numericVarType, column.NumericPrecision, column.NumericScale,
                                            isPrimaryKey);
                        }
                        else
                        {
                            sb.AppendFormat("{0}({1}){2}", numericVarType, column.NumericPrecision, isPrimaryKey);
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0}{1}", numericVarType, isPrimaryKey);
                    }
                    break;
                case OleDbType.Boolean:
                    sb.AppendFormat("{0}{1}", _booleanType, isPrimaryKey);
                    break;
                case OleDbType.DBDate:
                    sb.AppendFormat("{0}{1}", _dateTimeTypeName, isPrimaryKey);
                    break;
                case OleDbType.Binary:
                    sb.AppendFormat(_blobType);
                    break;
            }
            return sb.ToString();
        }
 private static string FormatColumn(TableInfo table, ColumnInfo column)
 {
     OleDbType dbType;
     if (table.IsDbTypeOverridden(column.Name, out dbType))
     {
         switch (dbType)
         {
             case OleDbType.Binary:
                 return string.Format("CAST({0} as Blob) as {0}", column.Name);
             default:
                 throw new InvalidOperationException(string.Format("Unsupported dbType {0} for column {1}", dbType, column.Name));
         }
     }
     return column.Name;
 }
Ejemplo n.º 3
0
        private static void AddColumn(TableInfo tableInfo, DataRow row)
        {
            const string numericPrecisionColumn = "NUMERIC_PRECISION";
            const string numericScaleColumn = "NUMERIC_SCALE";
            const string characterMaximumLengthColumn = "CHARACTER_MAXIMUM_LENGTH";

            var columnName = row["COLUMN_NAME"].ToString();
            if (tableInfo.IsIgnoredColumn(columnName))
                return;

            var dbType = (OleDbType) row["DATA_TYPE"];
            OleDbType overiddenDbType;
            if (tableInfo.IsDbTypeOverridden(columnName, out overiddenDbType))
                dbType = overiddenDbType;

            var ordinalPosition = (Int64) row["ORDINAL_POSITION"];
            int? numericPrecision = null;
            if (row[numericPrecisionColumn] != DBNull.Value)
            {
                numericPrecision = int.Parse(row[numericPrecisionColumn].ToString());
            }
            int? numericScale = null;
            if (row[numericScaleColumn] != DBNull.Value)
            {
                numericScale = int.Parse(row[numericScaleColumn].ToString());
            }
            long? maxLength = null;
            if (row[characterMaximumLengthColumn] != DBNull.Value)
            {
                maxLength = long.Parse(row[characterMaximumLengthColumn].ToString());
            }

            long overriddenMaxLength;
            if (tableInfo.IsMaxLengthOverridden(columnName, out overriddenMaxLength))
            {
                maxLength = overriddenMaxLength;
            }

            switch (dbType)
            {
                case OleDbType.Char:
                case OleDbType.Integer:
                case OleDbType.Numeric:
                case OleDbType.Boolean:
                case OleDbType.DBDate:
                case OleDbType.Binary:
                    var columnInfo = new ColumnInfo(columnName, ordinalPosition, dbType, maxLength, numericPrecision,
                                                    numericScale,
                                                    tableInfo.PrimaryKey != null && columnName.Equals(tableInfo.PrimaryKey));
                    tableInfo.AddColumn(columnInfo);
                    break;
                default:
                    throw new InvalidOperationException(string.Format("Unknown database type: {0}", dbType));
            }
        }
Ejemplo n.º 4
0
 private void ConfigureParameter(IDbDataParameter parameter, ColumnInfo column)
 {
     parameter.ParameterName = _parameterNameBuilder(column.Name);
     switch (column.DbType)
     {
         case OleDbType.Char:
             if (column.CharacterMaxLength >= int.MaxValue)
             {
                 parameter.DbType = DbType.String;
             }
             else
             {
                 parameter.DbType = DbType.StringFixedLength;
                 parameter.Size = int.Parse(column.CharacterMaxLength.ToString());
             }
             break;
         case OleDbType.Integer:
             parameter.DbType = DbType.Int32;
             break;
         case OleDbType.Numeric:
             parameter.DbType = DbType.Int32;
             break;
         case OleDbType.Boolean:
             parameter.DbType = DbType.Boolean;
             break;
         case OleDbType.Binary:
             parameter.DbType = DbType.Binary;
             break;
         case OleDbType.DBDate:
             parameter.DbType = DbType.DateTime;
             break;
     }
 }
Ejemplo n.º 5
0
        private static void AddColumn(TableInfo tableInfo, DataRow row)
        {
            const string numericPrecisionColumn       = "NUMERIC_PRECISION";
            const string numericScaleColumn           = "NUMERIC_SCALE";
            const string characterMaximumLengthColumn = "CHARACTER_MAXIMUM_LENGTH";

            var columnName = row["COLUMN_NAME"].ToString();

            if (tableInfo.IsIgnoredColumn(columnName))
            {
                return;
            }

            var       dbType = (OleDbType)row["DATA_TYPE"];
            OleDbType overiddenDbType;

            if (tableInfo.IsDbTypeOverridden(columnName, out overiddenDbType))
            {
                dbType = overiddenDbType;
            }

            var ordinalPosition  = (Int64)row["ORDINAL_POSITION"];
            int?numericPrecision = null;

            if (row[numericPrecisionColumn] != DBNull.Value)
            {
                numericPrecision = int.Parse(row[numericPrecisionColumn].ToString());
            }
            int?numericScale = null;

            if (row[numericScaleColumn] != DBNull.Value)
            {
                numericScale = int.Parse(row[numericScaleColumn].ToString());
            }
            long?maxLength = null;

            if (row[characterMaximumLengthColumn] != DBNull.Value)
            {
                maxLength = long.Parse(row[characterMaximumLengthColumn].ToString());
            }

            long overriddenMaxLength;

            if (tableInfo.IsMaxLengthOverridden(columnName, out overriddenMaxLength))
            {
                maxLength = overriddenMaxLength;
            }

            switch (dbType)
            {
            case OleDbType.Char:
            case OleDbType.Integer:
            case OleDbType.Numeric:
            case OleDbType.Boolean:
            case OleDbType.DBDate:
            case OleDbType.Binary:
                var columnInfo = new ColumnInfo(columnName, ordinalPosition, dbType, maxLength, numericPrecision,
                                                numericScale,
                                                tableInfo.PrimaryKey != null && columnName.Equals(tableInfo.PrimaryKey));
                tableInfo.AddColumn(columnInfo);
                break;

            default:
                throw new InvalidOperationException(string.Format("Unknown database type: {0}", dbType));
            }
        }
Ejemplo n.º 6
0
 public TableInfo AddColumn(ColumnInfo columnInfo)
 {
     _columns.Add(columnInfo);
     return this;
 }