examples/lib use the new wrappers
This commit is contained in:
parent
ddb7185482
commit
b559285b78
133 changed files with 17038 additions and 151734 deletions
|
|
@ -23,7 +23,7 @@ proc dbError(db: TDbConn) {.noreturn.} =
|
|||
## raises an EDb exception.
|
||||
var e: ref EDb
|
||||
new(e)
|
||||
e.msg = $mysql_error(db)
|
||||
e.msg = $mysql.error(db)
|
||||
raise e
|
||||
|
||||
proc dbError*(msg: string) {.noreturn.} =
|
||||
|
|
@ -63,21 +63,21 @@ proc dbFormat(formatstr: TSqlQuery, args: openarray[string]): string =
|
|||
proc TryExec*(db: TDbConn, query: TSqlQuery, args: openarray[string]): bool =
|
||||
## tries to execute the query and returns true if successful, false otherwise.
|
||||
var q = dbFormat(query, args)
|
||||
return mysqlRealQuery(db, q, q.len) == 0'i32
|
||||
return mysql.RealQuery(db, q, q.len) == 0'i32
|
||||
|
||||
proc Exec*(db: TDbConn, query: TSqlQuery, args: openarray[string]) =
|
||||
## executes the query and raises EDB if not successful.
|
||||
var q = dbFormat(query, args)
|
||||
if mysqlRealQuery(db, q, q.len) != 0'i32: dbError(db)
|
||||
if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db)
|
||||
|
||||
proc newRow(L: int): TRow =
|
||||
newSeq(result, L)
|
||||
for i in 0..L-1: result[i] = ""
|
||||
|
||||
proc properFreeResult(sqlres: PMYSQL_RES, row: cstringArray) =
|
||||
proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
|
||||
if row != nil:
|
||||
while mysqlFetchRow(sqlres) != nil: nil
|
||||
mysqlFreeResult(sqlres)
|
||||
while mysql.FetchRow(sqlres) != nil: nil
|
||||
mysql.FreeResult(sqlres)
|
||||
|
||||
iterator FastRows*(db: TDbConn, query: TSqlQuery,
|
||||
args: openarray[string]): TRow =
|
||||
|
|
@ -85,13 +85,13 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
|
|||
## fast, but potenially dangerous: If the for-loop-body executes another
|
||||
## query, the results can be undefined. For Postgres it is safe though.
|
||||
Exec(db, query, args)
|
||||
var sqlres = mysqlUseResult(db)
|
||||
var sqlres = mysql.UseResult(db)
|
||||
if sqlres != nil:
|
||||
var L = int(mysql_num_fields(sqlres))
|
||||
var L = int(mysql.NumFields(sqlres))
|
||||
var result = newRow(L)
|
||||
var row: cstringArray
|
||||
while true:
|
||||
row = mysqlFetchRow(sqlres)
|
||||
row = mysql.FetchRow(sqlres)
|
||||
if row == nil: break
|
||||
for i in 0..L-1:
|
||||
setLen(result[i], 0)
|
||||
|
|
@ -104,19 +104,19 @@ proc GetAllRows*(db: TDbConn, query: TSqlQuery,
|
|||
## executes the query and returns the whole result dataset.
|
||||
result = @[]
|
||||
Exec(db, query, args)
|
||||
var sqlres = mysqlUseResult(db)
|
||||
var sqlres = mysql.UseResult(db)
|
||||
if sqlres != nil:
|
||||
var L = int(mysql_num_fields(sqlres))
|
||||
var L = int(mysql.NumFields(sqlres))
|
||||
var row: cstringArray
|
||||
var j = 0
|
||||
while true:
|
||||
row = mysqlFetchRow(sqlres)
|
||||
row = mysql.FetchRow(sqlres)
|
||||
if row == nil: break
|
||||
setLen(result, j+1)
|
||||
newSeq(result[j], L)
|
||||
for i in 0..L-1: result[j][i] = $row[i]
|
||||
inc(j)
|
||||
mysqlFreeResult(sqlres)
|
||||
mysql.FreeResult(sqlres)
|
||||
|
||||
iterator Rows*(db: TDbConn, query: TSqlQuery,
|
||||
args: openarray[string]): TRow =
|
||||
|
|
@ -138,10 +138,10 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
|
|||
## executes the query (typically "INSERT") and returns the
|
||||
## generated ID for the row or -1 in case of an error.
|
||||
var q = dbFormat(query, args)
|
||||
if mysqlRealQuery(db, q, q.len) != 0'i32:
|
||||
if mysql.RealQuery(db, q, q.len) != 0'i32:
|
||||
result = -1'i64
|
||||
else:
|
||||
result = mysql_insert_id(db)
|
||||
result = mysql.InsertId(db)
|
||||
|
||||
proc InsertID*(db: TDbConn, query: TSqlQuery, args: openArray[string]): int64 =
|
||||
## executes the query (typically "INSERT") and returns the
|
||||
|
|
@ -154,20 +154,20 @@ proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery,
|
|||
## runs the query (typically "UPDATE") and returns the
|
||||
## number of affected rows
|
||||
Exec(db, query, args)
|
||||
result = mysql_affected_rows(db)
|
||||
result = mysql.AffectedRows(db)
|
||||
|
||||
proc Close*(db: TDbConn) =
|
||||
## closes the database connection.
|
||||
if db != nil: mysqlClose(db)
|
||||
if db != nil: mysql.Close(db)
|
||||
|
||||
proc Open*(connection, user, password, database: string): TDbConn =
|
||||
## opens a database connection. Raises `EDb` if the connection could not
|
||||
## be established.
|
||||
result = mysqlInit(nil)
|
||||
result = mysql.Init(nil)
|
||||
if result == nil: dbError("could not open database connection")
|
||||
if mysqlRealConnect(result, "", user, password, database,
|
||||
0'i32, nil, 0) == nil:
|
||||
var errmsg = $mysql_error(result)
|
||||
Close(result)
|
||||
if mysql.RealConnect(result, "", user, password, database,
|
||||
0'i32, nil, 0) == nil:
|
||||
var errmsg = $mysql.error(result)
|
||||
db_mysql.Close(result)
|
||||
dbError(errmsg)
|
||||
|
||||
|
|
|
|||
|
|
@ -18,21 +18,18 @@ import
|
|||
when defined(Windows):
|
||||
import windows, ShellAPI, os
|
||||
|
||||
type
|
||||
PWindow* = PGtkWindow ## A shortcut for a GTK window.
|
||||
|
||||
proc info*(window: PWindow, msg: string) =
|
||||
## Shows an information message to the user. The process waits until the
|
||||
## user presses the OK button.
|
||||
when defined(Windows):
|
||||
discard MessageBoxA(0, msg, "Information", MB_OK or MB_ICONINFORMATION)
|
||||
else:
|
||||
var dialog = GTK_DIALOG(gtk_message_dialog_new(window,
|
||||
GTK_DIALOG_MODAL or GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", cstring(msg)))
|
||||
gtk_window_set_title(dialog, "Information")
|
||||
discard gtk_dialog_run(dialog)
|
||||
gtk_widget_destroy(dialog)
|
||||
var dialog = message_dialog_new(window,
|
||||
DIALOG_MODAL or DIALOG_DESTROY_WITH_PARENT,
|
||||
MESSAGE_INFO, BUTTONS_OK, "%s", cstring(msg))
|
||||
setTitle(dialog, "Information")
|
||||
discard run(dialog)
|
||||
destroy(PWidget(dialog))
|
||||
|
||||
proc warning*(window: PWindow, msg: string) =
|
||||
## Shows a warning message to the user. The process waits until the user
|
||||
|
|
@ -40,12 +37,12 @@ proc warning*(window: PWindow, msg: string) =
|
|||
when defined(Windows):
|
||||
discard MessageBoxA(0, msg, "Warning", MB_OK or MB_ICONWARNING)
|
||||
else:
|
||||
var dialog = GTK_DIALOG(gtk_message_dialog_new(window,
|
||||
GTK_DIALOG_MODAL or GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "%s", cstring(msg)))
|
||||
gtk_window_set_title(dialog, "Warning")
|
||||
discard gtk_dialog_run(dialog)
|
||||
gtk_widget_destroy(dialog)
|
||||
var dialog = DIALOG(message_dialog_new(window,
|
||||
DIALOG_MODAL or DIALOG_DESTROY_WITH_PARENT,
|
||||
MESSAGE_WARNING, BUTTONS_OK, "%s", cstring(msg)))
|
||||
setTitle(dialog, "Warning")
|
||||
discard run(dialog)
|
||||
destroy(PWidget(dialog))
|
||||
|
||||
proc error*(window: PWindow, msg: string) =
|
||||
## Shows an error message to the user. The process waits until the user
|
||||
|
|
@ -53,12 +50,12 @@ proc error*(window: PWindow, msg: string) =
|
|||
when defined(Windows):
|
||||
discard MessageBoxA(0, msg, "Error", MB_OK or MB_ICONERROR)
|
||||
else:
|
||||
var dialog = GTK_DIALOG(gtk_message_dialog_new(window,
|
||||
GTK_DIALOG_MODAL or GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", cstring(msg)))
|
||||
gtk_window_set_title(dialog, "Error")
|
||||
discard gtk_dialog_run(dialog)
|
||||
gtk_widget_destroy(dialog)
|
||||
var dialog = DIALOG(message_dialog_new(window,
|
||||
DIALOG_MODAL or DIALOG_DESTROY_WITH_PARENT,
|
||||
MESSAGE_ERROR, BUTTONS_OK, "%s", cstring(msg)))
|
||||
setTitle(dialog, "Error")
|
||||
discard run(dialog)
|
||||
destroy(PWidget(dialog))
|
||||
|
||||
|
||||
proc ChooseFileToOpen*(window: PWindow, root: string = ""): string =
|
||||
|
|
@ -82,21 +79,19 @@ proc ChooseFileToOpen*(window: PWindow, root: string = ""): string =
|
|||
else:
|
||||
result = ""
|
||||
else:
|
||||
var
|
||||
chooser: PGtkDialog
|
||||
chooser = GTK_DIALOG(gtk_file_chooser_dialog_new("Open File", window,
|
||||
GTK_FILE_CHOOSER_ACTION_OPEN,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OPEN, GTK_RESPONSE_OK, nil))
|
||||
var chooser = file_chooser_dialog_new("Open File", window,
|
||||
FILE_CHOOSER_ACTION_OPEN,
|
||||
STOCK_CANCEL, RESPONSE_CANCEL,
|
||||
STOCK_OPEN, RESPONSE_OK, nil)
|
||||
if root.len > 0:
|
||||
discard gtk_file_chooser_set_current_folder(chooser, root)
|
||||
if gtk_dialog_run(chooser) == cint(GTK_RESPONSE_OK):
|
||||
var x = gtk_file_chooser_get_filename(chooser)
|
||||
discard set_current_folder(chooser, root)
|
||||
if run(chooser) == cint(RESPONSE_OK):
|
||||
var x = get_filename(chooser)
|
||||
result = $x
|
||||
g_free(x)
|
||||
else:
|
||||
result = ""
|
||||
gtk_widget_destroy(chooser)
|
||||
destroy(PWidget(chooser))
|
||||
|
||||
proc ChooseFilesToOpen*(window: PWindow, root: string = ""): seq[string] =
|
||||
## Opens a dialog that requests filenames from the user. Returns ``@[]``
|
||||
|
|
@ -136,25 +131,23 @@ proc ChooseFilesToOpen*(window: PWindow, root: string = ""): seq[string] =
|
|||
if buf[i] == '\0': break
|
||||
for i in 0..result.len-1: result[i] = os.joinPath(path, result[i])
|
||||
else:
|
||||
var
|
||||
chooser: PGtkDialog
|
||||
chooser = GTK_DIALOG(gtk_file_chooser_dialog_new("Open Files", window,
|
||||
GTK_FILE_CHOOSER_ACTION_OPEN,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OPEN, GTK_RESPONSE_OK, nil))
|
||||
var chooser = file_chooser_dialog_new("Open Files", window,
|
||||
FILE_CHOOSER_ACTION_OPEN,
|
||||
STOCK_CANCEL, RESPONSE_CANCEL,
|
||||
STOCK_OPEN, RESPONSE_OK, nil)
|
||||
if root.len > 0:
|
||||
discard gtk_file_chooser_set_current_folder(chooser, root)
|
||||
gtk_file_chooser_set_select_multiple(chooser, true)
|
||||
discard set_current_folder(chooser, root)
|
||||
set_select_multiple(chooser, true)
|
||||
result = @[]
|
||||
if gtk_dialog_run(chooser) == cint(GTK_RESPONSE_OK):
|
||||
var L = gtk_file_chooser_get_filenames(chooser)
|
||||
if run(chooser) == cint(RESPONSE_OK):
|
||||
var L = get_filenames(chooser)
|
||||
var it = L
|
||||
while it != nil:
|
||||
add(result, $cast[cstring](it.data))
|
||||
g_free(it.data)
|
||||
it = it.next
|
||||
g_slist_free(L)
|
||||
gtk_widget_destroy(chooser)
|
||||
free(L)
|
||||
destroy(PWidget(chooser))
|
||||
|
||||
|
||||
proc ChooseFileToSave*(window: PWindow, root: string = ""): string =
|
||||
|
|
@ -178,22 +171,20 @@ proc ChooseFileToSave*(window: PWindow, root: string = ""): string =
|
|||
else:
|
||||
result = ""
|
||||
else:
|
||||
var
|
||||
chooser: PGtkDialog
|
||||
chooser = GTK_DIALOG(gtk_file_chooser_dialog_new("Save File", window,
|
||||
GTK_FILE_CHOOSER_ACTION_SAVE,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OPEN, GTK_RESPONSE_OK, nil))
|
||||
var chooser = file_chooser_dialog_new("Save File", window,
|
||||
FILE_CHOOSER_ACTION_SAVE,
|
||||
STOCK_CANCEL, RESPONSE_CANCEL,
|
||||
STOCK_OPEN, RESPONSE_OK, nil)
|
||||
if root.len > 0:
|
||||
discard gtk_file_chooser_set_current_folder(chooser, root)
|
||||
gtk_file_chooser_set_do_overwrite_confirmation(chooser, true)
|
||||
if gtk_dialog_run(chooser) == cint(GTK_RESPONSE_OK):
|
||||
var x = gtk_file_chooser_get_filename(chooser)
|
||||
discard set_current_folder(chooser, root)
|
||||
set_do_overwrite_confirmation(chooser, true)
|
||||
if run(chooser) == cint(RESPONSE_OK):
|
||||
var x = get_filename(chooser)
|
||||
result = $x
|
||||
g_free(x)
|
||||
else:
|
||||
result = ""
|
||||
gtk_widget_destroy(chooser)
|
||||
destroy(PWidget(chooser))
|
||||
|
||||
|
||||
proc ChooseDir*(window: PWindow, root: string = ""): string =
|
||||
|
|
@ -216,19 +207,17 @@ proc ChooseDir*(window: PWindow, root: string = ""): string =
|
|||
Result = $TempPath
|
||||
discard GlobalFreePtr(lpItemID)
|
||||
else:
|
||||
var
|
||||
chooser: PGtkDialog
|
||||
chooser = GTK_DIALOG(gtk_file_chooser_dialog_new("Select Directory", window,
|
||||
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OPEN, GTK_RESPONSE_OK, nil))
|
||||
var chooser = file_chooser_dialog_new("Select Directory", window,
|
||||
FILE_CHOOSER_ACTION_SELECT_FOLDER,
|
||||
STOCK_CANCEL, RESPONSE_CANCEL,
|
||||
STOCK_OPEN, RESPONSE_OK, nil)
|
||||
if root.len > 0:
|
||||
discard gtk_file_chooser_set_current_folder(chooser, root)
|
||||
if gtk_dialog_run(chooser) == cint(GTK_RESPONSE_OK):
|
||||
var x = gtk_file_chooser_get_filename(chooser)
|
||||
discard set_current_folder(chooser, root)
|
||||
if run(chooser) == cint(RESPONSE_OK):
|
||||
var x = get_filename(chooser)
|
||||
result = $x
|
||||
g_free(x)
|
||||
else:
|
||||
result = ""
|
||||
gtk_widget_destroy(chooser)
|
||||
destroy(PWidget(chooser))
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
## Currently only requesting URLs is implemented. The implementation depends
|
||||
## on the libcurl library!
|
||||
##
|
||||
## **Deprecated since version 0.8.6:** Use the ``httpclient`` module instead.
|
||||
## **Deprecated since version 0.8.8:** Use the ``httpclient`` module instead.
|
||||
##
|
||||
|
||||
{.deprecated.}
|
||||
|
|
@ -34,27 +34,27 @@ proc URLretrieveStream*(url: string): PStream =
|
|||
## retrieves the given `url` and returns a stream which one can read from to
|
||||
## obtain the contents. Returns nil if an error occurs.
|
||||
result = newStringStream()
|
||||
var hCurl = curl_easy_init()
|
||||
var hCurl = easy_init()
|
||||
if hCurl == nil: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_URL, url) != CURLE_OK: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION,
|
||||
curlwrapperWrite) != CURLE_OK: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, result) != CURLE_OK: return nil
|
||||
if curl_easy_perform(hCurl) != CURLE_OK: return nil
|
||||
curl_easy_cleanup(hCurl)
|
||||
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return nil
|
||||
if easy_setopt(hCurl, OPT_WRITEFUNCTION,
|
||||
curlwrapperWrite) != E_OK: return nil
|
||||
if easy_setopt(hCurl, OPT_WRITEDATA, result) != E_OK: return nil
|
||||
if easy_perform(hCurl) != E_OK: return nil
|
||||
easy_cleanup(hCurl)
|
||||
|
||||
proc URLretrieveString*(url: string): string =
|
||||
## retrieves the given `url` and returns the contents. Returns nil if an
|
||||
## error occurs.
|
||||
var stream = newStringStream()
|
||||
var hCurl = curl_easy_init()
|
||||
var hCurl = easy_init()
|
||||
if hCurl == nil: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_URL, url) != CURLE_OK: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION,
|
||||
curlwrapperWrite) != CURLE_OK: return nil
|
||||
if curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, stream) != CURLE_OK: return nil
|
||||
if curl_easy_perform(hCurl) != CURLE_OK: return nil
|
||||
curl_easy_cleanup(hCurl)
|
||||
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return nil
|
||||
if easy_setopt(hCurl, OPT_WRITEFUNCTION,
|
||||
curlwrapperWrite) != E_OK: return nil
|
||||
if easy_setopt(hCurl, OPT_WRITEDATA, stream) != E_OK: return nil
|
||||
if easy_perform(hCurl) != E_OK: return nil
|
||||
easy_cleanup(hCurl)
|
||||
result = stream.data
|
||||
|
||||
when isMainModule:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue