Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bart mosley committed Nov 10, 2010
1 parent 54fe5e8 commit 12cd467
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 137 deletions.
13 changes: 6 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Lines starting with '#' are considered comments.
# Ignore any file named foo.txt.
# Lines starting with '#' are considered comments--in fact, they are comments.
# ignore foo.txt
foo.txt
# Ignore (generated) compiled files,
# ignore compiled files
*.pyc
# Ignore objects and archives.
# ignore objects and archives
*.[oa]
# Ignore bash shell scripts
*.sh
* Ignore Espesso project files
# ignore project director for espresso editor
*.esproj

2 changes: 0 additions & 2 deletions README
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
see www.auturm-orm.org

4 changes: 1 addition & 3 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

# AUTUMN information
__copyright__ = 'Copyright (c) 2008 Jared Kuolt'

version = (0,5,1)
version_string = "Autumn ORM version %d.%d.%d (with bondgeek modifications)" % version
version_string = "Autumn ORM version %d.%d.%d (with bondgeek modifications" % version

19 changes: 18 additions & 1 deletion db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ def connect(self, dbtype, *args, **kwargs):
if dbtype == 'sqlite3':
import sqlite3
self.connection = sqlite3.connect(*args)
self.lastrowid_ = "LAST_INSERT_ROWID()"
elif dbtype == 'mysql':
import MySQLdb
self.connection = MySQLdb.connect(**kwargs)
self.lastrowid_ = "LAST_INSERT_ID()"
self.placeholder = '%s'

elif dbtype == 'pyodbc':
import pyodbc
self.connection = pyodbc.connect(**kwargs)
self.connection.autocommit = True
self.lastrowid_ = "LAST_INSERT_ID()"
self.placeholder = '?'

def lastrowid(self):
idCursor = self.connection.cursor()
qry = "SELECT %s AS last_row_id" % self.lastrowid_
idCursor.execute(qry)

rc = idCursor.fetchone()

return rc[0] if rc else None

class DBConn(object):
def __init__(self):
self.b_debug = False
Expand Down
106 changes: 3 additions & 103 deletions db/query.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,7 @@
from autumn.db import escape
from autumn.db.connection import autumn_db
from . import escape
from .connection import autumn_db

class Filter(object):
'''
Provides the ability to create queries using >,<, !=, etc.
Functions are defined as staticmethods, so no need to instatiate.
Usage:
say the model MyModel includes a numeric field 'amount'
>>> q = Query(model=MyModel).filter(amount = Filter.gt(100))
returns the records where amount > 100
'''
@staticmethod
def _output(op, value=None, column=None):
if not value and column:
return (op % escape(column),)
if value:
return (op, value)
else:
return (op,)

@staticmethod
def isnull():
return Filter._output(" is Null", None)

@staticmethod
def notnull():
return Filter._output(" is NOT Null", None)

@staticmethod
def gt(value=None, column=None): # Have class Column inherit from Filter, to just get op and % the value?
return Filter._output(">%s", value, column)

@staticmethod
def ge(value=None, column=None):
return Filter._output(">=%s", value, column)

@staticmethod
def lt(value=None, column=None):
return Filter._output("<%s", value, column)

@staticmethod
def le(value=None, column=None):
return Filter._output("<=%s", value, column)

@staticmethod
def ne(value=None, column=None):
return Filter._output("!=%s", value, column)

@classmethod
def eq(cls, value=None, column=None):
if not column and not value:
return cls.isnull()
return Filter._output("=%s", value, column)

@staticmethod
def between(value1, value2, NOTflag=None):
op = " NOT BETWEEN %s AND %s" if NOTflag else " BETWEEN %s AND %s"
lval = min(value1, value2)
uval = max(value1, value2)
return (op, lval, uval)

@staticmethod
def isin(value):
if hasattr(value, "__iter__"):
if len(value) > 1:
return Filter._output(" IN %s" % str(tuple(value)),)
else:
value = value[0]
return Filter.eq(value)

@staticmethod
def islike(value):
return Filter._output(" LIKE %s", value)

@staticmethod
def _getvalue(value):
if isinstance(value,tuple):
try:
return value[1]
except IndexError:
return value
else:
return value

@staticmethod
def _extractvalue(value):
if isinstance(value,tuple) and len(value)==1:
return False
return True

@staticmethod
def _extractvalues(itervals):
ll = []
for x in itervals:
if hasattr(x, "__iter__"):
ll.extend(x[1:])
else:
ll.append(x)
return ll
from .filter import Filter

class Query(object):
'''
Expand Down
4 changes: 2 additions & 2 deletions db/relations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from autumn.db.query import Query
from autumn.model import cache
from .query import Query
from ..model import cache

class Relation(object):

Expand Down
11 changes: 6 additions & 5 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from autumn.db.query import Query
from autumn.db import escape
from autumn.db.connection import autumn_db, Database
from autumn.validators import ValidatorChain
from .db.query import Query
from .db import escape
from .db.connection import autumn_db, Database
from .validators import ValidatorChain

class ModelCache(object):
models = {}
Expand Down Expand Up @@ -228,7 +228,8 @@ def _new_save(self):
cursor = Query.raw_sql(query, values, self.db)

if self._get_pk() is None:
self._set_pk(cursor.lastrowid)
lastrowid = self.db.conn.lastrowid()
self._set_pk(lastrowid)
return True

def _get_defaults(self):
Expand Down
12 changes: 7 additions & 5 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from autumn.db.connection import autumn_db
from autumn.model import Model
from autumn.db.relations import ForeignKey, OneToMany
from autumn import validators
from ..db.connection import autumn_db
from ..model import Model
from ..db.relations import ForeignKey, OneToMany
from .. import validators
import datetime

#autumn_db.conn.connect('sqlite3', '/tmp/example.db')
autumn_db.conn.connect('mysql', user='root', db='autumn')
#autumn_db.conn.connect('mysql', user='root', db='autumn_test')
autumn_db.conn.connect('pyodbc', driver='{MySQL ODBC 5.1 Driver}', server='localhost', database='autumn_test', user='root')


class Author(Model):
books = OneToMany('Book')
Expand Down
10 changes: 5 additions & 5 deletions tests/run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
import unittest
import datetime
from autumn.model import Model
from autumn.tests.models import Book, Author
from autumn.db.query import Query
from autumn.db import escape
from autumn import validators
from ..model import Model
from .models import Book, Author
from ..db.query import Query
from ..db import escape
from .. import validators

class TestModels(unittest.TestCase):

Expand Down
8 changes: 4 additions & 4 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from threading import local as threading_local

# Autumn ORM
from autumn.model import Model
from autumn.db.relations import ForeignKey, OneToMany
from autumn.db.query import Query
from autumn.db.connection import Database
from .model import Model
from .db.relations import ForeignKey, OneToMany
from .db.query import Query
from .db.connection import Database


"""
Expand Down

0 comments on commit 12cd467

Please sign in to comment.