using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace SQLite
{
public static class SQLiteLsp
{
[LispFunction("DoLite")]
public static ResultBuffer DoLite(ResultBuffer rbArgs)
{
ResultBuffer ret = new ResultBuffer();
TypedValue[] tb = rbArgs.AsArray();
if (tb.Length != 1)
{
ret.Add(new TypedValue((int)LispDataType.Nil));
return ret;
}
if (tb[0].TypeCode != (int)LispDataType.Text)
{
ret.Add(new TypedValue((int)LispDataType.Nil));
return ret;
}
string sql = tb[0].Value.ToString().ToUpper().Trim();
if (sql.Length < 6)
{
ret.Add(new TypedValue((int)LispDataType.Nil));
return ret;
}
switch (Regex.Match(sql, @"^[A-Z]+\b").Value)
{
case "SELECT":
System.Data.DataTable dt = DAL.ExecuteDataTable(sql);
if (dt == null)
{
ret.Add(new TypedValue((int)LispDataType.Nil));
return ret;
}
ret.Add(new TypedValue((int)LispDataType.ListBegin));
ret.Add(new TypedValue((int)LispDataType.ListBegin));
for (int i = 0; i < dt.Columns.Count; i++)
{
ret.Add(new TypedValue((int)LispDataType.Text, dt.Columns[有个i].ColumnName));
}
ret.Add(new TypedValue((int)LispDataType.ListEnd));
foreach (DataRow dr in dt.Rows)
{
ret.Add(new TypedValue((int)LispDataType.ListBegin));
for (int k = 0; k < dt.Columns.Count; k++)
{
switch (dr[k].GetType().ToString())
{
case ("System.String"):
case ("System.DateTime"):
ret.Add(new TypedValue((int)LispDataType.Text, dr[k].ToString()));
break;
case ("System.Int32"):
ret.Add(new TypedValue((int)LispDataType.Int32, (int)dr[k]));
break;
case ("System.Int16"):
ret.Add(new TypedValue((int)LispDataType.Int16, (short)dr[k]));
break;
case ("System.Boolean"):
if (Convert.ToBoolean(Convert.ToInt32(dr[k])))
ret.Add(new TypedValue((int)LispDataType.T_atom));
else
ret.Add(new TypedValue((int)LispDataType.Nil));
break;
default:
ret.Add(new TypedValue((int)LispDataType.Nil));
break;
}
}
ret.Add(new TypedValue((int)LispDataType.ListEnd));
}
ret.Add(new TypedValue((int)LispDataType.ListEnd));
break;
case "CREATE":
case "RENAME":
case "INSERT":
case "DELETE":
case "UPDATE":
case "DROP":
case "ALTER":
if (DAL.ExecuteNonQuery(sql))
ret.Add(new TypedValue((int)LispDataType.T_atom));
else
ret.Add(new TypedValue((int)LispDataType.Nil));
break;
default:
ret.Add(new TypedValue((int)LispDataType.Nil));
break;
}
return ret;
}
}
public static class DAL
{
private static string LiteSetting = "Data Source=D:\\SqltiteData.db"; // 自己的数据库
public static System.Data.DataTable ExecuteDataTable(string sql)
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
using (SQLiteConnection cn = new SQLiteConnection(LiteSetting))
{
using (SQLiteCommand cmd = new SQLiteCommand(sql, cn))
{
using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dt);
}
}
}
}
catch
{
return null;
}
return dt;
}
public static bool ExecuteNonQuery(string sql)
{
bool ret = false;
using (SQLiteConnection cn = new SQLiteConnection(LiteSetting))
{
cn.Open();
using (SQLiteTransaction trans = cn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand(sql, cn))
{
try
{
cmd.ExecuteNonQuery();
trans.Commit();
ret = true;
}
catch
{
trans.Rollback();
}
}
}
cn.Close();
}
return ret;
}
}
}