using ExportMap.cs;
using ExportMap.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Web;
namespace ExportMap.db
{
public class PubDBHelper
{
private static string insertPublishSql = "insert into lf.sys_publish(name, url, path, type, status, dirid, depid, min, max, json, create_user, geom, bak) values (@name, @url, @path, @type, @status, @dirid, @depid, @min, @max, @json, @create_user, @geom, @bak) returning id";
private static string insertMetaPubSql = "insert into lf.sys_meta_pub (metaid, pubid) select {0}, {1} from (select 1) temp where not exists (select 1 from lf.sys_meta_pub where metaid = {0} and pubid = {1})";
///
/// 插入元数据-数据发布表记录
///
public static int InsertMetaPub(int metaid, int pubid)
{
string sql = string.Format(insertMetaPubSql, metaid, pubid);
return Tool.DBHelper.ExecuteNonQuery(sql);
}
///
/// 插入数据发布表记录
///
///
public static int InsertPublish(SysPublish sys)
{
List list = Tool.GetParams(insertPublishSql, sys);
object obj = Tool.DBHelper.GetScalar(insertPublishSql, list.ToArray());
return obj == null ? 0 : Convert.ToInt32(obj);
}
///
/// 获取参数列表
///
public static List GetParams(string sql, T t)
{
List list = new List();
Type tType = typeof(T);
BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
int start = sql.IndexOf("@");
while (start != -1)
{
int end = sql.IndexOf(",", start);
if (end == -1) end = sql.IndexOf(")", start);
if (end == -1) end = sql.IndexOf(" ", start);
if (end == -1) end = sql.Length;
string name = sql.Substring(start + 1, end - start - 1);
PropertyInfo pi = tType.GetProperty(name, flags);
if (pi != null)
{
object value = pi.GetValue(t, null);
DbParameter dp = new NpgsqlParameter("@" + name, value);
list.Add(dp);
}
start = sql.IndexOf("@", end);
}
return list;
}
}
}