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
8d83f1b8
Commit
8d83f1b8
authored
Dec 29, 2018
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
改SQLiteHelper
parent
60903c07
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
146 additions
and
21 deletions
+146
-21
PropertiesManager.cs
Project.FLY.Misc/MISC/PropertiesManager.cs
+32
-3
IDBTable.cs
...FLY.OBJComponents/OBJComponents/Common/SQLite/IDBTable.cs
+2
-0
IgnoreAttribute.cs
...Components/OBJComponents/Common/SQLite/IgnoreAttribute.cs
+13
-0
SQLiteHelper.cs
...OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
+98
-18
OBJComponents.csproj
Project.FLY.OBJComponents/OBJComponents/OBJComponents.csproj
+1
-0
No files found.
Project.FLY.Misc/MISC/PropertiesManager.cs
View file @
8d83f1b8
...
...
@@ -68,22 +68,51 @@ namespace Misc
return
null
;
}
/// <summary>
/// 把 src内的全部属性,复制到 dest
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="src"></param>
/// <param name="dest"></param>
public
static
void
CopyTo
<
T
>(
T
src
,
T
dest
)
public
static
void
CopyTo
(
object
src
,
object
dest
)
{
Type
t
=
typeof
(
T
);
Type
t
=
src
.
GetType
();
if
(
src
.
GetType
()
!=
dest
.
GetType
())
{
throw
new
Exception
(
"CopyTo src.GetType() != dest.GetType()"
);
}
foreach
(
PropertyInfo
propertyInfo
in
t
.
GetProperties
())
{
if
(
propertyInfo
.
CanWrite
)
{
propertyInfo
.
SetValue
(
dest
,
propertyInfo
.
GetValue
(
src
,
null
),
null
);
}
else
{
//ICopiable
if
(
propertyInfo
.
PropertyType
.
IsSubclassOf
(
typeof
(
ICopiable
)))
{
ICopiable
copiable
=
propertyInfo
.
GetValue
(
dest
,
null
)
as
ICopiable
;
copiable
.
Copy
(
propertyInfo
.
GetValue
(
src
,
null
));
}
}
}
}
}
/// <summary>
/// 一般情况 用 Misc.PropertiesManager.CopyTo 就能解决问题
/// 但 只有get的属性,是没法复制的
/// 只能通过 继承 ICopiable, 解决 只能get 的属性 复制问题
/// </summary>
public
interface
ICopiable
{
/// <summary>
/// 从 src 复制全部数据过来!!!
///
/// </summary>
/// <param name="src"></param>
void
Copy
(
object
src
);
}
}
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/IDBTable.cs
View file @
8d83f1b8
...
...
@@ -12,5 +12,7 @@ namespace FLY.OBJComponents.Common.SQLite
void
Create
();
void
Init
(
string
connnectionString
);
}
}
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/IgnoreAttribute.cs
0 → 100644
View file @
8d83f1b8
using
System
;
namespace
FLY.OBJComponents.Common
{
public
class
IgnoreAttribute
:
Attribute
{
public
IgnoreAttribute
()
{
}
}
}
\ No newline at end of file
Project.FLY.OBJComponents/OBJComponents/Common/SQLite/SQLiteHelper.cs
View file @
8d83f1b8
...
...
@@ -145,6 +145,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
{
//忽略
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
IgnoreAttribute
),
false
).
Count
()
>
0
)
continue
;
if
(
fieldtext
!=
""
)
{
fieldtext
+=
","
;
...
...
@@ -200,6 +204,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
{
//忽略
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
IgnoreAttribute
),
false
).
Count
()
>
0
)
continue
;
if
(
fieldtext
!=
""
)
{
fieldtext
+=
","
;
...
...
@@ -233,6 +241,13 @@ namespace FLY.OBJComponents.Common.SQLite
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
)
{
//UPDATE table_name
...
...
@@ -246,6 +261,10 @@ namespace FLY.OBJComponents.Common.SQLite
PropertyInfo
[]
propertyInfos
=
type
.
GetProperties
();
foreach
(
var
propertyInfo
in
propertyInfos
)
{
//忽略
if
(
propertyInfo
.
GetCustomAttributes
(
typeof
(
IgnoreAttribute
),
false
).
Count
()
>
0
)
continue
;
if
(
fieldtext
!=
""
)
{
fieldtext
+=
","
;
...
...
@@ -340,18 +359,18 @@ namespace FLY.OBJComponents.Common.SQLite
public
DataTable
ExecuteReader
(
string
sql
)
{
DataTable
data
;
using
(
SQLiteConnection
conn
=
new
SQLiteConnection
(
ConnectionString
))
using
(
SQLiteConnection
conn
ection
=
new
SQLiteConnection
(
ConnectionString
))
{
conn
.
Open
();
SQLiteCommand
command
=
conn
.
CreateCommand
();
command
.
CommandText
=
sql
;
// 开始读取
SQLiteDataAdapter
adapter
=
new
SQLiteDataAdapter
(
command
);
data
=
new
DataTable
(
);
adapter
.
Fill
(
data
);
conn
.
Close
();
using
(
SQLiteCommand
command
=
new
SQLiteCommand
(
connection
))
{
connection
.
Open
();
command
.
CommandText
=
sql
;
// 开始读取
SQLiteDataAdapter
adapter
=
new
SQLiteDataAdapter
(
command
);
data
=
new
DataTable
(
);
adapter
.
Fill
(
data
);
connection
.
Close
(
);
}
}
return
data
;
}
...
...
@@ -371,10 +390,32 @@ namespace FLY.OBJComponents.Common.SQLite
connection
.
Open
();
command
.
CommandText
=
sql
;
command
.
ExecuteNonQuery
();
connection
.
Close
();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sql"></param>
/// <returns>The first column of the first row of the first resultset from the query.</returns>
public
object
ExecuteScalar
(
string
sql
)
{
object
obj
=
null
;
using
(
SQLiteConnection
connection
=
new
SQLiteConnection
(
ConnectionString
))
{
using
(
SQLiteCommand
command
=
new
SQLiteCommand
(
connection
))
{
connection
.
Open
();
command
.
CommandText
=
sql
;
obj
=
command
.
ExecuteScalar
();
connection
.
Close
();
}
}
return
obj
;
}
/// <summary>
/// 多行执行
/// </summary>
...
...
@@ -382,20 +423,20 @@ namespace FLY.OBJComponents.Common.SQLite
/// <returns></returns>
public
bool
QueryTran
(
IEnumerable
<
string
>
queryList
)
{
using
(
SQLiteConnection
conn
=
new
SQLiteConnection
(
ConnectionString
))
using
(
SQLiteConnection
conn
ection
=
new
SQLiteConnection
(
ConnectionString
))
{
using
(
SQLiteCommand
c
md
=
new
SQLiteCommand
(
con
n
))
using
(
SQLiteCommand
c
ommand
=
new
SQLiteCommand
(
connectio
n
))
{
conn
.
Open
();
conn
ection
.
Open
();
SQLiteTransaction
tran
=
conn
.
BeginTransaction
();
SQLiteTransaction
tran
=
conn
ection
.
BeginTransaction
();
bool
check
=
false
;
try
{
foreach
(
string
item
in
queryList
)
{
c
m
d
.
CommandText
=
item
;
c
m
d
.
ExecuteNonQuery
();
c
omman
d
.
CommandText
=
item
;
c
omman
d
.
ExecuteNonQuery
();
}
tran
.
Commit
();
check
=
true
;
...
...
@@ -408,7 +449,7 @@ namespace FLY.OBJComponents.Common.SQLite
}
finally
{
conn
.
Close
();
conn
ection
.
Close
();
}
return
check
;
}
...
...
@@ -416,5 +457,44 @@ namespace FLY.OBJComponents.Common.SQLite
}
/// <summary>
/// 输入DDLs 判断这些table都是否合法
/// </summary>
/// <param name="DDLs">key=tablename, value=DDL</param>
/// <returns></returns>
public
bool
IsTableValid
(
Dictionary
<
string
,
string
>
DDLs
)
{
//检测 table 是否合法
DataTable
data
=
ExecuteReader
(
"SELECT name,sql FROM sqlite_master WHERE type = 'table'"
);
//任意一个表不对,或者不存在,都必须重建
bool
isVaild
=
true
;
foreach
(
var
kv
in
DDLs
)
{
string
tablename
=
kv
.
Key
;
string
createtable_sql
=
kv
.
Value
;
var
sqls
=
from
r
in
data
.
AsEnumerable
()
where
(
string
)
r
[
"name"
]
==
tablename
select
r
[
"sql"
];
if
(
sqls
.
Count
()
==
0
)
{
//不存在该表,创建
isVaild
=
false
;
break
;
}
else
{
string
sql
=
(
string
)
sqls
.
First
();
if
(
sql
!=
createtable_sql
)
{
isVaild
=
false
;
break
;
}
}
}
return
isVaild
;
}
}
}
Project.FLY.OBJComponents/OBJComponents/OBJComponents.csproj
View file @
8d83f1b8
...
...
@@ -79,6 +79,7 @@
<Compile
Include=
"Common\PropertiesManager.cs"
/>
<Compile
Include=
"Common\SQLite\DBTable.cs"
/>
<Compile
Include=
"Common\SQLite\IDBTable.cs"
/>
<Compile
Include=
"Common\SQLite\IgnoreAttribute.cs"
/>
<Compile
Include=
"Common\SQLite\SQLiteHelper.cs"
/>
<Compile
Include=
"IService\IBuffer.cs"
/>
<Compile
Include=
"IService\IPLCProxySystemService.cs"
/>
...
...
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