FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

inserttable() to use str(datetime) rather than repr() by justinpryzby · Pull Request #93 · PyGreSQL/PyGreSQL · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions ext/pgconn.c
This file contains hidden or 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
Expand Up @@ -12,6 +12,9 @@
#include "pginternal.h"
#include "pgmodule.h"

/* Needs to be after Python.h */
#include "datetime.h"

/* Deallocate connection object. */
static void
conn_dealloc(connObject *self)
Expand Down Expand Up @@ -789,6 +792,11 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)

encoding = PQclientEncoding(self->cnx);

PyDateTime_IMPORT;
if (PyErr_Occurred()) {
return NULL; /* pass the error */
}

/* pre-allocate some memory for the query buffer */
if (!init_char_buffer(&buffer, 4096)) {
Py_DECREF(iter_row);
Expand Down Expand Up @@ -990,6 +998,14 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
ext_char_buffer_s(&buffer, t);
Py_DECREF(s);
}
else if (PyDate_Check(item) || PyDateTime_Check(item) ||
PyTime_Check(item) || PyDelta_Check(item)) {
PyObject *s = PyObject_Str(item);
const char *t = PyUnicode_AsUTF8(s);

ext_char_buffer_s(&buffer, t);
Py_DECREF(s);
}
else {
PyObject *s = PyObject_Repr(item);
const char *t = PyUnicode_AsUTF8(s);
Expand Down
7 changes: 7 additions & 0 deletions tests/test_classic_connection.py
This file contains hidden or 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
Expand Up @@ -11,6 +11,7 @@

from __future__ import annotations

import datetime as dt
import os
import threading
import time
Expand Down Expand Up @@ -1897,6 +1898,12 @@ def test_inserttable_no_column(self):
self.c.inserttable('test', data, [])
self.assertEqual(self.get_back(), [])

def test_inserttable_datetime_adapt(self):
data = [(dt.date(1999,1,2), dt.time(11,12,13))]
self.c.inserttable('test', data, ['dt', 'ti'])
self.assertEqual([i[4:6] for i in self.get_back()],
[tuple(str(j) for j in i) for i in data])

def test_inserttable_only_one_column(self):
data: list[tuple] = [(42,)] * 50
self.c.inserttable('test', data, ['i4'])
Expand Down

Back | FazBrowse Home | New Git URL