您的位置:首页 > 其它

开源项目DataTierGenerator学习(三)

2007-05-18 15:06 295 查看
1、生成用户
internal static void CreateUserQueries(string databaseName, string grantLoginName, string path, bool createMultipleFiles) {
if (grantLoginName.Length > 0) {
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, "GrantUserPermissions.sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
writer.Write(Utility.GetUserQueries(databaseName, grantLoginName));
}
}
}

2、生成插入的存储过程

internal static void CreateInsertStoredProcedure(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
// Create the stored procedure name
string procedureName = storedProcedurePrefix + table.Name + "Insert";
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];
if (column.IsIdentity == false && column.IsRowGuidCol == false) {
writer.Write("/t" + Utility.CreateParameterString(column, true));
if (i < (table.Columns.Count - 1)) {
writer.Write(",");
}
writer.WriteLine();
}
}
writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();

// Initialize all RowGuidCol columns
foreach (Column column in table.Columns) {
if (column.IsRowGuidCol) {
writer.WriteLine("SET @" + column.Name + " = NEWID()");
writer.WriteLine();
break;
}
}

writer.WriteLine("INSERT INTO [" + table.Name + "] (");

// Create the parameter list
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

// Ignore any identity columns
if (column.IsIdentity == false) {
// Append the column name as a parameter of the insert statement
if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t[" + column.Name + "],");
} else {
writer.WriteLine("/t[" + column.Name + "]");
}
}
}

writer.WriteLine(") VALUES (");

// Create the values list
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

// Is the current column an identity column?
if (column.IsIdentity == false) {
// Append the necessary line breaks and commas
if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t@" + column.Name + ",");
} else {
writer.WriteLine("/t@" + column.Name);
}
}
}

writer.WriteLine(")");

// Should we include a line for returning the identity?
foreach (Column column in table.Columns) {
// Is the current column an identity column?
if (column.IsIdentity) {
writer.WriteLine();
writer.WriteLine("SELECT SCOPE_IDENTITY()");
break;
} else if (column.IsRowGuidCol) {
writer.WriteLine();
writer.WriteLine("SELECT @" + column.Name);
break;
}
}

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}

3、生成更新的存储过程

internal static void CreateUpdateStoredProcedure(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
if (table.PrimaryKeys.Count > 0 && table.Columns.Count != table.PrimaryKeys.Count && table.Columns.Count != table.ForeignKeys.Count) {
// Create the stored procedure name
string procedureName = storedProcedurePrefix + table.Name + "Update";
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false) + ",");
} else {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false));
}
}
writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("UPDATE");
writer.WriteLine("/t[" + table.Name + "]");
writer.WriteLine("SET");

// Create the set statement
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column)table.Columns[i];

// Ignore Identity and RowGuidCol columns
if (table.PrimaryKeys.Contains(column) == false) {
if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name + ",");
} else {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name);
}
}
}

writer.WriteLine("WHERE");

// Create the where clause
for (int i = 0; i < table.PrimaryKeys.Count; i++) {
Column column = (Column) table.PrimaryKeys[i];

if (i > 0) {
writer.Write("/tAND [" + column.Name + "] = @" + column.Name);
} else {
writer.Write("/t [" + column.Name + "] = @" + column.Name);
}
}
writer.WriteLine();

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}

4、生成删除的存储过程
internal static void CreateDeleteStoredProcedure(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
if (table.PrimaryKeys.Count > 0) {
// Create the stored procedure name
string procedureName = storedProcedurePrefix + table.Name + "Delete";
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < table.PrimaryKeys.Count; i++) {
Column column = (Column) table.PrimaryKeys[i];

if (i < (table.PrimaryKeys.Count - 1)) {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false) + ",");
} else {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false));
}
}
writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("DELETE FROM");
writer.WriteLine("/t[" + table.Name + "]");
writer.WriteLine("WHERE");

// Create the where clause
for (int i = 0; i < table.PrimaryKeys.Count; i++) {
Column column = (Column) table.PrimaryKeys[i];

if (i > 0) {
writer.WriteLine("/tAND [" + column.Name + "] = @" + column.Name);
} else {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name);
}
}

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}

5、生成外键删除的存储过程
internal static void CreateDeleteAllByStoredProcedures(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
// Create a stored procedure for each foreign key
foreach (ArrayList compositeKeyList in table.ForeignKeys.Values) {
// Create the stored procedure name
StringBuilder stringBuilder = new StringBuilder(255);
stringBuilder.Append(storedProcedurePrefix + table.Name + "DeleteAllBy");

// Create the parameter list
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];
if (i > 0) {
stringBuilder.Append("_" + Utility.FormatPascal(column.Name));
} else {
stringBuilder.Append(Utility.FormatPascal(column.Name));
}
}

string procedureName = stringBuilder.ToString();
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];

if (i < (compositeKeyList.Count - 1)) {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false) + ",");
} else {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false));
}
}
writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("DELETE FROM");
writer.WriteLine("/t[" + table.Name + "]");
writer.WriteLine("WHERE");

// Create the where clause
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];

if (i > 0) {
writer.WriteLine("/tAND [" + column.Name + "] = @" + column.Name);
} else {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name);
}
}

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}

6、生成 关系表的查询存储过程
internal static void CreateSelectStoredProcedure(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
if (table.PrimaryKeys.Count > 0 && table.ForeignKeys.Count != table.Columns.Count) {
// Create the stored procedure name
string procedureName = storedProcedurePrefix + table.Name + "Select";
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < table.PrimaryKeys.Count; i++) {
Column column = (Column) table.PrimaryKeys[i];

if (i == (table.PrimaryKeys.Count - 1)) {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false));
} else {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false) + ",");
}
}

writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("SELECT");

// Create the list of columns
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t[" + column.Name + "],");
} else {
writer.WriteLine("/t[" + column.Name + "]");
}
}

writer.WriteLine("FROM");
writer.WriteLine("/t[" + table.Name + "]");
writer.WriteLine("WHERE");

// Create the where clause
for (int i = 0; i < table.PrimaryKeys.Count; i++) {
Column column = (Column) table.PrimaryKeys[i];

if (i > 0) {
writer.WriteLine("/tAND [" + column.Name + "] = @" + column.Name);
} else {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name);
}
}

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}

7、CreateSelectAllStoredProcedure

internal static void CreateSelectAllStoredProcedure(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
if (table.PrimaryKeys.Count > 0 && table.ForeignKeys.Count != table.Columns.Count) {
// Create the stored procedure name
string procedureName = storedProcedurePrefix + table.Name + "SelectAll";
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "]");
writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("SELECT");

// Create the list of columns
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t[" + column.Name + "],");
} else {
writer.WriteLine("/t[" + column.Name + "]");
}
}

writer.WriteLine("FROM");
writer.WriteLine("/t[" + table.Name + "]");

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}

8、生成为关系表的外键联合生成的存储过程

internal static void CreateSelectAllByStoredProcedures(Table table, string grantLoginName, string storedProcedurePrefix, string path, bool createMultipleFiles) {
// Create a stored procedure for each foreign key
foreach (ArrayList compositeKeyList in table.ForeignKeys.Values) {
// Create the stored procedure name
StringBuilder stringBuilder = new StringBuilder(255);
stringBuilder.Append(storedProcedurePrefix + table.Name + "SelectAllBy");

// Create the parameter list
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];
if (i > 0) {
stringBuilder.Append("_" + Utility.FormatPascal(column.Name));
} else {
stringBuilder.Append(Utility.FormatPascal(column.Name));
}
}

string procedureName = stringBuilder.ToString();
string fileName;

// Determine the file name to be used
if (createMultipleFiles) {
fileName = Path.Combine(path, procedureName + ".sql");
} else {
fileName = Path.Combine(path, "StoredProcedures.sql");
}

using (StreamWriter writer = new StreamWriter(fileName, true)) {
// Create the seperator
if (createMultipleFiles == false) {
writer.WriteLine();
writer.WriteLine("/******************************************************************************");
writer.WriteLine("******************************************************************************/");
}

// Create the drop statment
writer.WriteLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + procedureName + "]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)");
writer.WriteLine("/tdrop procedure [dbo].[" + procedureName + "]");
writer.WriteLine("GO");
writer.WriteLine();

// Create the SQL for the stored procedure
writer.WriteLine("CREATE PROCEDURE [dbo].[" + procedureName + "] (");

// Create the parameter list
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];

if (i < (compositeKeyList.Count - 1)) {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false) + ",");
} else {
writer.WriteLine("/t" + Utility.CreateParameterString(column, false));
}
}
writer.WriteLine(")");

writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine();
writer.WriteLine("SET NOCOUNT ON");
writer.WriteLine();
writer.WriteLine("SELECT");

// Create the list of columns
for (int i = 0; i < table.Columns.Count; i++) {
Column column = (Column) table.Columns[i];

if (i < (table.Columns.Count - 1)) {
writer.WriteLine("/t[" + column.Name + "],");
} else {
writer.WriteLine("/t[" + column.Name + "]");
}
}

writer.WriteLine("FROM");
writer.WriteLine("/t[" + table.Name + "]");
writer.WriteLine("WHERE");

// Create the where clause
for (int i = 0; i < compositeKeyList.Count; i++) {
Column column = (Column) compositeKeyList[i];

if (i > 0) {
writer.WriteLine("/tAND [" + column.Name + "] = @" + column.Name);
} else {
writer.WriteLine("/t[" + column.Name + "] = @" + column.Name);
}
}

writer.WriteLine("GO");

// Create the grant statement, if a user was specified
if (grantLoginName.Length > 0) {
writer.WriteLine();
writer.WriteLine("GRANT EXECUTE ON [dbo].[" + procedureName + "] TO [" + grantLoginName + "]");
writer.WriteLine("GO");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐