Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 25a1933

Browse files
committed
Merge pull request #205 from angelcolmenares/master
DDL methods
2 parents ffd434a + 86bbacb commit 25a1933

File tree

9 files changed

+585
-16
lines changed

9 files changed

+585
-16
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>12.0.0</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{E106F587-2DFF-4550-BA49-9A81A2EC344F}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<RootNamespace>ServiceStack.OrmLite.DDLTest</RootNamespace>
11+
<AssemblyName>ServiceStack.OrmLite.DDLTest</AssemblyName>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14+
<DebugSymbols>True</DebugSymbols>
15+
<DebugType>full</DebugType>
16+
<Optimize>False</Optimize>
17+
<OutputPath>bin\Debug</OutputPath>
18+
<DefineConstants>DEBUG;</DefineConstants>
19+
<ErrorReport>prompt</ErrorReport>
20+
<WarningLevel>4</WarningLevel>
21+
<ConsolePause>False</ConsolePause>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24+
<DebugType>none</DebugType>
25+
<Optimize>True</Optimize>
26+
<OutputPath>bin\Release</OutputPath>
27+
<ErrorReport>prompt</ErrorReport>
28+
<WarningLevel>4</WarningLevel>
29+
<ConsolePause>False</ConsolePause>
30+
</PropertyGroup>
31+
<ItemGroup>
32+
<Reference Include="System" />
33+
<Reference Include="nunit.framework">
34+
<HintPath>..\..\lib\tests\nunit.framework.dll</HintPath>
35+
</Reference>
36+
<Reference Include="FirebirdSql.Data.FirebirdClient">
37+
<HintPath>..\..\lib\FirebirdSql.Data.FirebirdClient.dll</HintPath>
38+
</Reference>
39+
<Reference Include="MySql.Data">
40+
<HintPath>..\..\lib\MySql.Data.dll</HintPath>
41+
</Reference>
42+
<Reference Include="ServiceStack.Common">
43+
<HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
44+
</Reference>
45+
<Reference Include="ServiceStack.Interfaces">
46+
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
47+
</Reference>
48+
<Reference Include="ServiceStack.Text">
49+
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
50+
</Reference>
51+
<Reference Include="System.ComponentModel.DataAnnotations" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Compile Include="Test.cs" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
57+
<ItemGroup>
58+
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
59+
<Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
60+
<Name>ServiceStack.OrmLite</Name>
61+
</ProjectReference>
62+
<ProjectReference Include="..\ServiceStack.OrmLite.Firebird\ServiceStack.OrmLite.Firebird.csproj">
63+
<Project>{317B64BA-D7A6-4A15-8719-821B38147C63}</Project>
64+
<Name>ServiceStack.OrmLite.Firebird</Name>
65+
</ProjectReference>
66+
<ProjectReference Include="..\ServiceStack.OrmLite.MySql\ServiceStack.OrmLite.MySql.csproj">
67+
<Project>{FD20F088-ACC0-4979-AC0C-8C541C2E7DE5}</Project>
68+
<Name>ServiceStack.OrmLite.MySql</Name>
69+
</ProjectReference>
70+
</ItemGroup>
71+
</Project>
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
using NUnit.Framework;
3+
using ServiceStack.OrmLite;
4+
using ServiceStack.OrmLite.Firebird;
5+
using ServiceStack.OrmLite.MySql;
6+
using System.Collections.Generic;
7+
using ServiceStack.Logging;
8+
using ServiceStack.Logging.Support.Logging;
9+
using System.ComponentModel.DataAnnotations;
10+
11+
namespace ServiceStack.OrmLite.DDLTest
12+
{
13+
14+
15+
[TestFixture()]
16+
public class Test
17+
{
18+
List<Dialect> dialects = new List<Dialect>();
19+
20+
[TestFixtureSetUp]
21+
public void TestFixtureSetup()
22+
{
23+
dialects.Add ( new Dialect{
24+
Provider= FirebirdOrmLiteDialectProvider.Instance,
25+
AddColumnString="ALTER TABLE Model ADD Column1 VARCHAR(128) ;",
26+
AlterColumnString="ALTER TABLE Model ALTER Column2 VARCHAR(50) ;",
27+
ChangeColumnNameString="ALTER TABLE Model ALTER OldColumn3 TO Column3 ;",
28+
AddFKString="ALTER TABLE Child ADD CONSTRAINT JustOneFK FOREIGN KEY (IdModel) REFERENCES Model (Id) ON UPDATE NO ACTION ON DELETE CASCADE;",
29+
CreateIndexString="CREATE UNIQUE INDEX JustIndexOnColumn3 ON Model(Column3);"
30+
31+
});
32+
dialects.Add ( new Dialect{
33+
Provider= MySqlDialectProvider.Instance,
34+
AddColumnString="ALTER TABLE `Model` ADD COLUMN `Column1` VARCHAR(255) NULL;",
35+
AlterColumnString="ALTER TABLE `Model` MODIFY COLUMN `Column2` VARCHAR(50) NULL;",
36+
ChangeColumnNameString="ALTER TABLE `Model` CHANGE COLUMN `OldColumn3` `Column3` VARCHAR(255) NULL;",
37+
AddFKString="ALTER TABLE `Child` ADD CONSTRAINT `JustOneFK` FOREIGN KEY (`IdModel`) REFERENCES `Model` (`Id`) ON UPDATE NO ACTION ON DELETE CASCADE;",
38+
CreateIndexString="CREATE UNIQUE INDEX `JustIndexOnColumn3` ON `Model`(`Column3`);"
39+
});
40+
41+
LogManager.LogFactory = new ConsoleLogFactory();
42+
}
43+
44+
[Test()]
45+
public void CanAddColumn ()
46+
{
47+
var model = typeof(Model);
48+
49+
foreach (var d in dialects)
50+
{
51+
OrmLiteConfig.DialectProvider=d.Provider;
52+
var fielDef = ModelDefinition<Model>.Definition.GetFieldDefinition<Model> (f => f.Column1);
53+
Assert.AreEqual(d.AddColumnString, (d.Provider.ToAddColumnStatement(model, fielDef)));
54+
}
55+
}
56+
57+
[Test()]
58+
public void CanAAlterColumn ()
59+
{
60+
var model = typeof(Model);
61+
62+
foreach (var d in dialects)
63+
{
64+
OrmLiteConfig.DialectProvider=d.Provider;
65+
var fielDef = ModelDefinition<Model>.Definition.GetFieldDefinition<Model> (f => f.Column2);
66+
Assert.AreEqual(d.AlterColumnString, (d.Provider.ToAlterColumnStatement(model, fielDef)));
67+
}
68+
69+
}
70+
71+
72+
[Test()]
73+
public void CanChangeColumnName ()
74+
{
75+
var model = typeof(Model);
76+
77+
foreach (var d in dialects)
78+
{
79+
OrmLiteConfig.DialectProvider=d.Provider;
80+
var fielDef = ModelDefinition<Model>.Definition.GetFieldDefinition<Model> (f => f.Column3);
81+
Assert.AreEqual(d.ChangeColumnNameString,(d.Provider.ToChangeColumnNameStatement(model, fielDef,"OldColumn3")));
82+
83+
}
84+
85+
}
86+
87+
[Test()]
88+
public void CanAddForeignKey ()
89+
{
90+
91+
foreach (var d in dialects)
92+
{
93+
OrmLiteConfig.DialectProvider=d.Provider;
94+
Assert.AreEqual(d.AddFKString,d.Provider.ToAddForeignKeyStatement<Child,Model>(f=>f.IdModel, fk=>fk.Id,OnFkOption.NoAction,OnFkOption.Cascade, "JustOneFK"));
95+
}
96+
}
97+
98+
99+
[Test()]
100+
public void CanCreateIndex ()
101+
{
102+
103+
foreach (var d in dialects)
104+
{
105+
OrmLiteConfig.DialectProvider=d.Provider;
106+
Assert.AreEqual(d.CreateIndexString, d.Provider.ToCreateIndexStatement<Model>(f=>f.Column3, "JustIndexOnColumn3", true) );
107+
108+
}
109+
110+
}
111+
112+
113+
}
114+
115+
public class Model
116+
{
117+
public Model()
118+
{
119+
}
120+
public int Id { get; set; }
121+
public string Column1{ get; set; }
122+
[StringLength(50)]
123+
public string Column2{ get; set; }
124+
public string Column3{ get; set; }
125+
126+
}
127+
128+
public class Child
129+
{
130+
public Child(){}
131+
public int Id{ get; set;}
132+
public int IdModel{ get; set;}
133+
134+
}
135+
136+
public class Dialect
137+
{
138+
public IOrmLiteDialectProvider Provider { get; set; }
139+
public string AddColumnString { get; set; }
140+
public string AlterColumnString { get; set; }
141+
public string ChangeColumnNameString { get; set; }
142+
public string AddFKString { get; set; }
143+
public string CreateIndexString { get; set; }
144+
145+
}
146+
}

src/ServiceStack.OrmLite.Firebird/FirebirdOrmLiteDialectProvider.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,7 @@ public override string ToCreateTableStatement(Type tableType)
334334
if (fieldDef.ForeignKey == null) continue;
335335

336336
var refModelDef = GetModel(fieldDef.ForeignKey.ReferenceType);
337-
338-
var modelName= modelDef.IsInSchema
339-
? modelDef.Schema + "_" + NamingStrategy.GetTableName(modelDef.ModelName)
340-
: NamingStrategy.GetTableName(modelDef.ModelName);
341-
342-
var refModelName= refModelDef.IsInSchema
343-
? refModelDef.Schema + "_" + NamingStrategy.GetTableName(refModelDef.ModelName)
344-
: NamingStrategy.GetTableName(refModelDef.ModelName);
345-
337+
346338
sbConstraints.AppendFormat(", \n\n CONSTRAINT {0} FOREIGN KEY ({1}) REFERENCES {2} ({3})",
347339
GetQuotedName(fieldDef.ForeignKey.GetForeignKeyName(modelDef, refModelDef, NamingStrategy, fieldDef)),
348340
GetQuotedColumnName(fieldDef.FieldName),
@@ -734,6 +726,49 @@ public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
734726
return result > 0;
735727
}
736728

729+
730+
#region DDL
731+
public override string ToAddColumnStatement(Type modelType, FieldDefinition fieldDef){
732+
733+
var column = GetColumnDefinition(fieldDef.FieldName,
734+
fieldDef.FieldType,
735+
fieldDef.IsPrimaryKey,
736+
fieldDef.AutoIncrement,
737+
fieldDef.IsNullable,
738+
fieldDef.FieldLength,
739+
fieldDef.Scale,
740+
fieldDef.DefaultValue);
741+
return string.Format("ALTER TABLE {0} ADD {1} ;",
742+
GetQuotedTableName(GetModel(modelType).ModelName),
743+
column);
744+
}
745+
746+
public override string ToAlterColumnStatement(Type modelType, FieldDefinition fieldDef)
747+
{
748+
749+
var column = GetColumnDefinition(fieldDef.FieldName,
750+
fieldDef.FieldType,
751+
fieldDef.IsPrimaryKey,
752+
fieldDef.AutoIncrement,
753+
fieldDef.IsNullable,
754+
fieldDef.FieldLength,
755+
fieldDef.Scale,
756+
fieldDef.DefaultValue);
757+
return string.Format("ALTER TABLE {0} ALTER {1} ;",
758+
GetQuotedTableName(GetModel(modelType).ModelName),
759+
column);
760+
}
761+
762+
public override string ToChangeColumnNameStatement(Type modelType,
763+
FieldDefinition fieldDef,
764+
string oldColumnName)
765+
{
766+
return string.Format("ALTER TABLE {0} ALTER {1} TO {2} ;",
767+
GetQuotedTableName(GetModel(modelType).ModelName),
768+
GetQuotedColumnName(oldColumnName),
769+
GetQuotedColumnName(fieldDef.FieldName));
770+
}
771+
#endregion DDL
737772
}
738773
}
739774

src/ServiceStack.OrmLite.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.OrmLite.Sqlite
100100
EndProject
101101
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.OrmLite.Oracle.Tests", "ServiceStack.OrmLite.Oracle.Tests\ServiceStack.OrmLite.Oracle.Tests.csproj", "{DE5CA3EB-010D-492A-A0B7-501D7941FA51}"
102102
EndProject
103+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.OrmLite.DDLTest", "ServiceStack.OrmLite.DDLTest\ServiceStack.OrmLite.DDLTest.csproj", "{E106F587-2DFF-4550-BA49-9A81A2EC344F}"
104+
EndProject
103105
Global
104106
GlobalSection(SolutionConfigurationPlatforms) = preSolution
105107
Debug|Any CPU = Debug|Any CPU
@@ -1095,6 +1097,34 @@ Global
10951097
{DE5CA3EB-010D-492A-A0B7-501D7941FA51}.STATIC_ONLY NO_EXPRESSIONS|Mixed Platforms.ActiveCfg = Release|Any CPU
10961098
{DE5CA3EB-010D-492A-A0B7-501D7941FA51}.STATIC_ONLY NO_EXPRESSIONS|Mixed Platforms.Build.0 = Release|Any CPU
10971099
{DE5CA3EB-010D-492A-A0B7-501D7941FA51}.STATIC_ONLY NO_EXPRESSIONS|x86.ActiveCfg = Release|Any CPU
1100+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1101+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|Any CPU.Build.0 = Debug|Any CPU
1102+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
1103+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
1104+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|x86.ActiveCfg = Debug|Any CPU
1105+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Debug|x86.Build.0 = Debug|Any CPU
1106+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Default|Any CPU.ActiveCfg = Debug|Any CPU
1107+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Default|Any CPU.Build.0 = Debug|Any CPU
1108+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Default|Mixed Platforms.ActiveCfg = Debug|Any CPU
1109+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Default|x86.ActiveCfg = Debug|Any CPU
1110+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|Any CPU.ActiveCfg = Debug|Any CPU
1111+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|Any CPU.Build.0 = Debug|Any CPU
1112+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|Mixed Platforms.ActiveCfg = Debug|Any CPU
1113+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|Mixed Platforms.Build.0 = Debug|Any CPU
1114+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|x86.ActiveCfg = Debug|Any CPU
1115+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.MonoTouch|x86.Build.0 = Debug|Any CPU
1116+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|Any CPU.ActiveCfg = Release|Any CPU
1117+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|Any CPU.Build.0 = Release|Any CPU
1118+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
1119+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
1120+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|x86.ActiveCfg = Release|Any CPU
1121+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.Release|x86.Build.0 = Release|Any CPU
1122+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|Any CPU.ActiveCfg = Debug|Any CPU
1123+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|Any CPU.Build.0 = Debug|Any CPU
1124+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|Mixed Platforms.ActiveCfg = Debug|Any CPU
1125+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|Mixed Platforms.Build.0 = Debug|Any CPU
1126+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|x86.ActiveCfg = Debug|Any CPU
1127+
{E106F587-2DFF-4550-BA49-9A81A2EC344F}.STATIC_ONLY NO_EXPRESSIONS|x86.Build.0 = Debug|Any CPU
10981128
EndGlobalSection
10991129
GlobalSection(SolutionProperties) = preSolution
11001130
HideSolutionNode = FALSE

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.Data;
15+
using System.Linq.Expressions;
1516

1617
namespace ServiceStack.OrmLite
1718
{
@@ -114,5 +115,19 @@ string ToSelectFromProcedureStatement(object fromObjWithProperties,
114115
string GetColumnTypeDefinition(Type fieldType);
115116

116117
string GetDropForeignKeyConstraints(ModelDefinition modelDef);
118+
119+
#region DDL
120+
string ToAddColumnStatement (Type modelType, FieldDefinition fieldDef);
121+
string ToAlterColumnStatement(Type modelType, FieldDefinition fieldDef);
122+
string ToChangeColumnNameStatement(Type modelType, FieldDefinition fieldDef, string oldColumnName);
123+
string ToAddForeignKeyStatement<T,TForeign>(Expression<Func<T,object>> field,
124+
Expression<Func<TForeign,object>> foreignField,
125+
OnFkOption onUpdate,
126+
OnFkOption onDelete,
127+
string foreignKeyName=null);
128+
string ToCreateIndexStatement<T>(Expression<Func<T,object>> field,
129+
string indexName=null, bool unique=false);
130+
#endregion DDL
131+
117132
}
118133
}

src/ServiceStack.OrmLite/ModelDefinition.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Collections.Generic;
1414
using System.Linq;
1515
using ServiceStack.DataAnnotations;
16+
using System.Linq.Expressions;
1617

1718
namespace ServiceStack.OrmLite
1819
{
@@ -96,6 +97,29 @@ public FieldDefinition[] AllFieldDefinitionsArray
9697
}
9798

9899
public List<CompositeIndexAttribute> CompositeIndexes { get; set; }
100+
101+
102+
public FieldDefinition GetFieldDefinition<T>(Expression<Func<T,object>> field)
103+
{
104+
var fn = GetFieldName (field);
105+
return FieldDefinitions.First(f=>f.Name==fn );
106+
}
107+
108+
string GetFieldName<T>(Expression<Func<T,object>> field){
109+
110+
var lambda = (field as LambdaExpression);
111+
if( lambda.Body.NodeType==ExpressionType.MemberAccess)
112+
{
113+
var me = lambda.Body as MemberExpression;
114+
return me.Member.Name;
115+
}
116+
else
117+
{
118+
var operand = (lambda.Body as UnaryExpression).Operand ;
119+
return (operand as MemberExpression).Member.Name;
120+
}
121+
}
122+
99123
}
100124

101125

0 commit comments

Comments
 (0)