-
Notifications
You must be signed in to change notification settings - Fork 1
/
PMLSQLite.cs
175 lines (158 loc) · 5.36 KB
/
PMLSQLite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections;
using System.Data;
using System.Data.SQLite;
using System.Text;
using Aveva.Core.PMLNet;
namespace PMLSQLite
{
/// <summary>
/// Represents a connection between PML and SQLite database.
/// </summary>
[PMLNetCallable()]
public class PMLSQLite
{
private SQLiteConnection conn;
[PMLNetCallable()]
public PMLSQLite()
{
conn = new SQLiteConnection("");
}
/// <summary>
/// Initializes a new instance with the specified connection string.
/// </summary>
/// <param name="constr">Connection string</param>
[PMLNetCallable()]
public PMLSQLite(string constr)
{
conn = new SQLiteConnection(constr);
}
[PMLNetCallable()]
public void Assign(PMLSQLite that)
{
if (conn != null) conn.Dispose();
conn = that.conn;
}
public override string ToString()
{
return conn.ConnectionString;
}
/// <summary>
/// Returns the full path for the currently open database.
/// </summary>
/// <returns>The full path for the currently open database</returns>
[PMLNetCallable()]
public string FileName()
{
return conn.FileName;
}
/// <summary>
/// Sets a filename for the data source. the environment variables in the filename are expanded.
/// </summary>
/// <param name="filename">Filename</param>
[PMLNetCallable()]
public void SetDataSource(string filename)
{
var csb = new SQLiteConnectionStringBuilder(conn.ConnectionString);
csb.DataSource = Environment.ExpandEnvironmentVariables(filename);
conn.ConnectionString = csb.ToString();
}
/// <summary>
/// Opens the connection.
/// </summary>
[PMLNetCallable()]
public void Open()
{
conn.Open();
}
/// <summary>
/// Closes the connection.
/// </summary>
[PMLNetCallable()]
public void Close()
{
conn.Close();
}
/// <summary>
/// Returns if the connection is open.
/// </summary>
/// <returns>true if the connection is openl, false otherwise</returns>
[PMLNetCallable()]
public bool IsOpen()
{
return (conn.State & ConnectionState.Open) == ConnectionState.Open;
}
/// <summary>
/// Returns the version of the SQLite database engine.
/// </summary>
/// <returns>The version of the SQLite database engine</returns>
[PMLNetCallable()]
public string Version()
{
return conn.ServerVersion;
}
/// <summary>
/// Backs up the database to another database.
/// </summary>
/// <param name="dest">Destination for backup</param>
[PMLNetCallable()]
public void Backup(PMLSQLite dest)
{
conn.BackupDatabase(dest.conn, dest.conn.Database, conn.Database, -1, null, -1);
}
/// <summary>
/// Returns if the connection is in autocommit mode.
/// </summary>
/// <returns>true if the connection is in autocommit mode, false otherwise</returns>
[PMLNetCallable()]
public bool AutoCommit()
{
return conn.AutoCommit;
}
/// <summary>
/// Returns the number of rows changed by the last INSERT, UPDATE, or DELETE statement.
/// </summary>
/// <returns>The number of rows changed by the last statement</returns>
[PMLNetCallable()]
public double Changes()
{
return conn.Changes;
}
/// <summary>
/// Returns the rowid set by the last INSERT statement.
/// </summary>
/// <returns>The rowid set by the last INSERT statement</returns>
[PMLNetCallable()]
public double LastInsertRowId()
{
return conn.LastInsertRowId;
}
/// <summary>
/// Returns a new <see cref="PMLSQLiteStatement"/> instance with the specified SQL statement.
/// </summary>
/// <param name="stmt">SQL statement</param>
/// <returns>A new <see cref="PMLSQLiteStatement"/> instance</returns>
[PMLNetCallable()]
public PMLSQLiteStatement Prepare(string stmt)
{
var cmd = conn.CreateCommand();
cmd.CommandText = stmt;
return new PMLSQLiteStatement(cmd);
}
/// <summary>
/// Returns a new <see cref="PMLSQLiteStatement"/> instance with the specified SQL statement.
/// </summary>
/// <param name="stmt">PML array of SQL statement strings</param>
/// <returns>A new <see cref="PMLSQLiteStatement"/> instance</returns>
[PMLNetCallable()]
public PMLSQLiteStatement Prepare(Hashtable stmt)
{
var sb = new StringBuilder();
for (double i = 1; i <= stmt.Count; i++)
{
sb.Append(stmt[i]).Append("\r\n");
}
return Prepare(sb.ToString());
}
}
}