Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
CREATE OR REPLACE FUNCTION SPIScalarFSharp() RETURNS integer AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    let command = new NpgsqlCommand("SELECT 2024;", conn)
    let value = command.ExecuteScalar()
 
    match value with
    | :? int as intValue -> Nullable<int>(intValue)
    | _ -> Nullable<int>()
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int4-spi', 'SPIScalarFSharp', SPIScalarFSharp() = 2024;
 
CREATE OR REPLACE FUNCTION SPIMultiQueryFsharp() RETURNS integer AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    let command = new NpgsqlCommand("SELECT 1; SELECT 2; SELECT 3", conn)
    let reader = command.ExecuteReader()
 
    let mutable sum = 0
    let mutable value = 0
 
    reader.Read()
    value <- reader.GetInt32(0)
    Elog.Info("[DEBUG - PLFSHARP TEST] Value read: " + value.ToString())
    sum <- sum + value
 
    reader.NextResult()
    reader.Read()
    value <- reader.GetInt32(0)
    Elog.Info("[DEBUG - PLFSHARP TEST] Value read: " + value.ToString())
    sum <- sum + value
 
    reader.NextResult()
    reader.Read()
    value <- reader.GetInt32(0)
    Elog.Info("[DEBUG - PLFSHARP TEST] Value read: " + value.ToString())
    sum <- sum + value
 
    Nullable(sum)
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int4-spi', 'SPIMultiQueryFsharp', SPIMultiQueryFsharp() = 6;
 
CREATE OR REPLACE FUNCTION SPISumIntegersFSharp(a integer, b integer, c integer) RETURNS integer AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    let query1 = "SELECT " + a.Value.ToString() + " as a, " + b.Value.ToString() + " as b, " + c.Value.ToString() + " as c"
    let query2 = "SELECT " + (2*a.Value).ToString() + " as a, " + (2*b.Value).ToString() + " as b, " + (2*c.Value).ToString() + " as c"
    let command = new NpgsqlCommand(query1 + "; " + query2, conn)
    let reader = command.ExecuteReader()
 
    let mutable sum = 0
    while reader.Read() do
        let _a = reader.GetInt32(0)
        let _b = reader.GetInt32(1)
        let _c = reader.GetInt32(2)
        Elog.Info("Returned values of the FIRST query: " + _a.ToString() + " | " + _b.ToString() + " | " + _c.ToString())
        sum <- sum + _a + _b + _c
 
    reader.NextResult() |> ignore
 
    while reader.Read() do
        let _a = reader.GetInt32(0)
        let _b = reader.GetInt32(1)
        let _c = reader.GetInt32(2)
        Elog.Info("Returned values of the SECOND query: " + _a.ToString() + " | " + _b.ToString() + " | " + _c.ToString())
        sum <- sum + _a + _b + _c
    Nullable(sum)
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int-spi-multiquery', 'SPISumIntegersFSharp1', SPISumIntegersFSharp(1, 2, 3) = 18;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int-spi-multiquery', 'SPISumIntegersFSharp2', SPISumIntegersFSharp(4, 0, 5) = 27;
 
CREATE OR REPLACE FUNCTION SPITestTruncateAndAlterFSharp() RETURNS BOOL AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    try
        let createCommand = new NpgsqlCommand("DROP TABLE IF EXISTS test_alter_truncate; CREATE TABLE test_alter_truncate (ID INTEGER); ALTER TABLE test_alter_truncate ADD COLUMN Name TEXT;", conn)
        ignore (createCommand.ExecuteNonQuery())
 
        let insertCommand = new NpgsqlCommand("INSERT INTO test_alter_truncate (ID, Name) VALUES (1, 'Test');", conn)
        ignore (insertCommand.ExecuteNonQuery())
 
        let checkCommand = new NpgsqlCommand("SELECT COUNT(*) FROM test_alter_truncate;", conn)
        let mutable countValue = checkCommand.ExecuteScalar()
 
        Elog.Info("Number of rows before truncation = " + countValue.ToString())
 
        let truncateCommand = new NpgsqlCommand("TRUNCATE TABLE test_alter_truncate;", conn)
        ignore (truncateCommand.ExecuteNonQuery())
 
        countValue <- checkCommand.ExecuteScalar()
        Elog.Info("Number of rows after truncation = " + countValue.ToString())
 
        match countValue with
        | :? int64 as intValue ->
            if intValue = 0 then Nullable(true)
            else Nullable(false)
        | _ -> Nullable(false)
    with
    | _ ->
        Nullable(false)
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-bool-spi', 'SPITestTruncateAndAlterFSharp', SPITestTruncateAndAlterFSharp();
 
CREATE OR REPLACE FUNCTION SPITestCreateFunctionFSharp(inputtext text) RETURNS text AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    try
        // SQL to create a function that duplicates the input text
        let createFunctionSQL = "CREATE OR REPLACE FUNCTION simple_duplicate_function(input text) RETURNS TEXT AS $BODY$ return input + input; $BODY$ LANGUAGE plcsharp;"
        let createFunctionCmd = new NpgsqlCommand(createFunctionSQL, conn)
        ignore (createFunctionCmd.ExecuteNonQuery())
 
        // Call the newly created function to test it
        let testFunctionCmd = new NpgsqlCommand("SELECT simple_duplicate_function(@input);", conn)
        testFunctionCmd.Parameters.AddWithValue("@input", inputtext)
        let result = testFunctionCmd.ExecuteScalar()
        Elog.Info("Returned value from plcsharp function: " + result.ToString())
 
        // Return the result as a string
        match result with
        | :? string as stringValue -> stringValue
        | _ -> ""
    with
    | _ ->
        ""
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-text-spi', 'SPITestCreateFunctionFSharp', SPITestCreateFunctionFSharp('Brazil') = 'BrazilBrazil';
 
CREATE OR REPLACE FUNCTION SPITestCreateDropIndexFSharp() RETURNS text AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    try
        // Drop the test table if it exists, then create it
        let dropCreateTableCmd = new NpgsqlCommand("DROP TABLE IF EXISTS test_index_table; CREATE TABLE test_index_table (id INTEGER, name TEXT);", conn)
        ignore (dropCreateTableCmd.ExecuteNonQuery())
 
        // Create an index on the test table
        let createIndexCmd = new NpgsqlCommand("CREATE INDEX idx_test ON test_index_table (id);", conn)
        ignore (createIndexCmd.ExecuteNonQuery())
 
        // Check if the index was created
        let checkIndexCreatedCmd = new NpgsqlCommand("SELECT COUNT(*) FROM pg_indexes WHERE indexname = 'idx_test';", conn)
        let indexCreatedCount = checkIndexCreatedCmd.ExecuteScalar()
 
        if (indexCreatedCount :?> int64) = 0L then
            raise (new Exception("Index creation failed"))
 
        // Drop the index
        let dropIndexCmd = new NpgsqlCommand("DROP INDEX idx_test;", conn)
        ignore (dropIndexCmd.ExecuteNonQuery())
 
        // Check if the index was removed
        let checkIndexDroppedCmd = new NpgsqlCommand("SELECT COUNT(*) FROM pg_indexes WHERE indexname = 'idx_test';", conn)
        let indexDroppedCount = checkIndexDroppedCmd.ExecuteScalar()
 
        if (indexDroppedCount :?> int64) > 0L then
            raise (new Exception("Index drop failed"))
 
        // Return success message
        "Index creation and drop test completed successfully"
    with
    | ex ->
        // Return the error message in case of an exception or error
        ex.Message
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-text-spi', 'SPITestCreateDropIndexFSharp', SPITestCreateDropIndexFSharp() = 'Index creation and drop test completed successfully';
 
CREATE OR REPLACE FUNCTION SPITestCreateDropViewFSharp() RETURNS text AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    try
        // Create a test view
        let createViewCmd = new NpgsqlCommand("CREATE OR REPLACE VIEW test_view AS SELECT 1 AS number;", conn)
        ignore (createViewCmd.ExecuteNonQuery())
 
        // Check if the view was created
        let checkViewCreatedCmd = new NpgsqlCommand("SELECT COUNT(*) FROM pg_views WHERE viewname = 'test_view';", conn)
        let viewCreatedCount = checkViewCreatedCmd.ExecuteScalar()
 
        if (viewCreatedCount :?> int64) = 0L then
            raise (new Exception("View creation failed"))
 
        // Drop the view
        let dropViewCmd = new NpgsqlCommand("DROP VIEW test_view;", conn)
        ignore (dropViewCmd.ExecuteNonQuery())
 
        // Check if the view was removed
        let checkViewDroppedCmd = new NpgsqlCommand("SELECT COUNT(*) FROM pg_views WHERE viewname = 'test_view';", conn)
        let viewDroppedCount = checkViewDroppedCmd.ExecuteScalar()
 
        if (viewDroppedCount :?> int64) > 0L then
            raise (new Exception("View drop failed"))
 
        // Return success message
        "View creation and drop test completed successfully"
    with
    | ex ->
        // Return the error message in case of an exception or error
        ex.Message
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-text-spi', 'SPITestCreateDropViewFSharp', SPITestCreateDropViewFSharp() = 'View creation and drop test completed successfully';
 
CREATE OR REPLACE FUNCTION SPIUseCSharpSPIFSharp(input TEXT) RETURNS text AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    try
        // Drop/Create a table
        let setupTableCmd = new NpgsqlCommand(
            "DROP TABLE IF EXISTS test_spi_table; " +
            "CREATE TABLE test_spi_table (value TEXT);", conn)
        ignore (setupTableCmd.ExecuteNonQuery())
 
        // Create a PLCSharp procedure
        let createProcCmdText =
            "CREATE OR REPLACE PROCEDURE insert_into_table(table_name TEXT, value TEXT) " +
            "LANGUAGE plcsharp AS $BODY$ " +
            "NpgsqlCommand command; " +
            "command = new NpgsqlCommand(\"INSERT INTO \" + table_name + " +
            " \" (value) VALUES ('\" + value + \"')\", null); " +
            "command.ExecuteNonQuery(); $BODY$;"
        let createProcCmd = new NpgsqlCommand(createProcCmdText, conn)
        ignore (createProcCmd.ExecuteNonQuery())
 
        // Call the PLCSharp procedure
        let callProcCmd = new NpgsqlCommand(
            "CALL insert_into_table('test_spi_table', '" + input.ToString() + "');", conn)
        ignore (callProcCmd.ExecuteNonQuery())
 
        // Check if the value was added
        let checkValueCmd = new NpgsqlCommand(
            "SELECT * FROM test_spi_table;", conn)
        let readValue = checkValueCmd.ExecuteScalar()
 
        // Drop the procedure
        let dropProcCmd = new NpgsqlCommand(
            "DROP PROCEDURE insert_into_table;", conn)
        ignore (dropProcCmd.ExecuteNonQuery())
 
        // Check if the procedure was removed
        let checkProcDroppedCmd = new NpgsqlCommand(
            "SELECT COUNT(*) FROM pg_proc WHERE proname = 'insert_into_table';", conn)
        let procDroppedCount = checkProcDroppedCmd.ExecuteScalar()
 
        if (procDroppedCount :?> int64) > 0L then
            raise (new Exception("Procedure drop failed"))
 
        match readValue with
        | :? string as stringValue -> stringValue
        | _ -> ""
    with
    | ex ->
        // Return the error message in case of an exception or error
        ex.Message
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-text-spi', 'SPIUseCSharpSPIFSharp', SPIUseCSharpSPIFSharp('use f# spi to call c# 🤘') = 'use f# spi to call c# 🤘';
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-text-spi', 'SPIUseCSharpSPIFSharp', SPIUseCSharpSPIFSharp('💻 🖥️ 🖨️ 🖱️ 💽 💾') = '💻 🖥️ 🖨️ 🖱️ 💽 💾';
 
CREATE OR REPLACE PROCEDURE SPITransactionTestRollbackFirstFSharp() AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    let batch = conn.CreateBatch()
    batch.BatchCommands.Add(new NpgsqlBatchCommand("DROP TABLE IF EXISTS TRANSACTION_TEST_FSHARP;"))
    batch.BatchCommands.Add(new NpgsqlBatchCommand("CREATE TABLE IF NOT EXISTS TRANSACTION_TEST_FSHARP (a INTEGER, b text);"))
    ignore (batch.ExecuteReader())
    let transaction = conn.BeginTransaction()
 
    for i = 0 to 9 do
        let commandText = "INSERT INTO TRANSACTION_TEST_FSHARP (a) VALUES (" + i.ToString() + ")"
        let command = new NpgsqlCommand(commandText, conn)
        use reader = command.ExecuteReader()
        if i % 2 <> 0 then
            transaction.Commit()
        else
            transaction.Rollback()
 
    let readCommand = new NpgsqlCommand("SELECT * FROM TRANSACTION_TEST_FSHARP", conn)
    use reader = readCommand.ExecuteReader()
    let mutable counter = 0
    while reader.Read() do
        let value = reader.GetInt32(0)
        Elog.Info("Row[" + counter.ToString() + "] = " + value.ToString())
        counter <- counter + 1
$$ LANGUAGE plfsharp;
CALL SPITransactionTestRollbackFirstFSharp();
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-spi-transaction', 'SPITransactionTestRollbackFirstFSharp', CASE WHEN SUM(a) = 25 THEN TRUE ELSE FALSE END AS RESULT FROM TRANSACTION_TEST_FSHARP;
 
CREATE OR REPLACE FUNCTION SPIBatchCompoundParametersFSharp(init INTEGER, inc INTEGER) RETURNS INTEGER AS $$
    let conn = new NpgsqlConnection()
    conn.Open() |> ignore
    use _ = conn
    let batch = conn.CreateBatch()
    batch.BatchCommands.Add(new NpgsqlBatchCommand("DROP TABLE IF EXISTS SPI_COMPOUND_TESTS;"))
    batch.BatchCommands.Add(new NpgsqlBatchCommand("CREATE TABLE IF NOT EXISTS SPI_COMPOUND_TESTS (ID INTEGER);"))
 
    let mutable currentInit = init.Value
    for i = 1 to 5 do
        batch.BatchCommands.Add(new NpgsqlBatchCommand("INSERT INTO SPI_COMPOUND_TESTS (ID) VALUES (" + currentInit.ToString() + ");"))
        currentInit <- currentInit + inc.Value
 
    batch.BatchCommands.Add(new NpgsqlBatchCommand("UPDATE SPI_COMPOUND_TESTS SET ID = 2 * ID;"))
    batch.BatchCommands.Add(new NpgsqlBatchCommand("SELECT * FROM SPI_COMPOUND_TESTS;"))
    use reader = batch.ExecuteReader()
 
    // Skip to the last result set which contains the SELECT query results
    for i = 1 to 8 do
        reader.NextResult() |> ignore
 
    let mutable sum = 0
    while reader.Read() do
        let _a = reader.GetInt32(0)
        Elog.Info("Returned value = " + _a.ToString())
        sum <- sum + _a
 
    Elog.Info("Calling SPIBatchCompoundParametersFSharp with integer arguments.")
    Nullable(sum)
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int-spi-batch-query-compoud-parameters', 'SPIBatchCompoundParametersFSharp1', SPIBatchCompoundParametersFSharp(1, 1) = 30;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int-spi-batch-query-compoud-parameters', 'SPIBatchCompoundParametersFSharp2', SPIBatchCompoundParametersFSharp(5, 2) = 90;
 
CREATE OR REPLACE FUNCTION SPIIntegerArrayWithNullFSharp() RETURNS INTEGER AS $$
    let conn = new NpgsqlConnection() // Ensure proper connection string is provided
    use _ = conn
    conn.Open()
    let cmd = new NpgsqlCommand("SELECT @p1", conn)
    let p1 = new NpgsqlParameter("p1", NpgsqlDbType.Array ||| NpgsqlDbType.Integer)
    cmd.Parameters.Add(p1)
 
    let arr = Array.CreateInstance(typeof<Nullable<int>>, 5)
    arr.SetValue((int)1, 0)
    arr.SetValue(None, 1)
    arr.SetValue((int)3, 2)
    arr.SetValue(None, 3)
    arr.SetValue((int)5, 4)
 
    p1.Value <- arr
 
    use reader = cmd.ExecuteReader()
    reader.Read()
 
    let array = reader.GetFieldValue<Nullable<int>[]>(0)
 
    let nullCount = array |> Array.fold (fun acc x -> acc + (if x.HasValue then 0 else 1)) 0
    Nullable(nullCount)
$$ LANGUAGE plfsharp;
INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
SELECT 'f#-int-spi-array', 'SPIIntegerArrayWithNullFSharp', SPIIntegerArrayWithNullFSharp() = 2;