Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
d8728c2c
Commit
d8728c2c
authored
Dec 31, 2018
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sqlitehelper 支持 borther属性
parent
8d83f1b8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
41 deletions
+130
-41
IgnoreAttribute.cs
...Components/OBJComponents/Common/SQLite/IgnoreAttribute.cs
+9
-0
SQLiteHelper.cs
...OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
+121
-41
No files found.
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/IgnoreAttribute.cs
View file @
d8728c2c
...
...
@@ -10,4 +10,13 @@ namespace FLY.OBJComponents.Common
}
}
public
class
BortherAttribute
:
Attribute
{
}
public
class
ChildAttribute
:
Attribute
{
}
}
\ No newline at end of file
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
View file @
d8728c2c
...
...
@@ -130,17 +130,8 @@ namespace FLY.OBJComponents.Common.SQLite
}
}
public
static
string
GetCreateTableCommandText
(
Type
type
,
params
ArrayFieldTypeInfo
[]
arrayFieldTypeInfos
)
static
string
GetCreateTableCommandText_fieldText
(
Type
type
,
IEnumerable
<
ArrayFieldTypeInfo
>
arrayFieldTypeInfos
)
{
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
// column3 datatype,
// .....
// columnN datatype,
//)
string
tablename
=
GetTableName
(
type
);
string
fieldtext
=
""
;
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
...
...
@@ -154,6 +145,18 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext
+=
","
;
}
//这个属性 下面的全部属性,是同一个表
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
BortherAttribute
),
false
).
Count
()
>
0
)
{
//从arrayFieldTypeInfos 提取
string
startswith
=
propertyInfo
.
Name
+
"."
;
var
aftis
=
from
afti
in
arrayFieldTypeInfos
where
afti
.
PropertyName
.
StartsWith
(
startswith
)
select
new
ArrayFieldTypeInfo
(
afti
.
PropertyName
.
Substring
(
startswith
.
Length
),
afti
.
PropertyName
.
Length
);
fieldtext
+=
GetCreateTableCommandText_fieldText
(
propertyInfo
.
PropertyType
,
aftis
);
continue
;
}
if
(
propertyInfo
.
PropertyType
.
IsArray
)
//是数组,需要明确大小
{
int
length
=
arrayFieldTypeInfos
.
First
((
a
)
=>
a
.
PropertyName
==
propertyInfo
.
Name
).
Length
;
...
...
@@ -161,7 +164,7 @@ namespace FLY.OBJComponents.Common.SQLite
Type
elementType
=
propertyInfo
.
PropertyType
.
GetElementType
();
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
elementType
);
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
if
(
i
!=
0
)
...
...
@@ -184,22 +187,28 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext
+=
" PRIMARY KEY"
;
}
}
return
fieldtext
;
}
public
static
string
GetCreateTableCommandText
(
Type
type
,
params
ArrayFieldTypeInfo
[]
arrayFieldTypeInfos
)
{
//CREATE TABLE table_name(
//column1 datatype PRIMARY KEY,
//column2 datatype,
// column3 datatype,
// .....
// columnN datatype,
//)
string
tablename
=
GetTableName
(
type
);
string
fieldtext
=
GetCreateTableCommandText_fieldText
(
type
,
arrayFieldTypeInfos
);
string
commandText
=
string
.
Format
(
"CREATE TABLE {0} ({1})"
,
tablename
,
fieldtext
);
return
commandText
;
}
public
static
string
GetInsertCommandText
(
object
cell
)
{
//不用
//INSERT INTO TABLE_NAME[(column1, column2, column3,...columnN)]
//VALUES(value1, value2, value3,...valueN);
//使用
//INSERT INTO TABLE_NAME VALUES(value1, value2, value3,...valueN);
static
string
GetInsertCommandText_fieldText
(
object
cell
)
{
Type
type
=
cell
.
GetType
();
string
tablename
=
GetTableName
(
type
);
string
fieldtext
=
""
;
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
...
...
@@ -213,13 +222,23 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext
+=
","
;
}
object
o
=
propertyInfo
.
GetValue
(
cell
,
null
);
//这个属性 下面的全部属性,是同一个表
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
BortherAttribute
),
false
).
Count
()
>
0
)
{
fieldtext
+=
GetInsertCommandText_fieldText
(
o
);
continue
;
}
if
(
propertyInfo
.
PropertyType
.
IsArray
)
//是数组,需要明确大小
{
Type
elementType
=
propertyInfo
.
PropertyType
.
GetElementType
();
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
elementType
);
Array
a
=
propertyInfo
.
GetValue
(
cell
,
null
)
as
Array
;
Array
a
=
o
as
Array
;
for
(
int
i
=
0
;
i
<
a
.
Length
;
i
++)
{
...
...
@@ -233,22 +252,29 @@ namespace FLY.OBJComponents.Common.SQLite
{
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
propertyInfo
.
PropertyType
);
object
o
=
propertyInfo
.
GetValue
(
cell
,
null
);
fieldtext
+=
fieldTypeInfo
.
PtoS
(
o
);
}
}
return
fieldtext
;
}
public
static
string
GetInsertCommandText
(
object
cell
)
{
//不用
//INSERT INTO TABLE_NAME[(column1, column2, column3,...columnN)]
//VALUES(value1, value2, value3,...valueN);
//使用
//INSERT INTO TABLE_NAME VALUES(value1, value2, value3,...valueN);
Type
type
=
cell
.
GetType
();
string
tablename
=
GetTableName
(
type
);
string
fieldtext
=
GetInsertCommandText_fieldText
(
cell
);
string
commandText
=
string
.
Format
(
"INSERT INTO {0} VALUES({1})"
,
tablename
,
fieldtext
);
return
commandText
;
}
/// <summary>
/// condition 为 "WHERE ......"
/// </summary>
/// <param name="cell"></param>
/// <param name="condition"></param>
/// <returns></returns>
public
static
string
GetUpdateCommandText
(
object
cell
,
string
condition
)
static
string
GetUpdateCommandText_fieldText
(
object
cell
)
{
//UPDATE table_name
//SET column1 = value1, column2 = value2...., columnN = valueN
...
...
@@ -270,33 +296,62 @@ namespace FLY.OBJComponents.Common.SQLite
fieldtext
+=
","
;
}
object
o
=
propertyInfo
.
GetValue
(
cell
,
null
);
//这个属性 下面的全部属性,是同一个表
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
BortherAttribute
),
false
).
Count
()
>
0
)
{
fieldtext
+=
GetUpdateCommandText_fieldText
(
o
);
continue
;
}
if
(
propertyInfo
.
PropertyType
.
IsArray
)
//是数组,需要明确大小
{
Type
elementType
=
propertyInfo
.
PropertyType
.
GetElementType
();
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
elementType
);
Array
a
=
propertyInfo
.
GetValue
(
cell
,
null
)
as
Array
;
Array
a
=
o
as
Array
;
for
(
int
i
=
0
;
i
<
a
.
Length
;
i
++)
{
if
(
i
!=
0
)
fieldtext
+=
","
;
fieldtext
+=
string
.
Format
(
"{0}{1} = {2}"
,
propertyInfo
.
PropertyType
.
Name
,
i
,
fieldTypeInfo
.
PtoS
(
a
.
GetValue
(
i
)));
fieldtext
+=
string
.
Format
(
"{0}{1} = {2}"
,
propertyInfo
.
Name
,
i
,
fieldTypeInfo
.
PtoS
(
a
.
GetValue
(
i
)));
}
}
else
{
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
propertyInfo
.
PropertyType
);
object
o
=
propertyInfo
.
GetValue
(
cell
,
null
);
fieldtext
+=
string
.
Format
(
"{0} = {1}"
,
propertyInfo
.
PropertyType
.
Name
,
fieldTypeInfo
.
PtoS
(
o
));
fieldtext
+=
string
.
Format
(
"{0} = {1}"
,
propertyInfo
.
Name
,
fieldTypeInfo
.
PtoS
(
o
));
}
}
string
commandText
=
string
.
Format
(
"UPDATE {0} SET {1} {2}"
,
tablename
,
fieldtext
,
condition
);
return
fieldtext
;
}
/// <summary>
/// condition 为 "WHERE ......"
/// </summary>
/// <param name="cell"></param>
/// <param name="condition"></param>
/// <returns></returns>
public
static
string
GetUpdateCommandText
(
object
cell
,
string
condition
)
{
//UPDATE table_name
//SET column1 = value1, column2 = value2...., columnN = valueN
//WHERE[condition];
Type
type
=
cell
.
GetType
();
string
tablename
=
GetTableName
(
type
);
string
fieldtext
=
GetUpdateCommandText_fieldText
(
cell
);
string
commandText
=
$"UPDATE
{
tablename
}
SET
{
fieldtext
}
"
;
if
(!
string
.
IsNullOrEmpty
(
condition
))
commandText
+=
$"
{
condition
}
"
;
return
commandText
;
}
...
...
@@ -312,21 +367,39 @@ namespace FLY.OBJComponents.Common.SQLite
return
list
;
}
public
static
T
ToObj
<
T
>(
DataRow
dataRow
,
params
ArrayFieldTypeInfo
[]
arrayFieldTypeInfos
)
where
T
:
new
()
static
void
SetObj
(
object
t
,
DataRow
dataRow
,
IEnumerable
<
ArrayFieldTypeInfo
>
arrayFieldTypeInfos
)
{
Type
type
=
t
ypeof
(
T
);
T
t
=
new
T
();
Type
type
=
t
.
GetType
(
);
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
{
//忽略
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
IgnoreAttribute
),
false
).
Count
()
>
0
)
continue
;
Type
ptype
=
propertyInfo
.
PropertyType
;
//这个属性 下面的全部属性,是同一个表
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
BortherAttribute
),
false
).
Count
()
>
0
)
{
//从arrayFieldTypeInfos 提取
string
startswith
=
propertyInfo
.
Name
+
"."
;
var
aftis
=
from
afti
in
arrayFieldTypeInfos
where
afti
.
PropertyName
.
StartsWith
(
startswith
)
select
new
ArrayFieldTypeInfo
(
afti
.
PropertyName
.
Substring
(
startswith
.
Length
),
afti
.
PropertyName
.
Length
);
object
obj
=
propertyInfo
.
GetValue
(
t
,
null
);
SetObj
(
obj
,
dataRow
,
aftis
);
continue
;
}
if
(
ptype
.
IsArray
)
//是数组,需要明确大小
{
Type
elementType
=
propertyInfo
.
PropertyType
.
GetElementType
();
SQLiteFieldTypeInfo
fieldTypeInfo
=
FieldTypeInfo
.
Find
((
fti
)
=>
fti
.
PropertyType
==
elementType
);
int
length
=
arrayFieldTypeInfos
.
First
((
a
)
=>
a
.
PropertyName
==
propertyInfo
.
Name
).
Length
;
Array
array
=
(
Array
)
ptype
.
Assembly
.
CreateInstance
(
ptype
.
FullName
,
false
,
BindingFlags
.
CreateInstance
,
null
,
new
object
[]
{
length
},
null
,
null
);
...
...
@@ -344,6 +417,13 @@ namespace FLY.OBJComponents.Common.SQLite
propertyInfo
.
SetValue
(
t
,
o
,
null
);
}
}
}
public
static
T
ToObj
<
T
>(
DataRow
dataRow
,
params
ArrayFieldTypeInfo
[]
arrayFieldTypeInfos
)
where
T
:
new
()
{
Type
type
=
typeof
(
T
);
T
t
=
new
T
();
SetObj
(
t
,
dataRow
,
arrayFieldTypeInfos
);
return
t
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment