Guest User

Untitled

a guest
Oct 22nd, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using EsotericSQL;
  4.  
  5. namespace ExambleLibrary
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //Run your unit tests here.
  12.             //For integration tests, create a .state file.
  13.             //For example: ./automata/test/testmylib.state
  14.             //Write any tests you want to perform there.
  15.             //Next run it through the console.
  16.             //For example: push test testmylib auto
  17.             List<string> QueryOut;
  18.             List<double> QueryOutReal;
  19.  
  20.             Console.WriteLine("Opening connection");
  21.             SQLTools.OpenFile("test.db", "test");
  22.             Console.WriteLine("CREATE");
  23.             SQLTools.RunNonQuery("test", @"CREATE TABLE IF NOT EXISTS example (
  24.                    id INTEGER PRIMARY KEY AUTOINCREMENT,
  25.                    decision TEXT,
  26.                    value BIGINT
  27.                )");
  28.             Console.WriteLine("INSERT");
  29.             SQLTools.RunNonQuery("test", @"INSERT INTO example (decision, value) VALUES
  30.                    ('A', 1),
  31.                    ('A', 3),
  32.                    ('A', 4),
  33.                    ('B', 2),
  34.                    ('B', 3),
  35.                    ('C', 3)
  36.                ");
  37.             Console.WriteLine("SELECT test 1:");
  38.             QueryOut = SQLTools.RunFullQuery("test", @"SELECT * FROM example");
  39.             foreach (string s in QueryOut) Console.WriteLine(s);
  40.             Console.WriteLine("SELECT test 2:");
  41.             QueryOut = SQLTools.RunFullQuery("test", @"SELECT decision, AVG(value)
  42.                    FROM example
  43.                    GROUP BY decision
  44.                    ORDER BY AVG(value) DESC
  45.                ");
  46.             foreach (string s in QueryOut) Console.WriteLine(s);
  47.             Console.WriteLine("SELECT test 3:");
  48.             QueryOutReal = SQLTools.RunRealQuery("test", @"SELECT AVG(value)
  49.                    FROM example
  50.                    GROUP BY decision
  51.                    ORDER BY AVG(value) DESC
  52.                ");
  53.             foreach (double d in QueryOutReal) Console.WriteLine(d);
  54.             Console.WriteLine("Closing connection");
  55.             SQLTools.CloseFile("test");
  56.             Console.WriteLine("Removing test file");
  57.             SQLTools.DeleteFile("test.db");
  58.             Console.ReadKey();
  59.         }
  60.     }
  61.  
  62.     public class EsotericModule
  63.     {
  64.         //Delegate types used for binding code from the engine.
  65.         //Don't change these.
  66.         public delegate bool GetLogicDelegate(ulong CallID);
  67.         public delegate long GetIntegerDelegate(ulong CallID);
  68.         public delegate double GetRealDelegate(ulong CallID);
  69.         public delegate string GetStringDelegate(ulong CallID);
  70.         public delegate List<bool> GetLogicArrayDelegate(ulong CallID);
  71.         public delegate List<long> GetIntegerArrayDelegate(ulong CallID);
  72.         public delegate List<double> GetRealArrayDelegate(ulong CallID);
  73.         public delegate List<string> GetStringArrayDelegate(ulong CallID);
  74.         public delegate bool CallLogicDelegate(ulong CallID, string Code);
  75.         public delegate long CallIntegerDelegate(ulong CallID, string Code);
  76.         public delegate double CallRealDelegate(ulong CallID, string Code);
  77.         public delegate string CallStringDelegate(ulong CallID, string Code);
  78.         public delegate List<bool> CallLogicArrayDelegate(ulong CallID, string Code);
  79.         public delegate List<long> CallIntegerArrayDelegate(ulong CallID, string Code);
  80.         public delegate List<double> CallRealArrayDelegate(ulong CallID, string Code);
  81.         public delegate List<string> CallStringArrayDelegate(ulong CallID, string Code);
  82.         public delegate void CallVoidDelegate(ulong CallID, string Code);
  83.  
  84.         //This function defines the name of your library.
  85.         //This name will be used to call the library from the engine.
  86.         //For example: lib "esql" "open" "test.db" "test"
  87.         //             lib "esql" "close" "test"
  88.         //             etc.
  89.         public string GetName(ulong CallID) => "esql";
  90.  
  91.         //These functions are called when the engine tries to access the library.
  92.         //The function depends on the return type requested.
  93.         public bool GetLogic(ulong CallID)
  94.         {
  95.             switch (TakeStringParam(CallID))
  96.             {
  97.                 case "run": return SQLTools.RunLogicQuery(TakeStringParam(CallID), TakeStringParam(CallID))[0];
  98.                 default: throw new NotImplementedException();
  99.             }
  100.         }
  101.         public long GetInteger(ulong CallID)
  102.         {
  103.             switch (TakeStringParam(CallID))
  104.             {
  105.                 case "run": return SQLTools.RunIntegerQuery(TakeStringParam(CallID), TakeStringParam(CallID))[0];
  106.                 default: throw new NotImplementedException();
  107.             }
  108.         }
  109.         public double GetReal(ulong CallID)
  110.         {
  111.             switch (TakeStringParam(CallID))
  112.             {
  113.                 case "run": return SQLTools.RunRealQuery(TakeStringParam(CallID), TakeStringParam(CallID))[0];
  114.                 default: throw new NotImplementedException();
  115.             }
  116.         }
  117.         public string GetString(ulong CallID)
  118.         {
  119.             switch (TakeStringParam(CallID))
  120.             {
  121.                 case "run": return SQLTools.RunFullQuery(TakeStringParam(CallID), TakeStringParam(CallID))[0];
  122.                 default: throw new NotImplementedException();
  123.             }
  124.         }
  125.         public List<bool> GetLogicArray(ulong CallID)
  126.         {
  127.             switch (TakeStringParam(CallID))
  128.             {
  129.                 case "run": return SQLTools.RunLogicQuery(TakeStringParam(CallID), TakeStringParam(CallID));
  130.                 default: throw new NotImplementedException();
  131.             }
  132.         }
  133.         public List<long> GetIntegerArray(ulong CallID)
  134.         {
  135.             switch (TakeStringParam(CallID))
  136.             {
  137.                 case "run": return SQLTools.RunIntegerQuery(TakeStringParam(CallID), TakeStringParam(CallID));
  138.                 default: throw new NotImplementedException();
  139.             }
  140.         }
  141.         public List<double> GetRealArray(ulong CallID)
  142.         {
  143.             switch (TakeStringParam(CallID))
  144.             {
  145.                 case "run": return SQLTools.RunRealQuery(TakeStringParam(CallID), TakeStringParam(CallID));
  146.                 default: throw new NotImplementedException();
  147.             }
  148.         }
  149.         public List<string> GetStringArray(ulong CallID)
  150.         {
  151.             switch (TakeStringParam(CallID))
  152.             {
  153.                 case "run": return SQLTools.RunFullQuery(TakeStringParam(CallID), TakeStringParam(CallID));
  154.                 default: throw new NotImplementedException();
  155.             }
  156.         }
  157.         //This function represents void calls.
  158.         //Note the use of Take*Param(CallID) calls to obtain parameters.
  159.         public void GetVoid(ulong CallID)
  160.         {
  161.             switch (TakeStringParam(CallID))
  162.             {
  163.                 case "open":
  164.                     SQLTools.OpenFile(TakeStringParam(CallID), TakeStringParam(CallID));
  165.                     break;
  166.                 case "close":
  167.                     SQLTools.CloseFile(TakeStringParam(CallID));
  168.                     break;
  169.                 case "delete":
  170.                     SQLTools.DeleteFile(TakeStringParam(CallID));
  171.                     break;
  172.                 case "run":
  173.                     SQLTools.RunNonQuery(TakeStringParam(CallID), TakeStringParam(CallID));
  174.                     break;
  175.                 default: throw new NotImplementedException();
  176.             }
  177.         }
  178.  
  179.         //Use these properties to get parameters for your library call.
  180.         //Select the property based on the return value you want.
  181.         //Pass your CallID token into the first parameter.
  182.         public GetLogicDelegate TakeLogicParam { get; set; }
  183.         public GetIntegerDelegate TakeIntegerParam { get; set; }
  184.         public GetRealDelegate TakeRealParam { get; set; }
  185.         public GetStringDelegate TakeStringParam { get; set; }
  186.         public GetLogicArrayDelegate TakeLogicArrayParam { get; set; }
  187.         public GetIntegerArrayDelegate TakeIntegerArrayParam { get; set; }
  188.         public GetRealArrayDelegate TakeRealArrayParam { get; set; }
  189.         public GetStringArrayDelegate TakeStringArrayParam { get; set; }
  190.  
  191.         //Use these properties to call code inside of the engine.
  192.         //Select the property based on the return value you want.
  193.         //Pass your CallID token into the first parameter and a valid script statement into the second.
  194.         public CallLogicDelegate CallLogicCode { get; set; }
  195.         public CallIntegerDelegate CallIntegerCode { get; set; }
  196.         public CallRealDelegate CallRealCode { get; set; }
  197.         public CallStringDelegate CallStringCode { get; set; }
  198.         public CallLogicArrayDelegate CallLogicArrayCode { get; set; }
  199.         public CallIntegerArrayDelegate CallIntegerArrayCode { get; set; }
  200.         public CallRealArrayDelegate CallRealArrayCode { get; set; }
  201.         public CallStringArrayDelegate CallStringArrayCode { get; set; }
  202.         public CallVoidDelegate CallVoidCode { get; set; }
  203.     }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment