codesmith连接postgresql修复bug
转:CodeSmith7代码生成器针对PostgreSQL数据库无法使用的Bug修复全过程
我自己又修改过,完整的PostgreSQLSchemaProvider.cs文件如下
using Npgsql;
using NpgsqlTypes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions; namespace SchemaExplorer
{
public class PostgreSQLSchemaProvider : IDbSchemaProvider, IDbConnectionStringEditor
{
public string Name
{
get
{
return "PostgreSQLSchemaProvider";
}
} public string Description
{
get
{
return "PostgreSQL Schema Provider";
}
} public string ConnectionString
{
get
{
return string.Empty;
}
} public bool EditorAvailable
{
get
{
return false;
}
} public bool ShowEditor(string currentConnectionString)
{
return false;
} public TableSchema[] GetTables(string connectionString, DatabaseSchema database)
{
List<TableSchema> list = new List<TableSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand("select tablename, tableowner,obj_description(relfilenode,'pg_class') as pg_description from pg_catalog.pg_tables as t left join pg_catalog.pg_class as c on t.tablename = c.relname where t.schemaname = 'public' order by t.tablename", npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
if (!npgsqlDataReader.IsDBNull() && npgsqlDataReader.GetString().ToUpper() != "CODESMITH_EXTENDED_PROPERTIES")
{
list.Add(new TableSchema(database, npgsqlDataReader.GetString(), npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString(), DateTime.MinValue, new ExtendedProperty[] { new ExtendedProperty("CS_Description", npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString(), DbType.String, PropertyStateEnum.ReadOnly) }));
}
}
}
}
}
return list.ToArray();
} public IndexSchema[] GetTableIndexes(string connectionString, TableSchema table)
{
List<IndexSchema> list = new List<IndexSchema>();
if (table.Columns == null)
table.Columns.AddRange(GetTableColumns(connectionString, table));
DataSet ds = new DataSet();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select schemaname, tablename, indexname, tablespace from pg_catalog.pg_indexes where schemaname='public' and tablename = '{0}'", table.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataAdapter nda = new NpgsqlDataAdapter(npgsqlCommand))
{
nda.Fill(ds, "pg_indexes");
} foreach (DataRow item in ds.Tables["pg_indexes"].Rows)
{
string @string = string.Empty;
@string = item.Field<string>("indexname");
if (!string.IsNullOrEmpty(@string))
{
string text2 = string.Format("SELECT n.nspname AS schemaname, c.relname AS tablename, i.relname AS indexname, t.spcname AS \"tablespace\", a.attname as \"colname\", x.indisunique as \"unique\", x.indisprimary as \"primary\", x.indisclustered as \"clustered\" FROM pg_catalog.pg_index x JOIN pg_catalog.pg_class c ON c.oid = x.indrelid JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid JOIN pg_catalog.pg_attribute a ON a.attrelid = i.relfilenode LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_catalog.pg_tablespace t ON t.oid = i.reltablespace WHERE c.relkind = 'r'::\"char\" AND i.relkind = 'i'::\"char\" AND n.nspname='public' AND c.relname='{0}' AND i.relname ='{1}'", table.Name, @string); using (NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand(text2, npgsqlConnection))
{
using (NpgsqlDataAdapter nda2 = new NpgsqlDataAdapter(npgsqlCommand2))
{
nda2.Fill(ds, "pg_catalog");
}
}
List<string> list2 = new List<string>();
bool? isPrimaryKey = false;
bool? isUnique = false;
bool? isClustered = false;
foreach (DataRow item2 in ds.Tables["pg_catalog"].Rows)
{
isPrimaryKey = item2.Field<bool?>("primary");// (!npgsqlDataReader2.IsDBNull(6) && npgsqlDataReader2.GetBoolean(6));
isUnique = item2.Field<bool?>("unique"); //(!npgsqlDataReader2.IsDBNull(5) && npgsqlDataReader2.GetBoolean(5));
isClustered = item2.Field<bool?>("clustered"); //(!npgsqlDataReader2.IsDBNull(7) && npgsqlDataReader2.GetBoolean(7));
list2.Add(item2.Field<string>("colname"));
}
list.Add(new IndexSchema(table, @string, isPrimaryKey ?? false, isUnique ?? false, isClustered ?? false, list2.ToArray()));
list2.Clear();
ds.Tables["pg_catalog"].Clear();
}
}
}
}
return list.ToArray();
} public ColumnSchema[] GetTableColumns(string connectionString, TableSchema table)
{
List<ColumnSchema> list = new List<ColumnSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select column_name, is_nullable, character_maximum_length, numeric_precision, numeric_scale, data_type, udt_name,col_description(b.attrelid,b.attnum) as pg_description from information_schema.columns as a join pg_attribute as b on a.column_name=b.attname join pg_class as c on a.table_name=c.relname and b.attrelid = c.oid where b.attnum>0 and a.table_schema = 'public' and a.table_name='{0}'", table.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
bool allowDBNull = npgsqlDataReader.IsDBNull() || npgsqlDataReader.GetString() == "YES";
byte precision = (byte)(npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32());
int size = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
int scale = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
string name = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
string text2 = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
string type = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
list.Add(new ColumnSchema(table, name, PostgreSQLSchemaProvider.GetDbType(type), text2, size, precision, scale, allowDBNull, new ExtendedProperty[]
{
new ExtendedProperty("NpgsqlDbType", PostgreSQLSchemaProvider.GetNativeDbType(text2), DbType.String),
new ExtendedProperty("CS_Description",npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString(), DbType.String)
}));
}
if (!npgsqlDataReader.IsClosed)
{
npgsqlDataReader.Close();
}
}
}
}
return list.ToArray();
} public TableKeySchema[] GetTableKeys(string connectionString, TableSchema table)
{
List<TableKeySchema> list = new List<TableKeySchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("SELECT constraint_name as constrname FROM information_schema.table_constraints WHERE table_name = '{0}' AND constraint_type = 'FOREIGN KEY' AND constraint_schema='public'", table.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
string text2 = string.Format("SELECT px.conname as constrname, att.attname as colname, fore.relname as reftabname, fatt.attname as refcolname, CASE px.confupdtype WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END AS on_update, CASE px.confdeltype WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END AS on_delete, CASE px.contype WHEN 'p' THEN true WHEN 'f' THEN false END as IsPrimaryKey from pg_constraint px left join pg_class home on (home.oid = px.conrelid) left join pg_class fore on (fore.oid = px.confrelid) left join pg_attribute att on (att.attrelid = px.conrelid AND att.attnum = ANY(px.conkey)) left join pg_attribute fatt on (fatt.attrelid = px.confrelid AND fatt.attnum = ANY(px.confkey)) where (home.relname = '{0}') and px.contype = 'f' order by constrname", table.Name);
using (NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand(text2, npgsqlConnection))
{
NpgsqlDataAdapter npgsqlDataAdapter = new NpgsqlDataAdapter(npgsqlCommand);
DataSet dataSet = new DataSet();
npgsqlDataAdapter.Fill(dataSet, "constraint");
npgsqlDataAdapter.SelectCommand = npgsqlCommand2;
npgsqlDataAdapter.Fill(dataSet, "keys");
if (dataSet.Tables[].Rows.Count > )
{
dataSet.Relations.Add("Contraint_to_Keys", dataSet.Tables[].Columns["constrname"], dataSet.Tables[].Columns["constrname"]);
foreach (DataRow dataRow in dataSet.Tables[].Rows)
{
string name = dataRow["constrname"].ToString();
DataRow[] childRows = dataRow.GetChildRows("Contraint_to_Keys");
string[] array = new string[childRows.Length];
string[] array2 = new string[childRows.Length];
string name2 = table.Name;
string primaryKeyTable = childRows[]["reftabname"].ToString();
for (int i = ; i < childRows.Length; i++)
{
array2[i] = childRows[i]["colname"].ToString();
array[i] = childRows[i]["refcolname"].ToString();
}
list.Add(new TableKeySchema(table.Database, name, array2, name2, array, primaryKeyTable));
}
}
}
}
string text3 = string.Format("SELECT px.conname as constrname FROM pg_constraint px left join pg_class fore on fore.oid = px.confrelid where fore.relname = '{0}'", table.Name);
using (NpgsqlCommand npgsqlCommand3 = new NpgsqlCommand(text3, npgsqlConnection))
{
string text4 = string.Format("SELECT px.conname as constrname, fatt.attname as colname, home.relname as reftabname, att.attname as refcolname, CASE px.confupdtype WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END AS on_update, CASE px.confdeltype WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END AS on_delete, CASE px.contype WHEN 'p' THEN true WHEN 'f' THEN false END as IsPrimaryKey from pg_constraint px left join pg_class home on (home.oid = px.conrelid) left join pg_class fore on (fore.oid = px.confrelid) left join pg_attribute att on (att.attrelid = px.conrelid AND att.attnum = ANY(px.conkey)) left join pg_attribute fatt on (fatt.attrelid = px.confrelid AND fatt.attnum = ANY(px.confkey)) where (fore.relname = '{0}') order by constrname", table.Name);
using (NpgsqlCommand npgsqlCommand4 = new NpgsqlCommand(text4, npgsqlConnection))
{
NpgsqlDataAdapter npgsqlDataAdapter2 = new NpgsqlDataAdapter();
DataSet dataSet2 = new DataSet();
npgsqlDataAdapter2.SelectCommand = npgsqlCommand3;
npgsqlDataAdapter2.Fill(dataSet2, "constraint");
npgsqlDataAdapter2.SelectCommand = npgsqlCommand4;
npgsqlDataAdapter2.Fill(dataSet2, "keys");
if (dataSet2.Tables[].Rows.Count > )
{
dataSet2.Relations.Add("Contraint_to_Keys", dataSet2.Tables[].Columns["constrname"], dataSet2.Tables[].Columns["constrname"]);
foreach (DataRow dataRow2 in dataSet2.Tables[].Rows)
{
string name3 = dataRow2["constrname"].ToString();
DataRow[] childRows2 = dataRow2.GetChildRows("Contraint_to_Keys");
string[] array3 = new string[childRows2.Length];
string[] array4 = new string[childRows2.Length];
string foreignKeyTable = childRows2[]["reftabname"].ToString();
string name4 = table.Name;
for (int j = ; j < childRows2.Length; j++)
{
array4[j] = childRows2[j]["refcolname"].ToString();
array3[j] = childRows2[j]["colname"].ToString();
}
list.Add(new TableKeySchema(table.Database, name3, array4, foreignKeyTable, array3, name4));
}
}
}
}
}
return list.ToArray();
} public PrimaryKeySchema GetTablePrimaryKey(string connectionString, TableSchema table)
{
PrimaryKeySchema result = null;
DataSet ds = new DataSet();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select constraint_name from information_schema.table_constraints where constraint_schema='public' and table_name='{0}' and constraint_type='PRIMARY KEY'", table.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataAdapter nda = new NpgsqlDataAdapter(npgsqlCommand))
{
nda.Fill(ds, "table_constraints");
}
} string text2 = string.Format("select px.conname as ConstraintName, att.attname as ColumnName from pg_constraint px inner join pg_class home on (home.oid = px.conrelid) left join pg_attribute att on (att.attrelid = px.conrelid AND att.attnum = ANY(px.conkey)) where (home.relname = '{0}') and px.contype = 'p'", table.Name); using (NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand(text2, npgsqlConnection))
{
using (NpgsqlDataAdapter nda = new NpgsqlDataAdapter(npgsqlCommand2))
{
nda.Fill(ds, "pg_constraint");
}
} foreach (DataRow item in ds.Tables["table_constraints"].Rows)
{
List<string> list = new List<string>();
foreach (DataRow item2 in ds.Tables["pg_constraint"].Rows)
{
list.Add(item2.Field<string>("ColumnName"));
}
result = new PrimaryKeySchema(table, item.Field<string>("constraint_name"), list.ToArray());
} }
return result;
} public DataTable GetTableData(string connectionString, TableSchema table)
{
DataTable dataTable;
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
dataTable = new DataTable(table.Name);
string text = string.Format("SELECT * FROM {0}", table.Name);
using (NpgsqlDataAdapter npgsqlDataAdapter = new NpgsqlDataAdapter(text, npgsqlConnection))
{
npgsqlDataAdapter.Fill(dataTable);
}
if (npgsqlConnection.State != ConnectionState.Closed)
{
npgsqlConnection.Close();
}
}
return dataTable;
} public ExtendedProperty[] GetExtendedProperties(string connectionString, SchemaObjectBase schemaObject)
{
List<ExtendedProperty> list = new List<ExtendedProperty>();
if (schemaObject is ColumnSchema)
{
ColumnSchema columnSchema = schemaObject as ColumnSchema;
string text = string.Format("select pg_get_serial_sequence(c.table_name, c.column_name) as EXTRA, COLUMN_DEFAULT, data_type \r\n from pg_tables t\r\n INNER JOIN information_schema.columns c on t.tablename = c.table_name\r\n WHERE schemaname = '{0}' \r\n AND table_name = '{1}'\r\n AND COLUMN_NAME = '{2}'\r\n order by ordinal_position", columnSchema.Table.Database.Name, columnSchema.Table.Name, columnSchema.Name);
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (IDataReader dataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dataReader.Read())
{
string text2 = dataReader.IsDBNull() ? string.Empty : dataReader.GetString().ToLower();
string value = dataReader.IsDBNull() ? null : dataReader.GetString().ToUpper();
string value2 = dataReader.GetString().ToUpper();
bool flag = !string.IsNullOrEmpty(text2);
list.Add(new ExtendedProperty("CS_IsIdentity", flag, columnSchema.DataType));
if (flag)
{
list.Add(new ExtendedProperty("CS_IdentitySeed", , columnSchema.DataType));
list.Add(new ExtendedProperty("CS_IdentityIncrement", , columnSchema.DataType));
}
list.Add(new ExtendedProperty("CS_Default", value, DbType.String));
list.Add(new ExtendedProperty("CS_SystemType", value2, DbType.String));
list.Add(new ExtendedProperty("CS_Sequence", text2.ToUpper(), DbType.String));
}
if (!dataReader.IsClosed)
{
dataReader.Close();
}
}
}
if (npgsqlConnection.State != ConnectionState.Closed)
{
npgsqlConnection.Close();
}
}
}
return list.ToArray();
} public void SetExtendedProperties(string connectionString, SchemaObjectBase schemaObject)
{
throw new NotImplementedException();
} public ViewColumnSchema[] GetViewColumns(string connectionString, ViewSchema view)
{
List<ViewColumnSchema> list = new List<ViewColumnSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select column_name, is_nullable, character_maximum_length, numeric_precision, numeric_scale, data_type, udt_name,col_description(b.attrelid,b.attnum) as pg_description from information_schema.columns as a join pg_attribute as b on a.column_name=b.attname join pg_class as c on a.table_name=c.relname and b.attrelid = c.oid where b.attnum>0 and a.table_schema = 'public' and a.table_name='{0}' ORDER BY ordinal_position", view.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
bool allowDBNull = npgsqlDataReader.IsDBNull() || npgsqlDataReader.GetString() == "YES";
int size = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
byte precision = (byte)(npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32());
int scale = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
string text2 = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
string type = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
list.Add(new ViewColumnSchema(view, npgsqlDataReader.GetString(), PostgreSQLSchemaProvider.GetDbType(type), text2, size, precision, scale, allowDBNull, new ExtendedProperty[]
{
new ExtendedProperty("NpgsqlDbType", PostgreSQLSchemaProvider.GetNativeDbType(text2), DbType.String)
}));
}
}
}
}
return list.ToArray();
} public DataTable GetViewData(string connectionString, ViewSchema view)
{
DataTable dataTable;
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
dataTable = new DataTable(view.Name);
string text = string.Format("SELECT * FROM {0}", view.Name);
using (NpgsqlDataAdapter npgsqlDataAdapter = new NpgsqlDataAdapter(text, npgsqlConnection))
{
npgsqlDataAdapter.Fill(dataTable);
}
}
return dataTable;
} public ViewSchema[] GetViews(string connectionString, DatabaseSchema database)
{
List<ViewSchema> list = new List<ViewSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand("select viewname, viewowner from pg_catalog.pg_views where schemaname = 'public' order by viewname;", npgsqlConnection))
{
DataTable dt = new DataTable(); using (NpgsqlDataAdapter nda = new NpgsqlDataAdapter(npgsqlCommand))
{
nda.Fill(dt);
nda.Dispose();
}
foreach (DataRow item in dt.Rows)
{
list.Add(new ViewSchema(database, item.Field<string>("viewname"), item.Field<string>("viewowner"), DateTime.MinValue));
} }
}
return list.ToArray();
} public string GetViewText(string connectionString, ViewSchema view)
{
string result;
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select view_definition from information_schema.views where table_schema='public' and table_name = '{0}'", view.Name);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
result = (string)npgsqlCommand.ExecuteScalar();
}
}
return result;
} public CommandSchema[] GetCommands(string connectionString, DatabaseSchema database)
{
List<CommandSchema> list = new List<CommandSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand("SELECT routine_name, rolname, specific_name, data_type from information_schema.routines LEFT JOIN pg_catalog.pg_proc p ON p.proname = routine_name INNER JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace INNER JOIN pg_catalog.pg_authid a on a.oid = proowner WHERE routine_schema='public' ORDER BY routine_name ", npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
bool flag = !npgsqlDataReader.IsDBNull() && npgsqlDataReader.GetString().Trim().Equals("VOID", StringComparison.InvariantCultureIgnoreCase);
if (!flag || database.IncludeFunctions)
{
List<ExtendedProperty> list2 = new List<ExtendedProperty>
{
new ExtendedProperty("CS_Name", npgsqlDataReader.GetString(), DbType.String, PropertyStateEnum.ReadOnly),
new ExtendedProperty("CS_IsScalarFunction", flag, DbType.Boolean, PropertyStateEnum.ReadOnly),
new ExtendedProperty("CS_IsProcedure", flag, DbType.Boolean, PropertyStateEnum.ReadOnly),
new ExtendedProperty("CS_IsTrigger", npgsqlDataReader.GetString().Equals("TRIGGER", StringComparison.InvariantCultureIgnoreCase), DbType.Boolean, PropertyStateEnum.ReadOnly)
};
list.Add(new CommandSchema(database, npgsqlDataReader.GetString(), npgsqlDataReader.GetString(), DateTime.MinValue, list2.ToArray()));
}
}
if (!npgsqlDataReader.IsClosed)
{
npgsqlDataReader.Close();
}
}
}
if (npgsqlConnection.State != ConnectionState.Closed)
{
npgsqlConnection.Close();
}
}
return list.ToArray();
} public ParameterSchema[] GetCommandParameters(string connectionString, CommandSchema commandSchema)
{
string arg = commandSchema.ExtendedProperties["CS_Name"].Value as string;
List<ParameterSchema> list = new List<ParameterSchema>();
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select * from information_schema.parameters where specific_schema='public' and specific_name = '{0}' order by ordinal_position", arg);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
string name = npgsqlDataReader.IsDBNull() ? string.Empty : npgsqlDataReader.GetString();
int size = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
int scale = npgsqlDataReader.IsDBNull() ? : npgsqlDataReader.GetInt32();
byte precision = npgsqlDataReader.IsDBNull() ? (byte) : npgsqlDataReader.GetByte();
string @string = npgsqlDataReader.GetString();
list.Add(new ParameterSchema(commandSchema, name, PostgreSQLSchemaProvider.GetParameterDirection(npgsqlDataReader.GetString()), PostgreSQLSchemaProvider.GetDbType(@string), @string, size, precision, scale, false, new ExtendedProperty[]
{
new ExtendedProperty("NpgsqlDbType", PostgreSQLSchemaProvider.GetNativeDbType(@string), DbType.String)
}));
}
if (!npgsqlDataReader.IsClosed)
{
npgsqlDataReader.Close();
}
}
}
if (npgsqlConnection.State != ConnectionState.Closed)
{
npgsqlConnection.Close();
}
}
return list.ToArray();
} public CommandResultSchema[] GetCommandResultSchemas(string connectionString, CommandSchema command)
{
CommandResultSchema[] array = null;
string arg = command.ExtendedProperties["CS_Name"].Value as string;
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select data_type from information_schema.routines where specific_schema='public' and specific_name = '{0}'", arg);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
using (NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
while (npgsqlDataReader.Read())
{
string @string = npgsqlDataReader.GetString();
if (@string == "void")
{
array = new CommandResultSchema[];
}
else if (@string == "USER-DEFINED")
{
NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand(string.Format("SELECT t.typname, attname, a.typname from pg_type t JOIN pg_class on (reltype = t.oid) JOIN pg_attribute on (attrelid = pg_class.oid) JOIN pg_type a on (atttypid = a.oid) WHERE t.typname = (SELECT t.typname FROM pg_catalog.pg_proc p LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace INNER JOIN pg_type t ON p.prorettype = t.oid WHERE n.nspname = 'public' and proname = '{0}' ORDER BY proname);", command.Name), npgsqlConnection);
using (NpgsqlDataReader npgsqlDataReader2 = npgsqlCommand2.ExecuteReader(CommandBehavior.CloseConnection))
{
string text2 = null;
List<CommandResultColumnSchema> list = new List<CommandResultColumnSchema>();
while (npgsqlDataReader2.Read())
{
if (string.IsNullOrEmpty(text2))
{
text2 = npgsqlDataReader2.GetString();
}
string string2 = npgsqlDataReader2.GetString();
list.Add(new CommandResultColumnSchema(command, npgsqlDataReader2.GetString(), PostgreSQLSchemaProvider.GetDbType(string2), string2, , , , true, new ExtendedProperty[]
{
new ExtendedProperty("NpgsqlDbType", PostgreSQLSchemaProvider.GetNativeDbType(string2), DbType.String)
}));
}
array = new CommandResultSchema[]
{
new CommandResultSchema(command, text2, list.ToArray())
};
}
}
}
}
}
}
return array ?? new CommandResultSchema[];
} public string GetCommandText(string connectionString, CommandSchema commandSchema)
{
object result = string.Empty;
string arg = commandSchema.ExtendedProperties["CS_Name"].Value as string;
using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString))
{
npgsqlConnection.Open();
string text = string.Format("select routine_definition from information_schema.routines where specific_schema='public' and specific_name = '{0}'", arg);
using (NpgsqlCommand npgsqlCommand = new NpgsqlCommand(text, npgsqlConnection))
{
result = npgsqlCommand.ExecuteScalar();
}
}
if (result != null)
return string.Empty;
return result.ToString();
} public string GetDatabaseName(string connectionString)
{
Regex regex = new Regex("Database\\W*=\\W*(?<database>[^;]*)", RegexOptions.IgnoreCase);
string result;
if (regex.IsMatch(connectionString))
{
result = regex.Match(connectionString).Groups["database"].ToString();
}
else
{
result = connectionString;
}
return result;
} private static DbType GetDbType(string type)
{
DbType result;
switch (type)
{
case "bit":
case "bool":
case "boolean":
result = DbType.Boolean;
return result;
case "bytea":
result = DbType.Binary;
return result;
case "bpchar":
case "char":
case "character":
case "text":
case "varchar":
case "character varying":
result = DbType.String;
return result;
case "date":
result = DbType.Date;
return result;
case "float4":
case "single precision":
case "real":
result = DbType.Single;
return result;
case "float8":
case "double precision":
result = DbType.Double;
return result;
case "int2":
case "smallint":
result = DbType.Int16;
return result;
case "int4":
case "integer":
result = DbType.Int32;
return result;
case "int8":
case "bigint":
result = DbType.Int64;
return result;
case "money":
case "numeric":
result = DbType.Decimal;
return result;
case "time":
case "timetz":
case "time without time zone":
case "time without timezone":
case "time with time zone":
case "time with timezone":
result = DbType.Time;
return result;
case "interval":
case "timestamp":
case "timestamptz":
case "timestamp without time zone":
case "timestamp without timezone":
case "timestamp with time zone":
case "timestamp with timezone":
result = DbType.DateTime;
return result;
case "uuid":
result = DbType.Guid;
return result;
case "box":
case "circle":
case "inet":
case "line":
case "lseg":
case "path":
case "point":
case "polygon":
case "refcursor":
result = DbType.Object;
return result;
}
result = DbType.Object;
return result;
} private static NpgsqlDbType GetNativeDbType(string type)
{
string key;
switch (key = type.ToLower())
{
case "array":
return (NpgsqlDbType)(-);
case "bit":
return (NpgsqlDbType);
case "box":
return (NpgsqlDbType);
case "bool":
case "boolean":
return (NpgsqlDbType);
case "bytea":
return (NpgsqlDbType);
case "char":
return (NpgsqlDbType);
case "bpchar":
case "character":
case "varchar":
case "character varying":
return (NpgsqlDbType);
case "date":
return (NpgsqlDbType);
case "float4":
case "single precision":
case "real":
return (NpgsqlDbType);
case "float8":
case "double precision":
case "double":
return (NpgsqlDbType);
case "int2":
case "smallint":
return (NpgsqlDbType);
case "int4":
case "integer":
return (NpgsqlDbType);
case "int8":
case "bigint":
return (NpgsqlDbType);
case "money":
return (NpgsqlDbType);
case "name":
return (NpgsqlDbType);
case "numeric":
return (NpgsqlDbType);
case "text":
case "user-defined":
return (NpgsqlDbType);
case "oidvector":
return (NpgsqlDbType);
case "abstime":
return (NpgsqlDbType);
case "time":
case "time without time zone":
case "time without timezone":
return (NpgsqlDbType);
case "timetz":
case "time with time zone":
case "time with timezone":
return (NpgsqlDbType);
case "interval":
return (NpgsqlDbType);
case "timestamp":
case "timestamptz":
case "timestamp without time zone":
case "timestamp without timezone":
return (NpgsqlDbType);
case "timestamp with time zone":
case "timestamp with timezone":
return (NpgsqlDbType);
case "uuid":
return (NpgsqlDbType);
case "circle":
return (NpgsqlDbType);
case "inet":
return (NpgsqlDbType);
case "line":
return (NpgsqlDbType);
case "lseg":
return (NpgsqlDbType);
case "path":
return (NpgsqlDbType);
case "point":
return (NpgsqlDbType);
case "polygon":
return (NpgsqlDbType);
case "refcursor":
return (NpgsqlDbType);
case "xml":
return (NpgsqlDbType);
}
throw new ArgumentOutOfRangeException();
} private static ParameterDirection GetParameterDirection(string direction)
{
ParameterDirection result;
if (direction != null)
{
if (direction == "IN")
{
result = ParameterDirection.Input;
return result;
}
if (direction == "OUT")
{
result = ParameterDirection.Output;
return result;
}
}
result = ParameterDirection.InputOutput;
return result;
}
}
}
PostgreSQLSchemaProvider.cs
codesmith连接postgresql修复bug的更多相关文章
- [转载]基于TFS实践敏捷-修复Bug和执行代码评审
本主题阐释了这些功能,以继续这一关注虚拟敏捷团队成员的一天的教程. Peter 忙于编写一些代码以完成积压工作 (backlog) 项任务.但是,他的同事发现了一个阻碍他们工作的 Bug,他想立即修复 ...
- 献身说法---修复bug时的一些小技巧
最近,修复了项目当中的一些bug,觉着有些思路可以分享出来供大家借鉴. 场景一 开发环境中系统正常运行,测试环境中,部分机器未能正常运行. 解决过程:远程连接了测试环境中的机器,观察了系统的运行情况, ...
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...
- [课程设计]Scrum 3.5 多鱼点餐系统开发进度(修复Bug&美化页面)
Scrum 3.5 多鱼点餐系统开发进度(修复Bug&美化页面) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅 ...
- 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时 ...
- Cordova webapp实战开发:(7)如何通过简单的方法做到,不重新发布APP来修复bug、增加功能、或者躲开苹果的一些严格审核?
到<Cordova webapp实战开发:(6)如何写一个iOS下获取APP版本号的插件?>为止,我们已经大体学会了如何使用Cordova了,那些都是使用Cordova的开发者必备的技能. ...
- CodeSmith连接不上MySql数据库的解决办法
下载地址是http://dev.mysql.com/downloads/mirror.php?id=403020 请先注册登录后才能下载mysql-connector-net-6.3.7.msi这个文 ...
- [课程设计]Scrum 1. 9 多鱼点餐系统开发进度(最后页面完善&修复BUG&用户测试反馈)
[课程设计]Scrum 1. 9 多鱼点餐系统开发进度(最后页面完善&修复BUG&用户测试) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢 ...
- kali linux 系列教程之metasploit 连接postgresql可能遇见的问题
kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂 目录 kali linux 下metasploit 连接postgresql可能遇见的问题. ...
随机推荐
- VS2013创建Node.js C++ Addons的过程
首先看我的Node.js版本. node –v v6.11.4 然后参照这篇文章来做: https://nodejs.org/api/addons.html#addons_hello_world 安装 ...
- ERROR in index.web.js from UglifyJs
使用weexpack构建weex应用时,npm run serve一直报这个错误 ERROR in index.web.js from UglifyJs Unexpected token: name ...
- 教育单元测试mock框架优化之路(下)
转载:https://sq.163yun.com/blog/article/169563599967031296 四.循环依赖的解决 果然! 当我将@SpyBean应用到存在有循环依赖的Bean上时, ...
- (算法)等概率选出m个整数
题目: 从大小为n的整数数组A中随机选出m个整数,要求每个元素被选中的概率相同. 思路: n选m,等概率情况下,每个数被选中的概率为m/n. 方法: 初始化:从A中选择前m个元素作为初始数组: 随机选 ...
- 算法基础:删除字符串中出现次数最少的字符(Golang实现)
描写叙述: 实现删除字符串中出现次数最少的字符.若多个字符出现次数一样,则都删除.输出删除这些单词后的字符串. 字符串中其他字符保持原来的顺序. 输入: 字符串仅仅包括小写英文字母, 不考虑非法输入, ...
- 加快Android Studio的编译速度
从Eclipse切换到Android Studio后,感觉Android Studio的build速度比Eclipse慢很多,以下几个方法可以提高Android Studio的编译速度 使用Gradl ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【Zookeeper】源码分析之持久化(一)之FileTxnLog
一.前言 前一篇已经分析了序列化,这篇接着分析Zookeeper的持久化过程源码,持久化对于数据的存储至关重要,下面进行详细分析. 二.持久化总体框架 持久化的类主要在包org.apache.zook ...
- js setTimeout setInterval 第三个参数说明
1.api setTimeout: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout var timeoutID = ...
- 使用tar+pigz+ssh实现大数据的高效传输
以前我们跨主机拷贝大数据的时候,比如要拷贝超过100GB的mysql原始数据,我们通常的做法如下: 在源端打包压缩为tar.gz文件 采用scp或者rsync等方式拷贝到目标主机 在目标主机解压文件 ...