开源项目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");
}
}
}
}
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");
}
}
}
}
相关文章推荐
- 开源项目DataTierGenerator学习(一)
- 开源项目DataTierGenerator学习(四)
- 开源项目DataTierGenerator学习(二)
- hadoop data 相关开源项目(近期学习计划)
- 2016年最值得学习的五大开源项目
- iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇
- 准备通过开源项目学习SWT
- 控件代码IOS开源项目(2)之RadioButton单选控件学习
- 分享《通过开源项目去学习》
- 商业项目应该向开源项目学习什么?
- 2016年最值得学习的五大开源项目
- 百度DMLC分布式深度机器学习开源项目(简称“深盟”)上线了如xgboost(速度快效果好的Boosting模型)、CXXNET(极致的C++深度学习库)、Minerva(高效灵活的并行深度学习引擎)以及Parameter Server(一小时训练600T数据)等产品,在语音识别、OCR识别、人脸识别以及计算效率提升上发布了多个成熟产品。
- Android开发者应该深入学习的10个开源应用项目
- Android开发者必须深入学习的10个应用开源项目
- ASP.NET MVC 开源项目学习之ProDinner (二)
- iReaperPlus一个开源的工具项目,旨在获得MSDN学习资料,希望你能加入进来
- 众多Android 开源项目再次推荐,学习不可错过
- [置顶] 从React和React Native中学习Facebook在开源项目中的行为准则【code of conduct】
- J2EE学习中一些值得研究的开源项目
- 学习DDD与MVC系统架构的开源项目