Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d4cd17
commit e2167eb
Showing
5 changed files
with
78 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
# Package | ||
|
||
version = "0.2.9" | ||
version = "0.3.0" | ||
author = "luisacosta828" | ||
description = "Build Postgres extensions in Nim." | ||
srcDir = "src" | ||
license = "MIT" | ||
|
||
bin = @["build_extension"] | ||
bin = @["cli/pgxcrown"] | ||
binDir = "src/bin" | ||
|
||
#namedBin["build_extension"] = "pgxcrown" | ||
|
||
# Dependencies | ||
requires "nim >= 0.12.0" |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from strutils import parseInt, repeat, join, split | ||
from osproc import execCmd, execCmdEx | ||
import tables,os | ||
|
||
var nim_target_function* :string | ||
|
||
{.push inline .} | ||
|
||
proc compile_library(file:string) = discard execCmdEx "nim c -d:release --hints:off --opt:size --app:lib " & file & ".nim" | ||
proc getLibName(file:string):string = "lib"&extractFilename(file) | ||
proc getPostgresLibDir(): string = execCmdEx("pg_config --pkglibdir").output.split("\n")[0] | ||
|
||
proc moveTo( libname:string, postgreslib:string ) = discard execCmd "sudo mv "&libname&".so "&postgreslib | ||
|
||
proc extractV1Function( file:string ):string = | ||
execCmdEx("""grep -i pgv1 """ & file & """.nim | awk '{ print $2}' | tr "()" "\n" | head -n1""").output.split("\n")[0] | ||
|
||
proc createSQLFunction( file:string, nim_target_function: string): string = | ||
var create_function = "CREATE OR REPLACE FUNCTION "&nim_target_function&"_template" | ||
var types = {"Int16": "int", "Int32": "int", "Float4":"real", "Float8": "real", "Varchar": "varchar"}.toTable | ||
var get_type:tuple[output:string, exitCode:int] | ||
var return_type:tuple[output:string, exitCode:int] | ||
var total_args:int | ||
var param_builder:string = "(" | ||
var returns:string | ||
var args:seq[string] | ||
|
||
for key,value in types.pairs(): | ||
get_type = execCmdEx("grep -c get"&key&" "&file&".nim") | ||
return_type = execCmdEx("grep -c return"&key&" "&file&".nim") | ||
if get_type.exitCode == 0: | ||
total_args = parseInt(get_type.output.split("\n")[0]) - 1 | ||
for i in 0..total_args: | ||
args.add(value) | ||
if return_type.exitCode == 0: | ||
returns = " RETURNS "&value | ||
|
||
param_builder = param_builder & args.join(",") & ")" | ||
result = create_function & param_builder & returns & " as '"&getLibName(file)&".so' , '"&nim_target_function&"' LANGUAGE C STRICT;" | ||
|
||
|
||
proc build_pg_function*( file:string ):string = | ||
|
||
echo "Compiling: ",file, "..." | ||
compile_library(file) | ||
|
||
echo "Searching postgres libdir..." | ||
var postgreslib = getPostgresLibDir() | ||
|
||
var libname = getLibName(file) | ||
|
||
echo "Moving ",libname,".so to ", postgreslib | ||
|
||
libname.moveTo(postgreslib) | ||
|
||
nim_target_function = extractV1Function(file) | ||
|
||
echo "Creating SQL Function..." | ||
|
||
result = createSQLFunction(file,nim_target_function) | ||
|
||
{.pop.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import os, osproc | ||
|
||
import build_extension | ||
|
||
var build_opt = paramStr(1) | ||
|
||
if build_opt == "--build-extension": | ||
discard execCmd """ echo " """ & build_pg_function(paramStr(2)) & """ " > """ & nim_target_function & ".sql" | ||
echo "Created file: ",nim_target_function,".sql" | ||
else: | ||
echo "error" |