You've already forked linux-packaging-mono
Imported Upstream version 6.4.0.137
Former-commit-id: 943baa9f16a098c33e129777827f3a9d20da00d6
This commit is contained in:
parent
e9207cf623
commit
ef583813eb
73
mcs/tools/sqlmetal/src/DbLinq.Sqlite/Schema/DataCommand.cs
Normal file
73
mcs/tools/sqlmetal/src/DbLinq.Sqlite/Schema/DataCommand.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DbLinq.Sqlite.Schema
|
||||
{
|
||||
#if !MONO_STRICT
|
||||
public
|
||||
#endif
|
||||
static class DataCommand
|
||||
{
|
||||
public delegate T ReadDelegate<T>(IDataReader reader, string table);
|
||||
|
||||
public static List<T> Find<T>(IDbConnection connection, string sql, string pragma, ReadDelegate<T> readDelegate)
|
||||
{
|
||||
using (IDbCommand cmd = connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = sql;
|
||||
using (IDataReader rdr = cmd.ExecuteReader())
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
|
||||
while (rdr.Read())
|
||||
{
|
||||
string table = rdr.GetString(0);
|
||||
//string sqlPragma = @"PRAGMA foreign_key_list('" + table + "');";
|
||||
string sqlPragma = string.Format(pragma, table);
|
||||
using (IDbCommand cmdPragma = connection.CreateCommand())
|
||||
{
|
||||
cmdPragma.CommandText = sqlPragma;
|
||||
using (IDataReader rdrPragma = cmdPragma.ExecuteReader())
|
||||
{
|
||||
while (rdrPragma.Read())
|
||||
{
|
||||
list.Add(readDelegate(rdrPragma, table));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteDataContext.cs
Normal file
57
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteDataContext.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System.Data;
|
||||
|
||||
#if MONO_STRICT
|
||||
using System.Data.Linq;
|
||||
#else
|
||||
using DbLinq.Data.Linq;
|
||||
#endif
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
#if !MONO_STRICT
|
||||
public
|
||||
#endif
|
||||
class SqliteDataContext : DataContext
|
||||
{
|
||||
#if SQLITE_IS_REFERENCED
|
||||
public SqliteDataContext(string connStr)
|
||||
: base(new System.Data.SQLite.SQLiteConnection(connStr), new SqliteVendor())
|
||||
{
|
||||
}
|
||||
#endif
|
||||
public SqliteDataContext(IDbConnection conn)
|
||||
#if MONO_STRICT
|
||||
: base(conn)
|
||||
#else
|
||||
: base(conn, new SqliteVendor())
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using DbLinq.Util;
|
||||
using DbLinq.Vendor;
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
partial class SqliteSchemaLoader
|
||||
{
|
||||
protected virtual IDataTableColumn ReadColumn(IDataReader dataReader, string table)
|
||||
{
|
||||
var column = new DataTableColumn();
|
||||
column.TableSchema = "main";
|
||||
column.TableName = table;
|
||||
column.ColumnName = dataReader.GetString(1);
|
||||
column.UnpackRawDbType(dataReader.GetString(2));
|
||||
column.FullType = dataReader.GetString(2);
|
||||
column.Nullable = dataReader.GetInt64(3) == 0;
|
||||
column.PrimaryKey = dataReader.GetInt64(5) == 1;
|
||||
// SQLite says: if it is a primary key of integer type, then it is automatically generated
|
||||
column.Generated = column.PrimaryKey.Value && MapDbType(column.ColumnName, column) == typeof(int);
|
||||
return column;
|
||||
}
|
||||
|
||||
protected override IList<IDataTableColumn> ReadColumns(IDbConnection connectionString, string databaseName)
|
||||
{
|
||||
var sql = string.Format(SelectTablesFormat, "");
|
||||
const string pragma = @"PRAGMA table_info('{0}');";
|
||||
|
||||
return Schema.DataCommand.Find<IDataTableColumn>(connectionString, sql, pragma, ReadColumn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using DbLinq.Sqlite.Schema;
|
||||
using DbLinq.Util;
|
||||
using DataCommand = DbLinq.Sqlite.Schema.DataCommand;
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
partial class SqliteSchemaLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// represents one row from MySQL's information_schema.`Key_Column_Usage` table
|
||||
/// </summary>
|
||||
public class DataConstraint
|
||||
{
|
||||
public string ConstraintName;
|
||||
public string TableSchema;
|
||||
public string TableName;
|
||||
public string ColumnName;
|
||||
public string ReferencedTableSchema;
|
||||
public string ReferencedTableName;
|
||||
public string ReferencedColumnName;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string detail = ConstraintName == "PRIMARY"
|
||||
? TableName + " PK"
|
||||
: ConstraintName;
|
||||
return "KeyColUsage " + detail;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual DataConstraint ReadConstraint(IDataReader rdr, string table)
|
||||
{
|
||||
DataConstraint t = new DataConstraint();
|
||||
const int K_ID = 0;
|
||||
//const int K_SEQ = 1;
|
||||
const int K_TABLE = 2;
|
||||
const int K_FROM = 3;
|
||||
const int K_TO = 4;
|
||||
|
||||
t.TableSchema = "main";
|
||||
t.ReferencedTableSchema = "main";
|
||||
|
||||
t.ConstraintName = "fk_" + table + "_" + rdr.GetAsNumeric<int>(K_ID).ToString();
|
||||
t.TableName = table;
|
||||
t.ColumnName = rdr.GetAsString(K_FROM);
|
||||
|
||||
t.ReferencedTableName = UnquoteSqlName(rdr.GetAsString(K_TABLE));
|
||||
t.ReferencedColumnName = rdr.GetAsString(K_TO);
|
||||
return t;
|
||||
|
||||
}
|
||||
|
||||
protected virtual List<DataConstraint> ReadConstraints(IDbConnection conn, string db)
|
||||
{
|
||||
//Could perhaps use conn.GetSchema() instead
|
||||
//Warning... Sqlite doesnt enforce constraints unless you define some triggers
|
||||
|
||||
string sql = @" SELECT tbl_name FROM sqlite_master WHERE type='table' order by tbl_name";
|
||||
string sqlPragma = @"PRAGMA foreign_key_list('{0}');";
|
||||
|
||||
return DataCommand.Find<DataConstraint>(conn, sql, sqlPragma, ReadConstraint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using DbLinq.Vendor;
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
partial class SqliteSchemaLoader
|
||||
{
|
||||
public override IList<IDataName> ReadTables(IDbConnection connectionString, string databaseName)
|
||||
{
|
||||
string sql = string.Format(SelectTablesFormat, ", 'main'");
|
||||
return Util.DataCommand.Find<IDataName>(connectionString, sql, ReadDataNameAndSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
174
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteSchemaLoader.cs
Normal file
174
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteSchemaLoader.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using DbLinq.Schema;
|
||||
using DbLinq.Schema.Dbml;
|
||||
using DbLinq.Sqlite;
|
||||
using DbLinq.Sqlite.Schema;
|
||||
using DbLinq.Util;
|
||||
using DbLinq.Vendor;
|
||||
using DbLinq.Vendor.Implementation;
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
partial class SqliteSchemaLoader : SchemaLoader
|
||||
{
|
||||
private readonly IVendor vendor = new SqliteVendor();
|
||||
public override IVendor Vendor { get { return vendor; } set { } }
|
||||
|
||||
protected string UnquoteSqlName(string name)
|
||||
{
|
||||
var quotes = new[]{
|
||||
new { Start = "[", End = "]" },
|
||||
new { Start = "`", End = "`" },
|
||||
new { Start = "\"", End = "\"" },
|
||||
};
|
||||
foreach (var q in quotes)
|
||||
{
|
||||
if (name.StartsWith(q.Start) && name.EndsWith(q.End))
|
||||
return name.Substring(q.Start.Length, name.Length - q.Start.Length - q.End.Length);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// note: the ReadDataNameAndSchema relies on information order;
|
||||
// tbl_name MUST be first
|
||||
const string SelectTablesFormat =
|
||||
@" SELECT tbl_name{0}
|
||||
FROM sqlite_master
|
||||
WHERE type='table' AND
|
||||
tbl_name NOT LIKE 'sqlite_%'
|
||||
ORDER BY tbl_name";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a usable name for the database.
|
||||
/// </summary>
|
||||
/// <param name="databaseName">Name of the database.</param>
|
||||
/// <returns></returns>
|
||||
protected override string GetDatabaseName(string databaseName)
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(databaseName);
|
||||
}
|
||||
|
||||
protected override void LoadConstraints(Database schema, SchemaName schemaName, IDbConnection conn, NameFormat nameFormat, Names names)
|
||||
{
|
||||
var constraints = ReadConstraints(conn, schemaName.DbName);
|
||||
|
||||
//sort tables - parents first (this is moving to SchemaPostprocess)
|
||||
//TableSorter.Sort(tables, constraints);
|
||||
|
||||
// Deal with non existing foreign key database
|
||||
if (constraints != null)
|
||||
{
|
||||
foreach (DataConstraint keyColRow in constraints)
|
||||
{
|
||||
//find my table:
|
||||
string tableFullDbName = GetFullDbName(keyColRow.TableName, keyColRow.TableSchema);
|
||||
DbLinq.Schema.Dbml.Table table = schema.Tables.FirstOrDefault(t => tableFullDbName == t.Name);
|
||||
if (table == null)
|
||||
{
|
||||
WriteErrorLine("ERROR L46: Table '" + keyColRow.TableName + "' not found for column " + keyColRow.ColumnName);
|
||||
continue;
|
||||
}
|
||||
|
||||
bool isForeignKey = keyColRow.ConstraintName != "PRIMARY"
|
||||
&& keyColRow.ReferencedTableName != null;
|
||||
|
||||
if (isForeignKey)
|
||||
{
|
||||
LoadForeignKey(schema, table, keyColRow.ColumnName, keyColRow.TableName, keyColRow.TableSchema,
|
||||
keyColRow.ReferencedColumnName, keyColRow.ReferencedTableName,
|
||||
keyColRow.ReferencedTableSchema,
|
||||
keyColRow.ConstraintName, nameFormat, names);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// parse strings such as 'INOUT param2 INT' or 'param4 varchar ( 32 )'
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
/// <returns></returns>
|
||||
protected DbLinq.Schema.Dbml.Parameter ParseParameterString(string param)
|
||||
{
|
||||
param = param.Trim();
|
||||
var inOut = DbLinq.Schema.Dbml.ParameterDirection.In;
|
||||
|
||||
if (param.StartsWith("IN", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
inOut = DbLinq.Schema.Dbml.ParameterDirection.In;
|
||||
param = param.Substring(2).Trim();
|
||||
}
|
||||
if (param.StartsWith("INOUT", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
inOut = DbLinq.Schema.Dbml.ParameterDirection.InOut;
|
||||
param = param.Substring(5).Trim();
|
||||
}
|
||||
if (param.StartsWith("OUT", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
inOut = DbLinq.Schema.Dbml.ParameterDirection.Out;
|
||||
param = param.Substring(3).Trim();
|
||||
}
|
||||
|
||||
int indxSpace = param.IndexOfAny(new char[] { ' ', '\t' });
|
||||
if (indxSpace == -1)
|
||||
return null; //cannot find space between varName and varType
|
||||
|
||||
string varName = param.Substring(0, indxSpace);
|
||||
string varType = param.Substring(indxSpace + 1);
|
||||
|
||||
DbLinq.Schema.Dbml.Parameter paramObj = new DbLinq.Schema.Dbml.Parameter();
|
||||
paramObj.Direction = inOut;
|
||||
paramObj.Name = varName;
|
||||
paramObj.DbType = varType;
|
||||
paramObj.Type = ParseDbType(varType);
|
||||
|
||||
return paramObj;
|
||||
}
|
||||
|
||||
static System.Text.RegularExpressions.Regex re_CHARSET = new System.Text.RegularExpressions.Regex(@" CHARSET \w+$");
|
||||
/// <summary>
|
||||
/// given 'CHAR(30)', return 'string'
|
||||
/// </summary>
|
||||
protected string ParseDbType(string dbType1)
|
||||
{
|
||||
//strip 'CHARSET latin1' from the end
|
||||
string dbType2 = re_CHARSET.Replace(dbType1, "");
|
||||
|
||||
var dataType = new DataType();
|
||||
dataType.UnpackRawDbType(dbType2);
|
||||
return MapDbType(null, dataType).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
70
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteSqlProvider.cs
Normal file
70
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteSqlProvider.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using DbLinq.Data.Linq.Sql;
|
||||
using DbLinq.Vendor.Implementation;
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
#if !MONO_STRICT
|
||||
public
|
||||
#endif
|
||||
class SqliteSqlProvider : SqlProvider
|
||||
{
|
||||
public override SqlStatement GetInsertIds(SqlStatement table, IList<SqlStatement> autoPKColumn, IList<SqlStatement> inputPKColumns, IList<SqlStatement> inputPKValues, IList<SqlStatement> outputColumns, IList<SqlStatement> outputParameters, IList<SqlStatement> outputExpressions)
|
||||
{
|
||||
return "SELECT last_insert_rowid()";
|
||||
}
|
||||
|
||||
protected override SqlStatement GetLiteralStringLength(SqlStatement a)
|
||||
{
|
||||
return SqlStatement.Format("LENGTH({0})", a);
|
||||
}
|
||||
|
||||
protected override SqlStatement GetLiteralStringToUpper(SqlStatement a)
|
||||
{
|
||||
return SqlStatement.Format("UPPER({0})", a);
|
||||
}
|
||||
|
||||
protected override SqlStatement GetLiteralStringToLower(SqlStatement a)
|
||||
{
|
||||
return string.Format("LOWER({0})", a);
|
||||
}
|
||||
|
||||
protected override SqlStatement GetLiteralCount(SqlStatement a)
|
||||
{
|
||||
return "COUNT(*)";
|
||||
}
|
||||
|
||||
public override SqlStatement GetLiteral(bool literal)
|
||||
{
|
||||
if (literal)
|
||||
return "1";
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
206
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteVendor.cs
Normal file
206
mcs/tools/sqlmetal/src/DbLinq.Sqlite/SqliteVendor.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
#region MIT license
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
#endregion
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Data.Linq.Mapping;
|
||||
using System.Reflection;
|
||||
using DbLinq.Data.Linq;
|
||||
using DbLinq.Data.Linq.SqlClient;
|
||||
using DbLinq.Sqlite;
|
||||
using DbLinq.Util;
|
||||
using DbLinq.Vendor;
|
||||
|
||||
#if MONO_STRICT
|
||||
using DataContext = System.Data.Linq.DataContext;
|
||||
#else
|
||||
using DataContext = DbLinq.Data.Linq.DataContext;
|
||||
#endif
|
||||
|
||||
namespace DbLinq.Sqlite
|
||||
{
|
||||
/// <summary>
|
||||
/// SQLite - specific code.
|
||||
/// </summary>
|
||||
[Vendor(typeof(SqliteProvider))]
|
||||
#if !MONO_STRICT
|
||||
public
|
||||
#endif
|
||||
class SqliteVendor : Vendor.Implementation.Vendor
|
||||
{
|
||||
public override string VendorName { get { return "SQLite"; } }
|
||||
|
||||
protected readonly SqliteSqlProvider sqlProvider = new SqliteSqlProvider();
|
||||
public override ISqlProvider SqlProvider { get { return sqlProvider; } }
|
||||
|
||||
/// <summary>
|
||||
/// call SQLite stored proc or stored function,
|
||||
/// optionally return DataSet, and collect return params.
|
||||
/// </summary>
|
||||
public override System.Data.Linq.IExecuteResult ExecuteMethodCall(DataContext context, MethodInfo method
|
||||
, params object[] inputValues)
|
||||
{
|
||||
if (method == null)
|
||||
throw new ArgumentNullException("L56 Null 'method' parameter");
|
||||
|
||||
//check to make sure there is exactly one [FunctionEx]? that's below.
|
||||
//FunctionAttribute functionAttrib = GetFunctionAttribute(method);
|
||||
var functionAttrib = context.Mapping.GetFunction(method);
|
||||
|
||||
ParameterInfo[] paramInfos = method.GetParameters();
|
||||
//int numRequiredParams = paramInfos.Count(p => p.IsIn || p.IsRetval);
|
||||
//if (numRequiredParams != inputValues.Length)
|
||||
// throw new ArgumentException("L161 Argument count mismatch");
|
||||
|
||||
string sp_name = functionAttrib.MappedName;
|
||||
|
||||
using (IDbCommand command = context.Connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = sp_name;
|
||||
//SQLiteCommand command = new SQLiteCommand("select hello0()");
|
||||
int currInputIndex = 0;
|
||||
|
||||
List<string> paramNames = new List<string>();
|
||||
for (int i = 0; i < paramInfos.Length; i++)
|
||||
{
|
||||
ParameterInfo paramInfo = paramInfos[i];
|
||||
|
||||
//TODO: check to make sure there is exactly one [Parameter]?
|
||||
ParameterAttribute paramAttrib = paramInfo.GetCustomAttributes(false).OfType<ParameterAttribute>().Single();
|
||||
|
||||
string paramName = "?" + paramAttrib.Name; //eg. '?param1'
|
||||
paramNames.Add(paramName);
|
||||
|
||||
System.Data.ParameterDirection direction = GetDirection(paramInfo, paramAttrib);
|
||||
//SQLiteDbType dbType = SQLiteTypeConversions.ParseType(paramAttrib.DbType);
|
||||
IDataParameter cmdParam = command.CreateParameter();
|
||||
cmdParam.ParameterName = paramName;
|
||||
//cmdParam.Direction = System.Data.ParameterDirection.Input;
|
||||
if (direction == ParameterDirection.Input || direction == ParameterDirection.InputOutput)
|
||||
{
|
||||
object inputValue = inputValues[currInputIndex++];
|
||||
cmdParam.Value = inputValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdParam.Value = null;
|
||||
}
|
||||
cmdParam.Direction = direction;
|
||||
command.Parameters.Add(cmdParam);
|
||||
}
|
||||
|
||||
if (!functionAttrib.IsComposable)
|
||||
{
|
||||
//procedures: under the hood, this seems to prepend 'CALL '
|
||||
command.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
}
|
||||
else
|
||||
{
|
||||
//functions: 'SELECT myFunction()' or 'SELECT hello(?s)'
|
||||
string cmdText = "SELECT " + command.CommandText + "($args)";
|
||||
cmdText = cmdText.Replace("$args", string.Join(",", paramNames.ToArray()));
|
||||
command.CommandText = cmdText;
|
||||
}
|
||||
|
||||
if (method.ReturnType == typeof(DataSet))
|
||||
{
|
||||
//unknown shape of resultset:
|
||||
System.Data.DataSet dataSet = new DataSet();
|
||||
IDbDataAdapter adapter = CreateDataAdapter(context);
|
||||
adapter.SelectCommand = command;
|
||||
adapter.Fill(dataSet);
|
||||
List<object> outParamValues = CopyOutParams(paramInfos, command.Parameters);
|
||||
return new ProcedureResult(dataSet, outParamValues.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
object obj = command.ExecuteScalar();
|
||||
List<object> outParamValues = CopyOutParams(paramInfos, command.Parameters);
|
||||
return new ProcedureResult(obj, outParamValues.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static System.Data.ParameterDirection GetDirection(ParameterInfo paramInfo, ParameterAttribute paramAttrib)
|
||||
{
|
||||
//strange hack to determine what's a ref, out parameter:
|
||||
//http://lists.ximian.com/pipermain/mono-list/2003-March/012751.html
|
||||
bool hasAmpersand = paramInfo.ParameterType.FullName.Contains('&');
|
||||
if (paramInfo.IsOut)
|
||||
return System.Data.ParameterDirection.Output;
|
||||
if (hasAmpersand)
|
||||
return System.Data.ParameterDirection.InputOutput;
|
||||
return System.Data.ParameterDirection.Input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collect all Out or InOut param values, casting them to the correct .net type.
|
||||
/// </summary>
|
||||
private List<object> CopyOutParams(ParameterInfo[] paramInfos, IDataParameterCollection paramSet)
|
||||
{
|
||||
List<object> outParamValues = new List<object>();
|
||||
//Type type_t = typeof(T);
|
||||
int i = -1;
|
||||
foreach (IDataParameter param in paramSet)
|
||||
{
|
||||
i++;
|
||||
if (param.Direction == System.Data.ParameterDirection.Input)
|
||||
{
|
||||
outParamValues.Add("unused");
|
||||
continue;
|
||||
}
|
||||
|
||||
object val = param.Value;
|
||||
Type desired_type = paramInfos[i].ParameterType;
|
||||
|
||||
if (desired_type.Name.EndsWith("&"))
|
||||
{
|
||||
//for ref and out parameters, we need to tweak ref types, e.g.
|
||||
// "System.Int32&, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
|
||||
string fullName1 = desired_type.AssemblyQualifiedName;
|
||||
string fullName2 = fullName1.Replace("&", "");
|
||||
desired_type = Type.GetType(fullName2);
|
||||
}
|
||||
try
|
||||
{
|
||||
//fi.SetValue(t, val); //fails with 'System.Decimal cannot be converted to Int32'
|
||||
//DbLinq.util.FieldUtils.SetObjectIdField(t, fi, val);
|
||||
//object val2 = DbLinq.Util.FieldUtils.CastValue(val, desired_type);
|
||||
object val2 = TypeConvert.To(val, desired_type);
|
||||
outParamValues.Add(val2);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//fails with 'System.Decimal cannot be converted to Int32'
|
||||
//Logger.Write(Level.Error, "CopyOutParams ERROR L245: failed on CastValue(): " + ex.Message);
|
||||
}
|
||||
}
|
||||
return outParamValues;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user