Skip to content

Autoincrement is automatic if an ID <> 0 is set #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##########################################################
#### WhiteSource "Bolt for Github" configuration file ####
##########################################################

# Configuration #
#---------------#
ws.repo.scan=true
vulnerable.check.run.conclusion.level=failure
26 changes: 25 additions & 1 deletion src/SQLite.Net/SQLiteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,9 +1477,33 @@ public int Insert(object obj, string extra, Type objType)
}
}

bool usePkValue = false;
if (map.PK != null && map.PK.IsAutoInc)
{
var prop = objType.GetRuntimeProperty(map.PK.PropertyName);
if (prop != null)
{
var val = prop.GetValue(obj);
if (val != null && val.ToString() != "0")
{
usePkValue = true;
}
}
}

var replacing = string.Compare(extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) == 0;

var cols = replacing ? map.Columns : map.InsertColumns;
TableMapping.Column[] cols;

if (usePkValue)
{
cols = replacing ? map.Columns : map.InsertColumnsWithPk;
}
else
{
cols = replacing ? map.Columns : map.InsertColumns;
}

var vals = new object[cols.Length];
for (var i = 0; i < vals.Length; i++)
{
Expand Down
7 changes: 7 additions & 0 deletions src/SQLite.Net/TableMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public Column[] InsertColumns
{
get { return _insertColumns ?? (_insertColumns = Columns.Where(c => !c.IsAutoInc).ToArray()); }
}

[PublicAPI]
public Column[] InsertColumnsWithPk
{
get { return _insertColumns ?? (_insertColumns = Columns.ToArray()); }
}


[PublicAPI]
public void SetAutoIncPK(object obj, long id)
Expand Down