コード例 #1
0
        /// <summary>
        /// Check if a column of a table exists and create it if not.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="table">The table name.</param>
        /// <param name="column">The column name.</param>
        /// <param name="type">The type of the column.</param>
        /// <param name="valueDefault">The default value.</param>
        /// <param name="valueNotNull">Indicate if not null.</param>
        /// <returns>True if the column exists else false even if created.</returns>
        static public bool CheckColumn(this SQLiteConnection connection,
                                       string table,
                                       string column,
                                       string type,
                                       string valueDefault,
                                       bool valueNotNull)
        {
            if (table.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(table));
            }
            if (column.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(column));
            }
            if (type.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!valueDefault.IsNullOrEmpty())
            {
                valueDefault = " DEFAULT " + valueDefault;
            }
            if (valueNotNull)
            {
                valueDefault += " NOT NULL";
            }
            string sql = $"ALTER TABLE %TABLE% ADD COLUMN %COLUMN% {type} {valueDefault}";

            return(connection.CheckColumn(table, column, sql));
        }