示例#1
0
 private void Initialize()
 {
     Table.TableName = TableName;
     InitializeColumns();
     Table.PrimaryKey = PrimaryKeyColumns.Select(i => Table.Columns[i]).ToArray();
     IsInitialized    = true;
 }
 private void CreateTempTablePrimaryKey(string tempTableName)
 {
     using (var cmd = CreateCommandForPrimaryKey())
     {
         string pkList = string.Join(", ", PrimaryKeyColumns.Select(item => $"[{item}]"));
         cmd.CommandText = $"ALTER TABLE [{tempTableName}] " +
                           $"ADD CONSTRAINT [PK_{tempTableName.Trim(PrefixTempTable)}] " +
                           $"PRIMARY KEY NONCLUSTERED ({pkList})";
         cmd.ExecuteNonQuery();
     }
 }
示例#3
0
        public virtual string GetCreateSql(string tableName)
        {
            string sql = "CREATE ";

            if (IsVirtual)
            {
                sql += "VIRTUAL ";
            }

            sql += "TABLE " + SQLiteStatement.EscapeName(tableName);

            if (!IsVirtual)
            {
                sql += " (";
                sql += string.Join(",", Columns.Select(c => c.GetCreateSql(SQLiteCreateSqlOptions.ForCreateColumn)));

                if (PrimaryKeyColumns.Skip(1).Any())
                {
                    string pk = string.Join(",", PrimaryKeyColumns.Select(c => c.EscapedName));
                    if (!string.IsNullOrWhiteSpace(pk))
                    {
                        sql += ",PRIMARY KEY (" + pk + ")";
                    }
                }

                sql += ")";
            }

            if (DisableRowId)
            {
                // https://sqlite.org/withoutrowid.html
                sql += " WITHOUT ROWID";
            }

            if (IsVirtual)
            {
                sql += " USING " + Module;
                if (ModuleArguments != null && ModuleArguments.Length > 0)
                {
                    sql += "(" + string.Join(",", ModuleArguments) + ")";
                }
            }
            return(sql);
        }
        /// <summary>
        /// Creates a primary key for temporary table.
        /// </summary>
        /// <param name="tempTableName">Name of the temporary table.</param>
        /// <param name="columnName">Name of the column which must be created in temp table. If the value
        /// is <see langword="null"/>, no column is created, just the primary key.</param>
        private void CreateTempTablePrimaryKey(string tempTableName, string columnName)
        {
            using (var cmd = CreateCommandForPrimaryKey())
            {
                if (columnName != null)
                {
                    cmd.CommandText = "SELECT data_type FROM information_schema.columns " +
                                      $"WHERE table_name = '{DestinationTableName}' AND column_name = '{columnName}'";
                    string dataType = (string)cmd.ExecuteScalar();

                    cmd.CommandText = $"ALTER TABLE [{tempTableName}] ADD [{columnName}] [{dataType}] NOT NULL";
                    cmd.ExecuteNonQuery();
                }
                string pkList = string.Join(", ", PrimaryKeyColumns.Select(item => $"[{item}]"));
                cmd.CommandText = $"ALTER TABLE [{tempTableName}] " +
                                  $"ADD CONSTRAINT [PK_{tempTableName.Trim(PrefixTempTable)}] " +
                                  $"PRIMARY KEY NONCLUSTERED ({pkList})";
                cmd.ExecuteNonQuery();
            }
        }
示例#5
0
        public virtual string BuildCreateSql(string tableName)
        {
            string sql = "CREATE TABLE " + SQLiteStatement.EscapeName(tableName) + " (";

            sql += string.Join(",", Columns.Select(c => c.GetCreateSql(SQLiteCreateSqlOptions.ForCreateColumn)));

            if (PrimaryKeyColumns.Count() > 1)
            {
                string pk = string.Join(",", PrimaryKeyColumns.Select(c => c.EscapedName));
                if (!string.IsNullOrWhiteSpace(pk))
                {
                    sql += ",PRIMARY KEY (" + pk + ")";
                }
            }

            sql += ")";

            if (DisableRowId)
            {
                // https://sqlite.org/withoutrowid.html
                sql += " WITHOUT ROWID";
            }
            return(sql);
        }
        /// <inheritdoc/>
        /// <exception cref="ValidationException">Empty secondary key columns collection.</exception>
        protected override void ValidateState()
        {
            using (var ea = new ExceptionAggregator()) {
                ea.Execute(base.ValidateState);

                // Secondary key columns: empty set, duplicates
                var keyColumns = KeyColumns.Select(valueRef => valueRef.Value).ToList();
                if (keyColumns.Count == 0)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExEmptyKeyColumnsCollection, Path);
                    });
                }
                foreach (var group in keyColumns
                         .GroupBy(keyColumn => keyColumn)
                         .Where(group => group.Count() > 1))
                {
                    ea.Execute((_column) => {
                        throw new ValidationException(
                            string.Format(Strings.ExMoreThenOneKeyColumnReferenceToColumnX, _column.Name),
                            Path);
                    }, group.Key);
                }

                // Primary key columns
                if (PrimaryKeyColumns.Count != Parent.PrimaryIndex.KeyColumns.Count)
                {
                    ea.Execute(() => {
                        throw new ValidationException(Strings.ExInvalidPrimaryKeyColumnsCollection, Path);
                    });
                }
                for (int i = 0; i < PrimaryKeyColumns.Count; i++)
                {
                    var ref1 = PrimaryKeyColumns[i];
                    var ref2 = Parent.PrimaryIndex.KeyColumns[i];
                    if (ref1.Value != ref2.Value || ref1.Direction != ref2.Direction)
                    {
                        ea.Execute(() => {
                            throw new ValidationException(Strings.ExInvalidPrimaryKeyColumnsCollection, Path);
                        });
                    }
                }

                // Included columns
                var fullKeySet = EnumerableExtensions.ToHashSet(KeyColumns
                                                                .Select(cr => cr.Value)
                                                                .Concat(PrimaryKeyColumns.Select(cr => cr.Value)));

                foreach (var columnRef in IncludedColumns)
                {
                    if (fullKeySet.Contains(columnRef.Value))
                    {
                        ea.Execute(() => {
                            throw new ValidationException(Strings.ExInvalidIncludedColumnsCollection, Path);
                        });
                    }
                }

                foreach (var group in IncludedColumns
                         .GroupBy(keyColumn => keyColumn)
                         .Where(group => group.Count() > 1))
                {
                    ea.Execute((_column) => {
                        throw new ValidationException(
                            string.Format(Strings.ExMoreThenOneIncludedColumnReferenceToColumnX, _column.Name),
                            Path);
                    }, group.Key);
                }

                ea.Complete();
            }
        }
示例#7
0
 public virtual string BuildWherePrimaryKeyStatement() => string.Join(" AND ", PrimaryKeyColumns.Select(c => SQLiteStatement.EscapeName(c.Name) + "=?"));