Skip to content

Commit

Permalink
building cli for pgxcrown project
Browse files Browse the repository at this point in the history
  • Loading branch information
luisacosta828 committed Apr 28, 2021
1 parent 3d4cd17 commit e2167eb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 52 deletions.
7 changes: 5 additions & 2 deletions pgxcrown.nimble
@@ -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 added src/bin/cli/pgxcrown
Binary file not shown.
50 changes: 0 additions & 50 deletions src/build_extension.nim

This file was deleted.

62 changes: 62 additions & 0 deletions src/cli/build_extension.nim
@@ -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.}
11 changes: 11 additions & 0 deletions src/cli/pgxcrown.nim
@@ -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"

0 comments on commit e2167eb

Please sign in to comment.