{"id":23081626,"url":"https://github.com/ceyhunkerti/pl","last_synced_at":"2025-04-03T14:16:19.259Z","repository":{"id":92200066,"uuid":"96024751","full_name":"ceyhunkerti/pl","owner":"ceyhunkerti","description":"PL/SQL Commons","archived":false,"fork":false,"pushed_at":"2021-01-19T16:39:41.000Z","size":131,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T03:12:25.238Z","etag":null,"topics":["commons","logging","plsql"],"latest_commit_sha":null,"homepage":null,"language":"PLSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ceyhunkerti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-02T13:22:40.000Z","updated_at":"2023-10-30T14:43:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"f88063c1-dc89-4e72-8376-dcddf443a541","html_url":"https://github.com/ceyhunkerti/pl","commit_stats":null,"previous_names":["ceyhunkerti/pl"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceyhunkerti%2Fpl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceyhunkerti%2Fpl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceyhunkerti%2Fpl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceyhunkerti%2Fpl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ceyhunkerti","download_url":"https://codeload.github.com/ceyhunkerti/pl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247014520,"owners_count":20869376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commons","logging","plsql"],"created_at":"2024-12-16T13:54:24.081Z","updated_at":"2025-04-03T14:16:19.239Z","avatar_url":"https://github.com/ceyhunkerti.png","language":"PLSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\"\u003e\u003cimg alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by/4.0/88x31.png\" /\u003e\u003c/a\u003e\u003cbr /\u003eThis work is licensed under a \u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\"\u003eCreative Commons Attribution 4.0 International License\u003c/a\u003e.\n\n## PL/SQL Commons\n\n  Contains common utility and logging methods.\n  A simple proc that uses `pl` looks like the following;\n\n```sql\n  -- PL in action !\n  --\n  PROCEDURE PRC_PROC_NAME(\u003ci_input_var vartype\u003e, \u003co_output_var vartype\u003e ) IS\n  BEGIN\n    gv_proc := 'PRC_PROC_NAME'; -- name of the procedure\n    -- gv_pkg -- constant name of the package set globally once\n\n    -- initialize logger here\n    pl.logger := util.logtype.init(gv_pkg ||'.'||gv_proc);\n\n    --------------------\n    -- proc body here --\n    --------------------\n    -- gv_sql : global variable\n    --\n    gv_sql := '\n      -- sql statement to execute\n    ';\n    execute immediate gv_sql;\n\n    -- success message\n    pl.logger.success(SQL%ROWCOUNT,gv_sql);\n\n    -- !!! commit should be after success message\n    commit;\n\n  EXCEPTION\n    WHEN OTHERS THEN\n      -- error message\n      pl.logger.error(SQLCODE || ' : ' ||SQLERRM);\n      raise;\n  END;\n```\n\nYou can see the recent logs in `logs` table\n```sql\nselect * from util.logs order by 3 desc;\n```\n\n### INSTALLATION\n\n  You can put the objects under any schema you like, but you can create a utility\n  schema ,if you do not have already, and put all the objects under that schema.\n\n\n  * Create a schema named **util** with:\n    ```sql\n      create user util identified by \u003cpassword\u003e;\n    ```\n\n  * Grant privileges\n\n    ```sql\n      GRANT CONNECT, RESOURCE TO UTIL;\n\n      GRANT SELECT ON sys.dba_constraints TO util;\n\n      GRANT SELECT ON sys.dba_indexes TO util;\n\n      GRANT SELECT ON sys.dba_objects TO util;\n\n      GRANT SELECT ON sys.v_$lock TO util;\n\n      GRANT SELECT ON sys.v_$session TO util;\n\n      GRANT SELECT ON sys.v_$locked_object to util;\n\n      GRANT execute on sys.UTL_MAIL to util;\n    ```\n\n  * Change the current schema to **util**\n\n    ```sql\n      alter session set current_schema = util;\n    ```\n\n  * Run the contents of [init.ddl.sql](src/init.ddl.sql)\n\n  * Run the contents of [logtype.pks.sql](src/logtype/logtype.pks.sql) and [logtype.pkb.sql](src/logtype/logtype.pkb.sql) in order.\n\n  * Run the contents of [pl.pks.sql](src/pl/pl.pks.sql) and [pl.pkb.sql](src/pl/pl.pkb.sql) in order.\n\n  * Optionally create a public synonym for pl with;\n\n    ```sql\n      create public synonym pl for util.pl;\n\n      grant execute on util.logtype to public;\n\n      grant execute on util.pl to public;\n    ```\n\n\n### API\n\n  * **exec**\n  ```sql\n  -- execute given statement, raise exception if i_silent is set to false\n  --\n  -- Args:\n  --    [i_sql varchar2]: statement to execute\n  --    [i_silent boolean = False]: raise exception if i_silent is set to false, defaults to `False`\n  procedure exec(i_sql varchar2, i_silent boolean default false);\n  ```\n\n  * **exec_silent**\n  ```sql\n  -- execute given statement and ignore error\n  --\n  -- Args:\n  --    [i_sql varchar2]: statement to execute\n  procedure exec_silent(i_sql varchar2);\n  ```\n\n  * **parse_date**\n    ```sql\n    -- parse given string to date\n    --\n    -- Args:\n    --    [i_str number]: date in string\n    function parse_date (i_str varchar2) return date\n    ```\n\n  * **sleep**\n    ```sql\n    -- Sleep given number of milliseconds. !DOES NOT uses dbms_lock\n    --\n    -- Args:\n    --    [i_millis number]: milliseconds\n    -- Returns:\n    --    date: Returns date value of the given string\n    procedure sleep(i_millis in number)\n    ```\n\n\n  * **is_number**\n    ```sql\n    -- Checks if string is classified as a Number or not.\n    --\n    -- Args:\n    --    [i_str varchar2 = '']: The string to check.\n    -- Returns\n    --    boolean: Returns true if string is numeric.\n    function is_number(i_str varchar2) return boolean\n    ```\n\n  * **split**\n    ```sql\n    -- Splits string by separator.\n    --\n    -- Args:\n    --    [i_str varchar2 = '']: The string to split.\n    --    [i_split varchar2 = ',']: The separator pattern to split by.\n    --    [i_limit number = null]: The length to truncate results to.\n    -- Returns:\n    --    varchar2_table: Returns the string segments.\n    function split(\n      i_str varchar2,\n      i_split varchar2 default ',',\n      i_limit number default null\n    ) return dbms_sql.varchar2_table\n    ```\n\n  * **date_string**\n    ```sql\n    -- Returns a date as string containing to_date function.\n    -- Useful when used with 'execute immediate'\n    --\n    -- Args:\n    --    [i_date date]: The date object to convert to string to_char representation.\n    -- Returns:\n    --   varchar2: the date function string\n    --   example return value: `'to_date(''20120101 22:12:00'',''yyyymmdd hh24:mi:ss'')'`\n    function date_string(i_date date) return varchar2\n    ```\n\n  * **truncate_table**\n    ```sql\n    -- Truncates the given table\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    procedure truncate_table(i_owner varchar2, i_table varchar2)\n    ```\n\n  * **truncate_table**\n    ```sql\n    -- Truncates the given table\n    --\n    -- Args:\n    --    [i_table varchar2]: Schema and Name of the table eg. `owner.table_name`\n    procedure truncate_table(i_table varchar2)\n    ```\n\n  * **drop_table**\n    ```sql\n    -- Drops the given table\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [pib_ignore_err boolean = true]: when set to false raises error\n    procedure drop_table(i_owner varchar2, i_table varchar2, pib_ignore_err boolean default true);\n    ```\n\n  * **drop_table**\n    ```sql\n    -- Drops the given table\n    --\n    -- Args:\n    --    [i_table varchar2]: Schema and Name of the table eg: `owner.table_name`\n    procedure drop_table(i_table varchar2);\n    ```\n\n  * **table_exists**\n    ```sql\n    -- Checks whether the given table exists or not\n    --\n    -- Args:\n    --    [i_owner varchar2]\n    --    [i_table varchar2]: Name of the table\n    -- Returns:\n    --    boolean: True if table exists\n    function table_exists(i_owner varchar2, i_table varchar2) return boolean;\n    ```\n\n  * **gather_table_stats**\n    ```sql\n    -- Gather table/partition statistics\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_part_name varchar2 = null]: Name of the partition defaults to `null`\n    procedure gather_table_stats(\n      i_owner varchar2,\n      i_table varchar2,\n      i_part_name varchar2 default null)\n    ```\n\n  * **manage_constraints**\n    ```sql\n    -- Enable/Disable constraints for the given table.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_order varchar2 = 'enable']: DISABLE|ENABLE\n    procedure manage_constraints(\n      i_owner varchar2,\n      i_table varchar2,\n      i_order varchar2 default 'enable')\n    ```\n\n  * **enable_constraints**\n    ```sql\n    -- Enable constraints for the given table.s\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    procedure enable_constraints(i_owner varchar2, i_table varchar2)\n    ```\n\n  * **disable_constraints**\n    ```sql\n    -- Disable constraints for the given table.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    procedure disable_constraints(i_owner varchar2, i_table varchar2)\n    ```\n\n  * **manage_indexes**\n    ```sql\n    -- Unusable/Rebuild indexes for the given table.\n    --\n    -- Args:\n    --  [i_owner varchar2]: Schema of the table\n    --  [i_table varchar2]: Name of the table\n    --  [i_order varchar2 = 'enable']: DISABLE|ENABLE\n    --    DISABLE makes the indexes unusable\n    --    ENABLE rebuilds the indexes\n    procedure manage_indexes(\n      i_owner varchar2,\n      i_table varchar2,\n      i_order varchar2 default 'enable')\n    ```\n\n  * **enable_indexes**\n    ```sql\n    -- Rebuild indexes for the given table.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n\n    procedure enable_indexes(i_owner varchar2, i_table varchar2)\n    ```\n\n  * **disable_indexes**\n    ```sql\n    -- Make indexes unusable for the given table.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    procedure disable_indexes(i_owner varchar2, i_table varchar2)\n    ```\n\n  * **add_partitions**\n    ```sql\n    -- Adds partitions to the given table up to the date given by the `i_date` parameter.\n    --\n    -- Args:\n    --    [i_owner varchar2: Schema of the table\n    --    [i_table varchar2: Name of the table\n    --    [i_date date]: the date up to partitions will be added\n    procedure add_partitions(i_owner varchar2, i_table varchar2, i_date date)\n    ```\n\n  * **add_partition**\n    ```sql\n    -- Adds a single partition to the given table with the date given by the 'i_date' parameter.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_date date]: the date partition will be created for\n    procedure add_partition (i_owner varchar2, i_table varchar2,i_date date)\n    ```\n\n  * **truncate_partition**\n    ```sql\n    -- Truncates the given partition.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_partition varchar2]: name of the partition\n    procedure truncate_partition(i_owner varchar2, i_table varchar2, i_partition varchar2)\n    ```\n\n  * **drop_partition**\n    ```sql\n    -- Drops the given partition.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_partition varchar2]: name of the partition\n    procedure drop_partition(i_owner varchar2, i_table varchar2, i_partition varchar2)\n    ```\n\n  * **drop_partition_lt**\n    ```sql\n    -- Drops partitions less than the given date.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_date varchar2]: date boundary\n    procedure drop_partition_lt (i_owner varchar2, i_table varchar2, i_date date);\n    ```\n\n  * **drop_partition_lte**\n    ```sql\n    -- Drops partitions less than or equal to the given date.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_date varchar2]: date boundary\n    procedure drop_partition_lte(i_owner varchar2, i_table varchar2, i_date date)\n    ```\n\n  * **drop_partition_gt**\n\n    ```sql\n    -- Drops partitions greater than the given date.\n    --\n    -- Args:\n    --    [i_owner varchar2): Schema of the table\n    --    [i_table varchar2): Name of the table\n    --    [i_date varchar2): date boundary\n    procedure drop_partition_gt (i_owner varchar2, i_table varchar2, i_date date)\n    ```\n\n  * **drop_partition_gte**\n    ```sql\n    -- Drops partitions greater than or equal to the given date.\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_date varchar2]: date boundary\n    procedure drop_partition_gte (i_owner varchar2, i_table varchar2, i_date date)\n    ```\n\n  * **window_partitions**\n    ```sql\n    -- Manages partitions for the given table by fitting the partitions to the given date with i_date parameter\n    -- and given number by i_number_size parameter. Basically it adds partitions until i_date and drops partitions\n    -- older than i_window_size * (year|month|day)\n    --\n    -- Args:\n    --   [i_owner varchar2]: Schema of the table\n    --   [i_table varchar2]: Name of the table\n    --   [i_date varchar2]: date boundary\n    --   [i_window_size number]: number of partitions to keep\n    procedure window_partitions(\n      i_owner varchar2,\n      i_table varchar2,\n      i_date date,\n      i_window_size number)\n    ```\n\n  * **exchange_partition**\n    ```sql\n    -- Exchanges partition of table_1 with the table_2\n    --\n    -- Args:\n    --    [i_owner varchar2]: Schema of the table\n    --    [i_table varchar2]: Name of the table\n    --    [i_part_name varchar2]: partitions to be exchanged\n    --    [i_table_2 varchar2]: table to replace partition\n    --    [pib_validate boolean =false]: validate partition after exchange\n    procedure exchange_partition(\n      i_owner     varchar2,\n      i_table_1   varchar2,\n      i_part_name varchar2,\n      i_table_2   varchar2,\n      pib_validate  boolean default false\n    );\n    ```\n\n  * **enable_parallel_dml**\n    ```sql\n    -- Enable parallel dml for the current session.\n    procedure enable_parallel_dml\n    ```\n\n  * **disable_parallel_dml**\n\n    ```sql\n    -- Disable parallel dml for the current session.\n    procedure disable_parallel_dml\n    ```\n\n\n  * **async_exec**\n    ```sql\n      -- Execute given statement asynchronously.\n      --\n      -- Args:\n      --    [i_sql varchar2]: Statement to execute\n      --    [i_name varchar2 = 'ASYNC_EXEC']: Name of the dbms job entry\n\n      procedure async_exec(i_sql varchar2, i_name varchar2 default 'ASYNC_EXEC')\n    ```\n\n  * **set_param**\n    ```sql\n      -- Set parameter on `params` table\n      --\n      -- Args:\n      --    [i_name varchar2]: parameter name\n      --    [i_value varchar2]: parameter value\n      procedure set_param(i_name varchar2, i_value)\n    ```\n\n  * **find_param**\n    ```sql\n      -- Find given parameter\n      --\n      -- Args:\n      --    [i_name varchar2]: parameter name\n      -- Returns\n      --    varchar2: Returns parameter value\n      procedure find_param(i_name varchar2)\n    ```\n\n  * **param_exists**\n    ```sql\n      -- Check whether given parameter exists.\n      -- Args:\n      --    [i_name] (varchar2): parameter name\n      --    boolean: true if param exists false otherwise\n      function param_exists(i_name varchar2) return boolean;\n    ```\n\n  * **send_mail**\n    ```sql\n      -- Send mail to given recipients. Set mail server settings on `params` before\n      -- using this method!\n      -- Args:\n      --  ** Mail options\n      procedure send_mail(\n        i_to      varchar2,\n        i_subject varchar2,\n        i_body    varchar2,\n        i_cc      varchar2  default null\n        i_from    varchar2  default null\n      )\n    ```\n\n  * **is_email**\n    ```sql\n      -- Test given string is a valid email address\n      --\n      -- Args:\n      --   [i_email varchar2]: given email address\n      -- Returns:\n      --   boolean: true if input is a valid email address\n      function is_email(i_email varchar2)\n    ```\n\n  * **ddl**\n    ```sql\n      -- Retrieve metadata of the object(s). If only name is given returns all matching objects'' metadata\n      --\n      -- Args:\n      --    [i_name varchar2]: name of the object\n      --    [i_schema varchar2]: owner of the object\n      --    [i_dblk varchar2]: db-link for remote objects\n      --    [i_type varchar2 ='TABLE']: object type\n      -- Returns\n      --    boolean: true if param exists false otherwise\n      function ddl(\n        i_name varchar2,\n        i_schema varchar2 default null,\n        i_dblk varchar2 default null,\n        i_type varchar2 default 'TABLE'\n      ) return clob;\n    ```\n\n  * **print_locks**\n    ```sql\n    -- Print locked objects.\n    procedure print_locks\n    ```\n\n\n  * **println**\n    ```sql\n    -- Print to dbms out. Shortcut for dbms_output.put_line\n    --\n    -- Args:\n    --    [i_message varchar2]: Message to print\n    procedure println(i_message varchar2);\n    ```\n\n  * **printl**\n    ```sql\n    -- Print to dbms out. Shortcut for dbms_output.put_line\n    --\n    -- Args:\n    --    [i_message varchar2]: Message to print\n    procedure printl(i_message varchar2);\n    ```\n\n  * **p**\n    ```sql\n    -- Print to dbms out. Shortcut for dbms_output.put_line\n    --\n    -- Args:\n    --    [i_message varchar2]: Message to print\n    procedure p(i_message varchar2);\n    ```\n\n  * **print**\n    ```sql\n    -- Print to dbms out. Shortcut for dbms_output.put_line\n    --\n    -- Args:\n    --    [i_message] (varchar2): Message to print\n    procedure print(i_message varchar2);\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceyhunkerti%2Fpl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceyhunkerti%2Fpl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceyhunkerti%2Fpl/lists"}