internal override void CreateTable(DbRecordInfo info) { StringBuilder sb = new StringBuilder("CREATE TABLE " + info.TableName + "("); DbIdentityRecordInfo identityRecordInfo = info as DbIdentityRecordInfo; if (identityRecordInfo != null) { DbFieldInfo key = identityRecordInfo.PrimaryKey; if (key.FieldType == typeof(Guid)) { sb.Append(GetDefinition(key) + " NOT NULL"); } else { sb.Append(GetDefinition(key) + " NOT NULL IDENTITY(1,1)"); } sb.Append(','); } foreach (DbFieldInfo field in info.Fields) { sb.Append(GetDefinition(field)); sb.Append(','); } string[] keys = getPrimaryKeys(info); sb.AppendFormat("PRIMARY KEY ({0})", String.Join(",", keys)); DbIndexesInfo indexes = DbAttributesManager.GetIndexes(info.Fields); ProcessIndexes(sb, indexes.Unique, ",UNIQUE ({0})"); //TODO: (MSSQL) CREATE FULLTEXT INDEX //TODO: (MSSQL) CREATE INDEX // ProcessIndexes(sb, indexes.Indexes, ",KEY {1} ({0})"); // ProcessIndexes(sb, indexes.FullText, ",FULLTEXT KEY {1} ({0})"); //process foreign keys foreach (KeyValuePair <Type, DbFieldInfo> key in info.ForeignKeys) { DbIdentityRecordInfo ri = DbAttributesManager.GetRecordInfo(key.Key) as DbIdentityRecordInfo; if (ri == null) { throw new NdbException("Only DbIdentityRecord objects can be used as Foreign Keys"); } sb.AppendFormat( ",FOREIGN KEY ([{1}]) REFERENCES [{0}] ([{2}]) ON DELETE CASCADE ON UPDATE CASCADE" , ri.TableName , key.Value.Name , ri.PrimaryKey.Name); } sb.Append(")"); string query = sb.ToString(); ExecuteNonQuery(query); }
internal override void CreateTable(DbRecordInfo info) { string postQueries = ""; /*; * CREATE TABLE tablename ( * colname integer NOT NULL DEFAULT nextval('tablename_colname_seq') * ); * ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;*/ StringBuilder sb = new StringBuilder("CREATE TABLE " + info.TableName + " ("); DbIdentityRecordInfo identityRecordInfo = info as DbIdentityRecordInfo; if (identityRecordInfo != null) { DbFieldInfo key = identityRecordInfo.PrimaryKey; if (key.FieldType == typeof(Guid)) { sb.Append(GetDefinition(key) + " NOT NULL"); sb.Append(','); } else { string sequenceName = string.Format("{0}_{1}_seq", info.TableName, key.Name); sb.Insert(0, "CREATE SEQUENCE " + sequenceName + ";"); postQueries = string.Format("ALTER SEQUENCE {0}_{1}_seq OWNED BY {0}.{1};", info.TableName, key.Name); sb.Append(GetDefinition(key) + " NOT NULL DEFAULT nextval('" + sequenceName + "'::regclass)"); sb.Append(','); } } foreach (DbFieldInfo field in info.Fields) { sb.Append(GetDefinition(field)); sb.Append(','); } string[] keys = getPrimaryKeys(info); sb.AppendFormat("PRIMARY KEY ({0}), UNIQUE ({0})", String.Join(",", keys)); // sb.AppendFormat("CONSTRAINT \"PK_{1}\" PRIMARY KEY ({0})", String.Join(",", keys), info.TableName); // sb.AppendFormat("CONSTRAINT \"U_{1}\" UNIQUE ({0})", String.Join(",", keys), info.TableName); //Process indexes DbIndexesInfo indexes = DbAttributesManager.GetIndexes(info.Fields); ProcessIndexes(sb, indexes.Unique, ",UNIQUE ({0})"); // ProcessIndexes(sb, indexes.Indexes, ",KEY {1} ({0})"); // ProcessIndexes(sb, indexes.FullText, ",FULLTEXT KEY {1} ({0})"); //process foreign keys foreach (KeyValuePair <Type, DbFieldInfo> key in info.ForeignKeys) { DbIdentityRecordInfo ri = DbAttributesManager.GetRecordInfo(key.Key) as DbIdentityRecordInfo; if (ri == null) { throw new NdbException("Only DbIdentityRecord objects can be used as Foreign Keys"); } sb.AppendFormat( ",FOREIGN KEY ({1}) REFERENCES {0} ({2}) ON DELETE CASCADE ON UPDATE CASCADE" , ri.TableName , key.Value.Name , ri.PrimaryKey.Name); } sb.Append(");"); sb.Append(postQueries); string query = sb.ToString(); ExecuteNonQuery(query); }