|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the Initial |
| 3 | + * Developer's Public License Version 1.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * https://github.com/FirebirdSQL/NETProvider/blob/master/license.txt. |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed on |
| 9 | + * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either |
| 10 | + * express or implied. See the License for the specific |
| 11 | + * language governing rights and limitations under the License. |
| 12 | + * |
| 13 | + * All Rights Reserved. |
| 14 | + */ |
| 15 | + |
| 16 | +//$Authors = Jiri Cincura (jiri@cincura.net) |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Data; |
| 20 | +using System.Globalization; |
| 21 | +using System.Text; |
| 22 | + |
| 23 | +using FirebirdSql.Data.Common; |
| 24 | +using FirebirdSql.Data.FirebirdClient; |
| 25 | + |
| 26 | +namespace FirebirdSql.Data.Schema |
| 27 | +{ |
| 28 | + internal class FbFunctionArguments : FbSchema |
| 29 | + { |
| 30 | + #region Protected Methods |
| 31 | + |
| 32 | + protected override StringBuilder GetCommandText(string[] restrictions) |
| 33 | + { |
| 34 | + var sql = new StringBuilder(); |
| 35 | + var where = new StringBuilder(); |
| 36 | + |
| 37 | + sql.AppendFormat( |
| 38 | + @"SELECT |
| 39 | + null AS FUNCTION_CATALOG, |
| 40 | + null AS FUNCTION_SCHEMA, |
| 41 | + fa.rdb$function_name AS FUNCTION_NAME, |
| 42 | + fa.rdb$argument_name AS ARGUMENT_NAME, |
| 43 | + null AS PARAMETER_DATA_TYPE, |
| 44 | + fld.rdb$field_sub_type AS PARAMETER_SUB_TYPE, |
| 45 | + fa.rdb$argument_position AS ORDINAL_POSITION, |
| 46 | + CAST(fld.rdb$field_length AS integer) AS PARAMETER_SIZE, |
| 47 | + CAST(fld.rdb$field_precision AS integer) AS NUMERIC_PRECISION, |
| 48 | + CAST(fld.rdb$field_scale AS integer) AS NUMERIC_SCALE, |
| 49 | + CAST(fld.rdb$character_length AS integer) AS CHARACTER_MAX_LENGTH, |
| 50 | + CAST(fld.rdb$field_length AS integer) AS CHARACTER_OCTET_LENGTH, |
| 51 | + coalesce(fld.rdb$null_flag, fa.rdb$null_flag) AS COLUMN_NULLABLE, |
| 52 | + null AS CHARACTER_SET_CATALOG, |
| 53 | + null AS CHARACTER_SET_SCHEMA, |
| 54 | + cs.rdb$character_set_name AS CHARACTER_SET_NAME, |
| 55 | + null AS COLLATION_CATALOG, |
| 56 | + null AS COLLATION_SCHEMA, |
| 57 | + coll.rdb$collation_name AS COLLATION_NAME, |
| 58 | + null AS COLLATION_CATALOG, |
| 59 | + null AS COLLATION_SCHEMA, |
| 60 | + fa.rdb$description AS DESCRIPTION, |
| 61 | + fld.rdb$field_type AS FIELD_TYPE, |
| 62 | + {0} AS PACKAGE_NAME |
| 63 | + FROM rdb$function_arguments fa |
| 64 | + LEFT JOIN rdb$fields fld ON fa.rdb$field_source = fld.rdb$field_name |
| 65 | + LEFT JOIN rdb$character_sets cs ON cs.rdb$character_set_id = fld.rdb$character_set_id |
| 66 | + LEFT JOIN rdb$collations coll ON (coll.rdb$collation_id = fld.rdb$collation_id AND coll.rdb$character_set_id = fld.rdb$character_set_id)", |
| 67 | + MajorVersionNumber >= 3 ? "fa.rdb$package_name" : "null"); |
| 68 | + |
| 69 | + if (restrictions != null) |
| 70 | + { |
| 71 | + var index = 0; |
| 72 | + |
| 73 | + /* FUNCTION_CATALOG */ |
| 74 | + if (restrictions.Length >= 1 && restrictions[0] != null) |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + /* FUNCTION_SCHEMA */ |
| 79 | + if (restrictions.Length >= 2 && restrictions[1] != null) |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + /* FUNCTION_NAME */ |
| 84 | + if (restrictions.Length >= 3 && restrictions[2] != null) |
| 85 | + { |
| 86 | + where.AppendFormat("fa.rdb$function_name = @p{0}", index++); |
| 87 | + } |
| 88 | + |
| 89 | + /* PARAMETER_NAME */ |
| 90 | + if (restrictions.Length >= 4 && restrictions[3] != null) |
| 91 | + { |
| 92 | + if (where.Length > 0) |
| 93 | + { |
| 94 | + where.Append(" AND "); |
| 95 | + } |
| 96 | + |
| 97 | + where.AppendFormat("fa.rdb$argument_name = @p{0}", index++); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (where.Length > 0) |
| 102 | + { |
| 103 | + sql.AppendFormat(" WHERE {0} ", where.ToString()); |
| 104 | + } |
| 105 | + |
| 106 | + sql.Append(" ORDER BY PACKAGE_NAME, FUNCTION_NAME, ORDINAL_POSITION"); |
| 107 | + |
| 108 | + return sql; |
| 109 | + } |
| 110 | + |
| 111 | + protected override void ProcessResult(DataTable schema) |
| 112 | + { |
| 113 | + schema.BeginLoadData(); |
| 114 | + schema.Columns.Add("IS_NULLABLE", typeof(bool)); |
| 115 | + |
| 116 | + foreach (DataRow row in schema.Rows) |
| 117 | + { |
| 118 | + var blrType = Convert.ToInt32(row["FIELD_TYPE"], CultureInfo.InvariantCulture); |
| 119 | + |
| 120 | + var subType = 0; |
| 121 | + if (row["PARAMETER_SUB_TYPE"] != DBNull.Value) |
| 122 | + { |
| 123 | + subType = Convert.ToInt32(row["PARAMETER_SUB_TYPE"], CultureInfo.InvariantCulture); |
| 124 | + } |
| 125 | + |
| 126 | + var scale = 0; |
| 127 | + if (row["NUMERIC_SCALE"] != DBNull.Value) |
| 128 | + { |
| 129 | + scale = Convert.ToInt32(row["NUMERIC_SCALE"], CultureInfo.InvariantCulture); |
| 130 | + } |
| 131 | + |
| 132 | + row["IS_NULLABLE"] = (row["COLUMN_NULLABLE"] == DBNull.Value); |
| 133 | + |
| 134 | + var dbType = (FbDbType)TypeHelper.GetDbDataTypeFromBlrType(blrType, subType, scale); |
| 135 | + row["PARAMETER_DATA_TYPE"] = TypeHelper.GetDataTypeName((DbDataType)dbType).ToLowerInvariant(); |
| 136 | + |
| 137 | + if (dbType == FbDbType.Char || dbType == FbDbType.VarChar) |
| 138 | + { |
| 139 | + row["PARAMETER_SIZE"] = row["CHARACTER_MAX_LENGTH"]; |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + row["CHARACTER_OCTET_LENGTH"] = 0; |
| 144 | + } |
| 145 | + |
| 146 | + if (dbType == FbDbType.Binary || dbType == FbDbType.Text) |
| 147 | + { |
| 148 | + row["PARAMETER_SIZE"] = Int32.MaxValue; |
| 149 | + } |
| 150 | + |
| 151 | + if (row["NUMERIC_PRECISION"] == DBNull.Value) |
| 152 | + { |
| 153 | + row["NUMERIC_PRECISION"] = 0; |
| 154 | + } |
| 155 | + |
| 156 | + if ((dbType == FbDbType.Decimal || dbType == FbDbType.Numeric) && |
| 157 | + (row["NUMERIC_PRECISION"] == DBNull.Value || Convert.ToInt32(row["NUMERIC_PRECISION"]) == 0)) |
| 158 | + { |
| 159 | + row["NUMERIC_PRECISION"] = row["PARAMETER_SIZE"]; |
| 160 | + } |
| 161 | + |
| 162 | + row["NUMERIC_SCALE"] = (-1) * scale; |
| 163 | + } |
| 164 | + |
| 165 | + schema.EndLoadData(); |
| 166 | + schema.AcceptChanges(); |
| 167 | + |
| 168 | + // Remove not more needed columns |
| 169 | + schema.Columns.Remove("COLUMN_NULLABLE"); |
| 170 | + schema.Columns.Remove("FIELD_TYPE"); |
| 171 | + schema.Columns.Remove("CHARACTER_MAX_LENGTH"); |
| 172 | + } |
| 173 | + |
| 174 | + #endregion |
| 175 | + } |
| 176 | +} |
0 commit comments