📄 sqliteprovider.cs
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//*: emergetk: stateful web framework for the masses * File name: SQLiteProvider.cs * Description: SQLite Data Provider implementation. * * Author: Ben Joldersma * */using System;#if MONOusing Mono.Data.SqliteClient;#elseusing System.Data.SQLite;#endifusing System.Collections.Generic;using System.Text;using System.Data;using System.Reflection;namespace EmergeTk.Model{ public enum SqlExecutionType { Scalar, NonQuery, Reader, DataTable } public sealed class SQLiteProvider : DataProvider { private static readonly SQLiteProvider provider = new SQLiteProvider(); private SQLiteProvider() { DataProvider.RegisterProvider(typeof(SQLiteRecord),this); } static public SQLiteProvider Provider { get { return provider; } } public static T Execute<T>(string sql, SqlExecutionType mode, bool wrapTransaction) where T : class { string dbPath = Util.RootPath + "data.db"; System.Console.WriteLine("dbPath:" + dbPath);#if MONO SqliteConnection conn = new SqliteConnection("URI=file:" + dbPath);#else SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath);#endif conn.Open(); IDbCommand cmd = conn.CreateCommand(); IDbTransaction trans = null; if( wrapTransaction ) trans = conn.BeginTransaction(); cmd.CommandText = sql; T retVal = null; switch (mode) { case SqlExecutionType.Scalar: retVal = (T)cmd.ExecuteScalar(); break; case SqlExecutionType.NonQuery: cmd.ExecuteNonQuery(); break; case SqlExecutionType.Reader: retVal = cmd.ExecuteReader() as T; break; case SqlExecutionType.DataTable:#if MONO SqliteDataAdapter da = new SqliteDataAdapter(cmd);#else SQLiteDataAdapter da = new SQLiteDataAdapter(cmd as SQLiteCommand);#endif DataTable t= new DataTable(); da.Fill(t); retVal = t as T; break; } if( wrapTransaction )trans.Commit(); if (mode != SqlExecutionType.Reader) { cmd.Dispose(); conn.Close(); conn.Dispose(); } //TODO: need to release resources after reader is complete. return retVal; } public object ExecuteScalar(string sql, bool wrapTransaction) { return Execute<object>(sql, SqlExecutionType.Scalar, wrapTransaction); } public void ExecuteNonQuery(string sql, bool wrapTransaction) { Execute<object>(sql, SqlExecutionType.NonQuery, wrapTransaction); }#if MONO public SqliteDataReader ExecuteReader(string sql, bool wrapTransaction) { return Execute<SqliteDataReader>(sql, SqlExecutionType.Reader, wrapTransaction); }#else public SQLiteDataReader ExecuteReader(string sql, bool wrapTransaction) { return Execute<SQLiteDataReader>(sql, SqlExecutionType.Reader, wrapTransaction); }#endif public DataTable ExecuteDataTable(string sql, bool wrapTransaction) { return Execute<DataTable>(sql, SqlExecutionType.DataTable, wrapTransaction); } public IRecordList<T> Load<T>() where T : AbstractRecord, new() { return Load<T>("","ROWID"); } public IRecordList<T> Load<T>(params SortInfo[] sortInfos) where T : AbstractRecord, new() { return Load<T>(string.Empty, Util.Join(sortInfos)); } public IRecordList<T> Load<T>(string whereClause, string orderByClause) where T : AbstractRecord, new() { return Load<T>(whereClause, orderByClause, false); } public IRecordList<T> Load<T>(string whereClause, string orderByClause, bool FastLoad ) where T : AbstractRecord, new() { if (whereClause != null && whereClause.Length > 0) { whereClause = " WHERE " + whereClause; } if (orderByClause != null && orderByClause.Length > 0) { orderByClause = " ORDER BY " + orderByClause; } RecordList<T> records = new RecordList<T>(); Type type = typeof(T); string name = type.Name; if (!SQLiteRecord.TableExists(name)) return records; string star = FastLoad ? ", *" : ""; DataTable result = SQLiteProvider.Provider.ExecuteDataTable("SELECT ROWID" + star + " FROM " + name + whereClause + orderByClause, false); for (int i = 0; i < result.Rows.Count; i++) { T r; if( FastLoad ) r = SQLiteRecord.LoadFromDataRow<T>(result.Rows[i]); else r = SQLiteRecord.Load<T>(Convert.ToInt32(result.Rows[i]["ROWID"])); records.Add(r); } return records; } public IRecordList<T> Load<T>(params FilterInfo[] filterInfos) where T : AbstractRecord, new() { return Load<T>(Util.Join(filterInfos), string.Empty); } public IRecordList<T> Load<T>(FilterInfo[] filterInfos, SortInfo[] sortInfos ) where T : AbstractRecord, new() { return Load<T>(Util.Join(filterInfos), Util.Join(sortInfos)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -