Skip to content

Commit a86ccd0

Browse files
committed
Added optional Create flags
Added optional Create flags allowing easier use with POCO objects
1 parent 9e1ae73 commit a86ccd0

11 files changed

+917
-161
lines changed

.gitattributes

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

ImplicitChanges.txt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
76
2+
[Flags]
3+
public enum CreateFlags
4+
{
5+
None = 0,
6+
ImplicitPK = 1, // create a primary key for field called 'Id'
7+
ImplicitIndex = 2, // create an index for fields ending in 'Id'
8+
AllImplicit = 3, // do both above
9+
10+
AutoIncPK = 4 // force PK field to be auto inc
11+
}
12+
13+
14+
233
15+
public TableMapping GetMapping (Type type, CreateFlags createFlags = CreateFlags.None)
16+
{
17+
if (_mappings == null) {
18+
_mappings = new Dictionary<string, TableMapping> ();
19+
}
20+
TableMapping map;
21+
if (!_mappings.TryGetValue (type.FullName, out map)) {
22+
map = new TableMapping (type, createFlags);
23+
_mappings [type.FullName] = map;
24+
}
25+
return map;
26+
}
27+
28+
293
29+
public int CreateTable<T>(CreateFlags createFlags = CreateFlags.None)
30+
{
31+
return CreateTable(typeof (T), createFlags);
32+
}
33+
34+
308
35+
public int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None)
36+
{
37+
if (_tables == null) {
38+
_tables = new Dictionary<string, TableMapping> ();
39+
}
40+
TableMapping map;
41+
if (!_tables.TryGetValue (ty.FullName, out map)) {
42+
map = GetMapping(ty, createFlags);
43+
44+
45+
1436
46+
public TableMapping (Type type, CreateFlags createFlags = CreateFlags.None)
47+
48+
1464
49+
cols.Add (new Column (p, createFlags));
50+
51+
1603
52+
public Column (PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
53+
{
54+
var colAttr = (ColumnAttribute)prop.GetCustomAttributes (typeof(ColumnAttribute), true).FirstOrDefault ();
55+
56+
_prop = prop;
57+
Name = colAttr == null ? prop.Name : colAttr.Name;
58+
//If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead
59+
ColumnType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
60+
Collation = Orm.Collation (prop);
61+
IsAutoInc = Orm.IsAutoInc (prop);
62+
IsPK = Orm.IsPK (prop) || (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) && prop.Name == Orm.ImplicitPkName);
63+
64+
IsAutoInc = Orm.IsAutoInc(prop) || (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK));
65+
Indices = Orm.GetIndices(prop);
66+
if (!Indices.Any()
67+
&& !IsPK
68+
&& ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex)
69+
&& Name.EndsWith(Orm.ImplicitIndexSuffix)
70+
)
71+
{
72+
Indices = new IndexedAttribute[] {new IndexedAttribute()};
73+
}
74+
IsNullable = !IsPK;
75+
MaxStringLength = Orm.MaxStringLength (prop);
76+
}
77+
78+
1643
79+
public const string ImplicitPkName = "Id";
80+
public const string ImplicitIndexSuffix = "Id";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following attributes.
5+
// Change them to the values specific to your project.
6+
7+
[assembly: AssemblyTitle("Stocks")]
8+
[assembly: AssemblyDescription("An example of using sqlite-net")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("")]
12+
[assembly: AssemblyCopyright("")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19+
20+
[assembly: AssemblyVersion("1.0.*")]
21+
22+
// The following attributes are used to specify the signing key for the assembly,
23+
// if desired. See the Mono documentation for more information about signing.
24+
25+
//[assembly: AssemblyDelaySign(false)]
26+
//[assembly: AssemblyKeyFile("")]

examples/StocksImplicit/Main.cs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Path = System.IO.Path;
5+
6+
using SQLite;
7+
8+
namespace Stocks.CommandLine
9+
{
10+
class Program
11+
{
12+
public static void Main (string[] args)
13+
{
14+
new Program ().Run ();
15+
}
16+
17+
Database _db;
18+
19+
void Initialize ()
20+
{
21+
var dbPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "StocksImplicit.db");
22+
_db = new Database (dbPath);
23+
}
24+
25+
void DisplayStock (string stockSymbol)
26+
{
27+
var stock = _db.QueryStock (stockSymbol);
28+
29+
if (stock == null) {
30+
Console.WriteLine ("I don't know about {0}", stockSymbol);
31+
Console.WriteLine ("Run \"up {0}\" to update the stock", stockSymbol);
32+
} else {
33+
34+
//
35+
// Display the last 1 week
36+
//
37+
foreach (var v in _db.QueryValuations (stock)) {
38+
Console.WriteLine (" {0}", v);
39+
}
40+
41+
}
42+
}
43+
44+
void UpdateStock (string stockSymbol)
45+
{
46+
_db.UpdateStock(stockSymbol);
47+
}
48+
49+
void ListStocks ()
50+
{
51+
foreach (var stock in _db.QueryAllStocks ()) {
52+
Console.WriteLine (stock);
53+
}
54+
}
55+
56+
void DisplayBanner ()
57+
{
58+
Console.WriteLine ("Stocks - a demo of sqlite-net");
59+
Console.WriteLine ("Using " + _db.DatabaseName);
60+
Console.WriteLine ();
61+
}
62+
63+
void DisplayHelp (string cmd)
64+
{
65+
Action<string, string> display = (c, h) => { Console.WriteLine ("{0} {1}", c, h); };
66+
var cmds = new SortedDictionary<string, string> {
67+
{
68+
"ls",
69+
"\t List all known stocks"
70+
},
71+
{
72+
"exit",
73+
"\t Exit stocks"
74+
},
75+
{
76+
"up stock",
77+
"Updates stock"
78+
},
79+
{
80+
"help",
81+
"\t Displays help"
82+
},
83+
{
84+
"stock",
85+
"\t Displays latest valuations for stock"
86+
}
87+
};
88+
if (cmds.ContainsKey (cmd)) {
89+
display (cmd, cmds[cmd]);
90+
} else {
91+
foreach (var ch in cmds) {
92+
display (ch.Key, ch.Value);
93+
}
94+
}
95+
}
96+
97+
void Run ()
98+
{
99+
var WS = new char[] {
100+
' ',
101+
'\t',
102+
'\r',
103+
'\n'
104+
};
105+
106+
Initialize ();
107+
108+
DisplayBanner ();
109+
DisplayHelp ("");
110+
111+
for (;;) {
112+
Console.Write ("$ ");
113+
var cmdline = Console.ReadLine ();
114+
115+
var args = cmdline.Split (WS, StringSplitOptions.RemoveEmptyEntries);
116+
if (args.Length < 1)
117+
continue;
118+
var cmd = args[0].ToLowerInvariant ();
119+
120+
if (cmd == "?" || cmd == "help") {
121+
DisplayHelp ("");
122+
} else if (cmd == "exit") {
123+
break;
124+
} else if (cmd == "ls") {
125+
ListStocks ();
126+
} else if (cmd == "up") {
127+
if (args.Length == 2) {
128+
UpdateStock (args[1].ToUpperInvariant ());
129+
} else {
130+
DisplayHelp ("up stock");
131+
}
132+
} else {
133+
DisplayStock (cmd.ToUpperInvariant ());
134+
}
135+
}
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)