Esempio n. 1
0
        private static String ColumnsClause(string tableName, DataColumnCollection dcc, ConstraintCollection ccc)
        {
            string[] columns = new String[dcc.Count];

            Int32 index = 0;

            foreach (DataColumn dc in dcc)
            {
                String columnName = dc.ColumnName;
                if (columnName == "oid")
                {
                    columnName = "poid";
                }

                columns[index++] = string.Format(" \"{0}\" {1}{2}",
                                                 columnName, PostGisDbUtility.GetTypeString(dc.DataType),
                                                 dc.DefaultValue == DBNull.Value
                                                     ?
                                                 ""
                                                     :
                                                 String.Format(" DEFAULT {0}",
                                                               String.Format(CultureInfo.InvariantCulture, "{0}",
                                                                             dc.DefaultValue)));
                ;
            }
            index = 0;

            string[] constraints = new String[ccc.Count];
            foreach (Constraint c in ccc)
            {
                UniqueConstraint uc = c as UniqueConstraint;
                if (uc != null)
                {
                    if (uc.IsPrimaryKey)
                    {
                        constraints[index++] = String.Format(
                            ", CONSTRAINT \"{0}\" PRIMARY KEY ({1})",
                            String.Format("PK_{0}_{1}", tableName, uc.ConstraintName),
                            ColumnNamesToCommaSeparatedString(uc.Columns));
                    }
                    else
                    {
                        constraints[index++] = String.Format(
                            ", CONSTRAINT \"{0}\" UNIQUE ({1})",
                            String.Format("UNI_{0}_{1}", tableName, uc.ConstraintName),
                            ColumnNamesToCommaSeparatedString(uc.Columns));
                    }
                }

                ForeignKeyConstraint fc = c as ForeignKeyConstraint;
                if (fc != null)
                {
                    constraints[index++] =
                        String.Format(
                            " CONSTRAINT \"{0}\" FOREIGN KEY ({1}) REFERENCES {2} ({3}) MATCH FULL ON UPDATE {4} ON DELETE {5}",
                            String.Format("FK_{0}_{1}_{3}", tableName, fc.RelatedTable.TableName, uc.ConstraintName),
                            ColumnNamesToCommaSeparatedString(fc.Columns),
                            fc.RelatedTable.TableName,
                            ColumnNamesToCommaSeparatedString(fc.RelatedColumns),
                            ruleToAction(fc.UpdateRule),
                            ruleToAction(fc.DeleteRule)
                            );
                }
            }

            String constraintsClause = "";

            if (index > 0)
            {
                Array.Resize(ref constraints, index);
                constraintsClause = String.Join(String.Empty, constraints);
            }
            return(String.Join(",", columns) + constraintsClause);
        }