예제 #1
0
        /// <summary>
        /// Copies the table schema from a DmTableSurrogate object into a DmTable object.
        /// </summary>
        public void ReadSchemaIntoDmTable(DmTable dt)
        {
            if (dt == null)
            {
                throw new ArgumentNullException("dt", "DmTable");
            }

            dt.TableName        = this.TableName;
            dt.Culture          = new CultureInfo(this.CultureInfoName);
            dt.Schema           = this.Schema;
            dt.CaseSensitive    = this.CaseSensitive;
            dt.OriginalProvider = this.OriginalProvider;
            dt.SyncDirection    = this.SyncDirection;

            for (int i = 0; i < this.Columns.Count; i++)
            {
                DmColumn dmColumn = this.Columns[i].ConvertToDmColumn();
                dt.Columns.Add(dmColumn);
            }

            if (this.PrimaryKeys != null && this.PrimaryKeys.Count > 0)
            {
                DmColumn[] keyColumns = new DmColumn[this.PrimaryKeys.Count];

                for (int i = 0; i < this.PrimaryKeys.Count; i++)
                {
                    string columnName = this.PrimaryKeys[i];
                    keyColumns[i] = dt.Columns.First(c => dt.IsEqual(c.ColumnName, columnName));
                }

                DmKey key = new DmKey(keyColumns);

                dt.PrimaryKey = key;
            }
        }
        public string CreateTableCommandText()
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"CREATE TABLE {_trackingName.QuotedString} (");

            // Adding the primary key
            foreach (var pkColumn in _tableDescription.PrimaryKey.Columns)
            {
                var quotedColumnName = new ObjectNameParser(pkColumn.ColumnName, "\"", "\"").QuotedString;

                var columnTypeString = _mySqlDbMetadata.TryGetOwnerDbTypeString(pkColumn.OriginalDbType,
                                                                                pkColumn.DbType, false, false, _tableDescription.OriginalProvider,
                                                                                PostgreSqlSyncProvider.ProviderType);
                var unQuotedColumnType    = new ObjectNameParser(columnTypeString, "\"", "\"").UnquotedString;
                var columnPrecisionString = _mySqlDbMetadata.TryGetOwnerDbTypePrecision(pkColumn.OriginalDbType,
                                                                                        pkColumn.DbType, false, false, pkColumn.MaxLength, pkColumn.Precision, pkColumn.Scale,
                                                                                        _tableDescription.OriginalProvider, PostgreSqlSyncProvider.ProviderType);
                var columnType = $"{unQuotedColumnType} {columnPrecisionString}";

                stringBuilder.AppendLine($"{quotedColumnName} {columnType} NOT NULL, ");
            }

            // adding the tracking columns
            stringBuilder.AppendLine($"\"create_scope_id\" UUID NULL, ");
            stringBuilder.AppendLine($"\"update_scope_id\" UUID NULL, ");
            stringBuilder.AppendLine($"\"create_timestamp\" INT8 NULL, ");
            stringBuilder.AppendLine($"\"update_timestamp\" INT8 NULL, ");
            stringBuilder.AppendLine($"\"timestamp\" INT8 NULL, ");
            stringBuilder.AppendLine($"\"sync_row_is_tombstone\" INT2 NOT NULL default 0, ");
            stringBuilder.AppendLine($"\"last_change_datetime\" TIMESTAMP NULL, ");

            if (Filters != null && Filters.Count > 0)
            {
                foreach (var filter in Filters)
                {
                    var columnFilter = _tableDescription.Columns[filter.ColumnName];

                    if (columnFilter == null)
                    {
                        throw new InvalidExpressionException(
                                  $"Column {filter.ColumnName} does not exist in Table {_tableDescription.TableName.ToLowerInvariant()}");
                    }

                    var isPk = _tableDescription.PrimaryKey.Columns.Any(dm =>
                                                                        _tableDescription.IsEqual(dm.ColumnName.ToLowerInvariant(),
                                                                                                  filter.ColumnName.ToLowerInvariant()));
                    if (isPk)
                    {
                        continue;
                    }


                    var quotedColumnName = new ObjectNameParser(columnFilter.ColumnName, "\"", "\"").QuotedString;

                    var columnTypeString = _mySqlDbMetadata.TryGetOwnerDbTypeString(columnFilter.OriginalDbType,
                                                                                    columnFilter.DbType, false, false, _tableDescription.OriginalProvider,
                                                                                    PostgreSqlSyncProvider.ProviderType);
                    var unQuotedColumnType    = new ObjectNameParser(columnTypeString, "\"", "\"").UnquotedString;
                    var columnPrecisionString = _mySqlDbMetadata.TryGetOwnerDbTypePrecision(columnFilter.OriginalDbType,
                                                                                            columnFilter.DbType, false, false, columnFilter.MaxLength, columnFilter.Precision,
                                                                                            columnFilter.Scale, _tableDescription.OriginalProvider, PostgreSqlSyncProvider.ProviderType);
                    var columnType = $"{unQuotedColumnType} {columnPrecisionString}";

                    var nullableColumn = columnFilter.AllowDBNull ? "NULL" : "NOT NULL";

                    stringBuilder.AppendLine($"{quotedColumnName} {columnType} {nullableColumn}, ");
                }
            }

            stringBuilder.Append(" PRIMARY KEY (");
            for (var i = 0; i < _tableDescription.PrimaryKey.Columns.Length; i++)
            {
                var pkColumn         = _tableDescription.PrimaryKey.Columns[i];
                var quotedColumnName = new ObjectNameParser(pkColumn.ColumnName, "\"", "\"").QuotedObjectName;

                stringBuilder.Append(quotedColumnName);

                if (i < _tableDescription.PrimaryKey.Columns.Length - 1)
                {
                    stringBuilder.Append(", ");
                }
            }

            stringBuilder.Append("))");

            return(stringBuilder.ToString());
        }